Jump to content

  • Log In with Google      Sign In   
  • Create Account

Reading wrong values from Joystick for the first few milliseconds


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 doeme   Members   -  Reputation: 707

Like
0Likes
Like

Posted 09 October 2012 - 03:33 AM

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[i] = (DWORD)powf(2.0f, i);
   if (jb & button[i]) button[i]=1; else button[i]=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.

Sponsor:

#2 Faelenor   Members   -  Reputation: 396

Like
1Likes
Like

Posted 09 October 2012 - 06:42 AM

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.

#3 doeme   Members   -  Reputation: 707

Like
0Likes
Like

Posted 09 October 2012 - 07:31 AM

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.

#4 Amr0   Members   -  Reputation: 1114

Like
1Likes
Like

Posted 09 October 2012 - 08:14 AM

What do you mean by custom hardware? Maybe the behavior is a "feature" specific to this custom hardware.

#5 doeme   Members   -  Reputation: 707

Like
0Likes
Like

Posted 09 October 2012 - 08:36 AM

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.

#6 MarkS   Prime Members   -  Reputation: 882

Like
2Likes
Like

Posted 09 October 2012 - 06:16 PM

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.

#7 doeme   Members   -  Reputation: 707

Like
0Likes
Like

Posted 10 October 2012 - 03:34 AM

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.

#8 MarkS   Prime Members   -  Reputation: 882

Like
1Likes
Like

Posted 11 October 2012 - 03:19 PM

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.

#9 frob   Moderators   -  Reputation: 21475

Like
2Likes
Like

Posted 11 October 2012 - 03:43 PM

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.
Check out my personal indie blog at bryanwagstaff.com.

#10 MarkS   Prime Members   -  Reputation: 882

Like
1Likes
Like

Posted 11 October 2012 - 04:55 PM

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.

#11 doeme   Members   -  Reputation: 707

Like
1Likes
Like

Posted 12 October 2012 - 03:19 AM

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[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[i] = (DWORD)powf(2.0f, i);
			if (jb & button[i]) button[i]=1; else button[i]=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, 13 October 2012 - 06:00 AM.


#12 JohnnyCode   Members   -  Reputation: 235

Like
-5Likes
Like

Posted 12 October 2012 - 10:33 PM

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

#13 Bacterius   Crossbones+   -  Reputation: 8945

Like
0Likes
Like

Posted 13 October 2012 - 05:23 AM

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

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#14 jbadams   Senior Staff   -  Reputation: 18734

Like
0Likes
Like

Posted 13 October 2012 - 05:41 AM

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?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS