-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathswitches_and_servos.py
299 lines (240 loc) · 9.65 KB
/
switches_and_servos.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import pigpio # http://abyz.co.uk/rpi/pigpio/python.html
from gpiozero import Servo
from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep
import numpy as np
# global pi object for all TransmitterControl objects to use
PI = pigpio.pi()
class TransmitterControl:
def __init__(self, input_pin, weighting):
"""
Instantiate with input_pin of the PWM signal
to monitor.
Optionally a weighting may be specified. This is a number
between 0 and 1 and indicates how much the old reading
affects the new reading. It defaults to 0 which means
the old reading has no effect. This may be used to
smooth the data.
"""
self.input_pin = input_pin
# apply weighting
if weighting < 0.0:
weighting = 0.0
elif weighting > 0.99:
weighting = 0.99
self._new = 1.0 - weighting # Weighting for new reading.
self._old = weighting # Weighting for old reading.
self._high_tick = None
self._period = None
self._high = None
PI.set_mode(input_pin, pigpio.INPUT)
self._cb = PI.callback(input_pin, pigpio.EITHER_EDGE, self._cbf)
# initialize the previous position
sleep(.1) # some extra time to allow things to initialize
def _cbf(self, input_pin, level, tick):
if level == 1:
if self._high_tick is not None:
t = pigpio.tickDiff(self._high_tick, tick)
if self._period is not None:
self._period = (self._old * self._period) + (self._new * t)
else:
self._period = t
self._high_tick = tick
elif level == 0:
if self._high_tick is not None:
t = pigpio.tickDiff(self._high_tick, tick)
if self._high is not None:
self._high = (self._old * self._high) + (self._new * t)
else:
self._high = t
def get_pulse_width(self):
"""
Returns the PWM pulse width in microseconds.
"""
if self._high is not None:
return self._high
else:
return 0.0
def get_duty_cycle(self):
"""
Returns the PWM duty cycle percentage.
"""
if self._high is not None:
return 100.0 * self._high / self._period
else:
return 0.0
def release(self):
"""
Releases resources.
"""
self._cb.cancel()
class TransmitterSwitch(TransmitterControl):
def __init__(self, input_pin: int, positions: int, weighting=0.0):
"""
positions: the number of positions that the switch has (usually 2 or 3)
"""
super().__init__(input_pin, weighting)
self.positions = positions
# determine the thresholds for the button positions
pwm_min = 988 # Aeroscout 988, Guinea Pig 860
pwm_max = 2010 # Aeroscout 2010, Guinea Pig 2140
increment = (pwm_max - pwm_min) / self.positions
thresh = pwm_min
self.position_thresholds = []
for n in range(self.positions - 1):
thresh += increment
self.position_thresholds.append(thresh)
# initialize the previous position
self.previous_position = self.get_current_position()
def get_current_position(self) -> int:
"""
Returns the current position of the switch
"""
# get the pulse width
pw = self.get_pulse_width()
# correlate the pulse width to a button position
for n in range(self.positions - 1):
if pw < self.position_thresholds[n]:
current_position = n
break
else:
current_position = self.positions - 1
return current_position
def detect_position_change(self) -> int:
"""
Checks if the button position has changed from the previous call.
If so, the new position is returned.
If not, None is returned.
"""
current_position = self.get_current_position()
# check if the position has changed
if current_position == self.previous_position:
new_position = None
else:
new_position = current_position
# update self.previous_position for next call of this function
self.previous_position = current_position
# return the result
return new_position
class ServoHandler(TransmitterControl):
def __init__(self, input_pin, output_pin, fps, min_pw, max_pw, smoothing_dur=0, increments=None, weighting=0.0):
"""
smoothing_dur: the duration (in seconds) of servo reading smoothing.
increments: the number of discrete increments to which the servo can be moved.
"""
super().__init__(input_pin, weighting=0.0)
self.min_pw = min_pw
self.max_pw = max_pw
self.pw_range = self.max_pw - self.min_pw
# define servo
factory = PiGPIOFactory()
self.servo = Servo(output_pin, pin_factory=factory)
# anti-jitter smoothing settings
recent_servo_readings_list_len = int(np.round(fps * smoothing_dur))
if recent_servo_readings_list_len > 1:
self.smoothing = True
self.recent_servo_readings = [0 for n in range(recent_servo_readings_list_len)]
else:
self.smoothing = False
# anti-jitter increments
self.previous_actuated_value = 0 # initialize at center
if increments:
self.incremental_movement = True
# 2 is the full range of servo movement (-1 to 1)
full_range = 2
self.increment_length = full_range / increments
else:
self.incremental_movement = False
def pw_to_servo_value(self, pw) -> float:
if pw < self.min_pw:
servo_value = -1
elif pw > self.max_pw:
servo_value = 1
else:
servo_value = (pw - self.min_pw) / self.pw_range
servo_value = (servo_value - .5) * 2
return servo_value
def actuate(self, servo_value):
# optional anti-jitter filter
if self.incremental_movement:
if abs(servo_value - self.previous_actuated_value) < self.increment_length:
return self.previous_actuated_value
else:
self.previous_actuated_value = servo_value
# Ensure that value is within the acceptable range of -1 to 1
if servo_value > 1:
servo_value = 1
elif servo_value < -1:
servo_value = 1
# actuate servo
self.servo.value = servo_value
# return the value that was actually actuated
return servo_value
def read(self):
pw = self.get_pulse_width()
servo_value = self.pw_to_servo_value(pw)
# optional smoothing
if self.smoothing:
self.recent_servo_readings.append(servo_value)
del self.recent_servo_readings[0]
servo_value = np.average(self.recent_servo_readings)
return servo_value
class TrimReader(TransmitterControl):
def __init__(self, input_pin, pwm_min=990, pwm_max=2013, max_trim=5, weighting=0.0):
super().__init__(input_pin, weighting=0.0)
self.pwm_min = pwm_min
self.pwm_max = pwm_max
self.max_trim = max_trim
self.pwm_range = self.pwm_max - self.pwm_min
self.pwm_half_range = self.pwm_range / 2
self.mid_point = self.pwm_min + self.pwm_half_range
def read(self) -> int:
# get the pulse width
pw = self.get_pulse_width()
trim = (pw - self.mid_point) / self.pwm_half_range * self.max_trim
if trim > self.max_trim:
trim = self.max_trim
elif trim < -1 * self.max_trim:
trim = -1 * self.max_trim
return trim
class PWReader(TransmitterControl):
def __init__(self, input_pin):
"""For diagnostic purposes"""
super().__init__(input_pin, weighting=0.0)
def read(self) -> int:
print('Measuring PW values...')
testing_duration = 10 # seconds
fps = 30
freq = 1 / fps
iterations = int(np.round(testing_duration * fps))
pw = self.get_pulse_width()
min_pw, max_pw = pw, pw
pw_readings = []
for n in range(iterations):
# get the pulse width
pw = self.get_pulse_width()
pw_readings.append(pw)
if pw < min_pw:
min_pw = pw
elif pw > max_pw:
max_pw = pw
sleep(freq)
if n % fps == 0:
average = np.average(pw_readings)
print(f'average: {average}')
print(f'max_pw: {max_pw}')
print(f'min_pw: {min_pw}')
print('--------------------')
average = np.average(pw_readings)
print('--------------------')
print('--------------------')
print('FINAL RESULTS')
print(f'average: {average}')
print(f'max_pw: {max_pw}')
print(f'min_pw: {min_pw}')
print('--------------------')
print('--------------------')
# Demo
if __name__ == '__main__':
pitch_trim_reader = PWReader(25)
pitch_trim_reader.read()