Skip to content

Example

Harco Kuppens edited this page Jan 28, 2020 · 18 revisions

Showing how a simple example program can be run in the 3 different ways on the EV3.

Before we can run this example program we must install the 'ev3devlogging' library on the EV3.

This can be done easily in two possible ways:

  • with the Thonny IDE:
        select the menu item "Device > Install the ev3devlogging package on the EV3"
  • with the ev3dev command line tool:
        ev3dev install_logging

simple program: play starwars song

#!/usr/bin/env python3

print("start program")

from time import sleep

from ev3dev2 import sound
mySound=sound.Sound()

# import basic log function from logging library specially for ev3
from ev3devlogging import timedlog as log

# play beep sound
mySound.beep()

log("sleep")
sleep(3)

# print to screen, doesn't get logged in file
print("starwars")

# print to log file, not shown on EV3 screen
log("starwars song")

mySound.tone([
    (392, 350, 100), (392, 350, 100), (392, 350, 100), (311.1, 250, 100),
    (466.2, 25, 100), (392, 350, 100), (311.1, 250, 100), (466.2, 25, 100),
    (392, 700, 100), (587.32, 350, 100), (587.32, 350, 100),
    (587.32, 350, 100), (622.26, 250, 100), (466.2, 25, 100),
    (369.99, 350, 100), (311.1, 250, 100), (466.2, 25, 100), (392, 700, 100),
    (784, 350, 100), (392, 250, 100), (392, 25, 100), (784, 350, 100),
    (739.98, 250, 100), (698.46, 25, 100), (659.26, 25, 100),
    (622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200), (554.36, 350, 100),
    (523.25, 250, 100), (493.88, 25, 100), (466.16, 25, 100), (440, 25, 100),
    (466.16, 50, 400), (311.13, 25, 200), (369.99, 350, 100),
    (311.13, 250, 100), (392, 25, 100), (466.16, 350, 100), (392, 250, 100),
    (466.16, 25, 100), (587.32, 700, 100), (784, 350, 100), (392, 250, 100),
    (392, 25, 100), (784, 350, 100), (739.98, 250, 100), (698.46, 25, 100),
    (659.26, 25, 100), (622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200),
    (554.36, 350, 100), (523.25, 250, 100), (493.88, 25, 100),
    (466.16, 25, 100), (440, 25, 100), (466.16, 50, 400), (311.13, 25, 200),
    (392, 350, 100), (311.13, 250, 100), (466.16, 25, 100),
    (392.00, 300, 150), (311.13, 250, 100), (466.16, 25, 100), (392, 700)
])

Run in the 3 different ways on the EV3

On the EV3

First upload the program with Thonny using the upload button. After uploading we will see "starwars.py" listed on the EV3 in the "File Browser".

Then we have two possible ways to start the program:

  • on the EV3 open the "File Browser" > select "Starwars" > press "ENTER button"
  • within Thonny select the menu item "Device > Start current script on the EV3" ( menu item with lightning icon)

When we start the program we will see on the EV3:

  • an empty screen is opened (takes several seconds)
  • the text "start program" is displayed on the screen
  • a beep sound is played
  • sleeps 3 seconds
  • the text "starwars" is displayed on the screen
  • the starwars song is played

After the program has finished the Brickman GUI is displayed again on the EV3, so then we know that the program is finished. However by pressing the backspace button we can stop a program earlier. Brickman will then take care of stopping all sound/motors etc... for us.
Eg. if we would stop the program halfway the starwars song, then Brickman will stop the song for us.

If an error happens or any logging information is generated then an so called '.err.log' file gets created on the EV3 where all error and log messages are stored. In the starwars program we do some logging, so after running the starwars program we will see a file "starwars.py.err.log" on the EV3.

We can now press the LOG button with Thonny to fetch this log file to our pc for inspecting it.

The content of "starwars.py.err.log" is then:

[...] sleep 
[...] starwars song

Where within brackets the current time when the log is done is printed.

On the Simulator

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.

On the Thonny's Shell panel we can follow the stdout(print) and stderr(log+errors) output which the program produces when running:

>>> %Run starwars.py
start program
[...] sleep
starwars
[...] starwars song

This simple example does not move the robot, but only outputs text and plays sounds. For a more nice demo program which moves the robot in the simulator see the Demo Rover program.

On the PC remotely steering the EV3

Before you can use this mode you first must install an rpyc server on the EV3. This can be easily done in two ways:

  • with the Thonny IDE:
        select the menu item "Device > Install the rpyc server on the EV3."
  • with the ev3dev command line tool:
        ev3dev install_rpyc_server

After this is installed we can continue explaining this remote steering mode.

By default when running/debugging a program from Thonny it is always run in the simulator.

However you can change this behaviour by adding the following import at the beginning of your program:

#!/usr/bin/env python3

import ev3devrpyc

...

By importing the "ev3devrpyc" python module all calls to the ev3dev2 API are not send to the simulator anymore, but instead send to a real EV3 over a network connection.

Thus in remote control mode the program is run locally on the PC, but all the calls to the ev3dev2 library are forwarded to the EV3 using the so called RPyC protocol. So basically we are remotely steering the EV3 over the network from the PC.

This offcourse only works when there is a network connection to the EV3. Eg. when using a network connection over an USB-cable then it only works when the USB-cable is connected to the EV3. However we could also setup a wireless network connection over bluetooth or wifi. In that case it can be convenient to configure the specific IP-address in the "EV3" of the "Options" dialog within Thonny.

So when we press the "Run" button the program will be started on the PC, and we won't see any change on the display of the EV3. Though the EV3 will beep, and the after 3 seconds will play the starwars song.

On the Thonny's Shell panel we can follow the stdout(print) and stderr(log+errors) output which the program produces when running:

>>> %Run starwars.py
start program
[...] sleep
starwars
[...] starwars song

The program is running on the PC, and the EV3 is really just a slave getting commands from its PC master so now and then.

Outside of Thonny we can run the program steering the EV3 with the ev3dev command line tool as follows:

$ ev3dev start starwars.py
start program
[...] sleep
...

When you interrupt the execution of the program with ^C (CONTROL-C) then the starwars song continues on running, because once started it will not stop if the remote connection is disconnected.

Luckily we can force stopping the song by either pressing the 'stop' button in Thonny. Or by using the 'stop' command of the ev3dev command line tool. Both thoroughly stops everything running on the EV3:

$ ev3dev stop
stop programs and motors/sound on EV3
kill programs running on EV3
stop motors on EV3
stop sound on EV3
set leds back to default of green
finished