Skip to content

Commit 38a838c

Browse files
committed
rotary encoder class
1 parent 4aa21e0 commit 38a838c

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

rotary_encoder/motorencoder.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
from rotarydecoder import RotaryDecoder
55

6+
67
class MotorEncoder:
7-
"""
8-
Class that handles rotary decoder motors modelisation
8+
""" Class that handles rotary decoder motors modelisation
99
1010
The support class RotaryDecoder decodes mechanical rotary encoder
1111
pulses. See the file for more.
1212
1313
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 """
1615

1716
# default constructor
1817
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
2827
self._distance = 0
2928
self._ticks = 0
3029
self._PWM_value = 0
31-
self._speed = 0
30+
self._encoder_speed = 0
3231
self._is_moving = False
3332

3433
# other
3534
self._callback = self._pi.callback()
3635
self._motor_lock = threading.RLock()
3736
self._rotary_decoder = RotaryDecoder(pi, encoder_feedback_pin, callback)
3837

39-
4038
# GETTERS
41-
4239
# ticks
4340
def ticks(self):
4441
return self._ticks
@@ -53,16 +50,28 @@ def direction(self):
5350

5451
# speed
5552
def speed(self):
56-
return self._speed
53+
return self._encoder_speed
5754

5855
# is_moving
5956
def is_moving(self):
6057
return self._is_moving
6158

62-
6359
# 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()
6474

65-
def control(self, ):
6675

6776

6877
""" The stop function acquires the lock to operate on motor
@@ -76,8 +85,6 @@ def stop(self):
7685
self._motor_lock.release()
7786

7887
# OTHER
79-
8088
# callback cancelling
8189
def cancel_callback(self):
8290
self._rotary_decoder.cancel()
83-

0 commit comments

Comments
 (0)