Introduction:
In this section, we will discuss how to interface with a generic USB game controller and communicate with the Raspberry Pi on the robot. All code is written in Python 3 or higher. The scripts work by passing raw controller values from the client computer to the server (Raspberry Pi). The Raspberry Pi then interprets these values and controls the servos, interfacing through a separate python library ServoInterface.py
How To:
Prerequisites
In order to start this task, you must have:
- A functioning wireless access point on the Raspberry Pi
- The following installed on the host computer
- Python 3+
- Pygame
- Python-socket (Should be pre-installed)
Materials needed
- Generic USB Game Controller
- Host computer
- Robot (or at least its Raspberry Pi)
Basic Steps
- Power up the Raspberry Pi
- Connect the host computer to the Pi’s network
- Connect the game controller to the host computer
Resources:
- Put Server.py in the Raspberry Pi’s /home directory and run it with IDLE.
- Put ServoInterface.py in the same folder as Server.py
- Start Client.py on the server computer (ensure that the steps above are followed)
- If the process has succeeded, the server computer’s IP address will appear in the output of Server.py
- The robot will start responding to joystick inputs
Ensure the robot is working by following the Basic Steps listed above. Many features of the robot can be configured, if needed, follow the steps below for the modifications you want to make.
Change travel speed
speedMultiplier_rightStick is a multiplier for the joystick input. By default, this value is set to 100.0
Change arm servo speed
cameraSpeed keeps track of the travel speed for all arm servos. Change this to control the speed of the arm servos. Avoid changing speedMultiplier_leftStick or speedMultiplier_hat, as these are smoothing values. Changing these may result in shakier camera movements or extremely small movements.
Changing communication port
Change the TCP_PORT value in both Server.py and Client.py
Change button functions
buttonScripts maps all buttons to functions to execute when a button press is received. Replace printButton with the function that you want to be called when the button is pushed. The comments denote the button each element is connected to.
- Establish a connection between the server and the robot
- Send joystick positions from the server to the robot
- Act on joystick input
- (Optional) Robot sends operational data back to server (wheel rotation rate, arm position, servo modes, etc.)