In January, I decided my next Raspberry Pi Lego project would be a pan-tilt mechanism for mounting the raspberry pi camera. A pan tilt device allows one to point the camera using two motors - the combination of left / right panning with up / down tilting gives a high degree of freedom to point the camera to where you like. Given their rapid response times a servo motor seemed the most appropriate so I ordered a couple more from eBay in preparation.
The Raspberry Pi camera module comes with a fairly short 10cm ribbon cable to connect to the Pi. This short cable limits how much play could be supported in a pivoting camera base and so my first thoughts were to make a pan tilt mechanism that would carry the entire Pi. After some initial prototyping this didn't seem very practical. The center of gravity would need to be balanced well and the servo's would have a larger mass to manipulate which would slow them down. The Lego required to mount would also be large and ungainly.
While obvious in hindsight, the trick I realised would be to source a longer ribbon cable for the camera, giving plenty of slack for the camera to slew around. Forum posts indicate some surprisingly long usable cable lengths are possible with varying degrees of signal degradation, so I ordered a length of 25cm FPC ribbon cable from eBay. The trick here is to order a cable that has its exposed pins on opposite sides at either end. Cables whose exposed pins are on the same side would reverse the pinout to the camera and fail to work.
I searched around for some ideas on how to best implement the pan/tilt mount and the image below represents the mechanism at its most simple. One panning servo for the base - on top of which is mounted the tilt servo and camera.
The HXT900 servo's I have are the same form factor as above so I went about working out how to replicate this mount in Lego.
Using the dremelled out 6 stud plates from the marble sorting project, here are the results:
Panning base - applied a bit too much pressure with the locking pliers while dremmelling and the left most stud is slightly damaged.
Tilt servo packed with cardboard. There is additional packing cardboard behind the face to reduce backlash.
Since I was too impatient to wait for a camera controlled interface, I wrote up a joystick controller to test out the mechanism. OpenTK has a nice cross platform joystick implementation so as per my usual development process, I coded it all up did most testing on my Windows desktop before transferring the binaries to the Pi for execution.
Here is the mechanism at work.
The joystick controller (source) is timer based. When the timer fires a set of new samples are read from the joystick axes. The calculation on how much to move the servo is contained in a separate class - JoystickModifierStrategy. It reads the throttle axis to decide how big a jump to make on each tick. If throttle is high, the camera will slew further with each tick. If the throttle is low, then the camera moves with a finer precision. Once the required slew is calculated it is sent to the servos via PWM.
The pan tilt isn't as silky smooth as the Pixy camera - mine definitely has a notchy feel to it. I'm not entirely sure why that is - it could be their servos behave differently or perhaps the Pixie 50fps update rate smooths the responses out.