Control wheels individually with xbox 360 controller

I just got the GoPiGo to work with a wireless xbox 360 controller and thought I’d share how I did it for anyone interested.

Step 1: Install the driver for the xbox 360 controller on you Raspberry Pi

sudo apt-get install xboxdrv

Step 2: Test controller input

Connect the controller or wireless receiver to your Raspberry Pi and run the following command:
sudo xboxdrv
Events will be printed in the terminal when you press buttons on your controller.
If that doesn’t work, try using sudo xboxdrv --detach-kernel-driver.
Press control+c to stop testing and run sudo xboxdrv --silent & to keep listening for controller input in the background.

Step3: Get the GoPiGo files

Find instructions under step 3 at http://www.dexterindustries.com/GoPiGo/getting-started-with-your-gopigo-raspberry-pi-robot-kit/sdcard/

Step 4: Get XboxController.py

Download it from https://github.com/martinohanlon/XboxController/blob/master/XboxController.py and save it to the GoPiGo/Software/Python folder.

Step 5: Use my script to control your robot with the controller

This script lets you control the wheels individually with the two joysticks on the controller. Create a file called ‘vroom.py’ in the GoPiGo/Software/Python folder and copy in the following code:

#!/usr/bin/env python
from gopigo import *

import XboxController

# exit gracefully
def exit():
        stop()
        xboxCont.stop()
        print 'bye!'

# set speed for one of the two wheels
# side is m1_cmd (right) or m2_cmd (left)
# speed is between -255 (backwards) and 255 (forwards)
def setWheelSpeed(side, speed):
        direction = 1 if speed > 0 else 0
        absSpeed = int(abs(speed))
        write_i2c_block(address, side+[direction, absSpeed, 0])

# handle controller input
# this function prints input values so you can easily add and/or change behavior
def handleInput(controlId, value):
        print "Control id = {}, Value = {}".format(controlId, value)
        if controlId == 1:      # left joystick
                setWheelSpeed(m2_cmd, value)
        elif controlId == 3:    # right joystick
                setWheelSpeed(m1_cmd, value)
        elif controlId == 8:    # x button
                exit()

xboxCont = XboxController.XboxController(
    controllerCallBack = handleInput,
    joystickNo = 0,
    deadzone = 25,      # ignore very small joystick offsets
    scale = 255,        # match scale used by motors
    invertYAxis = True  # pushing joystick up now gives positive values
)

xboxCont.start()

Step 6: Control your GoPiGo!

Start the script with python vroom.py. Exit by pressing the x button on your controller.

@franklnrs
sorry cant get that script of yours to work. a lot of syntax error and then it says the code doesnt work