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 mbrobot import *
#from mbrobot_plus 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 need to adjust the time when turning left.

In contrast to the turtlegraphics, in which the robot can drive exactly draw a square, 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: Repeat 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-Loopwithout 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 mbrobot import *
#from mbrobot_plus import *
            
repeat: 
    forward()
    delay(2000)
    backward()
    delay(2000)
    left()
    delay(550)
► Copy to clipboard

Here it is useful to know how to abort a running programme: The easiest way is to switch off the power supply.


 

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 makes a similar movement to the previous example. Before it reverses, it stops, flashes twice and reverses to the starting point.

Program:

from mbrobot import *
#from mbrobot_plus import *

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

repeat 4:
    forward()
    delay(2000)
    stop()
    blink()
    blink()
    backward()
    delay(2000)
    left()
    delay(550)
stop()    
► Copy to clipboard
 

 



 

REMEMBER YOU...

 

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 differentiate 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, travels a short distance forwards, turns 180° and travels back to the starting point and turns back in the starting direction. It then 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. First solve the task in simulation mode.

 
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 alarm during this time. It then switches off the alarm and continues travelling on the circular path.