Jython
Deutsch   English 

2. Actuators


Actuators (also called actors) are active components that execute commands received from the microcontroller. Typical actors are the motors, but LEDs and the buzzer may also be considered as actors because they also react to commands. The following simple examples show how actors can be controlled with a Python program.  

Motors


Example 1: Robot movements
The robot moves forward a short distance, turns left and moves forward again. The mbRobot has a chassis with two motors. The command forward()starts rotating both motors with the same speed. sleep(2000) stops the program execution during 2000 milliseconds while the robot remains in forward motion during this time. The command left() switches the right motor forward and the left motor backward, causing a left turn. The rotation angle depends on the number of milliseconds in the following sleep() function. With 550 ms the robot rotates about 90° to the left.

# Mr2a.py
from mbrobot import *
            
forward()
delay(2000)
left()
delay(550)
forward()
delay(2000)
stop()    
► Copy to clipboard
 

Explanations of the program code:

from mbrobot import * imports the module mbrobot (commands for the motors and other components)
stop(): stops the motors

With a click on the green start button the same program is executed in simulation mode.

Example 2: Repeating program blocks

# Mr2b.py
from mbrobot import *
            
repeat 4: 
    forward()
    delay(2000)
    left()
    delay(550)
stop() 
► Copy to clipboard
 

Explanations of the program code:

repeat 4: Repeat loop. The commands in the indented block are repeated 4 times. Instead of the repeat loop, you can also use a while or a for loop. (The repeat program structure is not part of the Python language but added by TigerJython's environment to simplify programming for beginners).

Example 3: Driving round a curve
The robot moves on a left arc arc with a radius of about 0.1 m and then moves on a right arc. With setSpeed() the speed can be adjusted.

# Mr2c.py
from mbrobot import *

setSpeed(40)  
leftArc(0.2)
delay(3000)
rightArc(0.2)
delay(3000)
stop() 
► Copy to clipboard
 

The following commands are available for controlling robot movements:

 
 

forward()
backward()
left()
right()
leftArc(radius)
rightArc(radius)
setSpeed(speed)
stop()

moves the robot forward
moves the robot backwards
turns to the left
turns to the right
moves on a left curve
moves on a right curve
changes the speed (default 30)
stops both engines

 

All commands are non-blocking, i.e. they return immediately while the robot maintains its state of motion.


LEDs

Example 4: Switch left and right LED alternately on and off.
The command ledLeft.write_digital(1) switches on the left LED. The command ledLeft.write_digital(0) turns it off.

# Mr2d.py
from mbrobot import *

motL.rotate(50)
delay(3000)
motL.rotate(-35)
delay(2000)
motL.rotate(0)
► Copy to clipboard
 

Explanations of the program code:

sleep(500): the LEDs remain in the current state for 500 milliseconds


Example 5: Drive on a left or right curve and switch on the left or right LED.
Since the command leftArc(0.1) is non-blocking, the commands for LEDs can be executed at the same time.

# Mr2e.py
from mbrobot import *
# from mbrobot_plus import * 

repeat 10:
    setLED(1)
    delay(500)
    setLED(0)
    delay(500)
► Copy to clipboard
 

 

Buzzer

Example 6: Play sound using the buzzer
The buzzer can be found on the board behind the slot for the micro:bit. By importing the module music, you can play tones and even whole melodies.

# Mr2f.py
from music import *
 
repeat 10:             
    pitch(784, 400)
    pitch(524, 400)
► Copy to clipboard
 

Explanations of the program code:

pitch(784, 300): Plays a tone of 784 Hz during 300 milliseconds


Example 6: Play a piece of music with the buzzer

# Mr2g.py
from music import *

song = ENTERTAINER
play(song)
► Copy to clipboard
 

 

Explanations of the program code:

play(song): plays the song Entertainer. Some other music files are stored in TigerJython's distribution. These can be used directly (see documentation or TigerJython IDE under Help/APLU documentation).

 


Exercises:


1)


Create a course with some objects and write a corresponding program so that the robot travels from start to finish.

 


2)

The robot should move on a left arc until it has covered an entire circle. Then it should move on a right arc for the same length of time.
 


3)

A robot should reach 4 locations one after the other by first driving forward to the location, then returning backwards to the starting point and turning by about 90 degrees.
 


4)

Use the program before and add some sound, e.g. each time, before the robot moves forward, it emits a tone and before it moves backward, it emits a tone with lower frequency.
 


5)

The commands forward() , left() etc. control the whole vehicle, which consists of two motors. You can also switch on single motors and control their speed.  With motL.forward() only the left motor runs forward. Accordingly with motR.forward() only the right motor rotates. (You have to to import the module mbrobotmot.)

Add some code to the given program template so that
         a) the robot moves forward
         b) the robot moves backwards
         c) the robot rotates to the left or to the right at the location
         d) the robot travels on an arc of a circle

 
from mbrobotmot import *
motL.forward()
sleep(2000)
motL.stop()