Projects/Linkbot GamePad

From LinkbotLabs
Jump to: navigation, search

This brief article will help you get started controlling your Linkbot with a joystick or gamepad. For best results, you should already have a little knowledge of how to program in Python.


To do this tutorial, you will need:

  • A computer with Windows, Mac, or Linux, or a Raspberry Pi
  • Python3
  • PyLinkbot. Instructions for installing both Python3 and PyLinkbot can be found here
  • pygame
  • A gamepad or joystick that connects to your computer
  • A Linkbot-I with two wheels

The Python Code

The following Python code works for my particular setup, which is a ThrustMaster "X-Box" style game-pad connected directly to my computer's USB port. You may have to modify which axis controls what part of the robot on lines 54 and 55 to get it to work properly for you. Save this into a file called ""

#!/usr/bin/env python3                                                
import pygame                                                                    
import linkbot                                                                   
import sys                                                                       
import math                                                                      
def main():                                                                      
    serialID = input(                                                            
        'Please enter the serial ID of the Linkbot you want to control: ')       
        robot = linkbot.Linkbot(serialID)                                        
        print('Could not connect to robot.')                                     
    print('Press "CTRL-C" to quit.')                                             
    # Initialize pygame                                                          
    clock = pygame.time.Clock()                                                  
    done = False                                                                 
    while not done:                                                              
        for event in pygame.event.get():                                         
            if event.type == pygame.QUIT:                                        
                done = True                                                      
            if event.type == pygame.KEYDOWN:                                     
                if event.key.unicode == 'q':                                     
                    done = True                                                  
        # The following block of commented code can be used to print information 
        # about all connected joysticks and their current states.                
        for i in range (pygame.joystick.get_count()):                            
            joystick = pygame.joystick.Joystick(i)                               
            print('Joystick {}:'.format(i))                                      
            for i, axis in enumerate(                                            
                    map(joystick.get_axis, range(joystick.get_numaxes()))):      
                print('Axis {} value: {:>6.3f}'.format(i, axis))                 
            for i, button in enumerate(                                          
                    map(joystick.get_button, range(joystick.get_numbuttons()))): 
                print('Button {:>2} value: {}'.format(i,button))                 
        # For my setup, I want to use joystick 0 to control the robot.           
        # Axis 1 controls forward-backward (but is negative), and axis 0         
        # controls left/right.                                                   
        joystick = pygame.joystick.Joystick(0)                                   
        drive_axis = joystick.get_axis(1)                                        
        turn_axis = joystick.get_axis(0)                                         
        motorpower1 = (-255*drive_axis) + (255*turn_axis)                        
        motorpower3 = (255*drive_axis) + (255*turn_axis)                         
        robot.setMotorPowers(int(motorpower1), 0, int(motorpower3))              
        # Make button A make a siren sound                                       
        if joystick.get_button(0):                                               
            omega = 2*math.pi                                                    
            amplitude = 30                                                       
            f = amplitude*math.sin(omega * (pygame.time.get_ticks()/1000)) + 440 
if __name__ == '__main__':                                                       


Run the source code on a command line. In windows, the command will be something like


In Mac or Linux, it might be something like


When you run the program, it will ask you for a robot's Serial ID. Enter the four-digit alphanumeric ID. The robot can either be directly attached to your computer with a USB cable, or you can control it wirelessly if you have a dongle or second robot connected to your computer.