Deutsch English |
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. |
# Mr2a.py from mbrobot import * forward() delay(2000) left() delay(550) forward() delay(2000) stop() |
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() |
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() |
The following commands are available for controlling robot movements:
forward() |
moves the robot forward |
All commands are non-blocking, i.e. they return immediately while the robot maintains its state of motion.
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) |
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) |
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) |
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) |
|
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) |
|
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 |
from mbrobotmot import * motL.forward() sleep(2000) motL.stop() |