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.
What you need
Section titled “What you need”- 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.
How to run it
Section titled “How to run it”- Set the Mini to Programmable (above).
- Open 01-mini-drive.firmware-3.mobot and press Save.
- Hold it in the air until the monitor tells you to put it down.
What you should see and hear
Section titled “What you should see and hear”| 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 |
The words this program uses
Section titled “The words this program uses”| 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.
inverted
Section titled “inverted”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.
The program
Section titled “The program”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 — 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 aSmart 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 beenfitted the wrong way round."""
import time
left = robot.left_motorright = 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.invertedleft.inverted = not was_invertedleft.forward(50)right.forward(50)time.sleep(3)
left.stop()right.stop()left.inverted = was_invertedprint("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()The Mini’s motors speak a different language. This program shows the whole
Mini motor vocabulary: forward, backward, faster, stop(), brake(), “what are
you doing right now?”, and a wheel that has been fitted the wrong way round.
Project for firmware 2.x: 01-mini-drive.mobot. Open it in MoBot.
⚠ Before anything else: a fresh Mini ignores your program
Section titled “⚠ Before anything else: a fresh Mini ignores your program”A Smart Car with nothing else to do runs the program you saved. A Mini does
not. Out of the box the Mini starts in Manual driving, which loops forever
waiting for the phone, and never gets as far as your main.py. 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 just sits there flashing at the phone, that is what has happened — the program is fine, the Mini never reached it.
What you need
Section titled “What you need”- 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.
How to run it
Section titled “How to run it”- Set the Mini to Programmable (above).
- Open 01-mini-drive.mobot and press Save.
- Hold it in the air until the monitor tells you to put it down.
What you should see and hear
Section titled “What you should see and hear”| 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 direction 0 — the motors telling you what they are doing |
| then | the same direction, faster |
| then | both wheels reverse |
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 = True |
the left wheel turns backwards while the right one turns forwards, even though both were told “forward” |
| on the floor | forward, spin, forward, spin — then the RGB light goes green |
stop(), brake() and inverted
Section titled “stop(), brake() and inverted”stop()andbrake(). On the Mini,stop()coasts andbrake()holds. The big Smart Car’s wheels have both as well, on firmware 2.0.0 and newer.inverted. If somebody builds the Mini with a motor wired backwards, you do not have to rewrite every line of your program — you tell that one wheel it is inverted, once, and everything else stays the same. (The big Smart Car’s wheels haveinvertedtoo, on firmware 2.0.0 and newer.)
The spelling trap
Section titled “The spelling trap”| Smart Car | Mini |
|---|---|
setSpeed(50) |
set_speed(50) |
setDirection(0) |
set_direction(0) |
setDirectionAndSpeed(0, 50) |
set_direction_and_speed(0, 50) |
mobotROBOTICS |
mobotMINI |
Both halves change. A Smart Car program pasted onto a Mini fails on the very
first motor line with NameError: name 'mobotROBOTICS' isn't defined.
The program
Section titled “The program”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 — Drive the Mini (Python) (mini)## A reading copy of one file inside 01-mini-drive.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.
"""Drive the Mini - MoBot Smart Car Mini.
The Mini's motors speak a different language from the big Smart Car's.
Smart Car: left_motor.setDirectionAndSpeed(0, 50) Mini: left_motor.set_direction_and_speed(0, 50)
Same idea, different spelling - little_words_with_underscores instead ofwordsSquashedTogether. Copying a Smart Car program onto a Mini will not work,and this is the first place you find that out.
The good news: the Mini's motors can do things the Smart Car's cannot. They canstop() and brake(), they can tell you what they are doing, and they can be toldthat a wheel is fitted the wrong way round."""
import time
FORWARD = 0BACKWARD = 1
left = mobotMINI.left_motorright = mobotMINI.right_motor
# -------------------------------------------------------------------- 1 -----# Hold the Mini in the air for this bit.
print("Part 1: hold the Mini in the air.")mobotMINI.led1.on()time.sleep(3)mobotMINI.led1.off()
print("Both wheels forward, slowly...")left.set_direction_and_speed(FORWARD, 30)right.set_direction_and_speed(FORWARD, 30)time.sleep(1.5)
print(" left wheel says: speed", left.get_speed(), " direction", left.get_direction())print(" right wheel says: speed", right.get_speed(), " direction", right.get_direction())
print("Faster - speed only, direction stays as it was...")left.set_speed(70)right.set_speed(70)time.sleep(1.5)
print("Now backwards...")left.set_direction(BACKWARD)right.set_direction(BACKWARD)time.sleep(1.5)
# -------------------------------------------------------------------- 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.set_direction_and_speed(FORWARD, 90)right.set_direction_and_speed(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.
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.")
left.inverted = Trueleft.set_direction_and_speed(FORWARD, 50)right.set_direction_and_speed(FORWARD, 50)time.sleep(3)
left.stop()right.stop()left.inverted = Falseprint("Put back to normal. inverted is now", left.inverted)
# -------------------------------------------------------------------- 4 -----# On the floor: a little dance.
print("")print("Part 3: put the Mini on the floor. Dancing in 3 seconds.")time.sleep(3)
for _ in range(2): left.set_direction_and_speed(FORWARD, 55) right.set_direction_and_speed(FORWARD, 55) time.sleep(0.6) left.set_direction_and_speed(BACKWARD, 55) right.set_direction_and_speed(FORWARD, 55) # spin on the spot time.sleep(0.5)
left.brake()right.brake()mobotMINI.rgb.set_color(0, 255, 0)print("Finished.")time.sleep(1)mobotMINI.rgb.off()