deutsch     english    français     Print

 

5.1 REAL AND SIMULATION MODE

 

 

INTRODUCTION

 

A robot is usually understood as a computer-controlled machine that can perform an activity, previously done by humans. If the machine can also detect the surrounding environment with the help of cameras and sensors and can then react appropriately with actuators (motors, valves, speech synthesizers, etc.), we speak of an intelligent system. If the behavior of such a system is human-like, we speak of an android. .

A typical example of this is the movie robot WALL-E who has his own consciousness, so much that he is looking for spare parts for himself, as well as special objects that catch his interest, which he treasures in a collection. He is also able to solve a Rubik's cube, which is definitely seen as a sign of intelligence.  

Artificial Intelligence (AI) deals with the interesting question of exactly how a computer system can be described as "intelligent". In order to answer this question, we must first define what is meant by an “intelligent” machine. One possible approach to the definition of intelligence is the Turing Test.

Here you will be concerned with more simple questions and you will learn to handle a simple robot equipped with touch, light, sound, infrared, and ultrasound sensors, and also with two wheels driven by electric motors, which allow it to move forwards and backwards, and even rotate.

The motors and sensors are controlled by the built-in computer, which is why people also call robots an embedded system. If it often consists in a simple computer chip, it is called a microprocessor (or a microcontroller). Nowadays, embedded systems play an extremely important role and you can find them in many everyday devices, for instance in smartphones. Surprisingly, most coffee machines, washing machines, televisions, cameras, and other similar electronics are also embedded systems. In a modern car, there are up to 100 microcontrollers that act as embedded systems in places such as the engine control or the anti-blocking system. Therefore, you should be aware that you are also getting to know many general principles for embedded systems while learning about robots.

If the built-in processor runs a stand-alone program in order to control the robot, we speak of an autonomous robot. The built-in processor can also simply send the data collected from the sensors over a data communication canal to an external computer, and then obtain control commands from this computer. In this case, we speak of an remotely controlled robot. Finally, a robot can also be simulated, which usually means that the sensors, motors, etc. are depicted as software objects. A class construction then corresponds to the real-world assembly of robot components. In general practice, robots are usually first simulated on the computer since this can create a behavior to be studied with little effort and without any risks to the environment.

With the world famous robotics kit by LEGO Mindstorms you can learn the important aspects of robotics in a playful way. The kit consists of a microprocessor-controlled brick and a variety of components used to construct different robot models. The brick has gone through several stages of development: earlier it was called RCX, then NXT, and more recently EV3.

The EV3 brick is an embedded system with motors and sensors controlled by a modern ARM processor. If you open it, its electronic components will be visible.


 
PC port
4 motor ports
USB port
loudspeaker
SD card
32 bit ARM9 processor
Texas Instrument AM1808
with 64 MB RAM and
16 MB flash drive
4 sensor ports
       

Once you turn on the brick, a firmware starts on the microcontroller (or with EV3 the Linux operating system) and a simple menu appears on the display. With this, you can already run programs stored on the brick, in autonomous mode. For the external control mode on the EV3, you have to start a helper program (BrickGate) which interprets commands that are received through a Bluetooth connection, for example the command to turn on a motor in a certain rotation direction or to report back the measured value of a sensor. With the NXT, this program is a part of the firmware.

As with all embedded systems in robotics, you need an external computer on which you develop the robot programs. In autonomous mode the program is downloaded onto the brick, and in external control mode it runs on the PC.

Autonomous mode

  External control mode

1. Edit the program

 

 

2. Download and end the connection
3. Runs on the EV3

1. Edit the program

 

 

2. Runs on the PC.
Communicates with the EV3

 

  PROGRAMMING CONCEPTS:
Robots, androids, artificial intelligence, embedded systems, microprocessors, microcontrollers, blocking/non-blocking methods

 

 

PREPARATIONS

 

With TigerJython you can simulate the robot (simulation mode) or using autonomous or external control modes (real mode). You thereby use different class libraries which, however, offer the same programming interface (Application Programming Interface, API) so that the programs are practically identical for all modes. The only things that have to be adjusted are imports and possibly some timings used in the program.

Simulation mode:
If you do not have an NXT or an EV3 available to you, you can nonetheless work through the topic in simulation mode. The images required for the simulation are already included in the distribution of TigerJython.

