Skip to content

Commit 9d8cd9a

Browse files
committed
Added support for flipping encoder direction
1 parent 9b4e830 commit 9d8cd9a

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

XRPLib/encoder.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Encoder:
99
_counts_per_motor_shaft_revolution = 12
1010
resolution = _counts_per_motor_shaft_revolution * _gear_ratio # 585
1111

12-
def __init__(self, index, encAPin: int|str, encBPin: int|str):
12+
def __init__(self, index, encAPin: int|str, encBPin: int|str, flip_dir:bool=False):
1313
"""
1414
Uses the on board PIO State Machine to keep track of encoder positions.
1515
Only 4 encoders can be instantiated this way.
@@ -28,6 +28,7 @@ def __init__(self, index, encAPin: int|str, encBPin: int|str):
2828
self.sm = rp2.StateMachine(index, self._encoder, in_base=basePin)
2929
self.reset_encoder_position()
3030
self.sm.active(1)
31+
self.flip_dir = flip_dir
3132

3233
def reset_encoder_position(self):
3334
"""
@@ -52,6 +53,10 @@ def get_position_counts(self):
5253
counts = self.sm.get()
5354
if(counts > 2**31):
5455
counts -= 2**32
56+
57+
if self.flip_dir:
58+
counts *= -1
59+
5560
return counts
5661

5762
def get_position(self):

0 commit comments

Comments
 (0)