Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Tuesday, 16 September 2014

We do WEDO!

Unless you're a primary school teacher you've probably never heard of the Lego Wedo. We hadn't either, which is a shame because they're actually really cool. Everyone loves Lego, but as you've  probably noted in the last few posts, Lego EV3 is expensive and quite hard work to get started with. I know of one local high school where the design and technology team went with regular Lego Technic's Power Functions motors, as the PF components are about half the price of the equivalent EV3 components. They can build machines, and power them much more cheaply than an EV3 setup.

The downside is of course there are no PF sensors, and you can't hook them up to a computer.




Wow!! Every PF forum has people asking how do I make "smart" power functions, and the replies are all about hacking cables, when Lego sell a set to to exactly that

There it is Lego 9580. Look at those bricks at the front... They're Power Functions, but not as we know them.  There's a PF "M" motor, but on the right is the WeDo USB hub... That's right - it plugs into any computer, and replaces the PF battery box, so you can drive motors from the computer. But look what else - that's a tilt sensor, and an IR range sensor!!! All PF compatable.

We why have't we heard of it? Well as the ad's used to say "remember, Lego Wedo isn't available in the shops!". It's Lego education, which means its expensive and hard to find, but its out there (and its on Amazon!). Expect to pay around £120 for the set. Alternativly if you shop around, you might find the components available separately - the HUB costs about £35, while the sensors are £15-£20. £70 for the "cool parts" isn't cheap, but its cheaper than the equivalent EV3 kit.

The Wedo is targeted at primary school children, with some simple to use software (not included and another £70!) that lets you build, and control mini-machines. While its expensive, a lot of work has gone into supporting lesson plans and kids will go crazy for it, so its probably good value (ironically my daughter came home from school the same day my Wedo arrived, raving about how they were doing a new exciting project at school - they were using the Wedo!).

But given how obviously useful, and in demand the functionality of Wedo is in secondary schools, and among "older" Lego users, why isn't it a standard PF brick that's in every toy store? I guess the answer is Software - Lego have got software for KS2 children, built and ready to go. To sell Wedo to a wider audience it requires development and support of more general tools, which simply don't exist yet...

The good news is that the Wedo works in Scratch, so you can introduce kids to the Wedo there, and get them building. When they move to Sniff, its probably a good idea to revisit some of their Wedo projects again, and rework them in Sniff.

To get started you'll need to hook your Wedo up to a Pi, or generic Linux machine [Update: It works great on Windows and Mac now too, though wedoScan is Linux only]. Then run the Sniff setup file, and you should be able to type "wedoScan" to find and interrogate your Wedo.

Found Wedo on HIDraw 2
plug 1: Motor
plug 2: Tilt

It should report something like the above. It also turns the motors off which can be handy! If it doesn't report the connections correctly, run it again after a couple of seconds - the hub can report incorrectly if the motors are/have been running, and a recscan usually fixes it.

Now we know its all hooked up we can program it:

make wedo device
make wedoConnector number
make wedoValue number

Start by making a wedo device. If you like you can use the HIDraw number that wedoScan found (usefull if you have several hubs!), or simply let the device find the hub for itself.

when start
..set wedoConnector to 2
..tell wedo to "readTilt"
..say [wedoValue]

To get things going we specify the connector we're interested in, and in this case (as it has a Tilt sensor attached, we call "readTilt". The values returned in wedoValue are:
#0 Flat
#1 Forward (away from wire!)
#2 Back
#3 Left
#4 Right

Having read the Tilt sensor, we can use that to control the motor speed:

..if wedoValue=1
...change speed by 0.1
..if wedoValue=2
...change speed by -0.1
..if speed >1
...set speed to 1
..if speed <-1
...set speed to -1
..set wedoConnector to 1
..set wedoValue to speed
..tell wedo to "setMotorSpeed"

We adjust the speed variable, and then select the connector, and value we want to send. For a motor wedoValue should be between -1 and +1, with 0 being stopped.

There's also a readDistance command, which tries to return a value in metres, though that might be a bit inaccurate. Try running:
when start
..set wedoConnector to 2
..tell wedo to "readDistance"
..say [wedoValue]

and pick values appropriately.

And that's it! Lego Wedo control. You could also hook up the PF lights, and Servo (just use the motor commands) and build something really cool. If you're luck enough to have more than one Wedo then you should be able to hook up as many as you like (up to about 90 or so!). Just make a device for each, and give it a name:

make leftHub wedo device 2
make rightHub wedo device 2

Now for the really exciting bit... This works on Linux...The Lego EV3 runs linux... In principle it should be possible to run this on the EV3, and connect the EV3 to Power Functions using official lego blocks!!!! This is something of a holy grail in the EV3 forums. However there's a catch - the standard EV3 linux distro is quite old, so you'll need to update it to something that supports "HIDraw" (a way of accessing USB devices). Lego may provide this in a future update, but if you're prepared to go a little "of grid", you could try installing the ev3dev, or LeJos firmware, and try it!

The code in this post is in the Sniff/examples/Hosted/wedo folder. You can also find the source for wedoScan (and ev3dude) in Sniff/utils.

No comments:

Post a Comment