Skip to content

Search the documentation

Kit
Firmware

The robot object

On firmware 3.0, every program talks to one object:

  • robot on the Smart Car and the Mini.
  • home on the Smart Home.

You do not import it and you do not create it. The board builds it while it switches on and puts the name into your main.py. It is the same robot the blocks talk to. You are only typing the words yourself.

Each part of the robot is an attribute of the object. You reach it with a dot. A part has methods you call, and sometimes properties you read. A method is something the part can do, such as on(). A property is something you read, such as speed.

robot.led1.on() # a light
robot.left_motor.forward(50) # a wheel, at 50 percent
robot.distance_sensor.distance_cm() # a sensor reading, in centimetres

The last line works on the Smart Car. On the Mini, switch the distance sensor on first with robot.enable_distance_sensor().

Names say what a part does, not which chip it uses. The same idea has the same name on every kit. Speeds and brightness are in percent, 0 to 100. Colours are 0 to 255. Angles are in degrees.

These tables list the attributes. Every part has its own page in the reference: Smart Car, Mini, Smart Home.

Attribute What it is
left_motor, right_motor The two drive wheels: forward(speed), backward(speed), drive(speed), set_speed(speed), stop(), brake(), and speed.
led1, led2 The two lights: on(), off(), toggle(), set(on), set_brightness(percent).
buzzer Plays songs in the background: play_song(song), stop(), pause(), resume(), is_playing(), set_volume(percent).
line_sensor The five eyes that see a dark line on a light floor: read(), matches(pattern).
distance_sensor The ultrasound sensor: distance_cm(), distance_mm(), sees_object(). With nothing in front of it, it reads 200 centimetres.
color_sensor read(), is_color(color).
compass heading(), calibrate(), is_calibrated(), turn_to(heading).
battery voltage(). Not on the early L298N board, which has no battery sensor.
gamepad The Game Pad in the MoBot app: x, y, is_pressed(name), buttons().
bluetooth The link to the app: is_connected().
servo1 to servo4, servos Servo motors. Off until you call robot.enable_servos().
joystick1, joystick2 Two joysticks. Off until you call robot.enable_joysticks().
name, set_name(name), mode The robot’s Bluetooth name, and its mode number (255 is Programmable).

The Mini has the same left_motor, right_motor, led1, led2, buzzer, gamepad, bluetooth, name, set_name() and mode as the Smart Car, with the same words. It also has:

Attribute What it is
rgb_led The colour light: set_color(r, g, b), set_hex(color), set_brightness(percent), off(), and effects such as blink() and rainbow() that need tick() in your loop.
distance_sensor Off until you call robot.enable_distance_sensor().
line_sensor Two eyes. Off until you call robot.enable_line_sensor().

The distance sensor and the line sensor use the same two pins. Switch on the one you need.

Attribute What it is
temperature_sensor temperature() in degrees Celsius, humidity() in percent.
motion_sensor motion_detected().
gas_sensor, light_sensor level(), a raw reading from 0 to 4095.
knob position(), a raw reading from 0 to 4095.
button is_pressed().
battery voltage().
rfid The card reader: read_card(), is_card_present().
rgb_led The colour lamp: set_color(r, g, b).
led1, led2 In the firmware, but the current board has no light on either of them, so nothing lights. Use rgb_led for a light you can see.
display The 128 x 64 screen: show_text(text, x, y), clear(), and drawing methods.
number_display The three-digit display: show_number(n), show_text(text), set_digit(index, segments).
buzzer play_tone(frequency, duration), play_note(note, duration), play_notes(sequence).
servo The door: set_angle(angle), move_to(angle), center(), sweep().
fan on(), off(), set_speed(percent).
wifi The house’s Wi-Fi: is_connected(), ip(), house_id().
dashboard The house’s web page: start(), stop(), and add_switch(), add_slider(), add_color(), add_text().

On the current Smart Home board, neither led1 nor led2 has a light, and led1 also shares its pin with the number display. See Troubleshooting.

A part that is not there is None, which is Python’s word for “nothing here”. Reading it is not an error. The error comes when you use it, for example robot.servo1.center() when robot.servo1 is None.

A part can be None for three reasons:

  1. It is switched off until you ask. The Smart Car’s servos and joysticks, and the Mini’s distance sensor and line sensor, start as None. Call the enable_ method first.
  2. It did not start. While the robot switches on, it prints a line such as Left motor init error: for each part that failed, and goes on with the rest. On the Smart Home, rfid and display are None if they cannot be started.
  3. It is not fitted. The Smart Car’s battery is None on the early L298N board, which has no battery divider.

So check before you use a part that might be missing. This is how the Smart Car’s servo example does it:

print("Waking the servos up...")
robot.enable_servos()
arm = robot.servo1
if arm is None:
print("servo1 did not start. Is anything plugged into pin 32?")
raise SystemExit

raise SystemExit ends the program on the spot, so the lines after it do not run.

Your main.py can say robot without importing anything. Other files in your project do not get that name. If a file just says robot.line_sensor, it fails with NameError: name 'robot' isn't defined. Ask for the robot once, at the top of that file:

from smart_car import robot

On the Mini the line is from smart_car_mini import robot. On the Smart Home it is from smart_home import home. It is not a copy of the robot. It is the very same one that main.py uses.

Two small helpers are shared by all three kits. Import them like this:

from mobot import map_range, sleep_ms
  • map_range(value, in_min, in_max, out_min, out_max) converts a value from one range to another, for example 0 to 1023 to 0 to 100. The result is a float, which is a number with a decimal point, such as 25.0.
  • sleep_ms(ms) waits ms milliseconds. On a robot, inside one of the app’s modes it is also the place where the mode can be stopped. In your own program it simply waits.

Here is map_range at work in the Smart Car’s Game Pad example. The first lines import it. The second block turns how far the stick is pushed, from 0 to 127, into a wheel speed from 0 to TOP_SPEED:

import time
from mobot import map_range # stretches a number from one range into another
def drive_one(motor, mix):
"""mix runs -127..127. Negative means backwards."""
speed = int(map_range(abs(mix), 0, 127, 0, TOP_SPEED))

The robots also have SONGS, a dictionary of built-in songs for buzzer.play_song(). A dictionary is a collection that you look things up in by name, as in SONGS['demo']. Import it with from indicators import SONGS. On the Smart Home, the ready-made tunes are in SEQUENCES, with from sounds import SEQUENCES.