HomeTurtlegraficsGPanelRobotics WebTigerPython |
Python - Online |
Deutsch English |
YOU LEARN HERE... |
how to establish Bluetooth communication between two Bluetooth-enabled robots and how to control the Callibot remotely. |
SENDING AND RECEIVING |
|
EXAMPLES |
Example 1: Button click on Calliope causes an arrow to be displayed on Callibot
First load the programme onto the Callibot and switch on the power supply. Then you can remove the USB cable and use it for transmission and programme execution on the second Calliope. 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 send a message) or whether it receives a message (in which case it executes the corresponding command). The line display.show(Image.ARROW_E) causes a link arrow to be displayed. Program: import radio from calliope_mini import * 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 Callibot robot, in this example you use the Calliope (or micro:bit) as the transmitter and the Callibot as the receiver. As the transmitter does not need all Callibot commands and vice versa, it makes sense to use two different programmes. 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 travelling straight ahead. A massage is only sent if the state has changed (oldState != state). Program forthe transmitter: import radio from calliope_mini import * 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 sign is displayed on the Callibot after the programme starts so that you know that it is already running. 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. Program for the receiver: from callibot import * from calliope_mini 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 Calliope after downloading the programme. |
REMEMBER... |
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 |
|