Example 2: Full Sweep for 180 Degree Servo

examples/ex2_full_sweep_with_180_deg_servo.py
  1#-----------------------------------------------------------------------
  2# Pi Servo Hat - Example 2
  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# Sweep
 81#########################################
 82while True:
 83    for i in range(0, 180):
 84        print(i)
 85        test.move_servo_position(0, i, 180)
 86        time.sleep(.001)
 87    for i in range(180, 0, -1):
 88        print(i)
 89        test.move_servo_position(0, i, 180)
 90        time.sleep(.001)
 91
 92#########################################
 93# Code below may damage servo, use with caution
 94# Test sweep for full range of servo (outside 0 to 180 degrees).
 95# while True:
 96#     for i in range(-45, 200):
 97#         print(i)
 98#         test.move_servo_position(0, i, 180)
 99#         time.sleep(.001)
100#     for i in range(200, -45, -1):
101#         print(i)
102#         test.move_servo_position(0, i, 180)
103#         time.sleep(.001)