Example 3: Get Servo Position for 180 Degree Servo

examples/ex3_get_position_180_deg_servo.py
 1#-----------------------------------------------------------------------
 2# Pi Servo Hat - Example 3
 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("Input: ", end = '')
85        print(i, end = '')
86        test.move_servo_position(0, i, 180)
87        print(" Estimated Pos: ", end = '')
88        print(test.get_servo_position(0, 180))
89        time.sleep(.05)
90    for i in range(180, 0, -1):
91        print("Input: ", end = '')
92        print(i, end = '')
93        test.move_servo_position(0, i, 180)
94        print(" Estimated Pos: ", end = '')
95        print(test.get_servo_position(0, 180))
96        time.sleep(.05)