TigerJython4Kids robotics Robotik
buggy
 Deutsch   English

## 8. MOVING ROBOT

### YOU LEARN HERE...

 how to construct and program a moving robot.

### ROBOT "BUGGY"

 With little effort, you can assemble a moving robot that has two motors and two light sensors to detect changes in the floor structure. Find a construction manual here. The most important component of the buggy robot is the micro:bit. It assumes the function of a brain, which actuates actors (motors, display) on the basis of sensor data (e.g. bright/dark values of light sensors).

The following block diagram of a "autonomous machine" is very general. You may easily recognize the components on your buggy robot.

Since the micro:bit is part of the machine, it is also called an "embedded system".

### EXAMPLES

Before you tackle a more sophisticated problem, you should always perform a few tests to verify the proper functioning of each component individually.

1. Tests with the left and right motor
The motors have two electric pin connections. Depending on the polarity of the supply voltage, a motor turns in one or the reverse direction. In order to regulate the speed of the motors, they are supplied with 3 V only during a certain time interval, the rest of the time the voltage is 0 V. Thus, for the left motor with input pins P0 and P16, the control signal has the following timing (the right motor has the input pins P12 and P8):

This type of a signal is called pulse width modulation (PWM). You generate a PWM signal at P0 with the command: pin0.write_analog(duty), where duty is a number between 0 and 1023 that determines how long is the ton time with respect to the full period T.

The following program makes the left motor first rotate in one direction for 2000 milliseconds with a moderate speed and then in reverse direction for the same time with lower speed. To power the motors, do not forget to turn on the small switch on the bottom of the buggy chassis. The same switch may also serve to switch the motors off at any time.

Program:

```from microbit import *

pin0.write_analog(200)
sleep(2000)
pin0.write_analog(0)

pin16.write_analog(100)
sleep(2000)
pin16.write_analog(0)
```
► Copy to clipboard

Perform the same test with the right motor using pins 12 and 8.

2. Tests with the light sensors
 The light sensors deliver a smaller or larger voltage, depending on whether the robot is on a light or dark underlay.

In addition, if the buggy is on a dark surface, the two red LEDs will light up. The left sensor is connected to P1, the right to P2. Your program "polls" the voltage at these terminals and writes out the values in the console window.

Program:

```from microbit import *

while True:
left = pin1.read_analog()
right = pin2.read_analog()
print("L: " + str(left) + " - R: " + str(right))
sleep(500)
```
► Copy to clipboard
 With a small screwdriver you can turn the potentiometer and change the switching level between light and dark. Adjust it so that your program detects if there is a light or dark underlay.

3. Controlling robots
 In the next program you add a simple button control. The robot should start the movement only by clicking  button A. For the robot to move forward, you have to rotate both motors forward with the same speed. If the right motor stops, the buggy turns to the right, rotate the right motor again, it drives forward again. The robot should stop on a dark surface.

Program:

```from microbit import *

while not button_a.was_pressed():
sleep(10)
pin0.write_analog(150)
pin12.write_analog(150)
sleep(2000)
pin12.write_analog(0)
sleep(2000)
pin12.write_analog(150)

while True:
left = pin1.read_analog()
print(left)
if left > 100:
pin0.write_analog(0)
pin12.write_analog(0)
sleep(10)
```
► Copy to clipboard

4. Use commands from the module mbutils

It is easier to control the buggy, if you use the commands from the module mbutils (see also Documentation):
 buggy_forward() buggy_backward() buggy_right() buggy_left() buggy_stop() buggy_leftArc(r) buggy_rightArc(r) buggy_setSpeed(speed) isDark(ldr)

 For a small change of direction, the commands buggy_rightArc(r) and buggy_leftArc(r) are appropriate, where you can choose r between 0 and 1. (r is the reduction factor by which one of the wheels turns more slowly than the other.) The buggy should drive on the edge of the black area. The strategy is simple: It has to drive straight when the left sensor "sees" dark and the right sensor sees bright.  It makes a right turn if both sensors see dark and a left turn if both sensors see bright.

Program:

```from microbit import *
from mbutils import *

display.show(Image.YES)
while not button_a.was_pressed():
sleep(10)
buggy_setSpeed(15)
buggy_forward()

while not button_b.was_pressed():
if isDark(ldrL) and not isDark(ldrR):
buggy_forward()
elif isDark(ldrL) and isDark(ldrR):
buggy_rightArc(0.6)
elif not isDark(ldrL) and not isDark(ldrR):
buggy_leftArc(0.6)
sleep(10)
buggy_stop()
display.show(Image.NO)
```
► Copy to clipboard

5. Remote control with a second micro:bit
 You want to remotely control the buggy via Bluetooth using a second micro:bit as remote control. If you press the left button A, the message "LEFT" is sent and the buggy moves to the left. If you press the right button, "RIGHT" is sent and it turns to the right, if you press both buttons at the same time, "FORWARD" is sent and the buggy moves forward. If you release the buttons, "STOP" is sent and the buggy stops.

The program for the buggy:

Program:

```from microbit import *
from mbutils import *
import radio

radio.on()
display.show(Image.YES)
buggy_setSpeed(20)
while True:
rec = radio.receive()
if rec == "FORWARD":
buggy_forward()
elif rec == "LEFT":
buggy_leftArc(0.6)
elif rec == "RIGHT":
buggy_rightArc(0.6)
elif rec == "STOP":
buggy_stop()
sleep(10)
```
► Copy to clipboard

The program for the control:

Program:

```import radio
from microbit 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

The remote control program is a typical example of state programming. Since the buttons are checked every 10 milliseconds in an endless while loop, you have to make sure that a message is sent only if the state changes (oldState! = state). Otherwise, a superfluous command is sent every 10 ms, unnecessarily burdening the system.

### MEMO

 In order to put a complex system into operation, you should test individual components and then assemble them all together. . A motor stays in some state until you put it in another state, e.g. if you call buggy_forward() the motor starts rotating and the function returns.

### EXERCISES

1.
 A bright background changes to a dark background like a barrier. The buggy drives straight to the barrier and then stops for 3 seconds. Afterwards it drives back 3 s and stops. a) Solve the problem by controlling the motors and light sensors directly via the output/input pin functions. b) Solve the task with the functions from the module mbutils.

2.
 Use a black sheet of paper with a large white circle. The buggy should run endlessly (until you click a button) from the middle to the ring, then drive back, turn a little and drive forward again.
3.
 Create a track shaped like the number 8 on a large sheet of paper (preferably with black, self-adhesive felt that you find in the hobby market). Write a program so that the buggy moves along the track until you click one of the buttons.
4.
 You want to remotely control the buggy via Bluetooth using the accelerometer of a second micro:bit. When the remote control is in horizontal position, the buggy should stop. If you tilt it forward, the buggy drives forward, if you tilt it backwards, it drives backwards, if you tilt it to the left, it drives to the left and if you tilt it to the right, it drives to the right.

5.
 PWM signals are also used to dim lamps. Instead of a motor, connect a small LED with a series resistor of about 1 kOhm and write a program that controls the brightness of the LED endlessly up and down. Do not forget the resistor in series, otherwise the LED will be destroyed.

1-1
##### Technical information:
The micro: bit was developed by the BBC to inspire British students to program. One million seventh-graders in the UK were equipped with this device.

The board costs about 20 Fr.

Where to Buy

www.pimoroni.com