HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
repeat
Deutsch   English   

3. REPEAT COMMANDS, FUNCTIONS

 

 

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

 


Example 1: Moving a square
To move a square, the robot must move forwards a distance four times and turn 90°. You use a repeat loop for the repetitive movement. This is introduced with repeatfollowed by the number of repetitions and a colon. The commands that are to be repeated must be indented.

 

Program:

from callibot import *
            
repeat 4: 
    forward()
    delay(2000)
    left()
    delay(550)
stop()
► Copy to clipboard

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.


 

Example 2: Repeating commands endlessly
In many situations, a robot carries out certain activities ‘endlessly’, or until it is switched off or the programme is aborted. For example, if it constantly has to report back sensor data. A repeat loop without a number of repetitions is used for this.

In your example, the robot visits four locations endlessly, which are on paths that are at right angles to each other, and returns to the starting point each time.


 


Program:

from callibot import *
            
repeat: 
    forward()
    delay(2000)
    backward()
    delay(2000)
    left()
    delay(550)
► Copy to clipboard

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
With named programme blocks, called functions in Python, you can structure your programmes better. It is an important programming technique because more complex programmes can be broken down into smaller, easily programmable sub-programmes with the help of functions. Functions are also used to advantage when a programme block is used several times.

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.


Programm:

from callibot import *

def blink():
    setLED(1)
    delay(500)
    setLED(0)
    delay(500)

repeat 2:
    forward()
    delay(2000)
    backward()
    blink()
    blink()
    stop()
► Copy to clipboard

 

 

REMEMBER ...

 

To repeat a programme block n times, use a repeat loop:
repeat n: 
    Instructions 
        
 

To repeat a programme block endlessly, use an infinite loop:

repeatn: 
    Instructions 
        
 

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.

Definition:
def name(): 
    Instructions 
        
Call:
name()

 

 

TO SOLVE BY YOURSELF

 

 

1.

The robot starts in the centre, moves a short distance forwards, turns 180° and moves back to the starting point and turns back in the starting direction. Then it turns about 120° to the right. It repeats this command sequence three times.

 


2.

The robot is to drive ‘endlessly’ first a circle on the left-hand bend and then a circle on the right-hand bend.

 


3.

Define a function step() with which your robot moves one step. Then call the function three times so that the robot moves the adjacent figure

 
4.

The robot moves ‘endlessly’ on a circular path with a radius of 0.2 metres. Each time it has travelled a complete circle, it stops for 3 seconds and switches on the LEDs during this time. It then switches off the LEDs and continues to move along the circular path.