Close

Custard Pi, Python and Servos

A project log for Web powered antique wind gauge

An antique volt meter paired with a Raspberry Pi to display real time wind data.

mechanicalsquidmechanicalsquid 12/02/2014 at 09:040 Comments

The cable, and the Custard Pi turned up.

First, I'm trying the servo on an external power supply. I'll try it from the Pi later, but as I already know it works from an external supply, I want to change the minimum possible.

With the servo signal pin connected to GPIO 22 (pin number 15 on the Custard Pi), a short python script to verify it moves:
import timefrom RPIO import PWM

lookup=(600,640,680,720,760,800,850,900,950,1000,1050,1090,1130,1170,1210,1250,1310,1370,1430,1490,1550,1660,1770,1880,1990,2100)
while True:
    for windspeed in range (0,6):
        print("Servo position: "+ str(lookup[windspeed]))
                servo=PWM.Servo()
        servo.set_servo(22,lookup[windspeed*5])
        time.sleep(1)
You'll notice I'm using a lookup. This is to account for the non-linearity in the resistor linkage. While I'm sure there's a mathematical technique to compute the equations of motion - there's only 25 positions. A lookup technique is far easier.

The script moves the pointer from 0 to 5, 10, 15, 20 and 25 before returning to 0. And it does.

But, the accuracy and repeatability of the servo is really pretty shocking. It never gets to quite the same position twice and always overshoots by as much as two ticks depending on the starting position.

While two ticks isn't really that big a deal - it equates to two knots, it's going to irk me somewhat, so I'll have to come up with something. Adam, in the comments has already pointed me at stepper motors designed for driving gauges. And I'm also told that digital servos are much better - this one is after all the cheapest I could get my hands on.

While I'm considering that though, there's no reason why I can't take my windspeed script of earlier and use it to drive my inaccurate servo:

from bs4 import BeautifulSoup
import requests
import time
from RPIO import PWM

lookup=(600,640,680,720,760,800,850,900,950,1000,1050,1090,1130,1170,1210,1250,1310,1370,1430,1490,1550,1660,1770,1880,1990,2100)

r=requests.get("http://www.bramblemet.co.uk/wap/")
data=r.text
soup=BeautifulSoup(data)
link=soup.find('a')
p=requests.get(r.url[:-18]+link.get('href'))
windpage=p.text
soup=BeautifulSoup(windpage)
windspeed=int(soup.prettify()[309:315])

if windspeed>25:
    windspeed=25
if windspeed<0:
    windspeed=0

print("Servo position: "+ str(lookup[windspeed]))

servo=PWM.Servo()
servo.set_servo(22,lookup[windspeed])
time.sleep(1)

It works! Also I added a cron to update it every 10 minutes.

Discussions