From 71b70467aaedafb0aa084fdc32451d66ab333ee5 Mon Sep 17 00:00:00 2001 From: rudi Date: Mon, 16 Dec 2024 15:25:32 +0100 Subject: [PATCH] 2nd commit --- Main.py | 0 calibrate.py | 51 +++++++++++++++++++++++++++++++++++++ min-max-demo.py | 25 +++++++++++++++++++ precision_positioning.py | 54 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+) delete mode 100644 Main.py create mode 100644 calibrate.py create mode 100644 min-max-demo.py create mode 100644 precision_positioning.py diff --git a/Main.py b/Main.py deleted file mode 100644 index e69de29..0000000 diff --git a/calibrate.py b/calibrate.py new file mode 100644 index 0000000..4575793 --- /dev/null +++ b/calibrate.py @@ -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 don’t 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! \ No newline at end of file diff --git a/min-max-demo.py b/min-max-demo.py new file mode 100644 index 0000000..9053f31 --- /dev/null +++ b/min-max-demo.py @@ -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) \ No newline at end of file diff --git a/precision_positioning.py b/precision_positioning.py new file mode 100644 index 0000000..e73326c --- /dev/null +++ b/precision_positioning.py @@ -0,0 +1,54 @@ +# Once you’ve 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. \ No newline at end of file