buttons

7. REMOTE CONTROLL VIA BLUETOOTH

 

Deutsch   English   
 

YOU LEARN HERE...

 

how to establish Bluetooth communication between two Bluetooth-enabled robots and how to remotely control the robot.

 

 

SENDING AND RECEIVING

 

The module radio integrated into TigerJython enables the programming of simple applications in which two robots communicate with each other via Bluetooth. Each robot can be both a transmitter and a receiver at the same time. First, both robots switch on the transmitter/receiver with radio.on(). This establishes a Bluetooth connection. Then, each robot can send a message (as a string) to the other robot with radio.send(msg) , which is stored there in a receive buffer. With radio.receive() , you retrieve the message and the buffer is deleted.

 

 

EXAMPLES

 
Example 1: Click the button on micro:bit to appear an arrow on the mbRobot

When button A on the micro:bit is pressed, the message ‘left’ is sent. The robot receives this message and displays a left arrow on its display. When button B on the micro:bit is pressed, the message ‘right’ is sent and the robot displays a right arrow. Since each micro:bit is both a transmitter and a receiver, you can reverse the process by pressing the buttons on the mbRobot to display the arrow on the microBit.

 
 Transmitter Receiver

First, load the programme onto the mbRobot and switch on the power supply. Then you can remove the USB cable and use it for transmission and programme execution on the second micro:bit.

The connection is established with radio.on(). After that, each device waits in an endless loop to see if a button is pressed (in which case it sendsa message) or if it receives a message (in which case it executes the corresponding command).
The line display.show(Image.ARROW_E) causes a left arrow to be displayed.

Program:


Warning: include(bsp/Mq7.html): Failed to open stream: No such file or directory in /var/www/html/engl/robotik/mbrobot/bluetooth.inc.php on line 86

Warning: include(): Failed opening 'bsp/Mq7.html' for inclusion (include_path='.:/usr/local/lib/php') in /var/www/html/engl/robotik/mbrobot/bluetooth.inc.php on line 86
► Copy to clipboard


Example 2: Controlling a robot with a second micro:bit

Since you want to control the moving robot, in this example you use the micro:bit as the transmitter and the mbRobot as the receiver. Since the transmitter does not need all mbRobot commands and vice versa, it makes sense to use two different programmes.

Control is as follows:

  • Button A pressed: drive left (LEFT state)
  • Button B pressed: drive right (RIGHT state)
  • Both buttons pressed: drive straight ahead (FORWARD state)
  • No button pressed: stop (state STOP)

So-called state programming is very useful in this example. There is no point in sending a ‘FORWARD’ message every 10 milliseconds if the other robot is already driving straight ahead. A message is only sent if the state has changed (oldState != state).

Program for the transmitter:

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)
► Copy to clipboard

After the programme starts, an "Ok"-sign sign is displayed on the mbRobot to let you know that it is ready. It then waits for commands, which it receives via Bluetooth. If it receives the message ‘FORWARD’, it executes the command forward() and remains in this state until it receives a new message. With “LEFT” it moves in a left arc, with ‘STOP’ it stops.

Program for 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)
► Copy to clipboard

To move freely with the control unit, you can use a power bank to supply power to the micro:bit after downloading the program.

 

 

REMEMBER...

 

To use the commands for Bluetooth communication, you must import the radio module. The connection is established with radio.on(). A message is sent with radio.send(msg), and with radio.receive() it is received on the other device and stored in a receive buffer until it is retrieved.

 

 

TO SOLVE YOURSELF

 

 

1.

Clicking button A on the micro:bit plays the melody ‘JUMP_UP’ on the mbRobot. Clicking button B plays the melody ‘JUMP_DOWN’.

 

2.

Clicking button A on the micro:bit causes the robot to move forward and a forward arrow appears on the display. Clicking button B causes the robot to stop and the image NO appears on the display. To see whether both devices are ready, the image YES should be displayed on both after starting.

3.

Using a second micro:bit, you should steer the mbRobot through an obstacle course by clicking on buttons A and B to tell it whether to drive straight ahead, turn left or right, or stop.