Skip to content

Search the documentation

Kit
Firmware

Sensor check — Smart Car (Python)

This example is written twice, once for each firmware. Pick the tab that matches the firmware of your Smart Car: the tab you pick is remembered on the other examples.

Firmware 3.0 renamed the Python names a program writes, and kept none of the old ones: a Smart Car on 2.x does not know the 3.0 names, and a Smart Car on 3.0 does not know the 2.x ones. The name map lists every change. If you open the example from inside MoBot, it picks the version for the firmware of your Smart Car. If you download a file from this page, use the one in the tab for your firmware.

A health check for the whole robot. Every second it reads every sensor it can find and prints one line each. Nothing moves — this one is for looking at the serial monitor while you wave your hand about.

Use it as the first thing you run on a robot that “isn’t working”. It tells you which parts woke up, which did not, and what each one is reading right now.

This is the version for a robot on firmware 3.0. For a robot that still runs firmware 2.x, MoBot opens the 2.x version of this example instead.

Project for firmware 3.0: 02-sensor-check.firmware-3.mobot. Open it in MoBot.

  • A Smart Car.
  • A USB cable and the serial monitor open.
  • Handy props: a sheet of white paper with a strip of black tape on it, and something red (a marker lid, a piece of card).
  1. Open 02-sensor-check.firmware-3.mobot in MoBot and press Run.
  2. Read the list at the top: that is which sensors this robot really has.
  3. Then watch the reports scroll past and make the numbers move.

First, once:

This robot has:
line_sensor yes
distance_sensor yes
color_sensor yes
compass yes
battery yes

The last line says NO - not on this board, or it failed to start on an early Smart Car with the L298N motor chip, which has no battery sensor.

Then a report every second, and LED 1 blinking once per report so you can tell it is still alive:

do this and this changes
hold the robot over white paper, then over the black tape the five Line eyes numbers flip between 0 and 1
put the black tape under the middle eye only exactly on the line: True
move your hand towards the front of the robot Distance counts down; under 20 cm it says something within 20 cm: True
hold something red under the colour sensor the first of the four colour numbers jumps up and looks red: True

With a clear space in front of the sensor (nothing for its sound to bounce off), Distance shows 200.0 cm. A reading of -1 is rare. It means the reading itself failed, and the line then says the reading failed.

If a sensor says NO in the list at the top, look at the robot’s own start-up messages: the firmware prints Distance sensor init error: and so on when a part fails, and then leaves it switched off until the robot restarts.

Every sensor on the robot starts life as None. If it fails to wake up, it stays None — the robot does not crash, it just quietly has no colour sensor. Then the first line of your program that says robot.color_sensor.read() fails with:

AttributeError: 'NoneType' object has no attribute 'read'

That is why every reading in this file is behind if have("color_sensor"):. There is no block that can ask this question, which is one good reason to come to Python.

This is a reading copy of the program in the project: editing it changes nothing, so open the project and edit it there.

main.py
# main.py — Sensor check (Python) (robotics)
#
# A reading copy of one file inside 02-sensor-check.firmware-3.mobot.
# Editing this copy changes nothing: open the project and edit it there.
# This is the entry point — the board runs it as main.py.
# Written for firmware 3.0; the 2.x version is 02-sensor-check.main.py.
"""Sensor check - MoBot Smart Car.
A health check for the whole robot. Every second it reads every sensor it can
find and prints one tidy line for each. Wave your hand, roll it over black tape,
hold something red in front of it, and watch the numbers change.
Two things this teaches that no block can:
1. A sensor that did not start up is set to None, not missing. So the honest
way to ask "do I have a colour sensor?" is `have("color_sensor")`, and a
program that skips the check crashes with
`AttributeError: 'NoneType' object has no attribute 'read'`.
2. Different robots have different sensors. The standard Smart Car has a
battery sensor; the early Smart Car with the L298N motor chip does not. The
same program runs on both, and simply says so.
"""
import time
PERIOD = 1.0 # seconds between reports
LINE_PATTERN = [0, 0, 1, 0, 0] # "only the middle eye sees the line"
RED = (30000, 8000, 8000) # red, green, blue as raw counts - see check_color()
def have(part):
"""True if this robot really has that part and it started up properly."""
return getattr(robot, part, None) is not None
def check_line():
"""The five line-following eyes underneath, left to right."""
eyes = robot.line_sensor.read()
middle = robot.line_sensor.read(2)
on_the_line = robot.line_sensor.matches(LINE_PATTERN)
print(" Line eyes ", eyes, " middle:", middle, " exactly on the line:", on_the_line)
def check_distance():
"""The distance sensor at the front."""
cm = robot.distance_sensor.distance_cm()
mm = robot.distance_sensor.distance_mm()
near = robot.distance_sensor.sees_object(20)
if cm == -1:
# -1 is rare: it means the reading itself failed. A clear space in
# front of the sensor is NOT -1, it reads 200 cm.
print(" Distance the reading failed")
else:
print(" Distance ", cm, "cm (", mm, "mm ) something within 20 cm:", near)
def check_color():
"""The colour sensor underneath.
read() gives four raw counts: red, green, blue and "clear" (how bright
it is altogether). They are big numbers - up to 65535 - not 0 to 255.
"""
reading = robot.color_sensor.read()
reddish = robot.color_sensor.is_color(RED, reading, tolerance=8000)
print(" Colour r,g,b,clear =", reading, " looks red:", reddish)
def check_battery():
"""The standard Smart Car has this; the early L298N Smart Car does not."""
volts = robot.battery.voltage()
print(" Battery ", volts, "V (6.0 V is empty, 9.0 V is full)")
print("Sensor check starting. Ctrl-C in the serial monitor to stop.")
print("This robot has:")
for part in ("line_sensor", "distance_sensor", "color_sensor", "compass", "battery"):
print(" ", part, "yes" if have(part) else "NO - not on this board, or it failed to start")
print("")
report = 0
while True:
report = report + 1
print("--- report", report, "---")
if have("line_sensor"):
check_line()
if have("distance_sensor"):
check_distance()
if have("color_sensor"):
check_color()
if have("battery"):
check_battery()
# A heartbeat, so you can tell the loop is alive even with the screen off.
robot.led1.toggle()
time.sleep(PERIOD)
  • Change LINE_PATTERN to [1, 1, 1, 1, 1] and hold the robot in the air — exactly on the line should become True.
  • Print only the distance, ten times a second, and use it as a tape measure.
  • Hold your red thing under the sensor, copy the three numbers it prints into RED, and make the tolerance smaller.