Anth's Computer Cave

Stirling vacuum, electronics

24th December, 2018

Updated 24th December, 2018

In the previous article we looked at the electronics for our robot, Stirling.

In this article we'll cover the software we are writing to control Stirling and, more importantly, to teach Stirling how to control itself.

We'll update the article regularly as we get our heads around everything.

A Stirling robotic vacuum cleaner. Picture: Anthony Hartup.
Stirling in its current stupid form

We have written a few programs for testing initial movement functions.

There is a basic command-line program that can turn the robot in place, or move it in any direction, and a similar program that also maps the routes between rooms. We've also built a more complex phone-based GUI the uses your phone as a steering wheel and throttle.

These programs should drive any dual-motor, differential-steer Raspi-operated vehicles.

Motor Tester

Motor_tester is the basic script we used to test some initial robot movements.

You start this program from an SHH terminal, then type commands to tell the robot how to move.

Typing turn_in_place("left", 60, 2) will swivell the robot left at 60% PWM for two seconds.

Typing drive_straight_or_veer("forwards", 0, 60, 5) will drive the vehicle straight ahead at 60% PWM for five seconds.

You can download the script here.

The only thing you'll need to change in the code will be the GPIO pin numbers for your motors. You'll find the pins array on line 16 of

assigning the PWM pins for motors

There are entries for each motor's forward and reverse pins, plus an enabler pin. If your motor controller doesn't need enabler pins you can remove the enab entries from the pins array.

Save the program and open an SSH terminal to your Raspberry Pi. Navigate to the robot test folder and open a Python terminal by typing:


At the prompt type:

import robot_test

To test forward movement, type:

robot_test.drive_straight_or_veer("forwards", 0, 60, 5)

The first argument tells the robot to move forwards. The next argument, 0, tells it to drive straight ahead. A negative value will veer left, a positive value will veer right. The third argument, 60, sets the PWM duty-cycle to 60%, while the final argument, 5, sets the motor run-time to five seconds.

Your robot should move forward now. I you find it veers in one direction with the steering set to zero, you have one motor that is faster than the other. We can fix that in the code. To close the program, type:


This will switch off and clean up the GPIO pins. Now press CONTROL + D to exit Python.

Open the file again and go to line 22.

Adjusting the PWM speed for motors

The adjustments array sets speed adjustment for individual motor pins. The default is the maximum, 1.0

If your robot is veering to the left it means the right motor is moving faster than the left motor. You can slow the right motor by reducing the value for the 'pwm_3' and 'pwm_4' entries in the array. For example, if the robot is only veering slightly left you could try changing to .99. Keep adjusting down until the robot moves straight.

Now you can try swivelling the robot in place. Type:

robot_test.turn_in_place("left", 60, 5)

Here the first argument is rotation direction, the second is PWM duty-cycle and the third is run-time in seconds.

The robot should rotate for five seconds. This should confirm your motor_speed adjustment. If the robot spins evenly your motors are spinning at the same speed.

That's all this initial script does, next week we'll have the next version, RobotMoves.


RobotMoves will become the movement module for all of Stirling's other programs to control.

This builds on our initial script to provide a comlete movement system. It has functions for automatially calibrating motor speeds, converting run-time to meters and degrees.

It will have all the communications and interrupts required to work with the programs running Stirlings sensors to enable collision avoidance, unstable-ground detection, etc.

It also has gradual take-off, with the PWM duty-cycles accelerating gradually to avoid jolting.

RobotMoves should be ready next week.

Robot Map Trainer

Robot Map Trainer is another command-line program that will train Stirling to navigate the Cave

First you assign a home-room where the robot will normally live. Then you enter the name of an adjoining room you wish to drive to, then follow the prompts to maually drive the robot there.

The program will log each move and also calculate a return path to its original location.

Each room has a map entry for each adjacent room, and there is an overall list that tells the robot which rooms it needs to traverse to reach non-adjacent rooms.

Note that these are approximate measurements. As mentioned, Stirling is currently completely blind, and has no knowledge of it's surroundings. As we add new awareness sensors to Stirling, we'll ad functions to fine-tune these routes.

Robot Map Trainer will be available in a couple of weeks.

Phone Wheel

Phone Wheel is our phone GUI that turns your phone in a steering wheel and throttle.

It's comically erratic at the moment. The accelerometer and gyroscope in smart phones are a bit fickle, and we need to build methods to detect and ignore irrational readings.

There are also bugs to iron out in our communications. Stirling occasionally takes off on its own wacky paths almost like it did with it's old brain.

This is an issue at the moment because we've included no over-current protections for the motors, meaning getting stuck against a wall or other solid object could quickly fry things.

Phone wheel will be a few weeks away yet.

That's all for today, check back soon.





Leave a comment on this article