Skip to content

Search the documentation

Kit
Firmware

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.

  • 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.
  1. Open 01-drive-a-square.firmware-3.mobot in MoBot.
  2. Plug the robot in and pick its port.
  3. Press Save. MoBot puts the program on the robot as main.py and 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.
  4. Hold the robot in the air for the first eight seconds. Then put it down.
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.

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.

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 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 that
name for you while it is switching on, and hands it to your program. It is the
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 100
SIDE_SECONDS = 1.2 # how long one side of the square takes
TURN_SECONDS = 0.6 # how long a corner takes - tune this one first
left = robot.left_motor
right = 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?")
  • Make TURN_SECONDS bigger or smaller until the corners are really 90°. Every robot needs a slightly different number — floor, battery and wheels all matter.
  • Change range(4) to range(3) and shorten the turn: a triangle.
  • Swap backward and forward in the corner lines to turn the other way.
  • Replace halt() with brake() on both wheels and listen: the robot stops much harder.