pid-balancer/precision_positioning.py

54 lines
1.8 KiB
Python
Raw Normal View History

2024-12-16 15:25:32 +01:00
# Once youve determined the min and max pulse width values you can use the “value” feature to position the servo arm
# anywhere between its limits.
# Setting the “value” parameter to a number between -1 and +1 moves the arm between its minimum and maximum positions.
# Examples include :
#
# Minimum position
# servo.value=-1
#
# Mid-point
# servo.value=0
#
# Maximum position
# servo.value=1
#
# Position between mid-point and maximum
# servo.value=0.5
#The script below generates a range of “value” numbers to sweep the servo between its maximum and minimum position
from gpiozero import Servo
from time import sleep
myGPIO = 17
myCorrection = 0
maxPW = (2.0 + myCorrection) / 1000
minPW = (1.0 - myCorrection) / 1000
servo = Servo(myGPIO, min_pulse_width=minPW, max_pulse_width=maxPW)
while True:
print("Set value range -1.0 to +1.0")
for value in range(0, 21):
value2 = (float(value) - 10) / 10
servo.value = value2
print(value2)
sleep(0.5)
print("Set value range +1.0 to -1.0")
for value in range(20, -1, -1):
value2 = (float(value) - 10) / 10
servo.value = value2
print(value2)
sleep(0.5)
# The servo should now move between its minimum, middle and maximum positions with a small delay in-between.
#
# The first “For” loop generates a set of integers between 0 and 20. The value has 10 subtracted from it to give a
# range -10 to 10. The value is then finally divided by 10 to give a range of -1 to +1. The original set of values
# contained 20 integers and we still have 20 steps in the sequence. To increase the number of steps to 40 you would
# replace 20 with 40 and subtract 20 rather than 10.
#
# The second “For” loop does the same thing but generates a sequence from +1 back to -1.