2nd commit

This commit is contained in:
Rudi klein 2024-12-16 15:25:32 +01:00
parent dc54db1c4c
commit 71b70467aa
4 changed files with 130 additions and 0 deletions

View File

51
calibrate.py Normal file
View File

@ -0,0 +1,51 @@
# 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!

25
min-max-demo.py Normal file
View File

@ -0,0 +1,25 @@
# Example Python Servo Script #1
# Once connected the easiest way to get your servo moving is to use the Gpiozero library in a Python script.
from gpiozero import Servo
from time import sleep
myGPIO = 17
servo = Servo(myGPIO)
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)

54
precision_positioning.py Normal file
View File

@ -0,0 +1,54 @@
# 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.