• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
doeme

Reading wrong values from Joystick for the first few milliseconds

13 posts in this topic

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):

[CODE]
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;

}
}
[/CODE]

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

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

Share this post


Link to post
Share on other sites
[quote name='Faelenor' timestamp='1349786553' post='4988322']
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.
[/quote]
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.
0

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

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

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

Share this post


Link to post
Share on other sites
[quote name='MarkS' timestamp='1349828170' post='4988541']
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
[/quote]
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.
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.
1

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

Share this post


Link to post
Share on other sites
[quote name='frob' timestamp='1349991792' post='4989272']
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.
[/quote]

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

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.

[CODE]
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;
}

[/CODE] Edited by doeme
1

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

Share this post


Link to post
Share on other sites
[quote name='JohnnyCode' timestamp='1350102780' post='4989671']
you use std::cout at the end, thus you output memory to a very place, some chip, driver away,
[/quote]
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...
0

Share this post


Link to post
Share on other sites
[quote name='JohnnyCode' timestamp='1350102780' post='4989671']
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
[/quote]
What do you think you just said? Could you try rephrasing that?
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0