# 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 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 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)128etc

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 on other sites
here is my simple sample code

#include <stdio.h>#include <assert.h>#include <SDL.h>intmain(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=0jaxis: which=0 axis=0 value=-32768jaxis: which=0 axis=0 value=32767jaxis: which=0 axis=0 value=32254jaxis: which=0 axis=0 value=32767jaxis: which=0 axis=0 value=128jaxis: which=0 axis=1 value=0jaxis: which=0 axis=1 value=-32768jaxis: which=0 axis=1 value=32767jaxis: which=0 axis=1 value=32254jaxis: which=0 axis=1 value=32767jaxis: 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=-32768jaxis: which=0 axis=0 value=128jaxis: which=0 axis=0 value=32767jaxis: which=0 axis=0 value=128jaxis: which=0 axis=1 value=-32768jaxis: which=0 axis=1 value=128jaxis: which=0 axis=1 value=32767jaxis: 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628337
• Total Posts
2982164

• 9
• 24
• 9
• 9
• 13