forked from bri3d/TC1791_CAN_BSL
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wavePWM.py
executable file
·392 lines (274 loc) · 10.4 KB
/
wavePWM.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env python
# wavePWM.py
# 2016-10-06
# Public Domain
import pigpio
class PWM:
"""
This class may be used to generate PWM on multiple GPIO
at the same time.
The following diagram illustrates PWM for one GPIO.
1 +------------+ +------------+
| GPIO | | GPIO |
|<--- on --->| |<--- on --->|
| time | | time |
0 -----+ +-------------+ +---------
on^ off^ on^ off^
+--------------------------+--------------------------+
^ ^ ^
|<------ cycle time ------>|<------ cycle time ------>|
cycle cycle cycle
start start start
The underlying PWM frequency is the same for all GPIO and
is the number of cycles per second (known as Hertz).
The frequency may be specified in Hertz or by specifying
the cycle time in microseconds (in which case a frequency
of 1000000 / cycle time is set).
set_frequency(frequency)
set_cycle_time(micros)
The PWM duty cycle (the proportion of on time to cycle time)
and the start of the on time within each cycle may be set on
a GPIO by GPIO basis.
The GPIO PWM duty cycle may be set as a fraction of the
cycle time (0-1.0) or as the on time in microseconds.
set_pulse_length_in_micros(gpio, length)
set_pulse_length_in_fraction(gpio, length)
The GPIO PWM start time within each cycle may be set as
a fraction of the cycle time from the cycle start (0-1.0)
or as the number of microseconds from the start of the cycle.
set_pulse_start_in_micros(gpio, start)
set_pulse_start_in_fraction(gpio, start)
There are two convenience functions to set both settings.
set_pulse_start_and_length_in_micros(gpio, start, length)
set_pulse_start_and_length_in_fraction(gpio, start, length)
It doesn't matter whether you use the micros or fraction
functions. That is a matter of personal choice.
pigpio waves are used to generate the PWM. Note that only
one wave can be transmitted at a time. So if waves are being
used to generate PWM they can't also be used at the same time
for another purpose.
A wave is generated of length 1000000/frequency microseconds.
The GPIO are switched on and off within the wave to set the
duty cycle for each GPIO. The wave is repeatedly transmitted.
Waves have a resolution of one microsecond.
You will only get the requested frequency if it divides
exactly into 1000000.
For example, suppose you want a frequency of 7896 cycles per
second. The wave length will be 1000000/7896 or 126 microseconds
(for an actual frequency of 7936.5) and there will be 126 steps
between off and fully on.
"""
_MAX_GPIO=32
def __init__(self, pi, frequency=1000):
"""
Instantiate with the Pi.
Optionally the frequency may be specified in Hertz (cycles
per second). The frequency defaults to 1000 Hertz.
"""
self.pi = pi
self.frequency = frequency
self.micros = 1000000.0 / frequency
self.used = [False]*self._MAX_GPIO
self.pS = [0.0]*self._MAX_GPIO
self.pL = [0.0]*self._MAX_GPIO
self.old_wid = None
self.stop = False
def set_frequency(self, frequency):
"""
Sets the PWM frequency in Hertz.
The change takes affect when the update function is called.
"""
self.frequency = float(frequency)
self.micros = 1000000.0 / self.frequency
def set_cycle_time(self, micros):
"""
Sets the PWM frequency by specifying the cycle time
in microseconds.
The frequency set will be one million divided by micros.
The change takes affect when the update function is called.
"""
self.micros = float(micros)
self.frequency = 1000000.0 / self.micros
def get_frequency(self):
"""
Returns the frequency in Hertz.
"""
return self.frequency
def get_cycle_length(self):
"""
Returns the cycle length in microseconds.
"""
return self.micros
def set_pulse_length_in_micros(self, gpio, length):
"""
Sets the GPIO on for length microseconds per cycle.
The PWM duty cycle for the GPIO will be:
(length / cycle length in micros) per cycle
The change takes affect when the update function is called.
"""
length %= self.micros
self.pL[gpio] = length / self.micros
if not self.used[gpio]:
self.pi.set_mode(gpio, pigpio.OUTPUT)
self.used[gpio] = True
def set_pulse_length_in_fraction(self, gpio, length):
"""
Sets the GPIO on for length fraction of each cycle.
The GPIO will be on for:
(cycle length in micros * length) microseconds per cycle
The change takes affect when the update function is called.
"""
self.set_pulse_length_in_micros(gpio, self.micros * length)
def set_pulse_start_in_micros(self, gpio, start):
"""
Sets the GPIO high at start micros into each cycle.
The change takes affect when the update function is called.
"""
start %= self.micros
self.pS[gpio] = start / self.micros
if not self.used[gpio]:
self.pi.set_mode(gpio, pigpio.OUTPUT)
self.used[gpio] = True
def set_pulse_start_in_fraction(self, gpio, start):
"""
Sets the GPIO high at start fraction into each cycle.
The change takes affect when the update function is called.
"""
self.set_pulse_start_in_micros(gpio, self.micros * start)
def set_pulse_start_and_length_in_micros(self, gpio, start, length):
"""
Sets the pulse start and length of each pulse in units
of microseconds.
The change takes affect when the update function is called.
"""
self.set_pulse_start_in_micros(gpio, start)
self.set_pulse_length_in_micros(gpio, length)
def set_pulse_start_and_length_in_fraction(self, gpio, start, length):
"""
Sets the pulse start and length of each pulse as a
fraction of the cycle length.
The change takes affect when the update function is called.
"""
self.set_pulse_start_in_fraction(gpio, start)
self.set_pulse_length_in_fraction(gpio, length)
def get_GPIO_settings(self, gpio):
"""
Returns the pulse start and pulse length as a fraction
of the cycle time.
"""
if self.used[gpio]:
return (True, self.pS[gpio], self.pL[gpio])
else:
return (False, 0.0, 0.0)
def update(self):
"""
Updates the PWM for each GPIO to reflect the current settings.
"""
null_wave = True
for g in range(self._MAX_GPIO):
if self.used[g]:
null_wave = False
on = int(self.pS[g] * self.micros)
length = int(self.pL[g] * self.micros)
micros = int(self.micros)
if length <= 0:
self.pi.wave_add_generic([pigpio.pulse(0, 1<<g, micros)])
elif length >= micros:
self.pi.wave_add_generic([pigpio.pulse(1<<g, 0, micros)])
else:
off = (on + length) % micros
if on < off:
self.pi.wave_add_generic([
pigpio.pulse( 0, 1<<g, on),
pigpio.pulse(1<<g, 0, off - on),
pigpio.pulse( 0, 1<<g, micros - off),
])
else:
self.pi.wave_add_generic([
pigpio.pulse(1<<g, 0, off),
pigpio.pulse( 0, 1<<g, on - off),
pigpio.pulse(1<<g, 0, micros - on),
])
if not null_wave:
if not self.stop:
new_wid = self.pi.wave_create()
if self.old_wid is not None:
self.pi.wave_send_using_mode(
new_wid, pigpio.WAVE_MODE_REPEAT_SYNC)
# Spin until the new wave has started.
while self.pi.wave_tx_at() != new_wid:
pass
# It is then safe to delete the old wave.
self.pi.wave_delete(self.old_wid)
else:
self.pi.wave_send_repeat(new_wid)
self.old_wid = new_wid
def cancel(self):
"""
Cancels PWM on the GPIO.
"""
self.stop = True
self.pi.wave_tx_stop()
if self.old_wid is not None:
self.pi.wave_delete(self.old_wid)
if __name__ == "__main__":
import time
import pigpio
import wavePWM
GPIO=[5, 6, 7, 8, 9, 10, 11, 12]
pi = pigpio.pi()
if not pi.connected:
exit(0)
"""
This code demonstrates four different methods of setting
the pulse start and length.
"""
pwm = wavePWM.PWM(pi) # Use default frequency
try:
for i in range(1000, 5400, 200):
if i % 400:
pwm.set_frequency(i)
else:
pwm.set_cycle_time(1000000.0/i)
cl = pwm.get_cycle_length()
# Method 1.
pwm.set_pulse_start_in_micros(5, cl/10)
pwm.set_pulse_length_in_micros(5, cl/2)
# Method 2.
pwm.set_pulse_start_and_length_in_micros(6, cl/10, cl/2)
# Method 3.
pwm.set_pulse_start_in_fraction(7, 0.1)
pwm.set_pulse_length_in_fraction(7, 0.5)
# Method 4.
pwm.set_pulse_start_and_length_in_fraction(8, 0.1, 0.5)
# Method 1.
pwm.set_pulse_start_in_micros(9, 4*cl/10)
pwm.set_pulse_length_in_micros(9, cl/2)
# Method 2.
pwm.set_pulse_start_and_length_in_micros(10, 4*cl/10, cl/2)
# Method 2.
pwm.set_pulse_start_in_fraction(11, 0.4)
pwm.set_pulse_length_in_fraction(11, 0.5)
# Method 4.
pwm.set_pulse_start_and_length_in_fraction(12, 0.4, 0.5)
pwm.update() # Apply all the changes.
time.sleep(0.05)
for g in GPIO:
pwm.set_pulse_length_in_micros(g, 0)
pwm.update()
time.sleep(1)
# Now a servo example
pwm.set_frequency(50)
pos = 0
for g in GPIO:
pwm.set_pulse_start_in_micros(g, pos)
pos += 1000
for i in range(500, 2501, 3):
for g in GPIO:
pwm.set_pulse_length_in_micros(g, i)
pwm.update()
except KeyboardInterrupt:
pass
print("\ntidying up")
pwm.cancel()
pi.stop()