Real mode:
Most examples will use the basic model of the LEGO Mindstorm NXT or EV3 robot, which moves with two motors and can be equipped with various sensors. As long as you do not change the basic functionality, you can also use your own deviated model. Since the robot communicates through Bluetooth, your PC must be Bluetooth compatible and enabled. Moreover you have to “pair” the brick with your computer.  

Using the LEGO NXT:
Make sure that the Java firmware leJOS is installed onto the LEGO NXT. Here you find instructions how to proceed.

To pair it with the computer, you proceed as you would with other external Bluetooth devices such as smartphones, Bluetooth handsets, printers, etc.

With Python, you can use the NXT only in its external control mode. For this, you write a regular Python program in TigerJython using the module ch.aplu.nxt and then hit the green Run button to start. First you will be asked for the Bluetooth name and then the connection with the brick will be established. During the program execution, a window with the connection information remains open. If you close this window, the connection to the NXT is interrupted.

 

If there are several LEGO NXT's in the room, the names for each must be different so that there are no conflicts. You can find a tool for changing the name here. You can also use the Bluetooth address instead of the Bluetooth name, which you can figure out with the help of different tools (one place to find it is shown above: it is written out in the connection dialog each time that a connection is established).

The BlueCove library is necessary for Bluetooth communication. Download the files here and unzip them in the subdirectory Lib of the directory in which tigerjython2.jar is located.

Using the LEGO EV3:
A Linux operating system runs on the EV3 that boots in conjunction with the leJOS software, which is located on the SD card. You can find a detailed guide on how to create the SD card here. If you remove the SD card you can use the EV3 in its original state. If the EV3 is started using leJOS you communicate with it through a Bluetooth PAN connection. To do this, you have to pair the PC with the brick and specify it as a network access point. You can find instructions here.

After booting the EV3 with leJOS and successfully connecting it via Bluetooth PAN, you need to start the BrickGate server on the brick, which you can find in the menu "programs".

To use the EV3 in autonomous mode, check both boxes on the libraries tab in the settings of TigerJython, activate EV3 download and run after download. You will then find an additional EV3 icon on the toolbar.


For the external control mode, you click on the green Run button as usual. Just as with the NXT, you will first be asked for the Bluetooth name and then a window will open with the connection information. If you want to run the same program autonomously, simply click on the EV3 button. The Python script is then downloaded onto the EV3 and executed there. Its name also appears on the display of the EV3 and it can always be executed again with the Enter button, even without a connection to the PC.

In the programs we assume that for the EV3 you are using the new motors and sensors from the EV3 product line. The EV3 color sensor also serves as a light sensor. However, the old NXT motors and sensors are still supported for the EV3. You simply have to put "Nxt" everywhere before the class name, i.e. NxtMotor, NxtGear, NxtTouchSensor, etc.

 

 

MOVING FORWARDS AND BACKWARDS, TURNING

 

In your first robot program, the robot should move for certain times, which are hard coded in the program. The robot library is object-oriented and depicts reality by a model. So, when in reality you pick up the LEGO brick when constructing the robot, in the software you create an instance of the class LegoRobot() with robot = LegoRobot(). Next you take two motors and put them together to a gear, which you can express as gear = Gear() in the software. Then you connect the gear motors to the motor ports A and B, which you formulate as addPart() in the software.

With the command gear.forward() you turn on both engines simultaneously with the same rotational speed and the robot moves straight ahead. This state of movement will remain the same until you undertake something else. However, the call returns immediately and your program continues onwards with the next instruction (this is called a non-blocking method). Therefore, your program has to ensure that the robot does something else after a certain amount of time. To do this, you can tell the program to wait using Tools.delay() and then change or stop the movement with another command.  

Once you send a movement command to the robot, the current state is ended and replaced by the new state. At the end of the program you should always call the method exit(). Once called, all motors are stopped and in the real mode the Bluetooth connection is also interrupted, which is necessary for the next program to start successfully. (If the program does not end correctly you might have to turn the brick off and on again.)

from simrobot import *
#from nxtrobot import *
#from ev3robot import *
  
robot = LegoRobot()
gear = Gear()
robot.addPart(gear)

gear.forward()
Tools.delay(2000)
gear.left()
Tools.delay(545)
gear.forward();
Tools.delay(2000)
robot.exit()
Highlight program code (Ctrl+C to copy, Ctrl+V to paste)

 

 

 

MEMO

 

A Gear has two motors. Instead of controlling the motors individually, you can use commands that affect both engines simultaneously.

