My new project is finished. Normally I build websites… This time, I wanted to create something real! :-)
So I drive my Raspberry Pi through my flat… with two continuous servo motors, wheels, wireless power and a smartphone camera control.
Insights without cover.
This is what I did…
First success: Pi with USB power bank spins wheels.
Servo motors have three inputs: 5 Volts power (red), Ground (brown), Signal (yellow). Connect these to the GPIO pins of the Raspberry Pi using jumper cables. Click here for the pinouts. It has two 5V pins at the top-right. Ground is spread a few times over different pins. For the signal pins I chose GPIO 17 and GPIO 27. They lie next to each other.
The signal is transferred with pulse-width modulation (PWM). This means that the signal alternates between 0 and 1 very fast. The duration (“width”) of the 1 becomes the setpoint for the velocity.
Now I can connect to the Raspberry Pi via SSH and test the servo motors with the command line. The pigs program is a neat command line tool for this purpose (pigs man page).
[Click here for the source code.]
Next I published the commands over HTTP. As a Java Web Developer this was a home game for me.
I run a Tomcat 8 Java Web Server on the Raspberry Pi. It has my custom Java Web Archive (WAR). A HttpServlet handles HTTP requests. It reads velocity setpoints from URL parameters and passes them to the pigs program via ProcessBuilder.
The second version is a little bit more sophisticated and understands command sequences:
An example request:
http://…/wheels?x=clear+left+800+right+800+wait+1000+stop
[Click here for the source code.]
Direct control of the robot with smartphone browser remote control. The remote control is a little bit rotated. Top left = Ahead. Top right = Right turn. Bottom left = Left turn. Bottom right = Backwards.
The simple remote controls directly sets speeds on the left and right wheels dependent on where I click / touch. When the click is released, the robot stops. This way, the Pi can be navigated… Yay!
[Click here for the source code.]
Detected robot cover coordinate system inside the web browser
But I want a cool controller!
This controller shall be opened in a smartphone / tablet web browser. It captures the camera video. The user points the camera to the robot. The signal orange marker stickers on the robot cover are found inside the image. This way, the robot position is detected. With the positions of the stickers, the robot coordinate system can be calculated, even when the image is distorted in perspective. When I click somewhere on the image, I know: This is 210mm ahead and 32mm to the left.
In more detail, the procedure comes down to these steps:
The remaining part is to execute commands like this: Turn 32 degrees clockwise, drive 240mm ahead.
Driving ahead is simple: Just set the same speed on both wheels (one wheel has to be inverted due to the setup). I tried to read the wheel speeds from the datasheet. However, the sheet had maximum values only and the calculated values turned out to be inaccurate. So I put the Pi on a book and found the milliseconds for one and 10 wheel revolutions by experimentation and with the speed I wanted. I know the wheel perimeter. So I can easily calculate the drive ahead duration for a given distance. Ahead: Check.
Tweaking wheel revolution duration
Turning around the center works like this: Drive one wheel forward. Drive the other wheel backwards with the same speed. Again, the calculation approach did not work very good — mainly due to friction. So I measured the time needed for a 360 degrees turnaround. Depending on the desired angle I rotate for the corresponding proportion of that time. The friction is a bit different every time, so the rotation might be a bit off. But the error is little and acceptable for my purposes. Rotation: Check.
And here it is, my Raspberry Pi on wheels with camera control:
Demo for the camera control: The Pi is detected inside the image. Clicks on the ground image are transformed to target robot coordinates and wheel commands.
This was a very nice project! I was glad to apply my knowledge on differential drive robots from my robotics studies to my own Raspberry Pi. It really made a lot of fun!
Controlling the servo motors was easier than expected.
Because of the Pi’s wifi access and the possibility to run a web server, I was in known terrain very fast. Thanks to HTML5 Camera / Video / Canvas, an image detection based remote control can be provided on a smartphone / tablet nowadays and this works really smooth.
Also, I had a lot of fun getting lost in the Adafruit online shop and learning system! There, you can get great inspiration for hardware projects of any kind. The package was shipped from New York to Germany in three days — awesome!
Let’s see what I build next… :-)