@@ -257,109 +257,109 @@ def _cb_button(self, gpio, level, tick):
257
257
cb ()
258
258
259
259
class MotorEncoder (object ):
260
- def __init__ (self , parent , _pigpio , pin_enable , pin_forward , pin_backward , pin_encoder ):
261
- self ._parent = parent
262
- self ._pigpio = _pigpio
263
- self ._pin_enable = pin_enable
264
- self ._pin_forward = pin_forward
265
- self ._pin_backward = pin_backward
266
- self ._pin_encoder = pin_encoder
267
- self ._direction = False
268
- self ._pin_duty = 0
269
- self ._pin_reverse = 0
270
- self ._power = 0.0
271
- self ._power_actual = 0.0
272
- self ._encoder_dist = 0
273
- self ._encoder_speed = 0.0
274
- self ._encoder_last_tick = 0
275
- self ._encoder_dist_target = 0
276
- self ._encoder_speed_target = 0.0
277
- self ._encoder_k_s_1 = 20
278
- self ._encoder_k_v_1 = 80
279
- self ._motor_stopping = False
280
- self ._motor_running = False
281
- self ._motor_stop_fast = True
282
- self ._pigpio .set_mode (self ._pin_encoder , pigpio .INPUT )
283
- self ._cb = self ._pigpio .callback (self ._pin_encoder , pigpio .RISING_EDGE , self ._cb_encoder )
284
- self ._motor_lock = threading .RLock ()
285
-
286
- def exit (self ):
287
- self ._cb .cancel ()
288
-
289
- def _cb_encoder (self , gpio , level , tick ):
290
- self ._motor_lock .acquire ()
291
- self ._encoder_dist += 1
292
- delta_ticks = tick - self ._encoder_last_tick if tick > self ._encoder_last_tick else tick - self ._encoder_last_tick + 4294967295
293
- self ._encoder_last_tick = tick
294
- self ._encoder_speed = 1000000.0 / delta_ticks #convert speed in steps per second
295
- #print "pin: " + str(self._pin_forward) + " dist: " + str(self._encoder_dist) + " target: " + str(self._encoder_dist_target)
296
- if self ._encoder_dist_target >= 0 and self ._motor_stop_fast :
297
- #delta_s is the delta (in steps)before the target to reverse the motor in order to arrive at target
298
- delta_s = max (min (self ._encoder_speed / self ._encoder_k_s_1 , 100 ), 0 )
299
- #print "pin: " + str(self._pin_forward) + " dist: " + str(self._encoder_dist) + " target: " + str(self._encoder_dist_target) + " delta_s: " + str(delta_s)
300
- if (self ._encoder_dist >= self ._encoder_dist_target - delta_s and
301
- not self ._motor_stopping and self ._motor_running ):
302
- self ._motor_stopping = True
303
- self ._pigpio .write (self ._pin_duty , 0 )
304
- self ._pigpio .set_PWM_dutycycle (self ._pin_reverse , self ._power )
305
- elif (self ._motor_running and
306
- ((self ._motor_stopping and
307
- self ._encoder_speed < self ._encoder_k_v_1 ) or
308
- (self ._motor_stopping and
309
- self ._encoder_dist >= self ._encoder_dist_target ))):
310
- self .stop ()
311
- logging .info ("dist: " + str (self ._encoder_dist ) + " speed: " + str (self ._encoder_speed ))
312
- if self ._encoder_dist_target >= 0 and not self ._motor_stop_fast :
313
- if self ._encoder_dist >= self ._encoder_dist_target :
314
- self .stop ()
315
- self ._parent ._cb_encoder (self , gpio , level , tick )
316
- self ._motor_lock .release ()
317
- if not self ._motor_running :
318
- self ._parent ._check_complete ()
319
-
320
- def control (self , power = 100.0 , elapse = - 1 , speed = 100.0 , steps = - 1 ):
321
- self ._motor_lock .acquire ()
322
- self ._direction = speed > 0
323
- self ._encoder_dist_target = steps
324
- self ._motor_stopping = False
325
- self ._motor_running = True
326
- self ._encoder_dist = 0
327
- self ._encoder_speed_target = abs (speed )
328
- self ._power = abs (power ) #TODO: initial power must be a function of desired speed
329
- self ._power_actual = abs (power ) #TODO: initial power must be a function of desired speed
330
- self ._pin_duty = self ._pin_forward if self ._direction else self ._pin_backward
331
- self ._pin_reverse = self ._pin_backward if self ._direction else self ._pin_forward
332
- self ._pigpio .write (self ._pin_reverse , 0 )
333
- self ._pigpio .set_PWM_dutycycle (self ._pin_duty , self ._power )
334
- self ._pigpio .write (self ._pin_enable , True )
335
- self ._motor_lock .release ()
336
- if elapse > 0 :
337
- time .sleep (elapse )
338
- self .stop ()
339
-
340
- def stop (self ):
341
- self ._motor_lock .acquire ()
342
- self ._motor_stopping = False
343
- self ._motor_running = False
344
- self ._pigpio .write (self ._pin_forward , 0 )
345
- self ._pigpio .write (self ._pin_backward , 0 )
346
- self ._motor_lock .release ()
347
-
348
- def distance (self ):
349
- return self ._encoder_dist
350
-
351
- def speed (self ):
352
- return self ._encoder_speed
353
-
354
- def stopping (self ):
355
- return self ._motor_stopping
356
-
357
- def running (self ):
358
- return self ._motor_running
359
-
360
- def adjust_power (self , power_delta ):
361
- self ._power_actual = min (max (self ._power + power_delta , 0 ), 100 )
362
- self ._pigpio .set_PWM_dutycycle (self ._pin_duty , self ._power_actual )
260
+ # def __init__(self, parent, _pigpio, pin_enable, pin_forward, pin_backward, pin_encoder):
261
+ # self._parent = parent
262
+ # # self._pigpio = _pigpio
263
+ # # self._pin_enable = pin_enable
264
+ # # self._pin_forward = pin_forward
265
+ # # self._pin_backward = pin_backward
266
+ # # self._pin_encoder = pin_encoder
267
+ # # self._direction = False
268
+ # self._pin_duty = 0
269
+ # self._pin_reverse = 0
270
+ # self._power = 0.0
271
+ # # self._power_actual = 0.0
272
+ # # self._encoder_dist = 0
273
+ # # self._encoder_speed = 0.0
274
+ # self._encoder_last_tick = 0
275
+ # self._encoder_dist_target = 0
276
+ # self._encoder_speed_target = 0.0
277
+ # self._encoder_k_s_1 = 20
278
+ # self._encoder_k_v_1 = 80
279
+ # self._motor_stopping = False
280
+ # self._motor_running = False
281
+ # self._motor_stop_fast = True
282
+ # # self._pigpio.set_mode(self._pin_encoder, pigpio.INPUT)
283
+ # # self._cb = self._pigpio.callback(self._pin_encoder, pigpio.RISING_EDGE, self._cb_encoder)
284
+ # # self._motor_lock = threading.RLock()
285
+
286
+ # def exit(self):
287
+ # self._cb.cancel()
288
+
289
+ def _cb_encoder (self , gpio , level , tick ):
290
+ self ._motor_lock .acquire ()
291
+ self ._encoder_dist += 1
292
+ delta_ticks = tick - self ._encoder_last_tick if tick > self ._encoder_last_tick else tick - self ._encoder_last_tick + 4294967295
293
+ self ._encoder_last_tick = tick
294
+ self ._encoder_speed = 1000000.0 / delta_ticks #convert speed in steps per second
295
+ #print "pin: " + str(self._pin_forward) + " dist: " + str(self._encoder_dist) + " target: " + str(self._encoder_dist_target)
296
+ if self ._encoder_dist_target >= 0 and self ._motor_stop_fast :
297
+ #delta_s is the delta (in steps)before the target to reverse the motor in order to arrive at target
298
+ delta_s = max (min (self ._encoder_speed / self ._encoder_k_s_1 , 100 ), 0 )
299
+ #print "pin: " + str(self._pin_forward) + " dist: " + str(self._encoder_dist) + " target: " + str(self._encoder_dist_target) + " delta_s: " + str(delta_s)
300
+ if (self ._encoder_dist >= self ._encoder_dist_target - delta_s and
301
+ not self ._motor_stopping and self ._motor_running ):
302
+ self ._motor_stopping = True
303
+ self ._pigpio .write (self ._pin_duty , 0 )
304
+ self ._pigpio .set_PWM_dutycycle (self ._pin_reverse , self ._power )
305
+ elif (self ._motor_running and
306
+ ((self ._motor_stopping and
307
+ self ._encoder_speed < self ._encoder_k_v_1 ) or
308
+ (self ._motor_stopping and
309
+ self ._encoder_dist >= self ._encoder_dist_target ))):
310
+ self .stop ()
311
+ logging .info ("dist: " + str (self ._encoder_dist ) + " speed: " + str (self ._encoder_speed ))
312
+ if self ._encoder_dist_target >= 0 and not self ._motor_stop_fast :
313
+ if self ._encoder_dist >= self ._encoder_dist_target :
314
+ self .stop ()
315
+ self ._parent ._cb_encoder (self , gpio , level , tick )
316
+ self ._motor_lock .release ()
317
+ if not self ._motor_running :
318
+ self ._parent ._check_complete ()
319
+
320
+ def control (self , power = 100.0 , elapse = - 1 , speed = 100.0 , steps = - 1 ):
321
+ self ._motor_lock .acquire ()
322
+ self ._direction = speed > 0
323
+ self ._encoder_dist_target = steps
324
+ self ._motor_stopping = False
325
+ self ._motor_running = True
326
+ self ._encoder_dist = 0
327
+ self ._encoder_speed_target = abs (speed )
328
+ self ._power = abs (power ) #TODO: initial power must be a function of desired speed
329
+ self ._power_actual = abs (power ) #TODO: initial power must be a function of desired speed
330
+ self ._pin_duty = self ._pin_forward if self ._direction else self ._pin_backward
331
+ self ._pin_reverse = self ._pin_backward if self ._direction else self ._pin_forward
332
+ self ._pigpio .write (self ._pin_reverse , 0 )
333
+ self ._pigpio .set_PWM_dutycycle (self ._pin_duty , self ._power )
334
+ self ._pigpio .write (self ._pin_enable , True )
335
+ self ._motor_lock .release ()
336
+ if elapse > 0 :
337
+ time .sleep (elapse )
338
+ self .stop ()
339
+
340
+ # def stop(self):
341
+ # self._motor_lock.acquire()
342
+ # self._motor_stopping = False
343
+ # self._motor_running = False
344
+ # self._pigpio.write(self._pin_forward, 0)
345
+ # self._pigpio.write(self._pin_backward, 0)
346
+ # self._motor_lock.release()
347
+
348
+ # def distance(self):
349
+ # return self._encoder_dist
350
+
351
+ # def speed(self):
352
+ # return self._encoder_speed
353
+
354
+ def stopping (self ):
355
+ return self ._motor_stopping
356
+
357
+ def running (self ):
358
+ return self ._motor_running
359
+
360
+ def adjust_power (self , power_delta ):
361
+ self ._power_actual = min (max (self ._power + power_delta , 0 ), 100 )
362
+ self ._pigpio .set_PWM_dutycycle (self ._pin_duty , self ._power_actual )
363
363
364
364
class TwinMotorsEncoder (object ):
365
365
def __init__ (self , apigpio , pin_enable , pin_forward_left , pin_backward_left , pin_encoder_left , pin_forward_right , pin_backward_right , pin_encoder_right ):
0 commit comments