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.
What you need
Section titled “What you need”- 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.
How to run it
Section titled “How to run it”- Open 06-name-your-robot.firmware-3.mobot, change
NAME = "Speedy"to whatever you like, and press Save. - Unplug the USB and switch the robot on from its battery.
- Open the app and scan. Your new name should be in the list.
- 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.
What you should see and hear
Section titled “What you should see and hear”| 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.
How the program reads the phone
Section titled “How the program reads the phone”| 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
Section titled “map_range”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_rangeThe 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 — 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 GamePad'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 timefrom 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.gamepadleft = robot.left_motorright = 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)Try changing it
Section titled “Try changing it”- Swap
forward + turnandforward - turnif 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.
Your program takes the phone connection over. The robot renames itself, makes itself visible to the app, waits for you to connect, and then your code — not the app — decides what the joystick means.
Project for firmware 2.x: 06-name-your-robot.mobot. Open it in MoBot.
What you need
Section titled “What you need”- 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.
How to run it
Section titled “How to run it”- Open 06-name-your-robot.mobot, change
NAME = "Speedy"to whatever you like, and press Save. - Unplug the USB and switch the robot on from its battery.
- Open the app and scan. Your new name should be in the list.
- Connect, and drive with the on-screen joystick.
What you should see and hear
Section titled “What you should see and hear”| when | what |
|---|---|
| after saving | Renaming this robot to: Speedy in the monitor |
| 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. Drive! |
| joystick up | both wheels go forward; harder push, faster |
| joystick left/right | the robot turns |
| button A | a short beep |
| button B | the robot stops dead |
| 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.
The two traps this example exists to show
Section titled “The two traps this example exists to show”1. A Smart Car running your own program hides from the app. When no mode is running, the Smart Car keeps its Bluetooth switched off until somebody holds down the little button wired to GPIO 13. So a child saves a program, opens the app, scans, and finds nothing — and there is no message anywhere explaining why. The one line
ble.enable_activation_gate(False)opens the door and keeps it open. There is no block for it. (The Mini has no such button and is not affected.) On a robot with firmware 2.1 or newer Bluetooth is always on, and this line does nothing. It is still needed on older firmware, and it does no harm.
2. map_range used to be in a different place on different boards.
On firmware 2.0.0 and newer it is a plain function inside BLE.py, so you write
from BLE import map_range, and it is also attached to the BLE object, so
ble.map_range(...) works too. Older firmware had it in only one of those two
places, depending on the board. This file tries the first and falls back to the
second, which is why it runs everywhere.
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 — Name your robot, then drive it (Python) (robotics)## A reading copy of one file inside 06-name-your-robot.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.
"""Name your robot, then drive it - MoBot Smart Car.
Your program takes over the phone connection.
Three things happen here. The first and the last have blocks (change thename, and the Game Pad's buttons and joystick); the middle one is Python only:
1. changeName() 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. enable_activation_gate(False) makes the robot show up in the app WITHOUT anyone holding the little button on the board down. A Smart Car running your own program normally hides until that button is pressed, which is a very confusing five minutes if you do not know about it.
3. Then your program reads the joystick and the buttons from the phone itself and decides what the wheels do. The app is only a remote control; the rules are yours."""
import time
# map_range stretches a number from one range into another. On firmware older# than 2.0.0 it lived in a different place on different boards, so we look for# it in both:try: from BLE import map_range # firmware 2.0.0 and newerexcept ImportError: map_range = mobotROBOTICS.ble.map_range # older firmware, DRV8833 board
NAME = "Speedy"TOP_SPEED = 70 # 0..100. Start low; a Smart Car is quicker than it looks.
ble = mobotROBOTICS.bleleft = mobotROBOTICS.left_motorright = mobotROBOTICS.right_motor
def halt(): left.setSpeed(0) right.setSpeed(0)
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.""" direction = 1 if mix >= 0 else 0 # the Smart Car's motors call forward 1 (both boards, measured) speed = int(map_range(abs(mix), 0, 127, 0, TOP_SPEED)) motor.setDirectionAndSpeed(direction, speed)
print("Renaming this robot to:", NAME)ble.changeName(NAME)
print("Opening the door so the app can see us without the button...")ble.enable_activation_gate(False)
print("Now open the MoBot app and connect to", NAME)while not ble.is_connected: mobotROBOTICS.led1.on() time.sleep(0.2) mobotROBOTICS.led1.off() time.sleep(0.8)
print("Connected. Drive!")mobotROBOTICS.led1.on()
while True: if not ble.is_connected: print("Phone went away. Stopping.") halt() mobotROBOTICS.led1.off() while not ble.is_connected: time.sleep(0.2) mobotROBOTICS.led1.on() continue
forward = ble.y # -128..127, push the stick up for positive turn = ble.x buttons = ble.isPressed # a dictionary: a, b, x, y, up, down, left, right
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 buttons["a"]: mobotROBOTICS.buzzer.play("0 C5 1 0", looping=False) if buttons["b"]: halt() mobotROBOTICS.led2.setState(1 if buttons["x"] else 0)
time.sleep(0.05)Try changing it
Section titled “Try changing it”- Swap
forward + turnandforward - turnif 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.