• Advertisement
Sign in to follow this  

Reading wrong values from Joystick for the first few milliseconds

This topic is 1928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

While refactoring a software that uses joystick-input from custom hardware, I found out that the software contained a small but ugly hack to get the joysticks working. Using the Win32-Multimedia-API the available joysticks are enumerated and afterwards reads the state of some buttons and axes. However after enumerating the device the application was put to sleep for half as second for every joystick, because else the state of the buttons was reported incorrectly. I know the state of the buttons, because they are hardwired in the devices.After this "initialisation" I am able to poll the joysticks as fast as I can without getting random results.


My guess is that it has to do with the update-rates of the joysticks, but I have found no information concerning this or how to retrieve this.
Has anybody a clue for this?

The code (a bit simplyfied):


JOYINFOEX structtmp;
for (int i = JOYSTICKID1; i < (JOYSTICKID1 +16); i++)
{
if (joyGetPosEx(i, &structtmp) == JOYERR_NOERROR) {
JOYINFOEX actualPos;
actualPos.dwSize = sizeof(JOYINFOEX);
actualPos.dwFlags = JOY_RETURNALL;

Sleep(500); //if no sleep -> data is crap after startup, why??

//get button states
if (joyGetPosEx(i, &actualPos) != JOYERR_NOERROR) {
std::cout << "Error reading joystick " << i << std::endl;
continue;;
}
DWORD jb = actualPos.dwButtons;
int button[32];
//get button bit of the 32 buttons
for (unsigned int i = 0; i < 32; i++) {
button = (DWORD)powf(2.0f, i);
if (jb & button) button=1; else button=0;
}
int type = button[31] + button[30] * 2 + button[29] * 4 + button[28] * 8 + button[27] * 16 + button[26] * 32 + button[25] * 64 + button[24] * 128;

std::cout << "Got Joystick: " << i << " of type " << type << std::endl;

}
}


The first call to joyGetPosEx returns garbage for the buttons as well.

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure if it's the cause of the problem, but the first call to joyGetPosEx is done with an uninitialized structtmp structure. From the documentation, that's illegal and the call should fail.

Share this post


Link to post
Share on other sites

I'm not sure if it's the cause of the problem, but the first call to joyGetPosEx is done with an uninitialized structtmp structure. From the documentation, that's illegal and the call should fail.

Thanks for the hint, but even with initialization the problem is still there, but it seems that I get at least always 0 from all the buttons now until the correct result shows up.

Share this post


Link to post
Share on other sites
What do you mean by custom hardware? Maybe the behavior is a "feature" specific to this custom hardware.

Share this post


Link to post
Share on other sites
By custom device I mean a prototype joystick that was specifically built for an academic project. I have full access to the hardware spec (and even the engineer that built it :) ) and I know that late initializing it's not a feature of the hardware, but that the "buttons" from the code are hardwired to be always pressed.

But it also happens with some off the shelf joysticks I tested, but of course with other buttons; however with these I cannot be sure how the hardware behaves.

Share this post


Link to post
Share on other sites
Is it a potentiometer-based joystick or switch-based? Is sounds like it is switch-based and there is no hardware debounce built into the circuit. Essentially, when a mechanical switch is pressed, it does not go from open to fully closed. The contacts bounce between open and closed for a few milliseconds. Depending on the intended use, this may or may not be an issue, but it always is an issue if the switch is connected to a microprocessor as the processor operates faster than the bouncing, resulting in jitter on the input until the bounce stops. Sleep is the best way to do software debounce if this is the case. All you can do is wait it out. Then send it back to have a hardware debounce circuit added.

I could be wrong, but that is the first thing to jump out at me.

Share this post


Link to post
Share on other sites

Is it a potentiometer-based joystick or switch-based? Is sounds like it is switch-based and there is no hardware debounce built into the circuit

Good point. According to the engineer that built the device the buttons are debounced and should not jitter, and for the buttons in question there is no mechanical part, they are set to a certain value by the microcontroller. However we brought the sleep-time down quite a bit by reading until we receivce something other than 0.

Share this post


