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 Callibot remotely.

 

 

SENDING AND RECEIVING

 

Thel radio module enables the programming of 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 establishes 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. Use radio.receive() to retrieve the message and the buffer is deleted.

 

 

EXAMPLES

 
Example 1: Button click on Calliope causes an arrow to be displayed on Callibot

When button A on the Calliope is pressed, the massage ‘left’ is sent. The Callibot receives this massage and shows a left arrow on its display. When button B on the Calliope is pressed, the message ‘right’ is sent and the Callibot displays a right arrow. As each Calliope is both a transmitter and receiver, you can press the buttons on the Callibot to cause the arrow to be displayed on the Calliope.

 

Transmitter

Receiver

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


Example 2: Controlling Callibot with a second Calliope

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:

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

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

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

 

 

1.

Click on button A on the Calliope to play the melody ‘JUMP_UP’ on the Callibot. Click on button B to play the melody ‘JUMP_DOWN’.

 

2.

Click on button A to start the robot moving forwards and a forward arrow appears on the display, click on button B to stop the robot and the image NO appears on the display. To show you whether both devices are already running, the image YES should be displayed on both after the start.

3.

With the help of a second Calliope, you should steer the Callibot 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.