Controlling the RPy-Rover’s motors

A rover is only a rover if it can move around. To do this, at the core of our Euro RPyRover is a Dagu Rover 5 chassis with tracks powered by two DC motors.  In this article we explain how to use a Raspberry Pi, Python and a Qik controller to drive these motors.

Dagu chassis

QiK motor controller


We used:

  • Raspberry Pi B+
  • Dagu Rover 5
  • Qik 2s9v1
  • USB battery pack (as used to charge mobile phones on the go)
  • AA batteries
  • Breadboard and wires


Qik 1 GND RPi 6 GND
Qik 2 Vcc RPi 1 3V3
Qik 4 UART TX RPi 10 RXD
Qik 7 and 8 M1 Motor 1
Qik 9 and 10 M0 Motor 0
Qik 11 GND Motor battery pack Negative
Qik 12 VMOT Motor battery pack Positive

Using the serial port

By default RPi outputs operating system logs to the serial port, so to use the serial port to communicate with a component we must first disable this feature.

You can see here how to do it in more detail, but basically you edit the codlin.txt file with the command:

sudo nano /boot/cmdline.txt

It should contain a line similar to:

dwc_otg.lpm_enable=0 rpitestmode=1
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1
root=/dev/mmcblk0p2 rootfstype=ext4 rootwait

You need to delete the two parameters in red to get the following:

dwc_otg.lpm_enable=0 rpitestmode=1 console=tty1
root=/dev/mmcblk0p2 rootfstype=ext4 rootwait

Save (Ctrl-X and then OK) and then reboot with the command

sudo reboot

Commanding the Qik

The function of the Qik 2s9v1 is to prevent the electrical noise and power demand of the motors from disturbing the microcontroller (the Raspberry Pi). In this circuit the RPi is powered by a USB battery pack and sends commands to the Qik through the serial bus, and the Qik drives the motors using a separate power source (the AA batteries).

The serial bus has just two wires (and a third for ground, naturally), so we need the RPi pins named TXD (transmit) and RXD (receive). The TX of the RPi connect to the RX of the Qik and vice-versa.

To command the two DC motors, named M0 and M1, we need to send  commands, through the serial bus. Each command is made of one command byte, followed by a parameter byte that represents the speed. Actually the speed is only 7 bits, so it’s value is between 0 (stopped) and 127 (maximum speed).

The Qik commands we’ll use are:

  • 0x88 (hexadecimal) = Motor 0 forward
  • 0x8A = Motor 0 reverse
  • 0x8C = Motor 1 forward
  • 0x8E = Motor 1 reverse

Example: to drive the motor M0 forward at half speed we would send (0x88, 0x40), because 0x40 (hexadecimal) is 64 in decimal (approximately half of 127).

Python program

To use the serial bus of the RPi we will use the PySerial library. On OS X or Linux computer it will already be installed. If you have a Windows machine you can learn to install it here.

In our Python program we  start by importing the necessary libraries and opening the serial port at 9600 baud (no need for higher baud rate).

import serial
import time
import math 

def open_serial_port():
    ser = serial.Serial('/dev/ttyAMA0', 9600)
    ser.write(bytearray([170]))# exit autodetect mode

We then define four functions to move the rover forward (both motors moving forward), backward (both motors reversed), rotate left (one motor reversed and the other forward) and right.

As changing the speed of the motors instantly from zero to the maximum speed shook the chassis we decided to increase the speed gradually over two seconds, varying the parameter  20 times at tenth of a second intervals. This the use of the “Math” and “Time” libraries, that come with Python, and additional functions to decelerate the speed gradually back to zero.

def forward():
    for t in range(0,21):

def break_forward():
    for t in range(20,-1):

You can download the motor library on the Euro RPy-Rover’s GitHub Python repository.

Next tutorial: Capturing images from the rover

One thought on “Controlling the RPy-Rover’s motors

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s