Wednesday, March 24, 2010

[matlabjoystick] Use a Joystick in Matlab

Have you ever wanted to use a joystick in Matlab? Yeah, I didn't think so. But just in case you did I wrote a Mex file to interface with SDL's joystick subsystem. By the way, SDL is a great easy to use library, I recommend it.

I wrote this for the "remote control wheelchair" my lab is working on. At first I tried using keyboard control, but the binary nature of a keypress made it difficult to set a constant speed. I can't image many people using this, but it could be useful in some situations.

Basically a joystick object gets initialized, and sends back an array of doubles, one for each axis, hat, and button. Optionally a mask can be set to filter buttons. For example my joystick has 14 axes/buttons, but I only care about the first two axes and the trigger button.

The following Matlab session demonstrates the use of a joystick object.

>> j = Joystick(0)
j =
Joystick handle

Properties:
joystick_number: 0
state_mask: [1 1 1 1 1 1 1 1 1 1 1 1 1 1]
Methods, Events, Superclasses
>> j.state()
ans =
Columns 1 through 11
0 0 0 0 0 0 0 0 0 0 0
Columns 12 through 14
0 0 0
>> m = j.state_mask
m =
Columns 1 through 11
1 1 1 1 1 1 1 1 1 1 1
Columns 12 through 14
1 1 1
>> m(5:end) = zeros; m(3) = 0;
>> m
m =
Columns 1 through 11
1 1 0 1 0 0 0 0 0 0 0
Columns 12 through 14
0 0 0
>> j.mask(m)
>> j.state()
ans =
0 0 0
>>

No comments:

Post a Comment