/
decoder.py
executable file
·114 lines (96 loc) · 3.94 KB
/
decoder.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2020-2021 by Murray Altheim. All rights reserved. This file is part
# of the Robot Operating System project, released under the MIT License. Please
# see the LICENSE file included as part of this package.
#
# author: Murray Altheim
# created: 2020-01-18
# modified: 2020-08-30
#
from colorama import init, Fore, Style
init()
try:
import pigpio
except ImportError as ie:
print(Fore.RED + "This script requires the pigpio module.\n"\
+ Fore.YELLOW + "Install with: pip3 install --user pigpio" + Style.RESET_ALL)
import mock.pigpio as pigpio
from lib.logger import Logger
class Decoder(object):
'''
Class to decode mechanical rotary encoder pulses, implemented
using pigpio's pi.callback() method.
Decodes the rotary encoder A and B pulses, e.g.:
+---------+ +---------+ 0
| | | |
A | | | |
| | | |
+---------+ +---------+ +----- 1
+---------+ +---------+ 0
| | | |
B | | | |
| | | |
----+ +---------+ +---------+ 1
'''
# ..........................................................................
def __init__(self, pi, orientation, gpio_a, gpio_b, callback, level):
'''
Instantiate the class with the pi and gpios connected to
rotary encoder contacts A and B. The common contact should
be connected to ground. The callback is called when the
rotary encoder is turned. It takes one parameter which is
+1 for clockwise and -1 for counterclockwise.
EXAMPLE
from lib.decoder import Decoder
def my_callback(self, step):
self._steps += step
pin_a = 17
pin_b = 18
decoder = Decoder(pi, pin_a, pin_b, my_callback)
...
decoder.cancel()
:param pi: the Pigpio connection to the Raspberry Pi
:param orientation: the motor orientation
:param gpio_a: pin number for A
:param gpio_b: pin number for B
:param callback: the callback method
:param level: the log Level
'''
self._log = Logger('enc:{}'.format(orientation.label), level)
self._pi = pi
self._gpio_a = gpio_a
self._gpio_b = gpio_b
self._log.info('pin A: {:d}; pin B: {:d}'.format(self._gpio_a,self._gpio_b))
self._callback = callback
self._level_a = 0
self._level_b = 0
self._last_gpio = None
self._pi.set_mode(self._gpio_a, pigpio.INPUT)
self._pi.set_mode(self._gpio_b, pigpio.INPUT)
self._pi.set_pull_up_down(self._gpio_a, pigpio.PUD_UP)
self._pi.set_pull_up_down(self._gpio_b, pigpio.PUD_UP)
# _edge = pigpio.RISING_EDGE # default
# _edge = pigpio.FALLING_EDGE
_edge = pigpio.EITHER_EDGE
self.callback_a = self._pi.callback(self._gpio_a, _edge, self._pulse_a)
self.callback_b = self._pi.callback(self._gpio_b, _edge, self._pulse_b)
# ..........................................................................
def _pulse_a(self, gpio, level, tick):
self._level_a = level
if level == 1 and self._level_b == 1:
self._callback(1)
# ..........................................................................
def _pulse_b(self, gpio, level, tick):
self._level_b = level;
if level == 1 and self._level_a == 1:
self._callback(-1)
# ..........................................................................
def cancel(self):
'''
Cancel the rotary encoder decoder.
'''
self.callback_a.cancel()
self.callback_b.cancel()
#EOF