Sunday, 3 January 2016

Mindstorming with Python

The motors that come with the Lego Mindstorms EV3 comes with motors with positional feedback and reasonably sophisticated controls. The 'brick' that interfaces with all the widgets can also easily be hacked to enable use of sensible programming languages rather than the visual language (which has some significant limitations) that Lego have developed.

The ev3dev update is especially good as it can easily be removed to revert back to the original state (it is installed on an SD card and no changes are made to the controller).

So here is a quick blog of what I have done to get up and riunning with python on the EV3. I did fall down a couple of bear pits along the way, so this describes how to get to a working solution.


Lego EV3 brick
probably from the standard kit.
Any old microSD card
Must NOT be microSDXC. I used an 8Gb card that I originally bought with a Raspberry Pi 2.
a PC which can access the microSD card
I used my laptop with a microSD to SD adapter. These days many SD cards are actually a microSD card with an adapter thrown in, and another SD card is always useful.
A USB cable to connect the brick to the laptop
the lego kit comes with one, but any old USB - mini USB cable will do.
my laptop runs Ubuntu (and windoze), but I'm using Ubuntu for this as it is easier to hook up to the brick once it is built, but there are plenty of instructions on how to do this with WIndoze if you really must.

The base build

  1.  Use the instructions on the ev3dev site to build the micro SD card. I used ev3-ev3dev-jessie-2015-12-30
  2. Setup networking on the brick so you can ssh onto the brick - the above instructions also explain how to do this. It is best to setup proper networking so that the brick has internet access to allow easy installation of further packages. I just used networking over USB to get going. Note this setup is Ubuntu specific, I have no idea if / how this could be done on Windoze.
  3. The menus on the brick were slightly different to those on the USB internet connection: Wireless and Networks - All Network Connections - Wired. Settings are as described in the USB internet connection link.
  4. The end of the USB internet connection shows how to start an ssh session to the brick.
  5. You should now be able to access and control sensors and motors from the command line, various examples are linked from the Using the EV3 Hardware Drivers section.

Getting Python running

This page on the ev3 web site links to the various 'official' language bindings as well as several unofficial ones. I tried a couple of the unofficial ones I had found through other routes, and fell foul of incompatible versions of the various packages, so I recommend you stick to the official ones.

Also the documentation and setup seems to be open to confusion as there is some very active discussion going on around this as of 3/1/16! So here is how I did it - it's all very simple once you have everything else setup correctly!

The best documentation of the interface I have found is here:
  1. setup the python ev3 bindings, fetching any other required packages:
    easy_install -U python-ev3dev
Now based on one of the demo programs I did a very simple test to see if it works!
from import *
Don't just import e3dev, as has been noted elsewhere this is (currently at least) a bad idea!
motors = [LargeMotor(address) for address in (OUTPUT_A, OUTPUT_D)]
setup whatever motors are currently connected in a list (change addresses as appropriate)
Check a motor is connected - should return 'true'
motors[0].duty_cycle_sp = 66
set the duty cycle non-zero so something will happen.
runs the motor while watching the duty cycle setting
motors[0].duty_cycle_sp = 15
adjust the duty cycle on the fly and see how low a value you can use with the motor still turning. There will be very little power though. One of my large motors still turns at 4, the other needs 5.
Now to try getting the motors synch'ed so my little cart runs straight...

No comments:

Post a Comment