Does the map block work? — Mini
A twenty-second test of one block. It is a three-line program, built so that anybody with a Mini can check that the map block works. It prints three numbered lines, so you can see exactly how far the program got.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Mini.
The program
Section titled “The program”Read the program as text
- MoBot Mini Start
- Print to serial
"1 of 3: about to use the map block ..." - set Motor 1 to Direction 1 with speed (map values of Read from joystick the value of Y from min : -128 max : 127 to min : 0 max : 100) %
- Print to serial
"2 of 3: the map block worked! The left wheel should be turning." - wait 3 seconds
- set Motor 1 to Direction 1 with speed 0 %
- Print to serial
"3 of 3: finished."
What the “map values” block is for
Section titled “What the “map values” block is for”A joystick gives numbers from -128 to 127. A wheel takes a speed from 0 to 100. They do not fit. The map block is the block that stretches one range onto the other, so a joystick pushed half way gives half speed.
08-ble-joystick shows the problem it is meant to solve.
What you need
Section titled “What you need”- A MoBot Mini, charged and switched on.
- A USB cable.
- Nothing else. You do not even need to connect a phone.
⚠️ First time? Read the “Before you send it” section of Blink and fade.
How to run it
Section titled “How to run it”- Open 09-ble-map-check.mobot in MoBot and press Send to Device.
- Open the serial monitor. Read what it says. That is the whole test.
What you should see
Section titled “What you should see”[MOBOT] 1 of 3: about to use the map block ...[MOBOT] 2 of 3: the map block worked! The left wheel should be turning.[MOBOT] 3 of 3: finished.and the left wheel turns for three seconds, at about half speed — because with the joystick sitting at 0, half way between -128 and 127, the map block gives about 50.
Stand the Mini on a book first, so the wheel spins in the air.
If it stops at line 1 of 3
Section titled “If it stops at line 1 of 3”If the monitor shows an AttributeError about map_range after line 1 of 3,
your Mini has older firmware, which had no map block. Update the firmware and try
again.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
mobot_blemap |
Stretches a number from one range onto another. |
mobot_blejoystick |
The number being stretched — the joystick’s up-and-down position. |
mobot_motordirspeed |
Where the stretched number ends up: the wheel’s speed. |
mobot_print |
The three numbered lines that tell you exactly how far the program got. |
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.
# Does the map block work? — mini (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 09-ble-map-check.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 09-ble-map-check.main.py.
from machine import Pinimport timefrom mobot import map_range
print("[MOBOT]", '1 of 3: about to use the map block ...')robot.left_motor.forward(map_range(robot.gamepad.y, (-128), 127, 0, 100))print("[MOBOT]", '2 of 3: the map block worked! The left wheel should be turning.')time.sleep(3)robot.left_motor.forward(0)print("[MOBOT]", '3 of 3: finished.')# Does the map block work? — mini## This is what MoBot puts on the board as main.py when you open# 09-ble-map-check.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
print("[MOBOT]", '1 of 3: about to use the map block ...')mobotMINI.left_motor.set_direction_and_speed(0, mobotMINI.ble.map_range(mobotMINI.ble.y, (-128), 127, 0, 100))print("[MOBOT]", '2 of 3: the map block worked! The left wheel should be turning.')time.sleep(3)mobotMINI.left_motor.set_direction_and_speed(0, 0)print("[MOBOT]", '3 of 3: finished.')