lib/QEI/QEI.h

Sat, 07 Nov 2020 19:23:21 +0100

author
David Douard <david.douard@sdf3.org>
date
Sat, 07 Nov 2020 19:23:21 +0100
changeset 47
11c57010e4f9
parent 27
8f2be7aaec00
permissions
-rw-r--r--

Attempt to improve the detection of packet collisions (not fixed yet)

27
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
1 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
2 * @author Aaron Berk
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
3 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
4 * @section LICENSE
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
5 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
6 * Copyright (c) 2010 ARM Limited
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
7 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
9 * of this software and associated documentation files (the "Software"), to deal
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
10 * in the Software without restriction, including without limitation the rights
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
12 * copies of the Software, and to permit persons to whom the Software is
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
13 * furnished to do so, subject to the following conditions:
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
14 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
15 * The above copyright notice and this permission notice shall be included in
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
16 * all copies or substantial portions of the Software.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
17 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
24 * THE SOFTWARE.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
25 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
26 * @section DESCRIPTION
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
27 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
28 * Quadrature Encoder Interface.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
29 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
30 * A quadrature encoder consists of two code tracks on a disc which are 90
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
31 * degrees out of phase. It can be used to determine how far a wheel has
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
32 * rotated, relative to a known starting position.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
33 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
34 * Only one code track changes at a time leading to a more robust system than
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
35 * a single track, because any jitter around any edge won't cause a state
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
36 * change as the other track will remain constant.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
37 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
38 * Encoders can be a homebrew affair, consisting of infrared emitters/receivers
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
39 * and paper code tracks consisting of alternating black and white sections;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
40 * alternatively, complete disk and PCB emitter/receiver encoder systems can
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
41 * be bought, but the interface, regardless of implementation is the same.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
42 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
43 * +-----+ +-----+ +-----+
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
44 * Channel A | ^ | | | | |
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
45 * ---+ ^ +-----+ +-----+ +-----
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
46 * ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
47 * ^ +-----+ +-----+ +-----+
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
48 * Channel B ^ | | | | | |
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
49 * ------+ +-----+ +-----+ +-----
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
50 * ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
51 * ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
52 * 90deg
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
53 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
54 * The interface uses X2 encoding by default which calculates the pulse count
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
55 * based on reading the current state after each rising and falling edge of
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
56 * channel A.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
57 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
58 * +-----+ +-----+ +-----+
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
59 * Channel A | | | | | |
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
60 * ---+ +-----+ +-----+ +-----
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
61 * ^ ^ ^ ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
62 * ^ +-----+ ^ +-----+ ^ +-----+
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
63 * Channel B ^ | ^ | ^ | ^ | ^ | |
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
64 * ------+ ^ +-----+ ^ +-----+ +--
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
65 * ^ ^ ^ ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
66 * ^ ^ ^ ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
67 * Pulse count 0 1 2 3 4 5 ...
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
68 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
69 * This interface can also use X4 encoding which calculates the pulse count
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
70 * based on reading the current state after each rising and falling edge of
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
71 * either channel.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
72 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
73 * +-----+ +-----+ +-----+
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
74 * Channel A | | | | | |
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
75 * ---+ +-----+ +-----+ +-----
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
76 * ^ ^ ^ ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
77 * ^ +-----+ ^ +-----+ ^ +-----+
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
78 * Channel B ^ | ^ | ^ | ^ | ^ | |
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
79 * ------+ ^ +-----+ ^ +-----+ +--
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
80 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
81 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
82 * Pulse count 0 1 2 3 4 5 6 7 8 9 ...
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
83 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
84 * It defaults
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
85 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
86 * An optional index channel can be used which determines when a full
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
87 * revolution has occured.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
88 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
89 * If a 4 pules per revolution encoder was used, with X4 encoding,
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
90 * the following would be observed.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
91 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
92 * +-----+ +-----+ +-----+
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
93 * Channel A | | | | | |
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
94 * ---+ +-----+ +-----+ +-----
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
95 * ^ ^ ^ ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
96 * ^ +-----+ ^ +-----+ ^ +-----+
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
97 * Channel B ^ | ^ | ^ | ^ | ^ | |
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
98 * ------+ ^ +-----+ ^ +-----+ +--
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
99 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
100 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
101 * ^ ^ ^ +--+ ^ ^ +--+ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
102 * ^ ^ ^ | | ^ ^ | | ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
103 * Index ------------+ +--------+ +-----------
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
104 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
105 * Pulse count 0 1 2 3 4 5 6 7 8 9 ...
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
106 * Rev. count 0 1 2
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
107 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
108 * Rotational position in degrees can be calculated by:
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
109 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
110 * (pulse count / X * N) * 360
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
111 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
112 * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
113 * of pulses per revolution.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
114 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
115 * Linear position can be calculated by:
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
116 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
117 * (pulse count / X * N) * (1 / PPI)
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
118 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
119 * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
120 * pulses per revolution, and PPI is pulses per inch, or the equivalent for
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
121 * any other unit of displacement. PPI can be calculated by taking the
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
122 * circumference of the wheel or encoder disk and dividing it by the number
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
123 * of pulses per revolution.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
124 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
125
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
126 #ifndef QEI_H
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
127 #define QEI_H
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
128
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
129 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
130 * Includes
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
131 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
132 #include "mbed.h"
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
133
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
134 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
135 * Defines
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
136 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
137 #define PREV_MASK 0x1 //Mask for the previous state in determining direction
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
138 //of rotation.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
139 #define CURR_MASK 0x2 //Mask for the current state in determining direction
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
140 //of rotation.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
141 #define INVALID 0x3 //XORing two states where both bits have changed.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
142
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
143 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
144 * Quadrature Encoder Interface.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
145 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
146 class QEI {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
147
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
148 public:
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
149
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
150 typedef enum Encoding {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
151
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
152 X2_ENCODING,
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
153 X4_ENCODING
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
154
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
155 } Encoding;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
156
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
157 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
158 * Constructor.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
159 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
160 * Reads the current values on channel A and channel B to determine the
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
161 * initial state.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
162 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
163 * Attaches the encode function to the rise/fall interrupt edges of
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
164 * channels A and B to perform X4 encoding.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
165 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
166 * Attaches the index function to the rise interrupt edge of channel index
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
167 * (if it is used) to count revolutions.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
168 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
169 * @param channelA mbed pin for channel A input.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
170 * @param channelB mbed pin for channel B input.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
171 * @param index mbed pin for optional index channel input,
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
172 * (pass NC if not needed).
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
173 * @param pulsesPerRev Number of pulses in one revolution.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
174 * @param encoding The encoding to use. Uses X2 encoding by default. X2
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
175 * encoding uses interrupts on the rising and falling edges
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
176 * of only channel A where as X4 uses them on both
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
177 * channels.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
178 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
179 QEI(PinName channelA, PinName channelB, PinName index,
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
180 int pulsesPerRev, Encoding encoding = X2_ENCODING,
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
181 const event_callback_t& ev_callback=NULL);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
182
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
183 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
184 * Reset the encoder.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
185 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
186 * Sets the pulses and revolutions count to zero.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
187 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
188 void reset(void);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
189
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
190 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
191 * Read the state of the encoder.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
192 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
193 * @return The current state of the encoder as a 2-bit number, where:
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
194 * bit 1 = The reading from channel B
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
195 * bit 2 = The reading from channel A
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
196 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
197 int getCurrentState(void);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
198
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
199 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
200 * Read the number of pulses recorded by the encoder.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
201 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
202 * @return Number of pulses which have occured.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
203 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
204 int getPulses(void);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
205
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
206 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
207 * Read the number of revolutions recorded by the encoder on the index channel.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
208 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
209 * @return Number of revolutions which have occured on the index channel.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
210 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
211 int getRevolutions(void);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
212
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
213 void attach(const event_callback_t& callback=NULL) {
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
214 _callback = callback;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
215 };
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
216
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
217 private:
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
218
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
219 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
220 * Update the pulse count.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
221 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
222 * Called on every rising/falling edge of channels A/B.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
223 *
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
224 * Reads the state of the channels and determines whether a pulse forward
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
225 * or backward has occured, updating the count appropriately.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
226 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
227 void encode(void);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
228
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
229 /**
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
230 * Called on every rising edge of channel index to update revolution
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
231 * count by one.
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
232 */
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
233 void index(void);
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
234
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
235 Encoding encoding_;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
236
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
237 InterruptIn channelA_;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
238 InterruptIn channelB_;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
239 InterruptIn index_;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
240
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
241 int pulsesPerRev_;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
242 int prevState_;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
243 int currState_;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
244
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
245 volatile int pulses_;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
246 volatile int revolutions_;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
247 event_callback_t _callback;
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
248
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
249 };
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
250
8f2be7aaec00 add QEI
David Douard <david.douard@sdfa3.org>
parents:
diff changeset
251 #endif /* QEI_H */

mercurial