4

I'm trying to talk to the WeDo USB interface from Python or really any real programming language.

I tried the clever WeDoMore library, but can't get it to bind the WeDo. I think this is because the WeDo is a HID device and the built in Mac OS HID driver binds to it before libusb, a dependency of WeDoMore, get's a chance to. But, my understanding is a little shaky.

The developers of WeDoMore are quite helpful, but don't use Macs themselves.

Any hints that would get me talking to the WeDo would be greatly appreciated. Scratch works, but I'm looking for a more standard programming environment.

3 Answers 3

1

Here's what I found researching your issue:

  1. It seems that the issue with Mac OS X's kernel claiming HID USB devices before libusb does is a known issue since OSX 10.5 (see here).
  2. Libusb has no functionality to alter this behavior, so it would be up to the WeDoMore developers to use an alternative way to utilize USB on the Mac if they want to build in this compatability. From the post in my bullet #1 it sounds like utilizing HIDAPI is a good way to do this.
  3. There might be a way to blacklist the WeDo USB interface under Mac OS X, but since I am not a Mac owner nor programmer, I cannot help you with the feasibility of this option. Details here. It seems to require the development of a 'KEXT' which requires an Apple Developer license to do.
1
  • I'm marking this as the "accepted" answer 'cause I don't think there's a better one available.
    – cbare
    May 6, 2015 at 23:08
3

I just created a Node.js library that supports reading and writing to the Lego WeDo kit: https://www.npmjs.com/package/wedo-support

Install

> npm install --save wedo-support

Use

const WeDo = require('wedo-support');
const wd = new WeDo.WeDo();

// Activating the first motor full forward:
wd.motorA = 100;

// Activating the second motor half speed/force backward:
wd.motorB = -50;

// Current value of the tilt sensor:
const theTilt = wd.tilt;

// Current distance value in meters of the distance sensor:
const theDistance = wd.distance;

This library uses the https://www.npmjs.com/package/node-hid library for USB support, which works on Mac OS X.

I'm on El Capitan and it works well. I haven't tested on Windows or Linux, so if someone wants to try it out and provide feedback / submit a pull request, I'd be happy to accept them.

I also determined that the motorA and motorB work with lights as well.

Happy WeDo-ing!

3

It is possible to drive the WeDo from Python on a Mac, without a kext. I've only tested motors since I don't (yet) have the other sensors. You need to access the USB HID device directly. There are a couple of prerequisites to install:

You'll likely want XCode and friends installed to be on the safe side.

There are a number of forks of cython-hidapi available so your mileage may vary depending on which version you use. I don't remember installation being problematic.

Once those are installed and your WeDo is plugged in you can do something, at a minimum, like:

import hid
import time

# vendor id, product id: 
dev = hid.device(1684, 3)

# turn both motors on
# HID format is reportID, message type, data1, data2, ...
dev.write([0x0, 0x40, 127, 127, 0x0, 0x0, 0x0, 0x0, 0x0])

time.sleep(1)

# turn them both off
dev.write([0x0, 0x40, 0, 0, 0x0, 0x0, 0x0, 0x0, 0x0])

The changes to get WeDoMore to use hidapi instead of generic usb on a Mac don't look to be too onerous. If I manage to get something committed I'll update my answer with a link.

[Edit: I've committed a fork of WeDoMore that adds support for motors on the Mac here. Again, I'll update if the pull request gets accepted]

Small side-note: The WeDo can also drive 9v motors with the correct converter cable. I think it's possible to read the RCX sensors as well, but mild soldering may be required (see, e.g. here for a proof of concept). But if the WeDo can be fooled into reading the devices then Python can be used with those other sensors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.