11-25-2015 12:47 PM
I recently tried to resurrect some older code that made use of a USB joystick and had problems getting it to work on my desktop computer. It would not return any errors but it also would not return any values from the operation of the joystick either.
The code is very simple. It starts with a call to the Initialize Joystick VI followed by one or more calls to joystickAcquire to read the state of the joystick followed by a call to closeJoystick.
The only argument is a device index to the Initialize Joystick VI. This is what the help says for that input to that VI:
"device index is the index of the joystick you want to initialize. The default is 0. Set device index to 0 if you have only one joystick connected to the computer."
That seems pretty straight forward. I had only one joystick connected, so I used a value of zero but it still did not work. The Windows 7 control-panel Joystick calibration worked fine with the joystick so I knew the problem must be with LabVIEW.
I soon tried the associated Query Input Devices VI found on the same palette. It returns 3 arrays, joystick info, key info and mouse info and shows the first two elements of each. I have a USB keyboard and a wireless USB mouse so the key and mouse info entries were not a surprise but I found it odd that the one Logitech joystick showed up as two Microsoft entries in the joystick info array.
At this point I tried device index values of 0, 1 & 2 with my test routine and still no luck. Eventually and for lack of any better ideas, I tried expanding the device name in the joystick info array to see if there was more than "Microsoft" to be found there and there was. There were two mouse entries in the joystick data! I then realized that I needed to also expand the view of the array items in case there were more than what were displayed to be seen. I found a keyboard device next and down at the bottom of the list there was the joystick (at a device index of 3, which I had not yet tried). Using a device index of 3 made my test routine work finally.
But now I'm left wondering why the mouse and keyboard devices are appearing mixed into the Joystick info array? Is this a bug?
If it is not a bug, how could code be written to pick out the correct device index for the joystick device without making assumptions as to names or numbers of axes or buttons? I'm thinking I now need to construct a pop-up dialog listbox with this name/axes/buttons information shown and always ask the user to pick out the correct device from the list, if there is more than one choice available.
Finally, if the NI folks ever rework the Query Input Devices VI, they should consider turning on the "show vertical scrollbars" in these three arrays as that provides a very good clue to those of us that otherwise miss the initial indications that there may be more data to be displayed in the arrays.
11-25-2015 03:13 PM
Joysticks are definitely squirrelly in LabVIEW -- I think it has something to do with how Microsoft handles these USB devices. I wrote some code to "find" the Joystick -- I used the Query Input Devices function, and then looked through the returned Array of Joystick Info for something whose Axes Total element was >0. If I found it, I used the index of that Array Element as the Device Index for Initialize Joystick, otherwise I returned "No Joy(sticks) Here".
Bob Schor
11-25-2015 03:39 PM
@Bob_Schor wrote:
I wrote some code to "find" the Joystick -- I used the Query Input Devices function, and then looked through the returned Array of Joystick Info for something whose Axes Total element was >0.
As you can see from my earlier screen shot of all the devices found on my system, one instance of the wireless mouse shows up with more than zero active axes. In this case your test could pick the wrong device. I've got the pop-up listbox window built and working now and that's how I'm going to run for the moment but it also occurred to me that I could test for typical joystick behavior by opening / reading / closing each device and discard all the devices that the joystickAcquire VI returned X/Y/Z [DBL] values of exactly zero because real joysticks rarely have zeros for these values, much less all three simultaneously.
11-26-2015 06:00 AM
Interesting that a mouse would register an Axis! OK, say "two or more axes". Seems more "device-specific" than relying on a non-zero value being read ...
BS