OK, so not quite the Sheldon Virtual Presence Device from Big Bang Theory but who knows…. maybe my next version?
I have had various bits of robots floating around for a while now and never actually pulled them all together. I’ve made both a Scratch, and a PS3 controlled, Pibrella powered bot which I must blog about one of these days. I also built one from LEGO motors and a SN754410 H-bridge a year or two back, but the motors were old (20+ years) and it didn’t get far.
4tronix MicRoCon – It’s an all in one platform that provides power conversion from 6-9v down to 5v to so it can power the Pi as well as an H-bridge to drive the motors. The other feature I will make use of is some 5v connectors for servos. Mine is the v1.1 from early 2014, v3 is the current version hence the change in looks.
Raspberry Pi Camera – If you don’t know what this is; it’s a webcam that plugs directly into the Pi, not via USB. Loads of places stock these
Car/Bot Chassis – Mine is one of these from EBay.
WiFi – Yes, you could do this wired, but that would be very limiting!
Flask– A Python based light weight web framework. It’s relatively straight forward to use and allows us to control the GPIO directly.
RPIO – For this project I have moved away from the commonly used RPi.GPIO library as the servo control is less “jittery”. The RPIO library uses C to better manage the precise timings needed. Ultimately driving the servos via an Arduino or similar would provide the best results, but that was more complicated than I wanted to go.
The two servos are attached to the MicRoCon board via the 3 pin headers
The motors are wired into this 4-way screw terminal
Then, using a liberal amount of blu-tak, tape and will power it’s all fixed to the chassis
servo.py – as the name suggests, this is a class to control a servo. When an instance of the class is created it takes the BCM pin number the servo is connected to. Once created, the instance has two methods
- setAngle(degrees) – set the angle of the servo between -90 and +90
- incAngle(increment) – specify the number of degrees to move the servo (+ or -)
Why BCM numbers? Mainly because they are default for RPIO
motor.py – This is a still “in progress” as I would like to add speed control via PWM (Pulse Width Modulation – i.e. turn the motor on and off quickly to reduce the rotation speed). However, for my initial needs it serves its purpose. On creation this class take two parameters; these are the two BCM pin numbers that control the motor direction on the H-bridge chip (i.e. forward and back).
- start(direction, speed, duration) – activates the motor.
- direction can be F[orward] or B[ackwards]
- speed (not used yet) will control how fast the motor goes
- duration denotes the number of seconds to run for . If duration is 0 (zero) then the motor is on until stop() is called
- stop() – stops all motor activity
camera_pi.py – Taken directly from here. It provides a streaming interface from the Pi camera module
templates/index.html – Flask uses a templates folder to manage the HTML pages it displays. This is the only page and provides the buttons to control the motors and an area to display the camera feed.
app.py – this is the brains of the outfit and acts as our web server. The camera, servo, and motor control are all done from here. The key functions of interest are
- video_feed() – This is called by the HTML to request a new image to display from the camera. The function gen() calls the Camera class from camera_pi.py and requests a new image
- move(direction) – this function takes the commands generated by the buttons on the web page and acts on the request to move servos or turn motors.
The video quality is not great, but you get the idea.
More of an aide–mémoire in case I stumble over these again, or warnings for the weary traveller.
We need more power! – The 4xAA batteries I had were fine running the wifi, camera and servos. When the motors were added the wifi connection would drop or the Pi would reset due to low voltage/current. There are two ways to solve this – more batteries (up to 9v on this board), or powering the Pi direct. I chose the latter option as I had a small(ish) 5.2Ah phone charging battery to hand. The upside is if the motor batteries fail the Pi doesn’t go with it, plus my portable battery has a charge indicator on it so I could see if the Pi is about to expire. I will try an 9v at some point as it will be far more compact.
Threading – No, we’re not talking sewing or wearable tech here. Threading allows two or more actions in a program to occur at the same time. The video feed is constantly updating, when a button is pressed this is a second action but it is blocked because the web page is still updating the image. Enabling threading in Flask allowed both actions to take place without conflicting with each other. This does carry some risks, and there are various dire warnings on the Internet about using it in production code. However this is for one or two users in a local environment. I think we’re safe!
Like all good hacking projects there’s still stuff to do.
- Speed in the motor control
- Catch exceptions in the video feed to stop the server crashing
- ‘Touch’ friendly, i.e. keep moving servo/motor while button pressed, stop when let go