Drive a square — 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.
The first Python program that moves the robot. The wheels speed up while you hold the robot in the air, then the robot drives four sides and four corners and tries to come back to where it started.
This is the Python version of what the Movements blocks do — but written by
hand, so you can put the four sides in a for loop instead of dragging four
copies of the same block.
This is the version for a robot on firmware 3.0, where the robot is called
robot. For a robot that still runs firmware 2.x, MoBot opens the 2.x version
of this example instead.
Project for firmware 3.0: 01-drive-a-square.firmware-3.mobot. Open it in MoBot.
What you need
Section titled “What you need”- A Smart Car, charged and switched on.
- Two wheels that turn freely.
- A USB cable, and about a metre of clear floor for part 2.
How to run it
Section titled “How to run it”- Open 01-drive-a-square.firmware-3.mobot in MoBot.
- Plug the robot in and pick its port.
- Press Save. MoBot puts the program on the robot as
main.pyand starts it, so you do not need to press the robot’s reset button. Or press Run to try it once without saving. If the robot was set to another mode in the app, MoBot asks whether to switch it to Programmable: answer Switch to Programmable. - Hold the robot in the air for the first eight seconds. Then put it down.
What you should see and hear
Section titled “What you should see and hear”| when | what |
|---|---|
| straight away | Part 1: hold the robot in the air. in the serial monitor, and LED 1 comes on |
| after 3 seconds | both wheels start turning forward, and get faster five times — you can hear the motors climb: speed 20, 40, 60, 80, 100 |
| then | the wheels stop and LED 1 goes out |
| after 3 more seconds | the robot drives forward, stops, spins on the spot, and repeats — four times |
| at the end | LED 2 comes on and the monitor says Finished. |
If the wheels never move but the messages appear, the program is running and the problem is the motors, the battery or the wiring — not your code.
The words this program uses
Section titled “The words this program uses”| you write | the wheel |
|---|---|
left.forward(45) |
turns forward at 45 % |
left.backward(45) |
turns backward at 45 % |
left.set_speed(80) |
changes to 80 % and keeps the way it was turning |
left.stop() |
lets go and rolls to a halt |
left.brake() |
stops at once and holds still |
left.drive(-45) |
one number for both: positive is forward, negative is backward, 0 stops |
left and right at the top of the program are just shorter names for
robot.left_motor and robot.right_motor. They are the same motors.
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 a square (Python) (robotics)## A reading copy of one file inside 01-drive-a-square.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-drive-a-square.main.py.
"""Drive a square - MoBot Smart Car.
Your first Python program that MOVES.
You never wrote "import robot" and you do not have to: the robot builds thatname for you while it is switching on, and hands it to your program. It is thesame robot the blocks talk to - you are just typing the words yourself now."""
import time
# Turn these numbers into your own robot. Bigger SPEED, bigger square.SPEED = 45 # how hard the wheels push, from 0 to 100SIDE_SECONDS = 1.2 # how long one side of the square takesTURN_SECONDS = 0.6 # how long a corner takes - tune this one first
left = robot.left_motorright = robot.right_motor
def halt(): """Stop both wheels.
stop() lets the wheels roll to a halt. brake() would hold them still instead - try it and listen for the difference. """ left.stop() right.stop()
# ---------------------------------------------------------------- part 1 ----# A speed ramp, with the robot HELD IN THE AIR so it cannot run away.## forward(speed) chooses the way AND the speed, in one go. set_speed(speed)# only changes how fast: the wheels keep turning the way they already were.
print("Part 1: hold the robot in the air. The wheels will speed up.")robot.led1.on()time.sleep(3)
left.forward(20)right.forward(20)
for speed in (20, 40, 60, 80, 100): print(" speed", speed) left.set_speed(speed) right.set_speed(speed) time.sleep(0.8)
halt()robot.led1.off()
# ---------------------------------------------------------------- part 2 ----# Now the square. Put the robot down on a big flat floor.
print("Part 2: put the robot on the floor. Driving in 3 seconds.")time.sleep(3)
for side in range(4): print("side", side + 1, "of 4") left.forward(SPEED) right.forward(SPEED) time.sleep(SIDE_SECONDS) halt() time.sleep(0.3)
print(" corner") # One wheel backward, one wheel forward: the robot spins on the spot. left.backward(SPEED) right.forward(SPEED) time.sleep(TURN_SECONDS) halt() time.sleep(0.3)
robot.led2.on()print("Finished. Did it come back to where it started?")Try changing it
Section titled “Try changing it”- Make
TURN_SECONDSbigger or smaller until the corners are really 90°. Every robot needs a slightly different number — floor, battery and wheels all matter. - Change
range(4)torange(3)and shorten the turn: a triangle. - Swap
backwardandforwardin the corner lines to turn the other way. - Replace
halt()withbrake()on both wheels and listen: the robot stops much harder.
The first Python program that moves the robot. The wheels speed up while you hold the robot in the air, then the robot drives four sides and four corners and tries to come back to where it started.
This is the Python version of what the Movements blocks do — but written by
hand, so you can put the four sides in a for loop instead of dragging four
copies of the same block.
Project for firmware 2.x: 01-drive-a-square.mobot. Open it in MoBot.
What you need
Section titled “What you need”- A Smart Car, charged and switched on.
- Two wheels that turn freely.
- A USB cable, and about a metre of clear floor for part 2.
How to run it
Section titled “How to run it”- Open 01-drive-a-square.mobot in MoBot.
- Plug the robot in and pick its port.
- Press Save. MoBot puts the program on the robot as
main.pyand starts it, so you do not need to press the robot’s reset button. Or press Run to try it once without saving. If the robot was set to another mode in the app, MoBot asks whether to switch it to Programmable: answer Switch to Programmable. - Hold the robot in the air for the first eight seconds. Then put it down.
What you should see and hear
Section titled “What you should see and hear”| when | what |
|---|---|
| straight away | Part 1: hold the robot in the air. in the serial monitor, and LED 1 comes on |
| after 3 seconds | both wheels start turning forward, and get faster five times — you can hear the motors climb: speed 20, 40, 60, 80, 100 |
| then | the wheels stop and LED 1 goes out |
| after 3 more seconds | the robot drives forward, stops, spins on the spot, and repeats — four times |
| at the end | LED 2 comes on and the monitor says Finished. |
If the wheels never move but the messages appear, the program is running and the problem is the motors, the battery or the wiring — not your code.
The two surprises in this program
Section titled “The two surprises in this program”1. This program stops a wheel with setSpeed(0).
Speed zero is the stop button on every Smart Car. The wheels also have .stop()
(coast) and .brake() (stop hard) on firmware 2.0.0 and newer; this program
keeps to setSpeed(0).
2. setDirection() on its own looks broken.
On the Smart Car, choosing a direction does not switch the power on. So
setDirection(FORWARD) by itself does nothing you can see — you have to follow it with
setSpeed(...). That is exactly what part 1 shows.
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 a square (Python) (robotics)## A reading copy of one file inside 01-drive-a-square.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 a square - MoBot Smart Car.
Your first Python program that MOVES.
You never wrote "import mobotROBOTICS" and you do not have to: the robot buildsthat name for you while it is switching on, and hands it to your program. It isthe same robot the blocks talk to - you are just typing the words yourself now."""
import time
# Turn these numbers into your own robot. Bigger SPEED, bigger square.SPEED = 45 # how hard the wheels push, from 0 to 100SIDE_SECONDS = 1.2 # how long one side of the square takesTURN_SECONDS = 0.6 # how long a corner takes - tune this one first
FORWARD = 1 # the Smart Car's motors call forward 1 (both boards, measured)BACKWARD = 0 # ...and this way is 0
def drive(left_way, left_speed, right_way, right_speed): """Tell both motors which way to turn and how fast, in one go.""" mobotROBOTICS.left_motor.setDirectionAndSpeed(left_way, left_speed) mobotROBOTICS.right_motor.setDirectionAndSpeed(right_way, right_speed)
def halt(): """Stop.
Speed zero IS the stop button. (The wheels also have .stop() and .brake() on firmware 2.0.0 and newer, but this program keeps to speed 0.) """ mobotROBOTICS.left_motor.setSpeed(0) mobotROBOTICS.right_motor.setSpeed(0)
# ---------------------------------------------------------------- part 1 ----# A speed ramp, with the robot HELD IN THE AIR so it cannot run away.## setDirection only chooses which way the wheels WILL go. On the Smart Car it# does not touch the power, so on its own it looks like nothing happened.# setSpeed is the part you can hear.
print("Part 1: hold the robot in the air. The wheels will speed up.")mobotROBOTICS.led1.on()time.sleep(3)
mobotROBOTICS.left_motor.setDirection(FORWARD)mobotROBOTICS.right_motor.setDirection(FORWARD)
for speed in (20, 40, 60, 80, 100): print(" speed", speed) mobotROBOTICS.left_motor.setSpeed(speed) mobotROBOTICS.right_motor.setSpeed(speed) time.sleep(0.8)
halt()mobotROBOTICS.led1.off()
# ---------------------------------------------------------------- part 2 ----# Now the square. Put the robot down on a big flat floor.
print("Part 2: put the robot on the floor. Driving in 3 seconds.")time.sleep(3)
for side in range(4): print("side", side + 1, "of 4") drive(FORWARD, SPEED, FORWARD, SPEED) time.sleep(SIDE_SECONDS) halt() time.sleep(0.3)
print(" corner") # One wheel forward, one wheel backward: the robot spins on the spot. drive(BACKWARD, SPEED, FORWARD, SPEED) time.sleep(TURN_SECONDS) halt() time.sleep(0.3)
mobotROBOTICS.led2.on()print("Finished. Did it come back to where it started?")Try changing it
Section titled “Try changing it”- Make
TURN_SECONDSbigger or smaller until the corners are really 90°. Every robot needs a slightly different number — floor, battery and wheels all matter. - Change
range(4)torange(3)and shorten the turn: a triangle. - Swap
FORWARDandBACKWARDin the corner line to turn the other way.