In the latest issue of HackSpace magazine, learn how to create a robot which can go forwards, backwards, sideways, diagonally, and turn on the spot. The mecanum wheels allow the robot to navigate the tightest of spaces.
Take your Raspberry Pi on the move by designing and making a wheeled robot This tutorial will explain how you can design and make your own robot. Start by designing your own chassis and mount mecanum wheels with full direction control. The robot will then need four separate motors individually controlled with a H-bridge driver for each wheel. Take control by learning how to use AntiMicroX to control the robot with a gamepad.
Mecanum wheels are a type of omnidirectional wheel which can be used to move a robot vehicle in multiple directions. They are particularly good at getting into tight spaces as the wheels can propel the robot forwards and backwards as normal, but can then also move diagonally or sideways without any forward or backwards movement.
This is achieved by having rollers angled around the wheel. The rollers of diagonally opposite wheels need to be in the same direction. Turning the wheels in a certain combination will determine the direction. This is easiest to understand through watching the video on YouTube:
Creating a chassis
For any kind of vehicle, you will need a chassis to mount the motors and electronics. You can use any method you choose. The base should be thin enough to accommodate the size of the wheels, but otherwise most materials can be used. You could use plywood, acrylic sheet, or even thick cardboard.
If you have access to a 3D printer then you can design your own 3D-printed chassis, or use this one. This is shown in Figure 1.
You will need some way of fixing the motors. Brackets are included in the 3D printer files, or you can purchase suitable brackets which are included in the component list.
Raspberry Pi Zero W
Whilst any model of Raspberry Pi can be used, a Raspberry Pi Zero W or a Raspberry Pi Zero 2 W are well-suited for a robot vehicle. They are small, making them easier to fit on a mobile robot, and they also have lower power requirements than some of the other models. The wireless capability is useful when programming the robot and for getting the controller working. A micro-USB to USB adapter is needed for connecting the controller. A shim adapter is also recommended.
Powering the robot
The motors are designed for 6 V, whereas a Raspberry Pi needs a 5 V power supply. In practice, you can normally use a single power supply for both your Raspberry Pi and the motors.
The different options are a 6 V power supply (4 × AA batteries) or a USB power bank. The breadboard layout shows both options. Raspberry Pi is powered through GPIO 2 using a diode which protects against reverse voltage and drops the voltage slightly. If using the USB power bank and the voltage is too low, then the diode bypassed if required. In either case, power must NOT be connected to your Raspberry Pi power input socket when powered through the GPIO.
H-bridge motor controller
To allow the motors to go both forwards and backwards, they each need an H-bridge. A good choice is the TB6612FNG driver. This driver can handle up to 1.2 A of current, and has two H-bridge circuits on a single SMD integrated circuit. To be able to use these with a breadboard, they are available on a SparkFun motor driver board. This is shown in Figure 2.
Along with the inputs for forwards and backwards, each H-bridge needs a PWM signal to set the speed of the motor. A single PWM output can be used from your Raspberry Pi, which will ensure that all the motors run at the same speed.
Wiring up the circuit
The wiring diagram is shown in Figure 3. This is just a case of wiring up the motor driver inputs with appropriate pins from your Raspberry Pi. Note that the breadboard has two different power rails. The top rail is connected to the 6 V or 5 V supply for the motors, whereas the bottom rail is connected to the 3.3 V pin on your Raspberry Pi to run the motor driver at 3.3 V (so that it is at the same voltage as the GPIO pins).
A schematic diagram is shown in Figure 4. Note that the labels beginning with D indicate connections to the motor driver input, and M is used for the motor connections.
Setting up the Raspberry Pi
When setting up your Raspberry Pi, you may find it easier to enable VNC, which you can do through the Raspberry Pi Configuration tool. This will allow you to connect from another computer using a VNC client. Using VNC, you won’t need to connect a keyboard or mouse, allowing the only USB port to be used for the USB dongle for the gamepad controller.
If using a game controller, then the AntiMicroX software is needed, which can be installed using:
sudo apt install antimicro
This will be configured later in the tutorial.
Programming motor control
The motors are controlled using GPIO Zero. There is an instance of
Motor for each of the motor drivers with a pin for forwards and reverse. There is then one more pin for the
PWMOutputDevice which is used to control the speed of all the motors.
direction holds the direction of all four motors based on the selected direction. These are
1 for the motor to go forward,
-1 for the motor to go in reverse, and
0 to stop. It does not provide speed control, which is included in the code available from the GitHub repository.
Adding keyboard support
One thing about Python is that it doesn’t directly provide a way to read input characters except when the ENTER key is pressed. There are various different ways around this, the one used here is a
getch() function which provides similar functionality to the C/C++
getch() function. This will pause the
while loop until a key is pressed, and then return the key into the
direction dictionary provides mapping between the numerical keys and the selected direction.
A wireless USB gamepad will be used, which needs to be mapped to the appropriate keys. An example gamepad is shown in Figure 5.
Using gamepad with AntiMicroX
AntiMicroX is an application which converts gamepad instructions into keyboard or similar instructions. Start AntiMicroX from the accessories menu. This will add a game controller icon to the top right of the screen. Clicking on that will allow you to map each of the buttons to the relevant keys, and this is illustrated in Figure 6.
You can map each button on the gamepad, or import the suggested layout which is included in the GitHub repository.
The gamepad must be recognised by Linux to work with AntiMicroX. Some controllers appear to work consistently, whereas others need reconnecting once or twice before they will work with AntiMicroX.
If the gamepad doesn’t work after a reboot, then you may need to try disconnecting and reconnecting the USB dongle after AntiMicroX is running. Then, choosing Update Joysticks from the menu.
The robot can be controlled using a game controller, or using a keyboard.
With some extra electronics and code, the robot can do more. There are holes included in the front of the chassis which can be used to add an ultrasonic distance sensor to detect and avoid nearby objects. Or, you could add a line sensor to allow your robot to follow a line drawn along the floor.