The class libraries for both the simulation and the real mode are designed so that their programs are almost identical. You can first develop your program in the simulation mode and then with a few adaptations you can execute it with the real robot.

You can use the EV3 autonomously or externally controlled. In both cases the BrickGate program must be started on the EV3, which receives and appropriately interprets the commands sent from Python. Since no errors are displayed while running in autonomous mode, you should always first test the program in external control mode (green button) and only then download it to the brick using the EV3 button and execute it there.

 

 

MOVING WITH BLOCKING METHODS

 

Instead of moving the robot forward with the command forward() and then telling the program to wait 2000 ms with delay(2000), you can use the blocking method forward(2000) which also moves the robot forward, but only returns after 2000 ms. There are also blocking variants for left() and right().

You can simplify the previous program slightly with blocking methods.
from simrobot import *
#from nxtrobot import *
#from ev3robot import *
  
robot = LegoRobot()
gear = Gear()
robot.addPart(gear)
gear.forward(2000)
gear.left(545)
gear.forward(2000)
robot.exit()
Highlight program code (Ctrl+C to copy, Ctrl+V to paste)

 

 

MEMO

 

You must distinguish between blocking and non-blocking methods. Non-blocking commands cause the robot to change state and return immediately. If you call a blocking method, the program freezes a certen time interval, i.e. the next statement is only executed when the time interval has expired.

At first glance, it might seem easier to always use blocking methods. But with them you get a major disadvantage. While your program is blocked you cannot execute any other actions, so for example, you could not read any sensor values! If the program hangs during execution, in external control mode you can cancel it by closing the connection information window. In autonomous mode in the case of an emergency, you can simultaneously press the two buttons DOWN+ENTER .

 

 

EXERCISES

 

1.

Write a program that makes your robot trace a square, using blocking methods.

 

2.

Write a program using non-blocking methods so that the robot moves along half-circle curves.

 

 
3.

Create a course with some objects and write a corresponding program so that the robot moves from the start to the finish line.

For simulation mode, you can use the background image bg.gif located in the subdirectory sprites, by displaying it with RobotContext.useBackground().


Using RobotContext.setStartPosition() you can set the robot to a specific location at the start of the program. (window coordinates are from 0 to 500, 0 is at the top left corner).
 
RobotContext.setStartPosition(200, 455)
RobotContext.useBackground("sprites/bg.gif")

You can also create your own image (the image size should be 501x501).

 

 

   

ADDITIONAL MATERIAL


 

INFRARED REMOTE CONTROL

 

A versatile infrared sensor comes with the EV3 robot that can be used in many ways. It is already included in the LEGO Home set (including the remote control box), but it must be purchased separately if you have the Education set. You can use the IRSensor in one of the three following ways:

Class Metrics
IRSeekSensor distance and direction to the IR source of the remote control
IRRemoteSensor pressed buttons of the remote control
IRDistanceSensor distance to a reflective target

 

The use of the remote control is fun and motivating for your first "tentative steps" with the robot. As opposed to a predetermined remote control program, you can set the actions that which are triggered when pressing the remote control through simple Python programming.

 

 


You decide to use the following actions in your program:

Remote control button Action
Left-Up moves forward on a left bend
Right-Up moves forward on a right bend
Left-Down+Right-Up moves straight forward
Left-Down stops
Right-Down ends program

 

from ev3robot import *

robot = LegoRobot()
gear = Gear()
robot.addPart(gear)
irs = IRRemoteSensor(SensorPort.S1)
robot.addPart(irs)
isRunning = True

while not robot.isEscapeHit() and isRunning:
    command = irs.getCommand()    
    if command == 1:
        gear.leftArc(0.2)
    if command == 3:
        gear.rightArc(0.2)
    if command == 5:
        gear.forward()
    if command == 2:
        gear.stop()
    if command == 4:
        isRunning = False       
robot.exit()
Highlight program code (Ctrl+C to copy, Ctrl+V to paste)

 

 

MEMO

 

The methods isEscapeHit(), isEnterHit(), isDownHit(), isUpHit(), isLeftHit(), isRightHit() return True, if you click the corresponding buttons on the EV3 in autonomous mode.

However, in external control mode, they pertain to the keyboard keys ESCAPE, ENTER, CURSOR-DOWN, CURSOR-UP, CURSOR-LEFT, CURSOR-RIGHT. For this, the connection information window must be active (click in it with the mouse to activate it).