Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 18 of 18

Thread: Joystick emulation with mouse

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Location
    Montana
    Posts
    7

    Joystick emulation with mouse

    Hey
    I'm working on a project in which I need a USB mouse to register as a two axis joystick. I asked around another forum, and someone said it could be done using a Teensy USB development board. Is this feasible? I'm trying to control a robot in a holonomic fashion resembling a first person shooter game in order to make it easier for people (my high-school robotics team) to drive. For some reason HID devices like USB mice and Keyboards aren't compatible with our system (National Instruments 4 slot cRIO-II / 8 slot cRIO-I), but Teensy boards are. We've already begun constructing a 28 key half-keyboard to use for directional control, and are just stuck on this. I know we could just use a real joystick or joystick module or even an emulator, but that's a last resort. Let me know what you think! Thanks!

  2. #2
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Let me preface this with, all this is theoretical stuff. I have little experience in the arduino world, im just piecing together what I have seen.

    I saw this on reddit:
    http://www.reddit.com/r/arduino/comm...e_and_made_it/

    now thats a super old school mouse, so the code in that reddit link wouldn't apply directly to a modern usb mouse, so you'll have to find some examples of people doing that with teensy / other arduino devices.

    one of the steps would be making the teensy device a host usb device:
    http://www.pjrc.com/teensy/td_libs_USBHostShield.html
    (don't have any documentation on doing this for the teensy 3.0)

    then you could interpret usb mouse by the teensy and output relevant joystick command to the computer, using the teensy:
    http://www.pjrc.com/teensy/td_joystick.html

  3. #3
    That shield does not work with teensy 3.0. I tried and it gives a lot of errors when trying to compile.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Quote Originally Posted by gltovar View Post
    one of the steps would be making the teensy device a host usb device:
    http://www.pjrc.com/teensy/td_libs_USBHostShield.html
    (don't have any documentation on doing this for the teensy 3.0)
    so for existing knowledgebase you'll probably have to toy around with a teensy2.0 =P
    I am pretty new to arduino in general, and i got bit by the 'library doesn't work with ARM' issue every now and again. In time it'll not be an issue.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Location
    Montana
    Posts
    7
    For my particular application, It really does not need to be all that accurate (just -1, 0, or 1 for each axis). The movement if the mouse is for rotation, which will be pretty slow to begin with, so we don't need anything more specific than +/- 1 (full speed). I've figured out that I don't actually need it to show up as an axis, just two integers; one for the X axis, and one for the Y axis (It might be easier that way, it might not). What I'm thinking I need to do is determine if the mouse is moving left or right (or up and down) and set the variable to 1 or -1 for a few milliseconds after moving, then reset to 0 when stopped for a particular amount of time.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,942
    Can you get access to the raw joystick electronics, rather than a USB cable? It's much easier to read the raw signals.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Location
    Montana
    Posts
    7
    What do you mean by that? We can cannibalize all sorts of things (we have quite a budget), but I'd prefer a USB mouse.

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,942
    Ok, I read this again and I'm confused. You're doing something about a mouse and a joystick. At first I thought you wanted Teensy to read a physical joystick and send virtual mouse movements to some system. But maybe it's the other way around? Maybe I missed some critical bit of text above.... but it's just not clear to me.

    If you explain again specifically what you're trying to accomplish, I'll try to help.

  9. #9
    I think it's backwards. He wants to physically use a mouse, but have the teensy emulate a joystick. Though I don't understand the 'ease of use' aspect he metions. Seems like an actual joystick would just as easy to use as a mouse.

  10. #10
    Junior Member
    Join Date
    Mar 2013
    Location
    Montana
    Posts
    7
    Yeah sorry about that. It's exactly the opposite. I want to use use Teensy to read a physical USB mouse and have it register as a Joystick/GamePad. I don't literally want to see a cursor moving, I want to use it like a joystick, with X Axis movement being one axis of a joystick and Y Axis movement being the other. That way I can send values into my driving program and use it for rotating left or right. I mentioned earlier that It doesn't necessarily need it to be registered as an axis, just two variables (X and Y) that fluctuate from -1 to +1 that I can call.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Lets just break down your barriers so you can know what you need to do.

    1) Get the teensy to have usb host mode, so you can plug a mouse into it. If someone knows how to do this for teensy 3.0 that'll help immensely. If you want a quick solution, you can get a teensy 2, and follow these instructions http://www.pjrc.com/teensy/td_libs_USBHostShield.html

    2) you need to be able to read in the mouse data from the host mode teensy. Not sure how to do this at all, so if any one knows where there are examples on how to interpret usb mouse data on teensy/arduino, that would be helpful .

    3) Interpret the mouse data, and use the teensy to output it to the computer.

    example of some one interpreting some mouse data on arduino:
    http://www.reddit.com/r/arduino/comm...e_and_made_it/

    tutorial on how to use the teensy joystick outputs
    http://www.pjrc.com/teensy/td_joystick.html

    you definitely have to take it from here, but if you gather information on this, step by step, you should reach your goal. =D

  12. #12
    Junior Member
    Join Date
    Mar 2013
    Location
    Montana
    Posts
    7
    Thanks everyone for all our help. I'm definitely planning on building a USB host shield. I found most of the hardware documentation, and was mostly hoping to see what I could learn from the programming side. We won't actually be building the robot for a while, so I'll have some time to look around for a way to do this. I won't hesitate to post the code it here if I ever figure it out. Thanks!

  13. #13
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,942
    This project would be much easier using a PS2 mouse instead of a USB mouse. PS2 is a much simpler protocol. I believe there's an Arduino library for reading a PS2 mouse, which only needs 2 ordinary pins (maybe one need to have interrupts).

    If you can use a PS2 mouse, I would absolutely recommend going in that direction.

    If the mouse really must be USB, then you'll need a USB host interface, as gltovar mentioned.

  14. #14
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    2,954
    Quote Originally Posted by PaulStoffregen View Post
    This project would be much easier using a PS2 mouse instead of a USB mouse. PS2 is a much simpler protocol. I believe there's an Arduino library for reading a PS2 mouse, which only needs 2 ordinary pins (maybe one need to have interrupts).

    If you can use a PS2 mouse, I would absolutely recommend going in that direction.

    If the mouse really must be USB, then you'll need a USB host interface, as gltovar mentioned.
    Many mice support a USB to PS/2 converter, though whether mice manufacturers still support it and include the dongle, I don't know. My KVM (keyboard, video, mouse switcher) is PS/2 based, so I the converters, but I suspect sooner or later, I will need to upgrade KVM's for USB only keyboard/mice. Of course going back further into history, there were serial mice as well, which used the 9 pin variant of the RS-232 connector.

  15. #15
    Junior Member
    Join Date
    Mar 2013
    Location
    Montana
    Posts
    7
    Sure :D If it seems more feasible to use PS/2 mouse, then so be it. I'm not sure we want to go as far as using a 9 pin RS-232. I've looked at the PS/2-Teensy interface and it seems pretty straightforward. How would that affect the programming side?

  16. #16
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,942
    I'd try this code first. It's very old (back from the days when Arduino used to create an "applet" directory), but looks like it ought to work.

    http://playground.arduino.cc/componentLib/Ps2mouse

    It has an example which claims to read a PS2 mouse and prints the info to the Arduino Serial Monitor. If that works, then all you should need to do is change the serial monitor printing to the joystick stuff you want, using the functions documented at this page:

    http://www.pjrc.com/teensy/td_joystick.html

    If it can't actually read a PS2 mouse, let me know? This one isn't on my library list, but it looks pretty simple. If it doesn't work, I might be able to fix it and add it to my list of libraries supported for Teensy.

  17. #17
    Junior Member
    Join Date
    Mar 2013
    Location
    Montana
    Posts
    7
    Thanks! Is the ps2dev library specific to a particular type of mouse, or could we just plug and play whichever mouse we choose (Razor Mamba, Microsoft trackball, etc. with PS/2 adapter to match the particular driver's style)? Hypothetically, if we used the PS/2 method, we wouldn't necessarily have to build our own keyboard for directional movement either, would we? Or is Teensy limited to the number of inputs?

  18. #18
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,942
    Quote Originally Posted by eKapalka View Post
    Thanks! Is the ps2dev library specific to a particular type of mouse, or could we just plug and play whichever mouse we choose (Razor Mamba, Microsoft trackball, etc. with PS/2 adapter to match the particular driver's style)?
    I'm not familiar with how the other PS/2 mice extended the protocol. They all seem to work pretty well on PCs without telling the software which mouse, so hopefully the protocol is similar enough that you can get the basic info.

    Maybe someone who's more familiar with PS/2 mouse protocols might be able to answer?


    Hypothetically, if we used the PS/2 method, we wouldn't necessarily have to build our own keyboard for directional movement either, would we? Or is Teensy limited to the number of inputs?
    Maybe you're asking about connecting both a PS2 keyboard and a PS2 mouse? The question isn't perfectly clear to me, but that's the closest I can get.

    The main issue I see is that PS2 mouse library appears to work by polling the pins, so it only listens for incoming data while waiting in the read() function. That's somewhat limiting, because if you spend much time doing anything else (more than sending the joystick output, which should be very quick because it just puts data into a USB buffer), you might miss the next incoming mouse data. The PS2 keyboard library is designed differently, with interrupts and a buffer, so incoming data is captured to memory as it arrives. That design lets you do other stuff and check for input when you want.

    I can think of a few ways to do this... some involving more software work (basically, rewriting parts of the libraries), some involving ways to use more hardware that would let you use the existing libraries as-is. But before I spend time writing much more, maybe it'd be best if I understood the question better?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •