Once we have a camera (or two) attached to our raspi helps us to discover one of the annoying limitations they have: they cannot move!
Fortunately, there are plenty of options for doing this. I decided to buy a couple of servos.
They are cheap, small and noisy.
There are lots of pages explaining the theory behind their inner working so we only will remind here just a couple of things: they have some rotation constraints (the ones I bought can just move 180 degrees) and the way to control them is by sending some pulses whose duration determines the angle (for interested people, you can have a look at How do servos work? -in English- or at Trabajar con Servos -in Spanish-).
From our program our mission will be to find the way to send the adequate pulses to the selected pin where we have connected the servo (remember: physical world-computer connection).
There are lots of examples in the net.
As usual, we are commenting on the main steps here, following the third program.
First the python modules that we need:
import RPi.GPIO as GPIO
The first one is used for sending instructions through the pins to our raspi. The second one is for managing time related data.
Now, some setup: we will make reference to the pins by their number and we configure the 11 pin as output.
Now, we are going to define the controller with a frequency of 50Hz and we’ll start it in the central position:
p = GPIO.PWM(11,50)
Finally, a bit more of code changing the position each second:
try: while True: print "Uno" p.ChangeDutyCycle(7.5) time.sleep(1) print "Dos" p.ChangeDutyCycle(12.5) time.sleep(1) print "Tres" p.ChangeDutyCycle(2.5) time.sleep(1)
That is, it starts at the central position and moves to both extremes. From the center it goes to one side, then to the contrary one and finally it returns to the initial position. You can see this movements in the following video:
By the end of the video you can see that we can control more than one servo (with the only limitation of the number of available pins). The code for this can be seen at:
We have added pin number 12 and we use two controllers (p1 and p2). Then, we just send instructions to each one in sequence. You can have a look at this mini-video:
We will see soon how to manage all the parts we have commented until now in order to finish the project.
This post has been posted originally in Spanish at: Añadiendo movimiento: servos.