This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servo0.py
60 lines (50 loc) · 1.61 KB
/
servo0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Author: Josh Huang
from gpiozero import Servo
import time
from gpiozero.pins.pigpio import PiGPIOFactory
import RPi.GPIO as GPIO
import os
factory = PiGPIOFactory()
servo = Servo(23, min_pulse_width=0.5/1000, max_pulse_width=2.5/1000, pin_factory=factory)
limit_angle = -40/90 # angle where servo hits camera platform
adjustment_angle = -10 # angle from min servo to horizontal
servo.value = None # prevents servo from moving upon init
class Servo0:
def __init__(self):
print("servo 0 initiated")
def test(self):
'''
Test that the servo runs by rotating the servo back and forth
'''
print('testing S0')
servo.mid()
time.sleep(1)
servo.max()
self.stop()
def rotate(self, degrees):
'''
Rotates the base servo 0 by a number of degrees, degrees given by IMU in main class
'''
# adjust rotation for angle of servo arm and get it to servo readable scale
rot = -degrees + adjustment_angle
rot = rot/90
# limit rot from limit_angle to 1
rot = limit_angle if rot < limit_angle else 1 if rot > 1 else rot
servo.value = rot
self.stop()
def stop(self):
'''
stops the servo from drawing more power, has sleep time before stop to allow time to move rotate servo
'''
time.sleep(1)
servo.value = None
def test():
# python -c 'import servo0; servo0.test()'
s0 = Servo0()
s0.test()
# servo.mid()
# time.sleep(1)
# servo.value = None
# servo.value = -1
# time.sleep(.2)
# s0.stop()