-
Notifications
You must be signed in to change notification settings - Fork 2
Example
#!/usr/bin/env python3
print("start program")
from time import sleep
# get ev3dev library in current context
import ev3devcontext
ev3=ev3devcontext.importModule("ev3dev.ev3")
# get logger without times
logger=ev3devcontext.getLogger(timing=False)
# all possible levels: DEBUG,INFO,WARNING,ERROR,CRITICAL
# only show log messages from level INFO or higher
import logging
logger.setLevel(logging.INFO)
## uncomment next line to disable logging
#logger.disabled=True;
logger.debug("debug msg")
logger.critical("critical msg")
logger.info("info msg")
logger.warn("warn msg")
logger.error("error msg")
# play beep sound
ev3.Sound.beep().wait()
logger.debug("sleep")
sleep(3)
# print to screen, doesn't get logged
print("starwars")
logger.info("starwars song")
ev3.Sound.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)
]).wait()TODO: simulator in development
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:
- pressing the "start" button in Thonny ( button with lightning icon)
- on the EV3 open the "File Browser" > select "Starwars" > press "ENTER button"
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 display again on the EV3, so when we see that we know the program is finished. However by pressing the backspace button we stop a program earlier. Brickman will 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 either any errors happened or any logging information is generated then an so called '.err.log' file gets created on the EV3. 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":
[CRITICAL] critical msg
[INFO] info msg
[WARNING] warn msg
[ERROR] error msg
[INFO] starwars song
Notice that the DEBUG messages are not logged!
When running/debugging a program from Thonny in remote control mode then the program is run locally on the PC, but all the calls to the ev3devcontext library are forwarded to the EV3 using the RPyC protocol, so basically remotely steering the EV3.
So when we press the "Run in remote control mode" 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:
>>> %Ev3RemoteRun starwars.py
start program
[CRITICAL] critical msg
[INFO] info msg
[WARNING] warn msg
[ERROR] error msg
starwars
[INFO] 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 commandline tool as follows:
$ ev3dev steer starwars.py start program [CRITICAL] critical msg ...
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. We can force stopping the song by using the 'stop' command which 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