Link to post
Share on other sites
I still think it is a hardware issue. Your are reading some sort of interference from the joystick. If the microcontroller is setting the value of the button, then the problem is between the microcontroller and the computer. It could be something as simple to fix (and infuriatingly hard to find) as a poor solder joint. It could also be a broker wire. Any number of things, but I am willing to bet it is a hardware problem.

Share this post


Link to post
Share on other sites
I haven't used them for years now, but back in the day...

[nostalgia music and faded edges]

... That was just the way joysticks behaved. You needed to give them a few iterations. You also generally needed to re-calibrate the joystick at every use. Usually while doing the calibration you needed to discard the first fifty or so samples because they were initially too jittery for useful values.

Share this post


Link to post
Share on other sites

I haven't used them for years now, but back in the day...

[nostalgia music and faded edges]

... That was just the way joysticks behaved. You needed to give them a few iterations. You also generally needed to re-calibrate the joystick at every use. Usually while doing the calibration you needed to discard the first fifty or so samples because they were initially too jittery for useful values.


And that was do to lack of hardware debouncing as I mentioned earlier. With hardware debounce, and he said it has this, you *should* not see this jitter. However, as he mentioned, the problem isn't coming from an actual mechanical button, but directly from an IO pin from the microcontroller. This leads me to believe that the problem is in the hardware between the microcontroller and the computer.

Share this post


Link to post
Share on other sites
Thank you all for the help and the hints. We've ruled bad soldering-connections almost out, by creating a few more devices of the same type and the all behave the exact same. On the average we're now down to waiting 20ms per joystick (even with multiple connected joysticks) until we get useful signals, by modifiying the code in a way that it only waits until we get something other than 0.

Further modifications by enumerating all joysticks first and then querying them in a separate loop yielded even better results with no need to sleep at all. (modified code below). So our guess is now that the microcontroller has a few ms to get ready to deliver the signals over usb. We will check this next week more in depth, but for now the solution is satisfying for our needs, even without being 100% sure of the cause. Again, thanks for all the help and input from you guys.


JOYINFOEX structtmp;
structtmp.dwSize = sizeof(JOYINFOEX);
structtmp.dwFlags = JOY_RETURNALL;
std::vector<int> connectedJoysticks;
// first enumerate all joysticks
for (int i = JOYSTICKID1; i < (JOYSTICKID1 +16); i++)
{
if (joyGetPosEx(i, &structtmp) == JOYERR_NOERROR) {
connectedJoysticks.push_back(i);
}
}
// then read the buttons only for the connected ones
for(unsigned int i = 0; i < connectedJoysticks.size(); i++)
{
JOYINFOEX actualPos;
actualPos.dwSize = sizeof(JOYINFOEX);
actualPos.dwFlags = JOY_RETURNALL;
int type = 0;
while(type == 0)
{
//get button states
if (joyGetPosEx(connectedJoysticks, &actualPos) != JOYERR_NOERROR) {
std::cout << "Error reading joystick " << i << std::endl;
continue;;
}
DWORD jb = actualPos.dwButtons;
int button[32];
//get button bit of the 32 buttons
for (unsigned int i = 0; i < 32; i++) {
button = (DWORD)powf(2.0f, i);
if (jb & button) button=1; else button=0;
}
type = button[31] + button[30] * 2 + button[29] * 4 + button[28] * 8 + button[27] * 16 + button[26] * 32 + button[25] * 64 + button[24] * 128;
}
std::cout << "Got Joystick: " << i << " of type " << type << std::endl;
}

Edited by doeme

Share this post


Link to post
Share on other sites
you use std::cout at the end, thus you output memory to a very place, some chip, driver away, maybe you do not know definition for the buttons constants only. See joystick buttons constants somehow

Share this post


Link to post
Share on other sites

you use std::cout at the end, thus you output memory to a very place, some chip, driver away,

Huh? cout is a handle to the process' standard output (stdout). It's meant to print out stuff on the screen, and that's exactly what it's doing here...

Share this post


Link to post
Share on other sites

you use std::cout at the end, thus you output memory to a very place, some chip, driver away, maybe you do not know definition for the buttons constants only. See joystick buttons constants somehow

What do you think you just said? Could you try rephrasing that?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement