Skip to content

Simulator example

Harco Kuppens edited this page Feb 28, 2020 · 8 revisions

Simulator example

In this page we give an example program which can both be run on the EV3 rover as on the simulator.

A more advanced version of the program can be seen in action in the following video: http://www.cs.ru.nl/lab/ev3dev2simulator.html .

To run the program on the rover just upload the program to the rover, and run it via the menu on the EV3.

To start the program on the simulator is very easy:

  • first we need to start the simulator by pressing the S button on the toolbar.
  • then just press run button with the Run current script popup if you hoover over it with the mouse.

The behaviour of the robot is then simulated in the simulator. This is convenient to quickly test programs when you momentarily don’t have access to an EV3.

When the program is running we can see output in Thonny’s Shell panel in two different colors:

  • black text : output we generate with the print() function. When running on the EV3 this is shown on the EV3's screen. This output stream is called 'stdout', which means it is the standard or default output channel.
  • red text: output we generate with the log() function, or when an error occurs. When running on the EV3 this output is stored in the log file. After running the program on the EV3 we can retreive this log file, and analyse the execution of the program. However on the simulator we see this output immediately in red! This output stream is called 'stderr', which means it is an alternative channel for output besides the default output which we separates errors and logging from the normal output the program should give.

DriveRectangle.py : drives a rectangle and then stops

#!/usr/bin/env python3

# imports
#------------
# import log function
from ev3devlogging import timedlog as log
# import ev3 API
from ev3dev2 import auto as ev3

# initialize
#------------
# initialize left and right motor as tank combo
tankDrive = ev3.MoveTank(ev3.OUTPUT_A, ev3.OUTPUT_D)

# initialize some constants
SPEED_FORWARD = ev3.SpeedPercent(30)     # set speed to 30% of maximum speed
SPEED_BACKWARD = ev3.SpeedPercent(-30)   # backward with same speed as forward
SPEED_ZERO   = ev3.SpeedPercent(0)       # stop motor (speed is zero)

TURN_TIME=0.62

# main loop
#-----------
log("drive forward")
tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_FORWARD, 2)

log("turn right")
tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_BACKWARD,TURN_TIME)

log("drive forward")
tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_FORWARD, 3)

log("turn right")
tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_BACKWARD, TURN_TIME)

log("drive forward")
tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_FORWARD, 2)

log("turn right")
tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_BACKWARD, TURN_TIME)

log("drive forward")
tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_FORWARD, 3)

log("turn right")
tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_BACKWARD, TURN_TIME)

log("finished")