pid-balancer/calibrate.py
2024-12-16 15:25:32 +01:00

51 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Example Python Servo Script #2 Calibrating Range
# The first script makes use of the Gpiozero defaults. It assumes the servo uses a signal frame width of 20ms.
# The pulse width for the minimum and maximum rotation is assumed to be 1ms and 2ms. This information should be
# available in the servo specification and I would avoid sellers that dont provide this data.
# I found that with the default settings my servo only moved +45/-45 degrees. I changed the pulse width parameters
# in order to get a full 90 degrees of rotation in either direction .
from gpiozero import Servo
from time import sleep
myGPIO = 17
myCorrection = 0.45
maxPW = (2.0 + myCorrection) / 1000
minPW = (1.0 - myCorrection) / 1000
servo = Servo(myGPIO, min_pulse_width=minPW, max_pulse_width=maxPW)
while True:
servo.mid()
print("mid")
sleep(0.5)
servo.min()
print("min")
sleep(1)
servo.mid()
print("mid")
sleep(0.5)
servo.max()
print("max")
sleep(1)
# The servo should now move between its minimum, middle and maximum positions with a small delay in-between.
#
# In this example the min pulse width is decreased from the default of 1 by a correction of 0.45 to 0.55ms.
# The max pulse width is increased from the default of 2 by 0.45 to 2.45ms. This gave my servo a full 90 degrees
# of rotation in both directions. The values “0.55” and “2.45” are divided by 1000 to convert them to milliseconds.
#
# There is nothing magical about “0.45”. It was just the correction that worked best for my servo.
#
# To work out these numbers I started with :
#
# myCorrection=0
# maxPW=(2.0+myCorrection)/1000
# minPW=(1.0-myCorrection)/1000
#
# and increased/decreased the correction number in increments of 0.05. This allowed me to find the biggest change
# I could make before the servo sounded unhappy.
#
# “myCorrection” has to be a number between 0 and 1 but is unlikely to ever need to be 1!