HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
buttons
Deutsch   English   

7. REMOTE CONTROL VIA BLUETOOTH

 

 

YOU LEARN HERE...

 

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

 

 

SENDING AND RECEIVING

 

The radiomodule integrated in TigerJython makes it possible to program simple applications in which two robots communicate with each other via Bluetooth. Each robot can be a transmitter and a receiver at the same time. First, both robots switch on the transmitter/receiver device with radio.on(). This creates a Bluetooth connection. Then each robot can use radio.send(msg)to send a message (as a string) to the other robot, which is stored in a receive buffer there. With radio.receive()you fetch the message and the buffer is deleted.

 

 

EXAMPLES

 
Example 1: Click on micro:bit buttons an arrow to be displayed on mbRobot

When button A on the micro:bit is pressed, the massage “left” is sent. The robot receives this massage and shows 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. As each micro:bit is a transmitter and receiver at the same time, you can press the buttons on the mbRobot to cause the arrow to be displayed on the microBit.

 
 

Sender

Receiver

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).
The line display.show(Image.ARROW_E) causes a link arrow to be displayed.

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


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

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:

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

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

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

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

 

 

1.

Click on button A on the micro:bit to play the melody “JUMP_UP” on the mbRobot. Click on button B to play the melody “JUMP_DOWN”.

 

2.

Clicking on the A button on the micro:bit starts the robot moving forward and a forward arrow appears on the display, clicking on the B button stops the robot and the image NO appears on the display. To see whether both devices are already running, the image YES should be displayed on both after the start.

3.

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