LEGO WeDO with Linux

After almost 2 years of procrastination I finally offered myself a LEGO WeDO USB Hub:

I just love the WeDO idea: with just a small and somewhat simple interface (the USB Hub) anyone can use a PC (nowadays a ubiquitous appliance) to control a motor or read a sensor. Great for educational uses, specially with STEM (Science, Technology, Engineering and Mathematics).

Sadly the LEGO WeDO family got much less attention than his older and bigger related, LEGO Mindstorms (at least in my country, Portugal). Although a bit less expensive than an NXT or an EV3, it’s not cheap so the EV3 seems a better investment (more pieces, more diversity, more power).

Early this year I used a Raspberry Pi with a Piface Digital board as a clumsy but much more powerful DIY alternative to the WeDO. This was in fact the trigger to start using LEGO again, event registering myself in a LUG.

This week someone opened an issue with ev3dev asking for “Any chance of adding Lego WeDo hub + motor and sensors?“. Right after I suggested wedo (a python library for WeDO) Ralph Hempel announced he was working in linux kernel support for WeDO (not just for ev3dev, could be used with other systems… like my laptop). Wow!

So I got a pair of WeDO USB hubs and a tilt sensor. And while waiting for Ralph’s work, I’m using WeDo with a variant of the wedo library: WeDoMore.

This is what you need to get WeDoMore working with Linux (both Ubuntu 14.10 and ev3dev tested, no need to use sudo in ev3dev because I’m already root): first download it and extract it, then:

sudo apt-get install python-pip
sudo pip install pyusb
cd WeDoMore-master
sudo ./setup.py install

With Ubuntu it was necessary to remove Ubuntu’s version of pyusb, much older than pypi version:

sudo apt-get remove pyusb

Now connect the WeDO USB Hub and check with ‘dmesg’ and ‘lsusb’.

On Ubuntu:

413.597110] usb 2-1.3: new low-speed USB device number 10 using ehci-pci
[  413.695628] usb 2-1.3: New USB device found, idVendor=0694, idProduct=0003
[  413.695639] usb 2-1.3: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[  413.695644] usb 2-1.3: Product: LEGO USB Hub V1.00
[  413.700722] hid-generic 0003:0694:0003.0009: hiddev0,hidraw3: USB HID v1.10 Device [LEGO USB Hub V1.00] on usb-0000:00:1d.0-1.3/input0
Bus 002 Device 013: ID 0694:0003 Lego Group

On ev3dev:

[ 1263.539342] usb 1-1.2: new low-speed USB device number 4 using ohci
[ 1263.698363] hid-generic 0003:0694:0003.0001: device has no listeners, quitting
Bus 001 Device 004: ID 0694:0003 Lego Group

So Ubuntu has a better knowledge of the WeDO USB Hub than ev3dev, don’t know why.

Now let’s test it from the python shell:

~$ python
Python 2.7.8 (default, Oct 20 2014, 15:05:19)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from wedo import WeDo
>>> wd = WeDo()
>>> wd.motor_a=100

If you have a motor connected to port A, it shell spin (note that as WeDO gets it power from USB the motor will get only 5V instead of ~9V as with Power Functions batteries… so it will spin slower).

To stop the motor and leave the shell:

>>> wd.motor_a=0
>>> exit()

As I also have a tilt sensor I made this simple control script:

#!/usr/bin/env python

from wedo import WeDo
from time import sleep

wd = WeDo()
print("Tilt me!")
while(True):
 if(wd.tilt==2): 
   wd.motor_a=35
 elif(wd.tilt==3):
   wd.motor_a=-35
 else:
   wd.motor_a=0

3 comentários em “LEGO WeDO with Linux”

  1. Olá Jorge!

    Muito obrigado mais uma vez. Este teu blog está fantástico.

    No meu caso, só consegui instalar on pyusb assim:

    sudo pip install pyusb==1.0.0b2

    E o acesso ao WeDo hub só se dá se for usando a conta de ‘root’.

    Agora, fico intrigado no porquê de o connector do motor permitir conexão doutro sensor/motor, mas fazendo alguns testes, se ponho um sensor, não funciona, se ponho outro motor, funciona em sincronismo com o primeiro. Será que é só para permitir adição doutros motores?

    O próximo item de minha lista é tentar controlar o brick NXT a partir da porta usb do brick EV3 usando ev3dev (semelhante a forma como se dá com o WeDo), tudo usando-se python é claro. Estou ciente do Monobrick (vide https://github.com/ev3dev/ev3dev/issues/92), mas preciso estudar melhor este caso.

    Acabei de encomendar um hub usb para os próximos experimentos.

    1. «o connector do motor permitir conexão doutro sensor/motor, mas fazendo alguns testes, se ponho um sensor, não funciona, se ponho outro motor, funciona em sincronismo com o primeiro. Será que é só para permitir adição doutros motores?»
      Sim, o conector Power Functions permite ligar vários dispositivos em paralelo/sincronizados, de modo que se podem controlar vários motores e luzes em simultâneo (atenção ao consumo total, uma porta USB num laptop deverá estar limitada a 500 mA, poderá ser necessário usar um HUB USB com alimentação externa).
      Yes, the Power Functions plug allow several devices to be controlled in parallel so we can use several motors or lights simultaneous (watch for total consumption, USB on a laptop is probably limited to just 500 mA, you might need a USB Hub with external power).

  2. Excellent tutorial. The best walkthrough I have seen so far. My project is for a remote pan and tilt Pi camera (picamera) controlled with a wiimote via Bluetooth (cwiid) and moved by WeDo motors. A remote on/off/power management switch (IR controlled) (msldigital.com) occupies the GPIO pins so they are not available to control motors.
    It works. Thank you.
    Happy Christmas.

Responder a Alan Cancelar resposta

O seu endereço de email não será publicado. Campos obrigatórios marcados com *