HomeTurtlegraficsGPanelRobotics WebTigerPython |
Python - Online |
Deutsch English |
YOU LEARN HERE... |
how a robot can repeat certain command sequences and how you can structure your programmes better with named programme blocks (functions). |
EXAMPLES |
Program: from callibot import * repeat 4: forward() delay(2000) left() delay(550) stop() Run the programme in simulation mode first. In real mode, you may have to adjust the time when turning left. In contrast to the turtlegrafics, in which the turtle can draw a square exactly, it is difficult for the real robot to drive exactly straight ahead and turn exactly at a right angle. This corresponds to reality, because no car will ever drive exactly straight ahead if the wheel position is fixed, i.e. the steering is locked. This is why robots are equipped with sensors that help them to correct these inaccuracies. So don't spend too much time teaching the robot to drive exactly square. |
Program: from callibot import * repeat: forward() delay(2000) backward() delay(2000) left() delay(550) It is useful to know how to cancel a running programme: The easiest way is to press the blue button at the back of the chassis.
|
Example 3: Structuring programmes with your own functions A function definition always begins with the keyword def, followed by a function name, a parameter bracket and a colon. The instructions in the function body are indented. The function is called in the main programme. In your example, you define a blink(), function that causes the red LED to light up once. In the main programme, the robot first moves forwards for 200 ms and then backwards for 2000 ms. While travelling backwards, it flashes both LEDs twice. It repeats this movement twice.
|
REMEMBER ... |
To repeat a programme block n times, use a repeat loop:
To repeat a programme block endlessly, use an infinite loop:
The indented lines are repeated until the programme is aborted by closing the terminal window. You can use functions to structure your programmes better and avoid code duplication. You must distinguish between the function definition and the function call.
|
TO SOLVE BY YOURSELF |
|