So I’ve been diving head first into LEGO’s newest Mindstorms generation, called EV3. But seeing as I’m a Linux geek and a programmer, using LEGO’s own graphical drag-and-drop programming language from their windows/mac software was completely out of the question! Now, the EV3 runs Linux out of the box, which is very cool, but it comes with some limitations, so to get going with a proper programming language, installing another Linux version was necessary. Luckily, the EV3 makes that so much simpler than the NXT did. Just pop a bootable OS on a micro SD card, stuff in the EV3, and voila, it will boot from the SD card! Pop the card back out, and the EV3 still has it’s factory firmware. No flashing needed! Neat!
So what programming language to use? Well, turns out some guy who calls himself Topikachu has already made a ready Linux image with Python installed, and even made Python libraries to interface with the EV3 sensors and motors. Does it get any better than that? He’s made a quick howto on how to install it, which you can read on his github. If you have a USB Wifi dongle too (currently only one is supported, but I’m sure more will be added in time), it was very easy to get it hooked up on wifi too. I just had to log in over usb as described, update the wpa_supplicant.conf and I was good to go without a cable. You may want to check out what IP your brick received before unplugging the usb cable though 😉
The Python API is really great, but I couldn’t find much documentation of it, so I had to read his code and look at his tests to figure out how to use it. Wasn’t too hard though, and here’s a little program I wrote just to test out that things were working (it doesn’t do much, basically just tries to crash :P)
#!/usr/bin/python import time from ev3.rawdevice import motordevice from ev3.rawdevice import analogdevice from ev3.rawdevice import uartdevice from ev3 import lego motordevice.open_device() analogdevice.open_device() uartdevice.open_device() A = 0x01 B = 0x02 C = 0x04 D = 0x08 right = A left = D both = A+D touch = lego.EV3TouchSensor(0) ir = lego.EV3IRSensor(3) ir.set_prox_mode() motordevice.speed(both,20) distance = 101 while True: time.sleep(1) if touch.is_pressed() == 1: motordevice.stop(both, brake=1) print "stopping\n" break cur_distance = ir.get_distance() if cur_distance > distance: print "searching\n" motordevice.stop(both, brake=1) motordevice.speed(A, 20) time.sleep(1) motordevice.stop(A, brake=1) motordevice.speed(both,20) distance = cur_distance motordevice.stop(both, brake=1)
That doesn’t look so hard, does it? Here’s some additional notes I made for myself to remember what the different functions do and how to use them.
Motors: A = 0x01 B = 0x02 C = 0x04 D = 0x08 PORTS = A+B # Access both A and B at the same time from ev3.rawdevice import motordevice motordevice.open_device() motordevice.speed(PORTS,20) motordevice.stop(B,brake=1) # brake makes it actually brake the motor, not just stop turning it from ev3.rawdevice import analogdevice analogdevice.open_device() touch = lego.EV3TouchSensor(0) # Note, the port numbering is 0-3, so the port marked 1 is 0, etc. touch.is_pressed() from ev3.rawdevice import uartdevice uartdevice.open_device() color = lego.EV3ColorSensor(3) color.set_color_mode() color.color_to_string() # This only works for color_mode color.set_ref_raw_mode() # raw values, -127 -> 127 color.get_value() # raw value from sensor sensor ir = lego.EV3IRSensor(2) ir.set_prox_mode() # proximity mode ir.get_distance() # for prox mode ir.set_remote_mode() # for using the remote control ir.get_remote_command() # get remote control command ir.set_seek_mode() # follow remote control when button pressed ir.get_all_direction_and_distance # for seek mode ir.get_direction_and_distance(chan) # for seek mode # This is how I managed to read info from a Hitechnic accelerometer sensor (originally made for the NXT) from ev3 import robot from ev3 import sensor robot.open_all_devices() iicsensor = IICSensor() iicsensor = sensor.IICSensor(1, 0x02) iicsensor.read(0x42)
Pingback: Lego Mindstorms EV3 mit python | Bastian Kuhn
Thanks for sharing this interesting topic. You should consider contribute up to the python-ev3 project this few lines of code, after a small refactoring. Actually the python-ev3 supplies only one (buggy) example, which is not enough to get familiar with the API. BTW, you write “only one Wifi dongle is currently supported”. Which one is it ?
Yes, I struggled a bit with it at first, I had to read through the code and the unit tests to figure out the basics on how to use the python libs. I’ll consider cleaning up my code and making a pull request.
The supported wifi dongle is explained here: http://www.technicbricks.com/2013/09/wifi-dongle-for-ev3.html
Basically it’s a Netgear WNA1100. That’s what the stock firmware supports anyway. I’m not sure about Lejos, or whatever modifications Topikachu has done to the kernel he’s using (as he doesn’t write anything about it). I only tried the Netgear as that’s what I had bought along with my EV3, and it worked out of the box with the python-ev3 kernel.
Pingback: Lego Mindstorms EV3 mit python | IT Blog
Any tips regarding the wifi dongle ?
I have the 8192cu and it is working in Lejos (java) but when I do ifconfig on the Python-ev3 there’s no interface found.
Sorry, I don’t know. I’ve only tried with the one LEGO says works with the EV3, and that worked out of the box for me. You’re probably better off asking the guy who made the Python-ev3.
Stas, have you got 8192cu working in your side? I am going to give a try for this small wifi dongle. Would be good to know your progress if any.
Stas,
I figured out how to add 8192cu support on this image:
You can get the driver from here: http://www.electrictea.co.uk/rpi/8192cu.tar.gz
install the driver:
cp 8192cu.ko /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/
load driver on system startup:
echo 8192cu >> /etc/modules
depmod -a
load driver:
insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/cp 8192cu.ko
ctrl_interface=/var/run/wpa_supplicant
network={
ssid=”your_wlan_ssid”
key_mgmt=WPA-PSK
psk=”your_wlan_presharedkey”
}
auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet dhcp
hwaddress ether xx:xx:xx:xx:xx:xx #hardware address ev3
auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant.conf
reboot or /etc/init.d/networking restart
it should get an ip address now, if not check your settings and the /var/log/syslog
good luck!
Alan
sorry for the typo 😉
load driver:
insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/8192cu.ko
hm the html layout messed up my code
vi /etc/wpa_supplicant.conf
ctrl_interface=/…
…your_wlan_presharedkey”
}
vi /etc/network/interfaces
auto lo…
…wpa-conf /etc/wpa_supplicant.conf
Great post, it’ll help me a lot – many thanks!
Quick question: how long did your first boot take? (On the GitHub page it says “It takes a long time on first boot”, but it’s been hours now, so I worry it’s just stuck.)
It does not take hours. It may take like a minute or two. But you’ll never get anything in the display, so it might have booted fine but you just don’t know it. Did you try to connect to it? I think the only thing that happened was a light came on or off or changed color or something, I don’t remember, it’s been some time since I played with it now. If you can’t get it to work, try to reflash the memory card, in case it got a corruption the first time.
The display kept showing that initial “mindstorms – starting…” message. It got stuck like that for over 10 hours. I was about to give up (I just cloned the ev3sources source code and was experimenting with sending C++ code via Bluetooth), but I’ll try reflashing the card, as you suggested.
Thanks for the help, Cecilie!
Prebuilt image is not working for me as well, I’ve built it myself as described in here https://github.com/hmml/python-ev3/tree/refactor/build-rootfs
It boots now, but another problem arised: motors are not working
And I’ve used topikachu’s branch rather than hmml’s.
We merged Python 2.7 into the leJOS sd card image and use a TCP/IP gateway between Python and leJOS. For more information consult http://www.aplu.ch
can we use it for mac
So you said you “read his code” because there was no documentation. Where are you finding all the code? Are you referring to the code in topikachu/python-ev3/ev3 directory? where do you find all the code on the EV3?
Thanks?