TigerJython4Kids
 robotics
repeat
Deutsch   English   

3. REPEAT LOOP and FUNCTIONS

 

 

YOU LEARN HERE...

 

how a robot can repeat certain command sequences and how you can structure your programs better with named program blocks (functions).

 

 

EXAMPLES

 


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

 

from mbrobot import *
#from mbrobot_plus import *
            
repeat 4: 
    forward()
    delay(2000)
    left()
    delay(550)
stop()
Copy to clipboard

First run the program in simulation mode. In real mode, you may have to adjust the time when turning left.

In contrast to the simulation, in which the robot can drive exactly through 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: Endless loop
In many situations, a robot carries out certain activities “endlessly”, or until it is switched off or the program is aborted. For example, if it has to constantly report back sensor data. A repeat-Schleife 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.

 


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 cancel a running program: The easiest way is with switsch

 


 

Example 3: Structure programs with your own functions
With named program blocks, called functions in Python, you can structure your programs better. It is an important programming technique because more complex programs can be broken down into smaller, easily programmable sub-programs with the help of functions. Functions are also used to advantage when a program 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 statements in the function body are indented. The function is called in the main program.

In your example, you define a blink() function that causes the red LED to light up once. In the main program, the robot makes a similar movement to the previous example. Before it moves backwards, it stops, flashes twice and moves backwards to the starting point.

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...

 

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

To repeat a program block endlessly, use an endless loop:
 repeat:
     Instructions

The indented lines are repeated until the program is terminated by closing the terminal window.

You can use functions to structure your programs better and avoid code duplication. You must differentiate between the function definition and the function call.

Definition:

Call:
def name():
     Instructions
name()

 

 

TO SOLVE YOURSELF

 

 

1.
The robot starts in the middle, travels a short distance forward, turns 180° and travels back to the starting point and turns back in the starting direction. It then turns approximately 120° to the right. It repeats this command sequence three times.  


2.
The robot should first make an “endless” 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 a 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 m. Each time it has covered a complete circle, it stops for 3 seconds and switches on the alarm during this time. It then switches off the alarm and continues on its circular path. First solve the task in simulation mode.