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)