-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrivesystemdutycycle.py
More file actions
314 lines (261 loc) · 11.8 KB
/
drivesystemdutycycle.py
File metadata and controls
314 lines (261 loc) · 11.8 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
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
"""
TODO
"""
from collections import deque
import datetime as dt
import threading
from typing import Tuple, Optional
################################################################################
################################################################################
################################################################################
class DutyCycleTimestamp:
"""
TODO
"""
################################################################################
def __init__(self, mytime : dt.datetime, value : bool ) -> None:
"""
TODO
"""
self.time = mytime
self.value = value # This records what we change to at the specified time!
return
################################################################################
def __str__(self) -> str:
"""
TODO
"""
return f"{self.time.strftime('%H:%M:%S.%f')} : {self.value}"
################################################################################
################################################################################
################################################################################
class DutyCycle(threading.Thread):
"""
TODO
"""
time_step = 0.01
# HERE IS THE TABLE FROM NANOMOTION FOR THE HR4 MOTORS
# This has been simplified a bit - the maximum speed of a motor in the motor box
# is 2000 encoders/s which is 10 mm/s. This effectively means we can neglect
# looking at the velocity variation, and the force will be the determining factor
# for which duty cycle we pick. The layout is 'letter' (as defined in the table),
# the maximum force which should be applied by this particular cycle (interpolated
# slightly to be the maximum force at 10 mm/s for safety), and then the time
# allowed on and total cycle length for air and then vacuum.
# A negative value means no duty cycle should be used.
DUTY_CYCLE_HR4_DICT = {
# L : FMAX , AIR, VACUUM
'A' : [ 1.7, [ -1.0, -1.0 ], [ -1.0, -1.0 ] ],
'B' : [ 5.0, [ -1.0, -1.0 ], [ 184.0, 418.2 ] ],
'C' : [ 7.6, [ -1.0, -1.0 ], [ 107.0, 411.5 ] ],
'D' : [ 9.7, [ -1.0, -1.0 ], [ 72.0, 423.5 ] ],
'E' : [ 11.5, [ 87.0, 111.5 ], [ 55.0, 423.1 ] ],
'F' : [ 13.7, [ 62.0, 110.7 ], [ 39.0, 433.3 ] ],
'G' : [ 14.5, [ 56.0, 112.0 ], [ 35.0, 437.5 ] ]
}
################################################################################
def __init__(self, force : float, environment : str) -> None:
"""
TODO
"""
# Fundamental properties of the duty cycle
self.infinite_run_time = False
self.time_allowed_on, self.total_cycle_length = self.get_duty_cycle_from_force_and_environment(force, environment) # seconds
print(self.time_allowed_on, self.total_cycle_length)
if self.time_allowed_on < 0 or self.total_cycle_length < 0:
self.infinite_run_time = True
self.resume_cycle_after = 1.0 # seconds
# Data management
self.list_of_timestamps = deque() # Container for timestamps and status changes
self.mav = 0.0 # The moving average of the motor's movement
self.is_motor_moving_now = False # This is status of motor's movement right now
self.motor_was_moving_at_cycle_beginning = False # This is status one duty cycle's worth ago
self.is_motor_movement_requested = False # Indicates if the user has requested movement
self.is_motor_resting = False # Indicates if the motor is resting
# Other
self.event = threading.Event() # Used for timeouts that can be cancelled
self.lock = threading.Lock() # Used to ensure data edited properly
# Initialise
super().__init__()
return
################################################################################
def run(self) -> None:
"""
TODO
"""
if self.infinite_run_time:
return
self.is_running = True
ctr = 0
rounding_digits = 2
factor = float(10**rounding_digits)
# Generate initial timestamp
self.stop_motor_moving()
# Start loop
while self.is_running:
# Get current time
start_loop_time = dt.datetime.now()
# GET THE LOCK FOR EDITING DATA
self.lock.acquire()
# Update MAV if we have timestamps in play
if len(self.list_of_timestamps) > 0:
# Remove times from queue if needed
if ( start_loop_time - self.list_of_timestamps[0].time ).total_seconds() > self.total_cycle_length:
self.motor_was_moving_at_cycle_beginning = self.list_of_timestamps[0].value
self.list_of_timestamps.popleft()
# Calculate MAV
if len(self.list_of_timestamps) > 0:
self.mav = 0.0
# Calculate in-between bits
for i in range(0,len(self.list_of_timestamps) - 1):
if self.list_of_timestamps[i].value:
self.mav += ( self.list_of_timestamps[i+1].time - self.list_of_timestamps[i].time ).total_seconds()
# Add to mav if we're currently moving
if self.list_of_timestamps[-1].value:
self.mav += ( start_loop_time - self.list_of_timestamps[-1].time ).total_seconds()
# Add to mav if previous status was TRUE
if self.motor_was_moving_at_cycle_beginning:
self.mav += self.total_cycle_length - ( start_loop_time - self.list_of_timestamps[0].time ).total_seconds()
# Round
self.mav = int( self.mav*factor + 0.5 )/factor
else:
# Set MAV based on status - motor running 100%
if self.motor_was_moving_at_cycle_beginning == True and self.is_motor_moving_now == True:
self.mav = self.total_cycle_length
# Motor running 0%
elif self.motor_was_moving_at_cycle_beginning == False and self.is_motor_moving_now == False:
self.mav = 0.0
# Should not encounter this
else:
raise ValueError("This value should not be possible. Examine code carefully!")
# RELEASE THE LOCK AS WE'RE DONE EDITING
self.lock.release()
# Now check if mav exceeds limits - pause if so
if self.mav >= self.time_allowed_on and self.is_motor_resting == False:
self.is_motor_resting = True
self.stop_motor_moving()
print("Threshold exceeded. Pausing motor")
# Check if we need to resume
if self.mav <= self.time_allowed_on - self.resume_cycle_after and self.is_motor_resting:
self.is_motor_resting = False
if self.is_motor_movement_requested:
self.start_motor_moving()
print("Resuming movement")
else:
print("Rest over, but not moving")
# Sleep a bit
ctr += 1
if ctr % 25 == 0:
print(self.is_motor_movement_requested, self.is_motor_moving_now, self.mav, start_loop_time.strftime('%H:%M:%S.%f'), ", ".join( [str(x) for x in self.list_of_timestamps] ))
end_loop_time = dt.datetime.now()
try:
self.event.wait( timeout = self.time_step - (1e-6)*( end_loop_time - start_loop_time ).microseconds )
except ValueError:
print( f"Requesting sleep for negative time not allowed (time difference is {self.time_step} - {(1e-6)*( end_loop_time - start_loop_time ).microseconds})" )
break
return
################################################################################
def start_motor_moving(self) -> None:
"""
TODO
"""
# Check if requested - don't do anything if it isn't
if self.is_motor_movement_requested == False:
return
# Check if already moving - don't do anything if it is
if self.is_motor_moving_now:
return
# Check if motor is currently paused
if self.is_motor_resting == True:
print("Motor movement has been requested, but motor currently paused. Will start when motor ready")
return
# Prevent editing by others
self.lock.acquire()
# Start moving motor
self.is_motor_moving_now = True
self.list_of_timestamps.append( DutyCycleTimestamp( dt.datetime.now(), True ) )
# Allow editing by others
self.lock.release()
return
################################################################################
def stop_motor_moving(self) -> None:
"""
TODO
"""
# Check if already stopped - don't do anything if it is
if self.is_motor_moving_now == False:
return
# Allow editing
self.lock.acquire()
# Stop moving motor
self.is_motor_moving_now = False
self.list_of_timestamps.append( DutyCycleTimestamp( dt.datetime.now(), False ) )
# Allow editing by others
self.lock.release()
return
################################################################################
def request_motor_movement(self) -> None:
self.is_motor_movement_requested = True
self.start_motor_moving()
return
################################################################################
def tell_motor_to_stop(self) -> None:
self.is_motor_movement_requested = False
self.stop_motor_moving()
return
################################################################################
def kill_thread(self) -> None:
"""
TODO
"""
self.event.set()
self.is_running = False
return
################################################################################
@staticmethod
def get_duty_cycle_from_force_and_environment( force : float, environment : str ):
"""
TODO
"""
# Check environment
if environment == 'air':
index = 1
elif environment == 'vacuum':
index = 2
else:
# ERROR - assume no movement allowed
print(f'\"{environment}\" is invalid. It needs to be \"air\" or \"vacuum\"')
return [0, float('inf')]
# Now get parameters
for value in DutyCycle.DUTY_CYCLE_HR4_DICT.values():
if force <= value[0]:
print(value[index])
return value[index]
# Return error
print('Force exceeds threshold. Will prevent motor\'s movement')
return [0, float('inf')]
def main():
d = DutyCycle(14.0, 'vacuum')
d.start()
while True:
try:
s = input('Press p and then enter\n')
if s == 'p':
if d.is_motor_movement_requested == True:
d.tell_motor_to_stop()
else:
d.request_motor_movement()
else:
print("Not p")
s = None
except KeyboardInterrupt:
d.kill_thread()
break
print(d.is_motor_moving_now)
print(d.motor_was_moving_at_cycle_beginning)
# a = DutyCycleTimestamp(datetime.now(),True)
# b = DutyCycleTimestamp(datetime.now(), False)
# c = [a,b]
# print(", ".join([str(x) for x in c]))
if __name__ == '__main__':
main()