Joystick controls the speed — Smart Car
The Game Pad’s stick gives numbers from about −128 to 127. A wheel wants a speed from 0 to 100. The map values block does that stretching for you, so the further you push the stick forward, the faster both wheels turn.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Car.
The program
Section titled “The program”Read the program as text
- MoBot Start
- Change MoBot’s name:
"MoBot-Speed" - Print to serial
"Open the Game Pad in the MoBot app, then push the stick forward and back." - wait 1 seconds
- repeat forever
- Print to serial (create text with
"wheel speed % = "(map values of Read from joystick the value of Y from min : -128 max : 127 to min : 0 max : 100)) - set Motor 1 to Direction 2 with speed (map values of Read from joystick the value of Y from min : -128 max : 127 to min : 0 max : 100) %
- set Motor 2 to Direction 2 with speed (map values of Read from joystick the value of Y from min : -128 max : 127 to min : 0 max : 100) %
- wait 0.15 seconds
- Print to serial (create text with
Read this first — the robot must be in Programmable
Section titled “Read this first — the robot must be in Programmable”Your program only runs while the robot’s mode is Programmable. The Game Pad does not change the mode: opening it never switches the robot to anything else. Only a mode tile in the app does that. So the order is:
- Set the robot to Programmable.
- Then open the Game Pad. Your program receives the stick and the buttons.
Do not tap the Manual driving tile. It sets the robot to Manual driving, and your program stops. To get it back, tap the Programmable tile: your program starts again from its first line.
An old version of the MoBot app switched the robot to Manual driving as soon as you opened the Game Pad, and that stopped your program. If your app has no Programmable tile, update the app first.
What you need
Section titled “What you need”- A Smart Car with both wheels fitted, running on its battery. Plug in the USB cable as well only if you want to read the serial monitor while you play.
- A phone or tablet with the MoBot app.
- A book to stand the robot on. The wheels turn as soon as the program starts: with no phone connected the stick reads 0, and 0 gives about half speed (see Why is the middle 50 and not 0? below).
How to run it
Section titled “How to run it”- Stand the robot on the book, wheels free.
- Open 11-joystick-speed.mobot, plug in, press Send to Device. If the robot is in another mode, MoBot asks: “The robot is in Manual driving. Switch it to Programmable to run your program?” Answer Switch to Programmable.
- Open the MoBot app and scan. The robot should now be listed as
MoBot-Speed. - Connect. On the home screen, under Activities, the app says Robot mode and the mode’s name. If it does not say Robot mode: Programmable, tap the Programmable tile. The app sets the robot to Programmable and opens the Programmable screen. Press the back arrow to return to the home screen.
- Tap Open Game Pad, and switch the pad to Joystick.
What you should see
Section titled “What you should see”In the card in the middle of the Game Pad, the app says Your program reads this pad while your program is running. (A robot with the oldest firmware, 2.0.0, shows no line there.) If it says No program on the robot yet, send the program again.
The serial monitor shows wheel speed % = … several times a second.
| what you do | what should happen |
|---|---|
| scan | the robot appears as MoBot-Speed |
| leave the stick in the middle | the number is about 50 and both wheels turn at middling speed |
| push the stick all the way forward (up) | the number climbs to near 100 and the wheels reach full speed |
| pull the stick all the way back (down) | the number falls to near 0 and the wheels stop |
| move the stick gently | the number moves gently with it, with no jumps, and the wheels follow |
| switch the pad to Digital and hold the up arrow | the number is near 100: the robot reads a held arrow as the stick pushed all the way that way |
| hold the down arrow | the number is near 0 and the wheels are still |
| let go of the arrow | the number goes back to about 50 |
The test passes if the printed number sweeps smoothly between roughly 0 and 100 as you move the stick from back to forward, and if the wheels follow it.
How low the wheels can go depends on the robot: at the very lowest numbers they may only buzz. Speed ramp shows the same thing.
It never ends by itself. Press Stop, or send another program, to stop it.
If your program does not run
Section titled “If your program does not run”Your program only runs while the robot’s mode is Programmable. If the robot was put in another mode while it was on the desk, press Send to Device and answer Switch to Programmable when MoBot asks. Or open the MoBot app, connect to the robot and tap the Programmable tile: the home screen then says Robot mode: Programmable. If the app has no Programmable tile, update the app.
If the name did not change
Section titled “If the name did not change”The new name is stored on the robot and used the next time Bluetooth starts. Switch the robot off and on again, then scan once more. The robot keeps its new name after it is switched off.
Why is the middle 50 and not 0?
Section titled “Why is the middle 50 and not 0?”Because that is exactly what you asked for. You told the block that −128 means 0 % and 127 means 100 %, so the middle of the stick lands in the middle of the speed range. If you would rather the wheels were still when the stick is centred, that is the first exercise below.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
mobot_blemap |
map values: takes a number in one range and gives the matching number in another. It reads: map values of the value from min a max b to min c max d. Here: the stick’s Y, from −128 to 127, to 0 to 100. |
mobot_blejoystick |
the stick’s X or Y, about −128 to 127. This program reads Y. |
mobot_motordirspeed |
sets a wheel’s direction and speed together. Direction 2 is the direction the code calls forward; which way that turns the wheel depends on how its motor was wired (see Which way do the wheels turn?). One block per wheel, both with the mapped number as the speed. |
mobot_blename |
renames the robot. Run it once, at the top. |
If the program stops with an error about map_range
Section titled “If the program stops with an error about map_range”The map values block is part of the firmware on every robot from firmware 3.0.
A Smart Car on older firmware may stop with an AttributeError that mentions
map_range. Update the firmware and send the program again.
The Python code
Section titled “The Python code”The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.
# Joystick controls the speed — robotics (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 11-joystick-speed.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.# The firmware 2.x version is 11-joystick-speed.main.py.
from machine import Pinimport timefrom mobot import map_range
robot.set_name('MoBot-Speed')print("[MOBOT]", 'Open the Game Pad in the MoBot app, then push the stick forward and back.')time.sleep(1)while True: print("[MOBOT]", ('wheel speed % = ' + str(map_range(robot.gamepad.y, (-128), 127, 0, 100)))) robot.left_motor.forward(map_range(robot.gamepad.y, (-128), 127, 0, 100)) robot.right_motor.forward(map_range(robot.gamepad.y, (-128), 127, 0, 100)) time.sleep(0.15)# Joystick controls the speed — robotics## This is what MoBot puts on the board as main.py when you open# 11-joystick-speed.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport time
mobotROBOTICS.ble.changeName('MoBot-Speed')print("[MOBOT]", 'Open the Game Pad in the MoBot app, then push the stick forward and back.')time.sleep(1)while True: print("[MOBOT]", ('wheel speed % = ' + str(mobotROBOTICS.ble.map_range(mobotROBOTICS.ble.y, (-128), 127, 0, 100)))) mobotROBOTICS.left_motor.setDirectionAndSpeed(1, mobotROBOTICS.ble.map_range(mobotROBOTICS.ble.y, (-128), 127, 0, 100)) mobotROBOTICS.right_motor.setDirectionAndSpeed(1, mobotROBOTICS.ble.map_range(mobotROBOTICS.ble.y, (-128), 127, 0, 100)) time.sleep(0.15)Try changing it
Section titled “Try changing it”- Set min out to −100 and max out to 100, and swap the two Set Motor blocks for the drive Motor at % (−100 to 100) block (a firmware 3.0 block). The middle of the stick is now close to 0, so the wheels are almost still there, and pulling the stick back turns them backward.
- Map the stick onto LED brightness (0-100) instead of speed, and steer a light instead of a robot.
- Map the distance sensor reading (say 0-100 cm) onto speed, so the robot slows down as it comes near a wall.