Skip to content

Search the documentation

Kit
Firmware

Name your robot, then drive it — 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.

Your program takes the phone connection over. The robot renames itself, waits for you to connect, and then your code — not the app — decides what the Game Pad’s stick and buttons mean.

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

Project for firmware 3.0: 06-name-your-robot.firmware-3.mobot. Open it in MoBot.

  • A Smart Car, on its battery (this one drives, so it should not be tethered to a laptop).
  • A phone with the MoBot app.
  • A USB cable to put the program on the robot, and the serial monitor for the messages.
  1. Open 06-name-your-robot.firmware-3.mobot, change NAME = "Speedy" to whatever you like, and press Save.
  2. Unplug the USB and switch the robot on from its battery.
  3. Open the app and scan. Your new name should be in the list.
  4. Connect, make sure the robot is set to Programmable (your program only runs in that mode), open the Game Pad and drive with the stick.
when what
after saving Renaming this robot to: Speedy in the monitor, then The robot is now called Speedy
while waiting LED 1 blinks slowly — one short flash a second. That means “not connected yet”
in the app’s scan list the robot appears as Speedy, not as MyMobot
when you connect LED 1 goes solid on and the monitor says Connected.
stick up both wheels go forward; harder push, faster
stick left/right the robot turns
button A a short beep
button B the robot stops
button X LED 2 lights while you hold it
walk away with the phone the wheels stop, LED 1 starts blinking again, and it waits for you to come back

Switch the robot off and on again: it is still called Speedy. The name is saved on the robot itself.

you write what you get
robot.gamepad.x, robot.gamepad.y where the stick is: -128 to 127, 0 in the middle; up and right are positive
robot.gamepad.is_pressed("a") True while that button is held: "a", "b", "x", "y", "up", "down", "left", "right"
robot.bluetooth.is_connected() True while a phone is connected
robot.set_name("Speedy"), robot.name the robot’s Bluetooth name, kept after power-off

The stick and buttons go back to the middle and “not pressed” half a second after the phone stops sending, and at once when it disconnects, so a robot never keeps driving on its own when the phone goes away.

map_range(value, in_min, in_max, out_min, out_max) stretches a number from one range into another — here, the stick’s 0 to 127 into a speed of 0 to TOP_SPEED. It is a helper of its own, so the program asks for it once at the top:

from mobot import map_range

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 — Name your robot, then drive it (Python) (robotics)
#
# A reading copy of one file inside 06-name-your-robot.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 06-name-your-robot.main.py.
"""Name your robot, then drive it - MoBot Smart Car.
Your program takes over the phone connection.
Two things happen here, and both have blocks (change the name, and the Game
Pad's buttons and joystick):
1. robot.set_name() gives the robot a new name. It is remembered forever, even
after you switch it off - so the robot in the corner of the classroom can be
called "Amina's rocket" instead of "MyMobot".
2. Then your program reads the joystick and the buttons of the Game Pad in the
MoBot app itself, and decides what the wheels do. The app is only a remote
control; the rules are yours.
"""
import time
from mobot import map_range # stretches a number from one range into another
NAME = "Speedy"
TOP_SPEED = 70 # 0..100. Start low; a Smart Car is quicker than it looks.
pad = robot.gamepad
left = robot.left_motor
right = robot.right_motor
def halt():
left.stop()
right.stop()
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
def drive_one(motor, mix):
"""mix runs -127..127. Negative means backwards."""
speed = int(map_range(abs(mix), 0, 127, 0, TOP_SPEED))
if mix >= 0:
motor.drive(speed) # drive() takes one signed speed:
else:
motor.drive(-speed) # positive is forward, negative backward
print("Renaming this robot to:", NAME)
robot.set_name(NAME)
print("The robot is now called", robot.name)
print("Now open the MoBot app and connect to", NAME)
while not robot.bluetooth.is_connected():
robot.led1.on()
time.sleep(0.2)
robot.led1.off()
time.sleep(0.8)
print("Connected. Open the Game Pad and drive!")
robot.led1.on()
while True:
if not robot.bluetooth.is_connected():
print("Phone went away. Stopping.")
halt()
robot.led1.off()
while not robot.bluetooth.is_connected():
time.sleep(0.2)
robot.led1.on()
continue
forward = pad.y # -128..127, push the stick up for positive
turn = pad.x
drive_one(left, clamp(forward + turn, -127, 127))
drive_one(right, clamp(forward - turn, -127, 127))
# Your own rules, decided here and not in the app:
if pad.is_pressed("a"):
robot.buzzer.play_song("0 C5 1 0")
if pad.is_pressed("b"):
halt()
robot.led2.set(pad.is_pressed("x"))
time.sleep(0.05)
  • Swap forward + turn and forward - turn if your robot steers the wrong way.
  • Make button Y play a whole song instead of one beep.
  • Make the top speed depend on a button: slow by default, fast while up is held.