IEEE Spectrum November, 2016 - 22
needed was integration, configuration, and
some glue software.
First, I needed to build an infrared control link, which is basically two infrared
LEDs operated via the Raspberry Pi's general purpose input/output (GPIO) connector and Lego's receiver. I used schematics
and instructions by Alex Bain to build the
hardware. For the software, I downloaded
and installed LIRC, a package that has support for decoding and transmitting signals
used by over 2,500 different infrared remote controls.
Getting the LIRC package to work with
my home-brew infrared link was a simple
matter of editing some configuration files
and specifying which GPIO pins I had wired
up for input and output.
Now I needed to get LIRC to send valid
Lego command signals. This means specifying the waveform-a pattern of infrared
pulses-that must be sent for each Lego
command. Fortunately, Lego has released a
document specifying the protocol and format of all commands (for example, a binary
value of 1 is transmitted by six pulses of IR
light at a frequency of 38 kilohertz, followed
by a pause of 553 microseconds). The Lego
Power Functions system supports up to
four receivers working on different channels, and each receiver has a red side and a
blue side, each of which can independently
control a motor.
Building on this information, Conor Cary
created lego-lirc, a Java program that generates command waveforms, complete
with the correct checksums, in a format that
LIRC understands. I downloaded lego-lirc
and, with the Lego documentation in hand,
created additional waveforms that allow
the transmission of PWM (pulse-width
modulation) commands. These commands
allow precise speed adjustment of Power
Function motors without requiring timing
loops in the application software. (To avoid
the hassle of running lego-lirc, you can just
download my file of generated LIRC waveforms directly from my GitHub repository
under the username of dspinellis.) To configure LIRC to use the Lego commands, I
copied the waveform to the LIRC configuration directory. I could then send Lego com22
|
NOV 2016
|
NORTh AMERICAN
|
BloCK BY BloCK
lego power Functions allow motors
to be controlled with infrared signals
[top]. signals can be generated by
connecting infrared leDs to a pi
[middle images]. A lego enclosure
holds the components [bottom].
SPECTRUM.IEEE.ORG
mands from the Pi's command line through
LIRC's irsend program.
The final step was to issue the LIRC commands from the Scratch environment. I
enabled "remote sensor connections" in
Scratch. This makes Scratch behave like a
local server running on the TCP port 42001.
Client software can connect to Scratch using this port and listen for messages from
Scratch programs. (It's also possible to
have the client software and Scratch environment run on separate machines, so you
could have the Raspberry Pi-based infrared interface controlled by a Scratch program running on a desktop computer, for
example.) I then installed Phillip Quiza's excellent scratchpy library, which allows you
to write Scratch clients in the Python programming language.
Finally, I wrote a Python script that receives Scratch broadcast messages specifying Lego remote commands, and runs
the LIRC command-line client to send
them (this is also available from my legopower-scratch GitHub repository). To run
the script, run the control.py program in a
separate terminal window and launch the
Scratch environment. While control.py is
running, it will display on its standard output the remote control messages it sends
or the errors it detects on the incoming
Scratch messages.
In Scratch, programs are constructed
by chaining together graphical blocks on
screen. Blocks perform functions such as
program-flow control and graphics manipulation. To send a message to a Lego
Power Functions receiver, a "broadcast"
block is used, with a simple text string of
the form "Lego
." So, for example, the message "Lego 2 blue -7" will send a signal by
way of the Python client and my transmitter to turn the motor connected to the blue
side of the receiver on channel 2 at full
speed, backward.
How does the system work in practice
with its intended audience? I tried it out with
a young budding engineer-who quickly
wrote a Scratch program to control Lego's
Volvo Wheel Loader kit with a computer's
arrow keys. -DiomiDis spinellis
DiomiDis spinellis (4)
RESOURCES_hANDS ON
http://www.control.py
http://www.control.py
http://SPECTRUM.IEEE.ORG
Table of Contents for the Digital Edition of IEEE Spectrum November, 2016
IEEE Spectrum November, 2016 - Cover1
IEEE Spectrum November, 2016 - Cover2
IEEE Spectrum November, 2016 - 1
IEEE Spectrum November, 2016 - 2
IEEE Spectrum November, 2016 - 3
IEEE Spectrum November, 2016 - 4
IEEE Spectrum November, 2016 - 5
IEEE Spectrum November, 2016 - 6
IEEE Spectrum November, 2016 - 7
IEEE Spectrum November, 2016 - 8
IEEE Spectrum November, 2016 - 9
IEEE Spectrum November, 2016 - 10
IEEE Spectrum November, 2016 - 11
IEEE Spectrum November, 2016 - 12
IEEE Spectrum November, 2016 - 13
IEEE Spectrum November, 2016 - 14
IEEE Spectrum November, 2016 - 15
IEEE Spectrum November, 2016 - 16
IEEE Spectrum November, 2016 - 17
IEEE Spectrum November, 2016 - 18
IEEE Spectrum November, 2016 - 19
IEEE Spectrum November, 2016 - 20
IEEE Spectrum November, 2016 - 21
IEEE Spectrum November, 2016 - 22
IEEE Spectrum November, 2016 - 23
IEEE Spectrum November, 2016 - 24
IEEE Spectrum November, 2016 - 25
IEEE Spectrum November, 2016 - 26
IEEE Spectrum November, 2016 - 27
IEEE Spectrum November, 2016 - 28
IEEE Spectrum November, 2016 - 29
IEEE Spectrum November, 2016 - 30
IEEE Spectrum November, 2016 - 31
IEEE Spectrum November, 2016 - 32
IEEE Spectrum November, 2016 - 33
IEEE Spectrum November, 2016 - 34
IEEE Spectrum November, 2016 - 35
IEEE Spectrum November, 2016 - 36
IEEE Spectrum November, 2016 - 37
IEEE Spectrum November, 2016 - 38
IEEE Spectrum November, 2016 - 39
IEEE Spectrum November, 2016 - 40
IEEE Spectrum November, 2016 - 41
IEEE Spectrum November, 2016 - 42
IEEE Spectrum November, 2016 - 43
IEEE Spectrum November, 2016 - 44
IEEE Spectrum November, 2016 - 45
IEEE Spectrum November, 2016 - 46
IEEE Spectrum November, 2016 - 47
IEEE Spectrum November, 2016 - 48
IEEE Spectrum November, 2016 - 49
IEEE Spectrum November, 2016 - 50
IEEE Spectrum November, 2016 - 51
IEEE Spectrum November, 2016 - 52
IEEE Spectrum November, 2016 - 53
IEEE Spectrum November, 2016 - 54
IEEE Spectrum November, 2016 - 55
IEEE Spectrum November, 2016 - 56
IEEE Spectrum November, 2016 - 57
IEEE Spectrum November, 2016 - 58
IEEE Spectrum November, 2016 - 59
IEEE Spectrum November, 2016 - 60
IEEE Spectrum November, 2016 - 61
IEEE Spectrum November, 2016 - 62
IEEE Spectrum November, 2016 - 63
IEEE Spectrum November, 2016 - 64
IEEE Spectrum November, 2016 - 65
IEEE Spectrum November, 2016 - 66
IEEE Spectrum November, 2016 - 67
IEEE Spectrum November, 2016 - 68
IEEE Spectrum November, 2016 - 69
IEEE Spectrum November, 2016 - 70
IEEE Spectrum November, 2016 - 71
IEEE Spectrum November, 2016 - 72
IEEE Spectrum November, 2016 - 73
IEEE Spectrum November, 2016 - 74
IEEE Spectrum November, 2016 - 75
IEEE Spectrum November, 2016 - 76
IEEE Spectrum November, 2016 - Cover3
IEEE Spectrum November, 2016 - Cover4
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1217
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1117
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1017
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0917
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0817
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0717
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0617
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0517
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0417
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0317
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0217
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0117
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1216
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1116
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1016
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0916
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0816
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0716
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0616
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0516
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0416
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0316
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0216
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0116
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1215
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1115
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1015
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0915
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0815
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0715
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0615
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0515
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0415
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0315
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0215
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0115
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1214
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1114
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1014
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0914
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0814
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0714
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0614
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0514
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0414
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0314
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0214
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0114
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1213
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1113
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1013
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0913
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0813
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0713
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0613
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0513
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0413
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0313
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0213
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0113
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1212
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1112
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1012
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0912
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0812
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0712
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0612
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0512
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0412
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0312
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0212
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0112
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1211
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1111
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1011
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0911
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0811
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0711
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0611
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0511
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0411
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0311
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0211
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0111
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1210
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1110
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1010
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0910
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0810
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0710
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0610
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0510
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0410
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0310
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0210
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0110
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1209
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1109
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1009
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0909
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0809
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0709
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0609
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0509
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0409
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0309
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0209
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0109
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1208
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1108
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1008
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0908
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0808
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0708
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0608
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0508
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0408
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0308
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0208
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0108
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1207
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1107
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_1007
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0907
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0807
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0707
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0607
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0507
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0407
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0307
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0207
https://www.nxtbook.com/nxtbooks/ieee/spectrum_na_0107
https://www.nxtbookmedia.com