Skip to content

Search the documentation

Kit
Firmware

Drive the Mini — Mini (Python)

This example is written twice, once for each firmware. Pick the tab that matches the firmware of your Mini: 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 Mini on 2.x does not know the 3.0 names, and a Mini 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 Mini. If you download a file from this page, use the one in the tab for your firmware.

The Mini’s whole motor vocabulary. Forward, backward, faster, stop(), brake(), “what are you doing right now?”, and a wheel that has been fitted the wrong way round.

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

Project for firmware 3.0: 01-mini-drive.firmware-3.mobot. Open it in MoBot.

⚠ Before anything else: a fresh Mini does not run your program

Section titled “⚠ Before anything else: a fresh Mini does not run your program”

A new Smart Car runs the program you save. A Mini does not, out of the box. It starts in Manual driving, the mode the phone app drives, and your program only runs while the Mini is set to Programmable.

Set it once, in either of two ways. The Mini keeps its mode, even when it is switched off.

From the computer. Press Save. If the Mini is in another mode, MoBot asks: “The robot is in Manual driving. Switch it to Programmable to run your program?” Answer Switch to Programmable. (If you answer Keep Manual driving, your program is saved but does not run.)

From the phone. Open the MoBot app, connect to the Mini and tap the Programmable tile. The home screen then says Robot mode: Programmable.

If MoBot says the robot did not answer, choose Programmable in the MoBot app and try again. If the app has no Programmable tile, update the app. Your program stays on the robot. To drive from the phone again, tap another tile.

If you save this file and the Mini does nothing, look at the serial monitor when it starts: a Mini in another mode with a program on it says [MOBOT] The robot is in ... mode, so your program is not running. The program is fine — the Mini never reached it.

  • A MoBot Smart Car Mini, charged, with both wheels free to turn.
  • A USB cable and the serial monitor.
  • A bit of floor for the last part.
  1. Set the Mini to Programmable (above).
  2. Open 01-mini-drive.firmware-3.mobot and press Save.
  3. Hold it in the air until the monitor tells you to put it down.
when what
at the start LED 1 on for three seconds — pick the Mini up
then both wheels turn forward, slowly, and the monitor prints back speed 30 — the motors telling you what they are doing
then the same direction, faster
then both wheels reverse, and the left one now says speed -70: the minus sign means backwards
stop() the wheels are let go and coast to a halt — you can still spin them with a finger
brake() after full speed the wheels stop dead, with a clear thump. This is the difference you are listening for
inverted flipped the left wheel turns the other way from the right one, even though both were told “forward”
on the floor forward, spin, forward, spin — then the RGB light goes green
you write the wheel
left.forward(30), left.backward(30) turns that way at 30 %
left.set_speed(70) changes to 70 % and keeps the way it was turning
left.drive(-55) one number for both: positive is forward, negative is backward, 0 stops
left.stop() lets go and coasts
left.brake() stops at once and holds still
left.speed the speed it was last told, -100 to 100: negative means backwards
left.inverted True when the wheel is fitted the other way round

The same words work on the big Smart Car. Only the things one robot has and the other does not are different: the Mini has a colour light (rgb_led), the Smart Car has a compass and a colour sensor.

If somebody builds the Mini with a motor wired backwards, you do not have to rewrite every line of your program — the wheel is told it is inverted, once, and everything else stays the same. The MoBot app can set it too, and the Mini remembers it, which is why part 2 flips the setting and then puts back whatever it was.

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 — Drive the Mini (Python) (mini)
#
# A reading copy of one file inside 01-mini-drive.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 01-mini-drive.main.py.
"""Drive the Mini - MoBot Smart Car Mini.
The Mini's motors speak the same language as the big Smart Car's:
robot.left_motor.forward(50)
means the same thing on both robots. That is what the name `robot` is for:
everything the two robots share is spelled the same, so the motor lines of a
Smart Car program work on a Mini too.
This program shows the whole motor vocabulary: forward, backward, faster,
stop() and brake(), "what are you doing right now?", and a wheel that has been
fitted the wrong way round.
"""
import time
left = robot.left_motor
right = robot.right_motor
# -------------------------------------------------------------------- 1 -----
# Hold the Mini in the air for this bit.
print("Part 1: hold the Mini in the air.")
robot.led1.on()
time.sleep(3)
robot.led1.off()
print("Both wheels forward, slowly...")
left.forward(30)
right.forward(30)
time.sleep(1.5)
print(" left wheel says: speed", left.speed)
print(" right wheel says: speed", right.speed)
print("Faster - set_speed() changes the speed and keeps the way...")
left.set_speed(70)
right.set_speed(70)
time.sleep(1.5)
print("Now backwards...")
left.backward(70)
right.backward(70)
time.sleep(1.5)
print(" left wheel says: speed", left.speed, " - negative means backwards")
# -------------------------------------------------------------------- 2 -----
# stop() and brake() are NOT the same thing, and you can hear the difference.
print("stop() - the wheels are let go and roll to a halt.")
left.stop()
right.stop()
time.sleep(2)
print("Spinning up again...")
left.forward(90)
right.forward(90)
time.sleep(1.5)
print("brake() - the wheels are held still. Listen: it stops much harder.")
left.brake()
right.brake()
time.sleep(2)
# -------------------------------------------------------------------- 3 -----
# A wheel that is fitted the other way round.
#
# The app can tell the Mini that a wheel is inverted, and the Mini remembers
# it. So this part flips whatever the left wheel is now, and puts it back after.
print("")
print("Part 2: 'inverted'. The LEFT wheel will now do the opposite of what it")
print("is told, while the right one behaves. Watch them turn opposite ways.")
was_inverted = left.inverted
left.inverted = not was_inverted
left.forward(50)
right.forward(50)
time.sleep(3)
left.stop()
right.stop()
left.inverted = was_inverted
print("Put back as it was. inverted is now", left.inverted)
# -------------------------------------------------------------------- 4 -----
# On the floor: a little dance, with drive() - one number for the way AND the
# speed: positive is forward, negative is backward.
print("")
print("Part 3: put the Mini on the floor. Dancing in 3 seconds.")
time.sleep(3)
for _ in range(2):
left.drive(55)
right.drive(55)
time.sleep(0.6)
left.drive(-55)
right.drive(55) # one wheel back, one forward: spin on the spot
time.sleep(0.5)
left.brake()
right.brake()
robot.rgb_led.set_color(0, 255, 0)
print("Finished.")
time.sleep(1)
robot.rgb_led.off()