Deutsch English |
YOU LEARN HERE... |
how to establish Bluetooth communication between two Bluetooth-enabled robots and how to control the robot remotely. |
SENDING AND RECEIVING |
|
EXAMPLES |
Example 1: Click on micro:bit buttons an arrow to be displayed on mbRobot
First load the program onto the mbRobot and switch on the power supply. Then you can remove the USB cable and use it for transmission and program execution on the second micro:bit. The connection is established with radio.on(). Each device then waits in an endless loop to see whether a button is pressed (in which case it sends a message) or whether it receives a message (in which case it executes the corresponding command). Program: from microbit import * import radio radio.on() while True: if button_a.was_pressed(): radio.send("left") elif button_b.was_pressed(): radio.send("right") rec = radio.receive() if rec != None: if rec == "left": display.show(Image.ARROW_E) elif rec == "right": display.show(Image.ARROW_W) sleep(10)
As you want to control the moving robot, in this example you use the micro:bit as the transmitter and the mbRobot as the receiver. As the transmitter does not need all mbRobot commands and vice versa, it makes sense to use two different programs. Control is as follows:
The so-called state programming is very useful in this example. It makes no sense to send a “FORWARD” message every 10 milliseconds if the other robot is already moving straight ahead. A massage is only sent if the state has changed (oldState != state). Program for the sender: from microbit import * import radio radio.on() display.show(Image.YES) state = "STOP" oldState = "" while True: if button_a.is_pressed() and button_b.is_pressed(): state = "FORWARD" elif button_a.is_pressed(): state = "LEFT" elif button_b.is_pressed(): state = "RIGHT" else: state = "STOP" if oldState != state: radio.send(state) oldState = state sleep(10) An "Ok"-signsign is displayed on the mbRobot after the program start so that you know that it is already there. It then waits for the commands it receives via Bluetooth. If it receives a “FORWARD” message, it executes the forward() command and remains in this state until it receives a new message. With “LEFT” it moves on a left turn, with “STOP” it stops. Programm or the receiver: from mbRobot import * from microbit import * import radio radio.on() display.show(Image.YES) while True: rec = radio.receive() if rec == "FORWARD": forward() elif rec == "LEFT": leftArc(0.1) elif rec == "RIGHT": rightArc(0.1) elif rec == "STOP": stop() sleep(10) So that you can move freely with the control unit, you can use a power bank to power the micro:bit after the program download. |
REMEMBER YOU... |
To be able to use the commands for Bluetooth communication, you must import the radio module. The connection is established with radio.on(). With radio.send(msg) a message is sent, with radio.receive() it is received at the other device and stored in a receive buffer until it is picked up. |
TO SOLVE BY YOURSELF |
|