Anth's Computer Cave

Stirling Robot software: RobotSensors

6th March, 2019

In this article we'll cover the RobotMoves sensor module, To see the other RobotMoves modules, click the Software Overview button above.

You can download the RobotMoves suite of modules here. This was updated on the 6th of February, 2019.

A Stirling robotic vacuum cleaner with added sensors and a new head. Picture: Anthony Hartup.
Stirling in its current form with its new head

Robot Sensors

Inside the robot_moves folder is

At the moment it handles distance-sensors and PIR movement-sensors, with more sensor-types on the way.

You import this into your program alongside robot_moves and check any or all of your sensors with single-line commands

You'll need to add your sensor details to the file.

On line 33 you can add distance sensors.

assigning GPIO pins for a distance sensor in RobotSensors

By default there are three sensors, forward, left and right. Change the 'echo' and 'trigger' pin numbers to suit your sensors. To disable a sensor, give the 'echo' and 'trigger' pins for that sensor a value of zero. For extra sensors you'll need to create new entries.

The 'adjustment' key is the sensor's setback in centimeters from the edge of your robot. In my case all Stirling's sensors are set back exactly three centimeters from the edge. This is automatically added to the final measurement.

On line 39 you can add PIR movement sensors.

assigning GPIO pins for a PIR movement sensor in RobotSensors

Change the 'pin' value to the GPIO nuber of your sensor's pin.

The 'event_state' entry denotes the type of PIR sensor you are using. Most PIRs are low by default and go high when they sense movement. For this type of sensor set the 'event_state' to 1. If instead your sensor is high by default and goes low on movement, set 'event_state' to 0.

To test your sensors, open a terminal in the robot_moves folder and type:


Then, at the Python prompt, type:

import robot_sensors

Distance sensors

To check the sample sensor called 'forward', type:


The program will print the distance in centimeters from that sensor.

By default the function will update the measurement in the distance-sensor array, but there may be times when you wish to get the distance without updating the stored distance. For example I use this feature to check left and right diagonal flanks using the forward sensor while still holding the original forward distance measured beforehand. To do this, type:

robot_sensors.check_distance('forward', store='no')

To check all your sensors, type:


Distance sensor timeouts

When the distance to measure is larger than the maximum range of the sensor, the check_distance function will timeout. You have several ways to handle these timeouts.

I find these sensors rarely timeout for any reason other than range, so for non-critical applications you can assume there is substantial clearance in that direction. You can set a default distance to assume when a timeout occurs.

You can set an assumed distance with the timeout_value on line 62.

My sensors have a range of three to four metres, so I set a very conservative estimate of 80cms when a timeout occurs, knowing the actuall distance will be much greater.

For mission-critical applications you really shouldn't assume anything. For those applications set the timeout value to zero and treat the timeout as an unkonwn error.

Movement sensors

To test your PIR movement sensor, type:


The function will return 'yes' if movement is detected, or 'no' if not.

To clean up the GPIO pins and close the program, type:




Previous: RobotMoves movent module

Next: RobotMoves Avoidance module



Leave a comment on this article

Leave a comment on this article