Sign in to follow this  
artm

SDL: bad joystick axis values at application start up

Recommended Posts

hi i'm trying to add game controller support to my sdl based game and I'm more or less done except for the following: each time the program starts up and i push the stick, say, left (axis 0 reads -32767) and then release it, the axis 0 reads 32767 - as if i pushed it right all the way. only after I also pushed the stick right (and then released) does it seem to figure out where the center is and settle to some number close to zero. the same is true for the vertical axis and other order (first push right). i'm using SDL 1.2.11 form darwin ports on mac os. my game controller is speedlink's Hornet Force Pad Touch Edition (SL-6526) (can't find it at speedlink website) is this normal behaviour? can i work around it? (i just talked to the previous owner of the game pad and she sais she never had this problem on linux / windows, she wasn't using sdl though) [Edited by - artm on October 6, 2006 2:03:49 PM]

Share this post


Link to post
Share on other sites
Could it be that you're reading the relative position and not the absolute ?

That is, when you move left, it says 'moved 30000 units left' when you move back to the middle it says 'moved 30000 units right'.
If so, i would expect that if you move it all the way left, then 1/2 way back to middle, it'll say -32000, +16000 or so.

To fix, just add the values it returns to get the absolute (ie, -32000 + 32000 = 0, which is the centre).

Wyzfen

Share this post


Link to post
Share on other sites
That's what I thought first, but if that was the case this behaviour would persist. What I'm getting is:

(app starts, joystick pos is "neutral")
128
(i push left)
-32768
(i release)
32767
(i push right)

from here on it works as expected
32767
(i release)
128
(i push left)
-32768
(i release)
128
(i push right)
32767
(i release)
128

etc


So for me it looks like some "autocalibration" is happening in the beginning, where the SDL learns the full range of the axis... or something.

I'm gonna write a test program now that does nothing but read and print axis values.

Share this post


Link to post
Share on other sites
here is my simple sample code


#include <stdio.h>
#include <assert.h>
#include <SDL.h>

int
main(int argc,char**argv)
{
assert (SDL_Init(SDL_INIT_VIDEO|// need video for events
SDL_INIT_JOYSTICK) == 0);
atexit(SDL_Quit);

SDL_Joystick * joy = NULL;
if(SDL_NumJoysticks()>0){
joy = SDL_JoystickOpen(0);
SDL_JoystickEventState(SDL_ENABLE);
}
assert(joy);

while(1) {
SDL_Event ev;
while(SDL_PollEvent(&ev)) {
switch(ev.type) {
case SDL_KEYDOWN:
switch(ev.key.keysym.sym) {
case SDLK_ESCAPE:
exit(0);
break;
}
break;
case SDL_JOYAXISMOTION:
printf("jaxis: which=%u axis=%u value=%d\n", ev.jaxis.which, ev.jaxis.axis, ev.jaxis.value);
break;
}
}
}
}





and here is it's output in response to my pushing the joystick left, releasing, right, releasing, up, releasing, down, releasing.


jaxis: which=0 axis=0 value=0
jaxis: which=0 axis=0 value=-32768
jaxis: which=0 axis=0 value=32767

jaxis: which=0 axis=0 value=32254
jaxis: which=0 axis=0 value=32767
jaxis: which=0 axis=0 value=128

jaxis: which=0 axis=1 value=0
jaxis: which=0 axis=1 value=-32768
jaxis: which=0 axis=1 value=32767

jaxis: which=0 axis=1 value=32254
jaxis: which=0 axis=1 value=32767
jaxis: which=0 axis=1 value=128




i inserted empty lines after each "release"

the triplets starting with value=0 don't make sense to me, the other two are almost ok, although i don't understand why this joystick would generate two events on a single press (this "joystick" is made of 4 microswitches or whatever this little buttons are called so when i push it right i press the right switch on once).

if i continue the experiment further joystick (or rather SDL) starts to behave as i expect:


jaxis: which=0 axis=0 value=-32768
jaxis: which=0 axis=0 value=128

jaxis: which=0 axis=0 value=32767
jaxis: which=0 axis=0 value=128

jaxis: which=0 axis=1 value=-32768
jaxis: which=0 axis=1 value=128

jaxis: which=0 axis=1 value=32767
jaxis: which=0 axis=1 value=128



this is in response to the same sequence of my thumb's actions. the bad pattern repeats only whenever i restart application.

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