Example 4: Change PWM Frequency for 180 Degree Servo

examples/ex4_change_pwm_frequency_180_deg_servo.py
  1#-----------------------------------------------------------------------
  2# Pi Servo Hat - Example 4
  3#-----------------------------------------------------------------------
  4#
  5# Written by  SparkFun Electronics, June 2019
  6# Author: Wes Furuya
  7#
  8# Compatibility:
  9#     * Original: https://www.sparkfun.com/products/14328
 10#     * v2: https://www.sparkfun.com/products/15316
 11# 
 12# Do you like this library? Help support SparkFun. Buy a board!
 13# For more information on Pi Servo Hat, check out the product page
 14# linked above.
 15#
 16# This program is distributed in the hope that it will be useful, but
 17# WITHOUT ANY WARRANTY without even the implied warranty of
 18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 19# General Public License for more details.
 20#
 21# You should have received a copy of the GNU General Public License
 22# along with this program.  If not, see <http:www.gnu.org/licenses/>.
 23#
 24#=======================================================================
 25# Copyright (c) 2019 SparkFun Electronics
 26#
 27# Permission is hereby granted, free of charge, to any person obtaining
 28# a copy of this software and associated documentation files (the
 29# "Software"), to deal in the Software without restriction, including
 30# without limitation the rights to use, copy, modify, merge, publish,
 31# distribute, sublicense, and/or sell copies of the Software, and to
 32# permit persons to whom the Software is furnished to do so, subject to
 33# the following conditions:
 34#
 35# The above copyright notice and this permission notice shall be
 36# included in all copies or substantial portions of the Software.
 37#
 38# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 39# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 40# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 41# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 42# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 43# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 44# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 45#=======================================================================
 46
 47"""
 48This example should be used with a 180 degree (range of rotation) servo
 49on channel 0 of the Pi Servo Hat.
 50
 51The extended code (commented out), at the end of the example could be
 52used to test the full range of the servo motion. However, users should
 53be wary as they can damage their servo by giving it a position outside
 54the standard range of motion.
 55"""
 56
 57import pi_servo_hat
 58import time
 59
 60# Initialize Constructor
 61test = pi_servo_hat.PiServoHat()
 62
 63# Restart Servo Hat (in case Hat is frozen/locked)
 64test.restart()
 65
 66# Test Run
 67#########################################
 68# Moves servo position to 0 degrees (1ms), Channel 0
 69test.move_servo_position(0, 0, 180)
 70
 71# Pause 1 sec
 72time.sleep(1)
 73
 74# Moves servo position to 180 degrees (2ms), Channel 0
 75test.move_servo_position(0, 180, 180)
 76
 77# Pause 1 sec
 78time.sleep(1)
 79
 80# 50 Hz Test
 81#########################################
 82# Set PWM Frequency to 50 Hz
 83test.set_pwm_frequency(50)
 84
 85# Sweep from 0 to 180 degrees and back
 86for i in range(0, 180):
 87    print("Input: ", end = '')
 88    print(i, end = '')
 89    test.move_servo_position(0, i, 180)
 90    print(" Estimated Pos: ", end = '')
 91    print(test.get_servo_position(0, 180))
 92    time.sleep(.01)
 93for i in range(180, 0, -1):
 94    print("Input: ", end = '')
 95    print(i, end = '')
 96    test.move_servo_position(0, i, 180)
 97    print(" Estimated Pos: ", end = '')
 98    print(test.get_servo_position(0, 180))
 99    time.sleep(.01)
100
101
102# 100 Hz Test
103#########################################
104# Set PWM Frequency to 100 Hz
105test.set_pwm_frequency(100)
106
107# Sweep from 0 to 180 degrees and back
108for i in range(0, 180):
109    print("Input: ", end = '')
110    print(i, end = '')
111    test.move_servo_position(0, i, 180)
112    print(" Estimated Pos: ", end = '')
113    print(test.get_servo_position(0, 180))
114    time.sleep(.05)
115for i in range(180, 0, -1):
116    print("Input: ", end = '')
117    print(i, end = '')
118    test.move_servo_position(0, i, 180)
119    print(" Estimated Pos: ", end = '')
120    print(test.get_servo_position(0, 180))
121    time.sleep(.05)
122
123
124# 200 Hz Test
125#########################################
126# Set PWM Frequency to 200 Hz
127test.set_pwm_frequency(200)
128
129# Sweep from 0 to 180 degrees and back
130for i in range(0, 180):
131    print("Input: ", end = '')
132    print(i, end = '')
133    test.move_servo_position(0, i, 180)
134    print(" Estimated Pos: ", end = '')
135    print(test.get_servo_position(0, 180))
136    time.sleep(.05)
137for i in range(180, 0, -1):
138    print("Input: ", end = '')
139    print(i, end = '')
140    test.move_servo_position(0, i, 180)
141    print(" Estimated Pos: ", end = '')
142    print(test.get_servo_position(0, 180))
143    time.sleep(.05)