Sunday, March 13, 2011

Logitech Dual Action USB Gamepad Interface Using Python

You can get a list of all input device files on your machine by using the command 'ls /dev/input'. My Logitech gamepad is showing up as 'js0'.


Using the cat command on the gamepad's device file 'cat /dev/input/js0' and then pressing some buttons on the game pad spews out a bunch of garbage in the terminal. This lets me know the gamepad is working but I am going to have to do a bit of programming to make some sense of all this gibberish.


Using python we can open and read the device pipe as if it were a normal file and print it in a slightly more readable format.

import sys

# Open the js0 device as if it were a file in read mode.
pipe = open('/dev/input/js0', 'r')

# Loop forever.
while 1:

    # For each character read from the /dev/input/js0 pipe...
    for char in pipe.read(1):
 
        # write to the standard output the string representation of 'char'.
        sys.stdout.write(repr(char))
  
        # Flush the stdout pipe.
        sys.stdout.flush()


By observing the output of the simple python program I notice that each event on the gamepad (button down, button up, axis movement) produces 8 bytes of data. I then went on to modify the program to assemble the incoming data into eight byte messages and print each message to the screen.

# Open the js0 device as if it were a file in read mode.
pipe = open('/dev/input/js0', 'r')

# Create an empty list to store read characters.
msg = []

# Loop forever.
while 1:

    # For each character read from the /dev/input/js0 pipe...
    for char in pipe.read(1):
 
        # append the integer representation of the unicode character read to the msg list.
        msg += [ord(char)]
 
        # If the length of the msg list is 8...
        if len(msg) == 8:
 
            # Print the msg list.
            print msg
   
            # Reset msg as an empty list.
            msg = []


By observing the data in this format I am able to determine a few things:
  • Bytes 0, 1, 2,and 3 appear to count up as messages stream in. This could possibly be useful to determine the order or time relationship between messages.
  • Bytes 4 and 5 are the value of the message.
  • Byte 6 seems to serves two functions.
    • When the device pipe is first opened we receive messages that indicate how many buttons and how many joystick axis are on the gamepad. If the byte is 129 this represents a button or if it is 130 it represent a joystick axis.
    • After these initial messages if the byte is 1 it represents a button event or if its 2 it represents a axis event.
  • Byte 7 identifies the button or axis number that triggered the message.
Using this information I modified the program again to output messages based on the gamepad events.

# Open the js0 device as if it were a file in read mode.
pipe = open('/dev/input/js0', 'r')

# Create an empty list to store read characters.
msg = []

# Loop forever.
while 1:

    # For each character read from the /dev/input/js0 pipe...
    for char in pipe.read(1):
 
        # append the integer representation of the unicode character read to the msg list.
        msg += [ord(char)]
 
        # If the length of the msg list is 8...
        if len(msg) == 8:
 
         # Button event if 6th byte is 1
            if msg[6] == 1:
                if msg[4]  == 1:
                    print 'button', msg[7], 'down'
                else:
                    print 'button', msg[7], 'up'
            
            # Axis event if 6th byte is 2
            elif msg[6] == 2:
                print 'axis', msg[7], msg[5]
   
            # Reset msg as an empty list.
            msg = []


From here I am just a hop, skip, and a jump away from wrapping this into a class and saving the state of the buttons and axis into some sort of easily used data structures. I will be posting a follow up post when I get this concept into a usable format.

Wednesday, February 23, 2011

Big Guns...

The tank guns I was planning on using on my mech Gold Rush were just not cutting it for me. Problems included gear skipping and motors burning up. So I was forced to ditch them and went for something a bit bigger.

Tuesday, February 22, 2011

MechController v1

Spent some time working on the micro controller board that will be going into Gold Rush.


Its a super simple board based around the AVR ATMEGA 644 device. It features only the connections needed for an ax-12 based mech. 5 Ax-12 ports for the four leg and a turret lines, four pin header to attach an xbee explorer board, two three pin headers to digital IO to control dual gun circuitry, separate screw terminals for logic and motor power and an ISP header for programming.

I added 'Faux Silkscreen' by printing out and gluing a sheet of paper with the parts layout and labels onto the ugly fiberglass board.

Friday, January 14, 2011

Trossen Robotics DIY Project Contest #8 Entry

For my entry to the Trossen Robotics DIY Project Contest #8 I have decided to create and document the design of a simple PID velocity DC motor controller. While this idea is not new or novel I feel that many people can take what I have learned from this project and implement it into their own designs. Also this is a project that I can use myself as a component in future robotic projects.



My entry can be found at the following link:
http://upgrayd.blogspot.com/p/pid-velocity-motor-controller.html