-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDRVS.py
More file actions
185 lines (161 loc) · 5.38 KB
/
Copy pathDRVS.py
File metadata and controls
185 lines (161 loc) · 5.38 KB
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
from machine import Pin, PWM
from time import sleep_ms
class DRV8833:
FAST_DECAY = 0
SLOW_DECAY = 1
MAX_DUTY_CYCLE = 0xFFFF # 65535
MIN_DUTY_CYCLE = 0
def throttle_a(self, throttle: float, decay_mode: int = SLOW_DECAY):
self.__throttle(self.motor_a_in_1, self.motor_a_in_2, throttle, decay_mode)
def throttle_b(self, throttle: float, decay_mode: int = SLOW_DECAY):
self.__throttle(self.motor_b_in_1, self.motor_b_in_2, throttle, decay_mode)
def stop_a(self, hard: bool = False):
if hard:
self.throttle_a(0.0)
else:
self.__stop(self.motor_a_in_1, self.motor_a_in_2)
def stop_b(self, hard: bool = False):
if hard:
self.throttle_b(0.0)
else:
self.__stop(self.motor_b_in_1, self.motor_b_in_2)
def deinit(self):
self.stop_a()
self.stop_b()
self.motor_a_in_1.deinit()
self.motor_a_in_2.deinit()
self.motor_b_in_1.deinit()
self.motor_b_in_2.deinit()
pass
def __stop(self, pin1: PWM, pin2: PWM):
pin1.duty_u16(self.MIN_DUTY_CYCLE)
pin2.duty_u16(self.MIN_DUTY_CYCLE)
def __throttle(self, pin1: PWM, pin2: PWM, throttle: float, decay_mode: int):
if not -1.0 <= throttle <= 1.0:
raise ValueError("Throttle value is out of range [ -1, 1 ]!")
duty_cycle = int(abs(throttle) * self.MAX_DUTY_CYCLE)
if throttle == 0:
self.__stop(pin1, pin2)
if decay_mode == self.SLOW_DECAY:
if 0 < throttle <= 1:
pin1.duty_u16(self.MAX_DUTY_CYCLE - duty_cycle)
pin2.duty_u16(self.MAX_DUTY_CYCLE)
elif -1 <= throttle < 0:
pin1.duty_u16(self.MAX_DUTY_CYCLE)
pin2.duty_u16(self.MAX_DUTY_CYCLE - duty_cycle)
elif decay_mode == self.FAST_DECAY:
if 0 < throttle <= 1:
pin1.duty_u16(self.MIN_DUTY_CYCLE)
pin2.duty_u16(duty_cycle)
elif -1 <= throttle < 0:
pin1.duty_u16(duty_cycle)
pin2.duty_u16(self.MIN_DUTY_CYCLE)
else:
raise ValueError(
"decay_mode must be one of either FAST_DECAY or SLOW_DECAY!"
)
def __init__(self, ain1, ain2: PWM, bin1: PWM, bin2: PWM) -> None:
self.motor_a_in_1 = ain1
self.motor_a_in_2 = ain2
self.motor_b_in_1 = bin1
self.motor_b_in_2 = bin2
def __del__(self):
self.deinit()
class DRVS:
pos = 0
seq_pos = 0
delay = 5
seq = {}
def __init__(self, pins=[], throttle=[-1,1], limit=[0, 99999], wait=delay, slow_decay=False, frq=50000):
self.seq_pos = 0
self.pos = 0
self.min = limit[0]
self.max = limit[1]
self.low = throttle[0]
self.high = throttle[1]
self.delay = wait
self.seq = {
0: {
0: self.high,
1: self.high
},
1: {
0: self.low,
1: self.high
},
2: {
0: self.low,
1: self.low
},
3: {
0: self.high,
1: self.low
}
}
self.drv = DRV8833(
PWM(Pin(pins[0], Pin.OUT), freq=frq),
PWM(Pin(pins[1], Pin.OUT), freq=frq),
PWM(Pin(pins[2], Pin.OUT), freq=frq),
PWM(Pin(pins[3], Pin.OUT), freq=frq)
)
def seq_set(self):
self.seq = {
0: {
0: self.high,
1: self.high
},
1: {
0: self.low,
1: self.high
},
2: {
0: self.low,
1: self.low
},
3: {
0: self.high,
1: self.low
}
}
def seq_n(self, direction=1):
if direction == 1:
if self.seq_pos >= 3:
return 0
else:
return self.seq_pos + 1
else:
if self.seq_pos <= 0:
return 3
else:
return self.seq_pos - 1
def step(self, steps=1, direction=1, wait=delay, halt=False):
self.seq_set()
remaining_steps = steps
for _ in range(steps):
if self.pos+direction >= self.min and self.pos+direction <= self.max:
thisStep = self.seq_n(direction)
self.drv.throttle_a(self.seq[thisStep][0])
self.drv.throttle_b(self.seq[thisStep][1])
self.seq_pos = thisStep
self.pos+=direction
remaining_steps -= 1
sleep_ms(wait)
else:
exceeded = "Min"
if self.pos+direction >= self.max:
exceeded = "Max"
raise ValueError(
exceeded + " travel reached. Could not complete " + str(remaining_steps) + " remaining steps."
)
break
if halt == True:
self.drv.stop_a()
self.drv.stop_b()
return self.pos
def sleep(self, wt=wait):
sleep_ms(wt)
def halt(self):
self.drv.stop_a()
self.drv.stop_b()
def release(self):
self.drv.deinit()