-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoPiServo.py
162 lines (143 loc) · 4.39 KB
/
RoPiServo.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# GPIO
import RPi.GPIO as _g
# time utilities
import time as _t
# multiprocessing
import multiprocessing as _mp
# configuration
from config import servo_conf as _conf
# RO-PI SERVO SENSORS
class RoPiServo:
# initialize
def __init__(self):
# set PINs on BOARD
if _conf['DEVEL_LOG'] :
print("Initializing Servos...")
print("> pin 1:", _conf['servo_1_pin'])
print("> pin 2:", _conf['servo_2_pin'])
_g.setmode(_g.BOARD)
_g.setup(_conf['servo_1_pin'], _g.OUT)
_g.setup(_conf['servo_2_pin'], _g.OUT)
# current directions
self.angle1 = _conf['angle1']
self.angle2 = _conf['angle2']
self.direction1 = "u"
self.direction2 = "l"
# queues and processes
if _conf['DEVEL_LOG'] : print("Initializing queues and processes...")
self.queue1 = _mp.Queue()
self.queue2 = _mp.Queue()
self.process1 = _mp.Process(target=self.P1)
self.process2 = _mp.Process(target=self.P2)
self.process1.start()
self.process2.start()
if _conf['DEVEL_LOG'] : print("...init done!")
# terminate
def terminate(self) :
if _conf['DEVEL_LOG'] : print("Servos termination...")
self.clearQueue(self.queue1)
self.clearQueue(self.queue2)
self.process1.terminate()
self.process2.terminate()
# clean queue
def clearQueue(self, q) :
while not q.empty() :
q.get()
# activate pin
def activate(self, p) :
_g.output(p, _g.HIGH)
# deactivate pin
def deactivate(self, p) :
_g.output(p, _g.LOW)
# set and start pwm
def startPWM(self, pin) :
pwm = _g.PWM(pin, _conf['frequency'])
pwm.start(0)
return pwm
# stop pwm
def stopPWM(self, pwm) :
pwm.stop()
# set angle
def setAngle(self, angle, pwm, pin) :
duty = angle / 18 + 2
self.activate(pin)
pwm.ChangeDutyCycle(duty)
_t.sleep(.1)
self.deactivate(pin)
pwm.ChangeDutyCycle(0)
# control process 1
def P1(self) :
self.worker(_conf['angle1'], _conf['servo_1_pin'], self.queue1)
# control process 2
def P2(self) :
self.worker(_conf['angle2'], _conf['servo_2_pin'], self.queue2)
# worker
def worker(self, angle, pin, queue) :
pwm = self.startPWM(pin)
while True :
if not queue.empty() :
_a = queue.get()
if not _a == angle :
if not _a == 90 :
_a = _a + angle
if _a > 0 and _a < 180 :
angle = _a
self.setAngle(_a, pwm, pin)
#actions
def reset(self) :
if _conf['DEVEL_LOG'] : print("reset")
self.clearQueue(self.queue1)
self.clearQueue(self.queue2)
self.queue1.put(_conf['angle1'])
self.queue2.put(_conf['angle2'])
def up(self) :
if _conf['DEVEL_LOG'] : print("up")
if not self.direction1 == "u" :
self.clearQueue(self.queue1)
self.direction1 = "u"
self.queue1.put(_conf['angle_backward'])
def down(self) :
if _conf['DEVEL_LOG'] : print("down")
if not self.direction1 == "d" :
self.clearQueue(self.queue1)
self.direction1 = "d"
self.queue1.put(_conf['angle_forward'])
def left(self) :
if _conf['DEVEL_LOG'] : print("left")
if not self.direction2 == "l" :
self.clearQueue(self.queue2)
self.direction2 = "l"
self.queue2.put(_conf['angle_forward'])
def right(self) :
if _conf['DEVEL_LOG'] : print("right")
if not self.direction2 == "r" :
self.clearQueue(self.queue2)
self.direction2 = "r"
self.queue2.put(_conf['angle_backward'])
# DEBUG
if __name__ == "__main__":
print ("Welcome! Testing Servos:")
time = 3
servo = RoPiServo()
print ("Go up 15 times...")
for x in range(15):
servo.up()
_t.sleep(time)
print ("Go left 15 times...")
for x in range(15):
servo.left()
_t.sleep(time)
print ("Go right 15 times...")
for x in range(15):
servo.right()
_t.sleep(time)
print ("Go down 15 times...")
for x in range(15):
servo.down()
_t.sleep(time)
print ("Reset...")
servo.reset()
_t.sleep(time)
print ("Goodbye!")
servo.terminate()
_g.cleanup()