Scaling API-first – The story of a global engineering organization
A survey of robotics in Ruby
1. A Survey of Robotics
in Ruby
Josh Adams | @knewter
Adam Dill | @adamdill
Saturday, June 15, 13
A
This is Josh Adam from Isotope 11
I am Adam DIll from Inside.com
And how can it not be fun, were talking about robots!
2. This talk has two parts
• Survey / knowledge dump
• Project narrative
Saturday, June 15, 13
J:
This talk has a couple of parts. The first one is really just a braindump - lots of good information on the library landscape.
After that, we’ll dig into building a project together
3. Part the first
A Survey of Robotics with Ruby
Saturday, June 15, 13
A
So lets dive into the survey so you guys can see what is out there
4. Robots?
• A machine capable of carrying out a complex series of actions
automatically.
• (esp. in science fiction) A machine resembling a human being
and able to replicate certain human movements and
functions.
• Robotics: The branch of technology that deals with the
design, construction, operation, and application of robots.
Saturday, June 15, 13
A:
What is a robot?
a robot is a force multiplier, it lets you take a simple action and cause a cascade of more complex decisions and actions
JOSH
5. Robots?
• A machine capable of carrying out a complex series of actions
automatically.
• (esp. in science fiction) A machine resembling a human being
and able to replicate certain human movements and
functions.
• Robotics: The branch of technology that deals with the
design, construction, operation, and application of robots.
LAM
ESaturday, June 15, 13
7. State of Hobby Robotics
• Growing rapidly
• lots of good mags
• My 3 favorites =>
• Costs have fallen
• Getting really approachable
Saturday, June 15, 13
A
mass production of sensors for phones has made this affordable
while the maker movement has made this approachable approachable
great references are available, make first among them
8. State of Hobby Robotics
with Ruby
• http://artoo.io/ - Sinatra for Robots
• https://github.com/hparra/ruby-serialport/
• http://www.rock-robotics.org - heavily c based, wired
together with Ruby
• right now, Python is better, and certainly more active, and
more mature
• long term, Ruby is a great choice
Saturday, June 15, 13
A
Artoo is like sinatra for Robotics. It’s definitely leading the pack right now.
Serialport is an easy way to use serial devices, but there are some baud rate issues.
Rock robotics is mostly C-based, but wired together with Ruby
Python’s presently outpacing us in robots, but Ruby is the future.
J
Why is Ruby such a good choice, Adam?
9. Why is Ruby a great choice?
• All the normal reasons Ruby is great
• DSLs
• Metaprogramming
• Developer happiness
Saturday, June 15, 13
A
All the normal reasons:
DSLs, Metaprogramming, and you can have fun *writing* your robots, not just playing with them.
10. Artoo.io Example
Saturday, June 15, 13
J
So we aren’t going to dig into artoo too much, but it’s definitely worth checking out.
Artoo’s awesome and provides a common interface for various
devices. It’s really made for controlling toys, which is fun and
an easy way to start.
It also gives you an easy way to roll a web API interface for your system.
(It also uses Celluloid, which is easily my favorite Ruby library)
11. Where to start?
• Get easy wins early:
• Sphero - about $130
• Parrot ARDrone - about $150 refurb
Saturday, June 15, 13
A
Don’t start with a giant project
It’s very easy to get bogged down in a small hardware problem (resistor values, etc)
J
When should I learn Ohm’s Law, etc?
A
Right. So make sure to get some easy wins up front to help keep yourself motivated.
I suggest you buy either a sphero or a Parrot ARDrone and just play with it.
12. Sphero
• http://www.gosphero.com/
• Bluetooth enabled ball
• drives itself around
• best dog toy ever
• waterproof
Saturday, June 15, 13
A
This is a sphero. It’s basically just a ball with a motor inside of it.
It’s bluetooth-controlled (via serialport), it’s a lot of fun and really simple.
These are great for someone just learning to program to see real-world results of the code they’re typing, so it’d be good for
teaching kids and keeping them engaged.
There are some great KidsRuby Artoo resources for the Sphero
13. Sphero with Ruby
Saturday, June 15, 13
A
Cool Sphero gems, and artoo supports it.
You can see we’re just giving it a speed and a direction. It doesn’t get much simpler than this.
(DEMO)
14. Saturday, June 15, 13
A
Here’s an example of using Artoo to control an armada (fleet?) of spheros
What’s cool here is you can see how multitasking works with this evented architecture.
15. Parrot ARDrone
• http://ardrone2.parrot.com/
• Acts as its own wifi AP
• Connect to it over wifi, it’s
just a device with an IP
• Send UDP packets to it
• Stream video from 2
cameras
Saturday, June 15, 13
J
So the other easy toy is the Parrot ARDrone. This guy creates his own WiFi access point that you connect to, then you can just
send him UDP packets to control him. He’s also got a couple of video cameras.
Like we said, it’s $150 for a refurb and you can get a new one for around $250
16. Parrot ARDrone
Saturday, June 15, 13
J
Jim Weirich wrote the `argus` gem to control it via Ruby
Artoo supports it
(DEMO)
17. Rolling your own
• What if you don’t want a ball or a quadcopter? What if you
want to be master of your own fate?
Saturday, June 15, 13
J
So now you’ve got your easy wins and you’re still interested. What’s next?
18. Microcontrollers
A microcontroller is a small computer on
a single IC containing a processor,
memory, and programmable I/O. -
wikipedia
Saturday, June 15, 13
A
Microcontrollers are essentially tiny computers. The real keys are they are on a single board, and have I/O.
19. Microcontrollers!!!!!11!!!
wtfbbqhax
Saturday, June 15, 13
J
The arduino showed up right around the same time Make magazine did, and they had a huge impact on hobbyists (though people
that were doing microcontrollers before that will hate me for saying that). Still, it’s only a 16MHz device with virtually no storage,
no networking, and it had to be programmed in C. Now we have a whole range of options, from the Beaglebone Black to the
PCDuino, which offers a 1GHz chip, 1GB of RAM, a Mali 400 GPU, and an Arduino-compatible pinout (so you can take advantage
of existing shields). Those are both far more expensive and power hungry than the almighty Raspberry Pi, though. With a RasPi,
you get 700MHz, 512MB RAM, and networking for $35, as well as lots of GPIO to hook into. It just really hits a sweet spot.
20. Microcontrollers!!!!!11!!!
wtfbbqhax
Arduino
Saturday, June 15, 13
J
The arduino showed up right around the same time Make magazine did, and they had a huge impact on hobbyists (though people
that were doing microcontrollers before that will hate me for saying that). Still, it’s only a 16MHz device with virtually no storage,
no networking, and it had to be programmed in C. Now we have a whole range of options, from the Beaglebone Black to the
PCDuino, which offers a 1GHz chip, 1GB of RAM, a Mali 400 GPU, and an Arduino-compatible pinout (so you can take advantage
of existing shields). Those are both far more expensive and power hungry than the almighty Raspberry Pi, though. With a RasPi,
you get 700MHz, 512MB RAM, and networking for $35, as well as lots of GPIO to hook into. It just really hits a sweet spot.
21. Microcontrollers!!!!!11!!!
wtfbbqhax
Beaglebone
Black
Arduino
Saturday, June 15, 13
J
The arduino showed up right around the same time Make magazine did, and they had a huge impact on hobbyists (though people
that were doing microcontrollers before that will hate me for saying that). Still, it’s only a 16MHz device with virtually no storage,
no networking, and it had to be programmed in C. Now we have a whole range of options, from the Beaglebone Black to the
PCDuino, which offers a 1GHz chip, 1GB of RAM, a Mali 400 GPU, and an Arduino-compatible pinout (so you can take advantage
of existing shields). Those are both far more expensive and power hungry than the almighty Raspberry Pi, though. With a RasPi,
you get 700MHz, 512MB RAM, and networking for $35, as well as lots of GPIO to hook into. It just really hits a sweet spot.
22. Microcontrollers!!!!!11!!!
wtfbbqhax
Beaglebone
Black
Arduino
PCDuino
Saturday, June 15, 13
J
The arduino showed up right around the same time Make magazine did, and they had a huge impact on hobbyists (though people
that were doing microcontrollers before that will hate me for saying that). Still, it’s only a 16MHz device with virtually no storage,
no networking, and it had to be programmed in C. Now we have a whole range of options, from the Beaglebone Black to the
PCDuino, which offers a 1GHz chip, 1GB of RAM, a Mali 400 GPU, and an Arduino-compatible pinout (so you can take advantage
of existing shields). Those are both far more expensive and power hungry than the almighty Raspberry Pi, though. With a RasPi,
you get 700MHz, 512MB RAM, and networking for $35, as well as lots of GPIO to hook into. It just really hits a sweet spot.
23. Microcontrollers!!!!!11!!!
wtfbbqhax
Beaglebone
Black
Arduino
PCDuino
Raspberry Pi
Saturday, June 15, 13
J
The arduino showed up right around the same time Make magazine did, and they had a huge impact on hobbyists (though people
that were doing microcontrollers before that will hate me for saying that). Still, it’s only a 16MHz device with virtually no storage,
no networking, and it had to be programmed in C. Now we have a whole range of options, from the Beaglebone Black to the
PCDuino, which offers a 1GHz chip, 1GB of RAM, a Mali 400 GPU, and an Arduino-compatible pinout (so you can take advantage
of existing shields). Those are both far more expensive and power hungry than the almighty Raspberry Pi, though. With a RasPi,
you get 700MHz, 512MB RAM, and networking for $35, as well as lots of GPIO to hook into. It just really hits a sweet spot.
24. Raspberry Pi
The Raspberry Pi is a credit-
card sized computer that plugs
into your TV and a keyboard.
It’s a capable little PC which
can be used for many of the
things that your desktop PC
does, like spreadsheets, word-
processing and games. It also
plays high-definition video.
Saturday, June 15, 13
A
Josh will go ahead and pass around the Pi
[run through ports]
25. RaspberryPi Setup
• dd image to memory card
• plug it all up (power last)
• about a dozen setup questions
• Debian LXDE setup (lightweight x11)
• can also run Arch
• straight console if that's all you want
Saturday, June 15, 13
A
setup is really easy, dd takes about 30 min, but other than that your running in under 15 min
26. RaspberryPi:
Running Ruby
• install RVM normally
• install ruby via rvm
• takes a long time to compile
• 1.9.3 seems to work better, mostly due to compile times and
gem compatibility
• expect this to change in the coming months
Saturday, June 15, 13
((Josh setup RC car))
A
Getting ruby running is about as easy, I used RVM, compile takes a few hours
1.9.3 seems to be the best right now, I expect this will change soon
27. RaspberryPi GPIO and Tips
• be careful with the Pi, need buffering, grab some transistors
or a ULN2003 to protect it.
• limited hardware PWM out of the box (1, shared with audio).
We’ll get you around that limitation a little later.
• You need at least a 1-amp power supply, and you probably
want a powered USB hub if you want more than kb/mouse.
Saturday, June 15, 13
A
28. RaspberryPi Tips (cont)
• Get a Pi Cobbler breakout board to make your life easier
• Cirago wifi/bluetooth dongle for $11, works great
• Buy a nice case (or 3d print one :D :D :D)
• Use a class 10 SD card if you can. dd is *slooooow* on a bad
card.
• There’s a camera module for $25.
Saturday, June 15, 13
A
explain pi cobbler is the ribbon cable attached to the one going around, it makes breadboarding easier
cirago wifi/bluetooth seems to work great for basic IO and is low powered
cases keep you from shorting it out, i put some heatsinks so i can overclock mine a bit
class 10 sd cards are great, makes dd faster, and UX faster
29. GPIO Examples
using pi_piper gem, see: https://github.com/jwhitehorn/pi_piper
Event driven Raspberry Pi GPIO programming in Ruby
Saturday, June 15, 13
J
32. Control schemes
• Serial Ports
• Connection Types
• Bluetooth
• USB
• GPIO
• Or my favorite, straight DRb over the network
Saturday, June 15, 13
A
lots of ways to control hardware, serial ports you ust write too like appending to a text file, can attach over bluetooth usb or gpio
(J interupts)
DRB
35. So you have a RasPi
• We made it blink earlier with `pi_piper`
• What’s the next quick win we can get?
• (note: most of the upcoming code is super quick-n-dirty)
• Our goal is just to demystify this stuff - none of this code is
beyond a Ruby Nuby.
Saturday, June 15, 13
A
So we got some easy wins with the toys earlier, and we took a microcontroller and made an LED blink
What is our next steps?
Going forward we have a lot of quick code, goal isn’t to provide code examples but to demystify how this works. No magic here.
38. That was cool. Now what?
• How’s about some more direct control?
• Android -> JRuby <- DRb -> MRI -> RasPi -> Blinkenlight
Saturday, June 15, 13
A
So all this is cool, but how can we get into some more direct control of robots?
talk through adding android and DRB
J
Something about how awful writing custom protocols is
compared to DRb
39. Saturday, June 15, 13
J
This is the pickaxe example of DRb objects basically, completely standard...talk through class briefly
40. So now android?
• Eww, java
• Not the worst thing in the world
• Tiny code
Saturday, June 15, 13
J
You came to this conference looking for Java code and we know it.
49. RGB LED
Saturday, June 15, 13
A
What’s the next obvious step past a single red led?
A single RGB LED. Let’s make it happen
50. RGB LED
• So now we have an LED, so what’s the next simplest way to
ratchet up?
Saturday, June 15, 13
A
What’s the next obvious step past a single red led?
A single RGB LED. Let’s make it happen
51. RGB LED
• So now we have an LED, so what’s the next simplest way to
ratchet up?
• RGB LED, controlled by a color picker on the android device,
that changes color in realtime.
Saturday, June 15, 13
A
What’s the next obvious step past a single red led?
A single RGB LED. Let’s make it happen
52. RGB LED
• So now we have an LED, so what’s the next simplest way to
ratchet up?
• RGB LED, controlled by a color picker on the android device,
that changes color in realtime.
• Problem: RGB LED requires analog levels, but we only have
digital with pi_piper.
Saturday, June 15, 13
A
What’s the next obvious step past a single red led?
A single RGB LED. Let’s make it happen
53. Pulse Width Modulation
Saturday, June 15, 13
A
So you can use PWM to simulate analog levels, but you’re really just switching them on and off. If an output is switched on 10% of
the time, evenly distributed, then it makes an LED look like it’s been sent a 10% max output signal.
Later we’ll use them for logic level stuff, but for now we’ll just treat PWM like fake-analog.
54. Pulse Width Modulation
• RasPi only has one hardware PWM pin, and it’s shared with
the audio output (so you can’t use PWM and play sound at the
same time)
• So you can run pi-blaster to get 8 PWM outputs. It’s a
userland driver that hooks into the true PWM pin’s interrupt
and uses it to drive any GPIO pin like it’s PWM
• https://github.com/sarfata/pi-blaster/
Saturday, June 15, 13
J
pi-blaster, up to 8 PWM
55. Pulse Width Modulation
• To completely turn off pin1:
echo "1=0" > /dev/pi-blaster
• To completely turn on pin1:
echo "1=1" > /dev/pi-blaster
• To set pin1 to a PWM of 20%
echo "1=0.2" > /dev/pi-blaster
Saturday, June 15, 13
A
Here’s how you use pi-blaster - everything’s just a file, and you write commands to it
56. Saturday, June 15, 13
J
Here’s a really basic class representing our rgb led. I’m just showing the red channel code here.
57. This class is actually
like 200 lines long :-
Saturday, June 15, 13
J
If we’d used any metaprogramming at all here, it’d have been much smaller. I promised quick-n-dirty code right?
58. Buuuuut
• Turns out that most RGB LEDs expect the common leg to be
high, and the channel-specific legs to be a modulated ground
(our code assumed the common leg would be ground)
• Luckily, Ruby’s awesome so when I ran into this when testing
it took all of three seconds to fix it up. NOTE: I also had to
run the 3.3V line to the common leg, then you’re just
modulating the difference (so 100% duty cycle for a given leg
means no voltage potential difference, or ‘off’)
Saturday, June 15, 13
J
59. ZOMG
Saturday, June 15, 13
J
Hey hey, object oriented programming is awesome.
Anyway, let’s go ahead and see what the android interface for this project looks like
60. Saturday, June 15, 13
A
Just a basic color picker, nothing fancy here. As you change the slider, we want the led updating in real time
61. Saturday, June 15, 13
J
More java code. Last time we ran code each time you hit a button, but now we have a slider. At first I ran code on each change
event from the slider, but it turns out DRb can’t handle 200 messages/second and it would get laggy.
62. Timer Task that fires 20 times
per second. That seems to be
a sweet spot.
Saturday, June 15, 13
J
So my solution to this is to sample the UI 20x/second and send a message over DRb each time
63. If you’re going this route, have a single
method that accepts the state of the
User Interface. Then handle it on
the server side (rather than multiple
messages, one per user interface
component)
Saturday, June 15, 13
J
I found out later that apparently that’s what RC Controllers in the real world do anyway, so that was serendipitous. So there’s
basically all the code, how well does it work?
65. So what’s next?
• Let’s finish this project up with something
fun.
• You go to the thrift store, and you buy one
of these bad boys for $2. This is the way
to go for this stuff by the way - there’s a
TON of old toys waiting to be awesome
again.
• Tank steering R/C base - this is the RAD2.
Saturday, June 15, 13
A
Alright, so we’ve got PWM control of a ruby process from android now. What next?
Let’s finish up by building an RC Tank steering thingie...
bullet points...
66. Speed Construction
• Take a Sabertooth 2x12RC ($65)
• Add a RasPi
• Add a 12V battery
• Add a cirago wifi/bt stick
• Mutilate a usb micro cable
Saturday, June 15, 13
J
Put all this stuff together aaaand
68. PWM and the Sabertooth
• So to control the Sabertooth,
you send a PWM signal per-
motor.
• 2000us pulse width: 100%
forward
• 1500us pulse width: idle
• 1000us pulse width: 100%
reverse
Mapped
to 0..1
values for
pi-blaster
Saturday, June 15, 13
J
Explain sabertooth logic levels, and show how we’re mapping them to pi-blaster levels
69. -1.0
1.0
Saturday, June 15, 13
A
Here’s the interface. Just two sliders, mapped from -1 to 1. Your standard tank steering controls
70. Map those values from the last
slide into our pwm output values
for pi-blaster
Saturday, June 15, 13
J
74. Thank you for coming
Saturday, June 15, 13
J
Thanks for being so attentive. As a rather generous thanks for attending our talk, we’ve provided free iPads under everyone’s
seats.
OMG THAT GUY ACTUALLY LOOKED of course we didn’t do that
A
Once again, thanks for coming. Any questions?
75. Saturday, June 15, 13
J
Thanks for being so attentive. As a rather generous thanks for attending our talk, we’ve provided free iPads under everyone’s
seats.
OMG THAT GUY ACTUALLY LOOKED of course we didn’t do that