Reading wrong values from Joystick for the first few milliseconds

Started by
12 comments, last by jbadams 11 years, 6 months ago
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.
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.

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.
What do you mean by custom hardware? Maybe the behavior is a "feature" specific to this custom hardware.
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.
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.

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.
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.
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.

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.

This topic is closed to new replies.

Advertisement