3
3
4
4
from rotarydecoder import RotaryDecoder
5
5
6
+
6
7
class MotorEncoder :
7
- """
8
- Class that handles rotary decoder motors modelisation
8
+ """ Class that handles rotary decoder motors modelisation
9
9
10
10
The support class RotaryDecoder decodes mechanical rotary encoder
11
11
pulses. See the file for more.
12
12
13
13
Every movement method must acquire lock in order not to have
14
- concurrency problems on GPIO READ/WRITE
15
- """
14
+ concurrency problems on GPIO READ/WRITE """
16
15
17
16
# default constructor
18
17
def __init__ (self , pi , enable_pin , forward_pin , backward_pin , encoder_feedback_pin ):
@@ -28,17 +27,15 @@ def __init__(self, pi, enable_pin, forward_pin, backward_pin, encoder_feedback_p
28
27
self ._distance = 0
29
28
self ._ticks = 0
30
29
self ._PWM_value = 0
31
- self ._speed = 0
30
+ self ._encoder_speed = 0
32
31
self ._is_moving = False
33
32
34
33
# other
35
34
self ._callback = self ._pi .callback ()
36
35
self ._motor_lock = threading .RLock ()
37
36
self ._rotary_decoder = RotaryDecoder (pi , encoder_feedback_pin , callback )
38
37
39
-
40
38
# GETTERS
41
-
42
39
# ticks
43
40
def ticks (self ):
44
41
return self ._ticks
@@ -53,16 +50,28 @@ def direction(self):
53
50
54
51
# speed
55
52
def speed (self ):
56
- return self ._speed
53
+ return self ._encoder_speed
57
54
58
55
# is_moving
59
56
def is_moving (self ):
60
57
return self ._is_moving
61
58
62
-
63
59
# MOVEMENT
60
+ """ The stop function acquires the lock to operate on motor
61
+ then writes a 0 on movement pins to stop the motor
62
+ and releases the lock afterwards """
63
+ def control (self , speed = 100.0 ):
64
+ self ._motor_lock .acquire () # acquiring lock
65
+ self ._distance = 0 #resetting distance everytime motor is used
66
+ self ._direction = 1 if speed > 0 else - 1 # setting direction according to speed
67
+
68
+ if (self ._direction ):
69
+ self .stop ()
70
+ self ._pigpio .set_PWM_dutycycle ()
71
+ else
72
+ self .stop ()
73
+ self ._pigpio .set_PWM_dutycycle ()
64
74
65
- def control (self , ):
66
75
67
76
68
77
""" The stop function acquires the lock to operate on motor
@@ -76,8 +85,6 @@ def stop(self):
76
85
self ._motor_lock .release ()
77
86
78
87
# OTHER
79
-
80
88
# callback cancelling
81
89
def cancel_callback (self ):
82
90
self ._rotary_decoder .cancel ()
83
-
0 commit comments