You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Me again, the webmaster of ev3python.com where I try to make it easier for beginners to make a start with ev3dev and ev3 Python.
I haven't been able to write text or graphics to the EV3 LCD screen using EV3 Python except by running the ridiculously complex code on http://www.ev3dev.org/docs/tutorials/using-ev3-lcd/ Thirty lines of code just to draw two lines and a circle?! There has to be a better way. And how do I display test, or an image file. Please help!
The Screen class has draw property, which is just an instance of pillow's (python imaging library) ImageDraw.Draw class. See pillow's documentation here: https://pillow.readthedocs.io/en/3.3.x/
All operations are applied to internally help image, which is dumped to the actual LCD when update() method is called. Here is an example that draws an alternating smiley-frawny face on EV3 screen:
Your code gives me the following error:
File "smiley.py", line 4, in
screen = ev3.Screen()
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 2310, in init
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2030, in new
return Image()._new(core.fill(mode, size, color))
TypeError: integer argument expected, got float
Once I've managed to write or draw to the screen, how do I get Brickman back?
I am still using python2 on ev3 (its the default I think). It looks like division on line 2310 should be changed to integer division (//) for this to work in python3. I am afraid I won't be able to make a pull request in a couple of weeks (on vacation, away from computer). For now, you could try to run the script with python2.
Once I've managed to write or draw to the screen, how do I get Brickman back?
When you run your scripts from brickman menu, you should get your own screen. It will switch back to brickman when your script finishes. If you run your script from ssh session, you would get weird results: brickman and your script will both write to the same screen.
I've never yet run a script from Brickman. I have to modify the file permissions before I can do that, don't I? Will Brickman know that it should use Python3?
@dlech has spoken! To run an application that writes to the display via SSH:
Run sudo chvt 6. This just switches the display to show you an empty terminal instead of Brickman, so that Brickman doesn't mess with you.
Run sudo conspy. This will make your SSH session show the same terminal that is on the EV3's display. Log in to the prompt it shows you.
Run your program. You should see it show up on the EV3 display, as you'd expect.
You can work from this terminal like normal, run your program multiple times, etc.
Press ESC 3 times to exit the display terminal
Run sudo chvt 1 to get Brickman back.
It seems a bit complicated, but it's reasonably easy. If you don't mind a little blinking cursor showing up on top of your program, I believe you can skip steps 2 and 4. This also means that the console isn't small, because instead of using the one on the tiny EV3 display, you are using a normal one and just displaying your code's shapes on the display.
As for running it with Brickman, I believe you just need to do two things (once per Python program).
Run chmod +x my-file.py, using the real name of your file
Add the line #!/usr/bin/env python3 to the top of your file. Make sure it is the very fist line!
I am still using python2 on ev3 (its the default I think). It looks like division on line 2310 should be changed to integer division (//) for this to work in python3.
This change has already been made, however @ndward is using version 0.6.0, which doesn't support Python 3 in full. See #195 (comment).
It's common to run, via SSH, Python scripts that can only be terminated with Ctrl+C. What is the equivalent trick to stop the same scripts if they are launched from within Brickman?
I followed the instructions above to make a script executable from Brickman:
Run chmod +x my-file.py, using the real name of your file
Add the line #!/usr/bin/env python3 to the top of your file. Make sure it is the very fist line!
And it worked for this script
#!/usr/bin/env python3
# so that script can be run from Brickman
from ev3dev.auto import * # needed for Sound
Sound.beep()
but not for this one (I used chmod as suggested)
#!/usr/bin/env python3
# so that script can be run from Brickman
from ev3dev.auto import *
from time import sleep
m = LargeMotor('outB')
m.run_to_rel_pos(position_sp=360, duty_cycle_sp=60, stop_command="hold")
sleep(5)
The same script works fine via SSH.
How come the line #!/usr/bin/env python3
is needed? Aren't comments ignored?
And is it OK to add anything at the end of that line?
When the first line starts with a #! followed by a command, its called a shebang. Bash knows to execute the command specified and to pass the rest of the script as its argument.
but not for this one (I used chmod as suggested)
Make sure the file has unix line-endings. When you forget to convert from Windows format, bash thinks that an extra \r in the end of the first line is part of the command.
I thought I had the LCD nailed but I have a small problem. This script runs forever and if I start the script from Brickman I can't exit it properly with a long-press on the Back button ( the brick just starts toggling back and forth between Brickman and the graphic display). I have to a long press on Back and Enter to restart the brick.
#!/usr/bin/env python3
from time import sleep
from ev3dev.ev3 import *
lcd = Screen()
smile = True
while True:
lcd.clear()
lcd.draw.ellipse(( 20, 20, 60, 60))
lcd.draw.ellipse((118, 20, 158, 60))
if smile:
lcd.draw.arc((20, 80, 158, 100), 0, 180)
else:
lcd.draw.arc((20, 80, 158, 100), 180, 360)
smile = not smile
lcd.update()
sleep(1)
Do you agree that the simplest way to do a screen capture is to run this at the command line? fbgrab "screenshot.png"
It seems to work for me. It saves a black and white png image called 'screenshot.png' into the current folder.
Do you agree that the simplest way to do a screen capture is to run this at the command line?
Yes. That's actually what my Python script does; it just goes a step further in recoloring and scaling it.
This script runs forever and if I start the script from Brickman I can't exit it properly with a long-press on the Back button ( the brick just starts toggling back and forth between Brickman and the graphic display).
I'm not sure what's going on there, but I was able to repro it. I know that Brickman sends SIGTERM with pkill, but for a reason which I am unaware of it doesn't work for Brickman but does for me. Simplest workaround is to run pkill python3 after attempting to end it with Brickman.
Yes, when I changed lcd.image.paste((0,0), logo) into lcd.image.paste(logo, (0,0)) it worked, so I deduce that the simplest code to display an image would be
#!/usr/bin/env python3fromev3dev.ev3import*fromtimeimportsleepfromPILimportImage, ImageDraw, ImageFont, ImageOpslcd=Screen()
logo=Image.open('pics/Bomb.bmp')
lcd.image.paste(logo, (0,0))
lcd.update()
sleep(6) # when running from Brickman, need time to admire image
In fact the linefrom PIL import Image, ImageDraw, ImageFont, ImageOps could be shortened to from PIL import ImageDraw but perhaps it is a good habit to use the fuller line?
In fact the linefrom PIL import Image, ImageDraw, ImageFont, ImageOps could be shortened to from PIL import ImageDraw but perhaps it is a good habit to use the fuller line?
I guess a good habit is to just import what is necessary. Here you only use Image to load the logo, so you could omit other imports. You may also need ImageFont to load fonts. ImageOps is no longer necessary since you found a better way to load and display pictures. And you don't need to explicitly import ImageDraw because Screen class creates the draw handle for you.
... and you said in an earlier message that "you can omit Image from PIL imports. That is already imported by ev3dev.ev3." so the whole PIL import line can go, leaving
#!/usr/bin/env python3
from ev3dev.ev3 import *
from time import sleep
lcd = Screen()
logo = Image.open('pics/Bomb.bmp')
lcd.image.paste(logo, (0,0))
lcd.update()
sleep(6) # when running from Brickman, need time to admire image
That accidentally works when you do from ev3dev.ev3 import *, as this brings into global namespace everything the ev3 module defines and imports. But in general a user has no way of knowing what is being imported by the specific module, so its better to explicitly import modules one uses.
Here Image module is used explicitly to load the bomb logo, while lcd.draw and lcd.image are instances constructed by the Screen class constructor. Strictly speaking, a user should not even know/care what classes are these instances of (except for that they have same interfaces as ImageDraw.Draw and Image).
Is it possible to use print() to print to the EV3 LCD screen when a script is run from Brickman? I was expecting this script to work in Brickman but it does not, though it works fine from SSH.
#!/usr/bin/env python3
# this prints the cosine of 45 degrees
# works from SSH but not from Brickman
from time import sleep
import math
print (math.cos(math.radians(45)))
sleep(6) # give enough time for result to be read in Brickman
Activity
ddemidov commentedon Sep 4, 2016
The
Screenclass hasdrawproperty, which is just an instance of pillow's (python imaging library) ImageDraw.Draw class. See pillow's documentation here: https://pillow.readthedocs.io/en/3.3.x/All operations are applied to internally help image, which is dumped to the actual LCD when
update()method is called. Here is an example that draws an alternating smiley-frawny face on EV3 screen:ndward commentedon Sep 4, 2016
Your code gives me the following error:
File "smiley.py", line 4, in
screen = ev3.Screen()
File "/usr/local/lib/python3.4/dist-packages/python_ev3dev-0.6.0-py3.4.egg/ev3dev/core.py", line 2310, in init
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2030, in new
return Image()._new(core.fill(mode, size, color))
TypeError: integer argument expected, got float
Once I've managed to write or draw to the screen, how do I get Brickman back?
ddemidov commentedon Sep 4, 2016
I am still using python2 on ev3 (its the default I think). It looks like division on line 2310 should be changed to integer division (
//) for this to work in python3. I am afraid I won't be able to make a pull request in a couple of weeks (on vacation, away from computer). For now, you could try to run the script with python2.ndward commentedon Sep 4, 2016
No worries, for I'm just leaving also for a two week vacation.
ddemidov commentedon Sep 4, 2016
When you run your scripts from brickman menu, you should get your own screen. It will switch back to brickman when your script finishes. If you run your script from ssh session, you would get weird results: brickman and your script will both write to the same screen.
WasabiFan commentedon Sep 4, 2016
@ddemidov I can test that out and open a PR if it fixes the problem.
Also, about the screen thing: I think there's a way to do it via SSH as well but can't remember the command. @dlech will have do advise on that.
ndward commentedon Sep 4, 2016
I've never yet run a script from Brickman. I have to modify the file permissions before I can do that, don't I? Will Brickman know that it should use Python3?
WasabiFan commentedon Sep 4, 2016
@dlech has spoken! To run an application that writes to the display via SSH:
sudo chvt 6. This just switches the display to show you an empty terminal instead of Brickman, so that Brickman doesn't mess with you.sudo conspy. This will make your SSH session show the same terminal that is on the EV3's display. Log in to the prompt it shows you.sudo chvt 1to get Brickman back.It seems a bit complicated, but it's reasonably easy. If you don't mind a little blinking cursor showing up on top of your program, I believe you can skip steps 2 and 4. This also means that the console isn't small, because instead of using the one on the tiny EV3 display, you are using a normal one and just displaying your code's shapes on the display.
As for running it with Brickman, I believe you just need to do two things (once per Python program).
chmod +x my-file.py, using the real name of your file#!/usr/bin/env python3to the top of your file. Make sure it is the very fist line!dlech commentedon Sep 4, 2016
You have to press ESC 3 times to exit
conspyWasabiFan commentedon Sep 4, 2016
Fixed.
WasabiFan commentedon Sep 5, 2016
This change has already been made, however @ndward is using version 0.6.0, which doesn't support Python 3 in full. See #195 (comment).
ndward commentedon Sep 5, 2016
It's common to run, via SSH, Python scripts that can only be terminated with Ctrl+C. What is the equivalent trick to stop the same scripts if they are launched from within Brickman?
ddemidov commentedon Sep 5, 2016
You can long press back button to end any program that was started with brickman. Another useful shortcut is back+enter to reset the brick.
ndward commentedon Sep 5, 2016
I followed the instructions above to make a script executable from Brickman:
Run
chmod +x my-file.py, using the real name of your fileAdd the line
#!/usr/bin/env python3to the top of your file. Make sure it is the very fist line!And it worked for this script
but not for this one (I used chmod as suggested)
The same script works fine via SSH.
How come the line
#!/usr/bin/env python3is needed? Aren't comments ignored?
And is it OK to add anything at the end of that line?
ddemidov commentedon Sep 5, 2016
When the first line starts with a
#!followed by a command, its called a shebang. Bash knows to execute the command specified and to pass the rest of the script as its argument.Make sure the file has unix line-endings. When you forget to convert from Windows format, bash thinks that an extra
\rin the end of the first line is part of the command.42 remaining items
WasabiFan commentedon Sep 27, 2016
Yeah, those errors make sense; glad to see it works overall though!
ndward commentedon Sep 27, 2016
I thought I had the LCD nailed but I have a small problem. This script runs forever and if I start the script from Brickman I can't exit it properly with a long-press on the Back button ( the brick just starts toggling back and forth between Brickman and the graphic display). I have to a long press on Back and Enter to restart the brick.
ndward commentedon Sep 28, 2016
Do you agree that the simplest way to do a screen capture is to run this at the command line?
fbgrab "screenshot.png"It seems to work for me. It saves a black and white png image called 'screenshot.png' into the current folder.
WasabiFan commentedon Sep 28, 2016
Yes. That's actually what my Python script does; it just goes a step further in recoloring and scaling it.
I'm not sure what's going on there, but I was able to repro it. I know that Brickman sends SIGTERM with
pkill, but for a reason which I am unaware of it doesn't work for Brickman but does for me. Simplest workaround is to runpkill python3after attempting to end it with Brickman.ddemidov commentedon Sep 28, 2016
My bad, should be
lcd.image.paste(logo, (0,0))(reordered arguments). Also, not sure how your above examples of paste worked like this.ndward commentedon Sep 28, 2016
Yes, when I changed
lcd.image.paste((0,0), logo)intolcd.image.paste(logo, (0,0))it worked, so I deduce that the simplest code to display an image would beIn fact the line
from PIL import Image, ImageDraw, ImageFont, ImageOpscould be shortened tofrom PIL import ImageDrawbut perhaps it is a good habit to use the fuller line?ddemidov commentedon Sep 28, 2016
I guess a good habit is to just import what is necessary. Here you only use
Imageto load the logo, so you could omit other imports. You may also needImageFontto load fonts.ImageOpsis no longer necessary since you found a better way to load and display pictures. And you don't need to explicitly importImageDrawbecauseScreenclass creates thedrawhandle for you.ndward commentedon Sep 28, 2016
... and you said in an earlier message that "you can omit Image from PIL imports. That is already imported by ev3dev.ev3." so the whole PIL import line can go, leaving
ddemidov commentedon Sep 28, 2016
That accidentally works when you do
from ev3dev.ev3 import *, as this brings into global namespace everything theev3module defines and imports. But in general a user has no way of knowing what is being imported by the specific module, so its better to explicitly import modules one uses.Here
Imagemodule is used explicitly to load the bomb logo, whilelcd.drawandlcd.imageare instances constructed by theScreenclass constructor. Strictly speaking, a user should not even know/care what classes are these instances of (except for that they have same interfaces asImageDraw.DrawandImage).Baseline: I would still import
Imageexplicitly.ndward commentedon Oct 12, 2016
Is it possible to use
print()to print to the EV3 LCD screen when a script is run from Brickman? I was expecting this script to work in Brickman but it does not, though it works fine from SSH.ddemidov commentedon Oct 15, 2016
The following example works for me (from brickman):
Keep in mind that the default font is tiny and its possible to overlook small amount of text.
ndward commentedon Oct 16, 2016
Yes, that works fine, thank you, and so does the script that was failing for me previously - I don't know what was wrong.
ddemidov commentedon Oct 16, 2016
Ok, it looks like this may be closed now. Feel free to reopen if necessary.
anzy0621 commentedon Apr 1, 2019
How do I input some coordinate values into the lego ev3 brick so that the robot and return to the x, y position that was provided as an input to it