User | Revision | Line number | New contents of line |
kaushalpkk |
0:6b30097d9a45
|
1
|
/*
|
kaushalpkk |
0:6b30097d9a45
|
2
|
name: encoder.h
|
kaushalpkk |
0:6b30097d9a45
|
3
|
date: 04-june-2011
|
kaushalpkk |
0:6b30097d9a45
|
4
|
header description: contains functions and external variables to interface quadrature encoder
|
kaushalpkk |
0:6b30097d9a45
|
5
|
compiler-library: the mbed library interface is used on the whole..
|
kaushalpkk |
0:6b30097d9a45
|
6
|
compatability: can also be compatable with generalised code with diffrerent hardware and software
|
kaushalpkk |
0:6b30097d9a45
|
7
|
hardware dependency: works on rover 5 model platform interfaced with mbed controller
|
kaushalpkk |
0:6b30097d9a45
|
8
|
|
kaushalpkk |
0:6b30097d9a45
|
9
|
indentation: <mbed default intendation - 4 spaces>
|
kaushalpkk |
0:6b30097d9a45
|
10
|
resources: http://www.sparkfun.com/products/10336
|
kaushalpkk |
0:6b30097d9a45
|
11
|
http://letsmakerobots.com/node/24031
|
kaushalpkk |
0:6b30097d9a45
|
12
|
http://www.sparkfun.com/datasheets/Robotics/Rover%205%20Introduction.pdf
|
kaushalpkk |
0:6b30097d9a45
|
13
|
*/
|
kaushalpkk |
0:6b30097d9a45
|
14
|
#include "mbed.h"
|
kaushalpkk |
0:6b30097d9a45
|
15
|
#include "quadratureEncoder.h"
|
kaushalpkk |
0:6b30097d9a45
|
16
|
//gets a bit position by shifting 1
|
kaushalpkk |
0:6b30097d9a45
|
17
|
#define Abit 0
|
kaushalpkk |
0:6b30097d9a45
|
18
|
#define Bbit 1
|
kaushalpkk |
0:6b30097d9a45
|
19
|
|
kaushalpkk |
0:6b30097d9a45
|
20
|
#define _BV(x) (1 << x)
|
kaushalpkk |
0:6b30097d9a45
|
21
|
|
kaushalpkk |
0:6b30097d9a45
|
22
|
// sbi sets a particular bit and cbi clears the bit (target byte-word, bit position)
|
kaushalpkk |
0:6b30097d9a45
|
23
|
#define sbi(y,x) y|=_BV(x)
|
kaushalpkk |
0:6b30097d9a45
|
24
|
#define cbi(y,x) y&=~_BV(x)
|
kaushalpkk |
0:6b30097d9a45
|
25
|
|
kaushalpkk |
0:6b30097d9a45
|
26
|
quadratureEncoder::quadratureEncoder(PinName pinA, PinName pinB):
|
kaushalpkk |
0:6b30097d9a45
|
27
|
_pinA(pinA), _pinB(pinB){
|
kaushalpkk |
0:6b30097d9a45
|
28
|
_pinA.rise(this, &quadratureEncoder::ARise);
|
kaushalpkk |
0:6b30097d9a45
|
29
|
_pinA.fall(this, &quadratureEncoder::AFall);
|
kaushalpkk |
0:6b30097d9a45
|
30
|
_pinB.rise(this, &quadratureEncoder::BRise);
|
kaushalpkk |
0:6b30097d9a45
|
31
|
_pinB.fall(this, &quadratureEncoder::BFall);
|
kaushalpkk |
0:6b30097d9a45
|
32
|
|
kaushalpkk |
0:6b30097d9a45
|
33
|
_oldState = 0x00;
|
kaushalpkk |
0:6b30097d9a45
|
34
|
_nowState = 0x00;
|
kaushalpkk |
0:6b30097d9a45
|
35
|
_moved = 0;
|
kaushalpkk |
0:6b30097d9a45
|
36
|
_count = 0;
|
kaushalpkk |
0:6b30097d9a45
|
37
|
}
|
kaushalpkk |
0:6b30097d9a45
|
38
|
|
kaushalpkk |
0:6b30097d9a45
|
39
|
////////////////////////////////////////////////////////////////////////////////////////
|
kaushalpkk |
0:6b30097d9a45
|
40
|
int quadratureEncoder::getCount() {
|
kaushalpkk |
1:52fba054be6c
|
41
|
return _count;
|
kaushalpkk |
0:6b30097d9a45
|
42
|
}
|
kaushalpkk |
0:6b30097d9a45
|
43
|
|
kaushalpkk |
0:6b30097d9a45
|
44
|
void quadratureEncoder::resetCount() {
|
kaushalpkk |
0:6b30097d9a45
|
45
|
_count = 0;
|
kaushalpkk |
0:6b30097d9a45
|
46
|
}
|
kaushalpkk |
0:6b30097d9a45
|
47
|
|
kaushalpkk |
0:6b30097d9a45
|
48
|
void quadratureEncoder::setCount(int setCounter) {
|
kaushalpkk |
0:6b30097d9a45
|
49
|
_count = setCounter;
|
kaushalpkk |
0:6b30097d9a45
|
50
|
}
|
kaushalpkk |
0:6b30097d9a45
|
51
|
|
kaushalpkk |
0:6b30097d9a45
|
52
|
|
kaushalpkk |
0:6b30097d9a45
|
53
|
|
kaushalpkk |
0:6b30097d9a45
|
54
|
////////////////////////////////////////////////////////////////////////////////////////
|
kaushalpkk |
0:6b30097d9a45
|
55
|
void quadratureEncoder::resetMoved() {
|
kaushalpkk |
0:6b30097d9a45
|
56
|
_moved = 0;
|
kaushalpkk |
0:6b30097d9a45
|
57
|
}
|
kaushalpkk |
0:6b30097d9a45
|
58
|
|
kaushalpkk |
0:6b30097d9a45
|
59
|
void quadratureEncoder::setMoved(int setMover) {
|
kaushalpkk |
0:6b30097d9a45
|
60
|
_moved = setMover;
|
kaushalpkk |
0:6b30097d9a45
|
61
|
}
|
kaushalpkk |
0:6b30097d9a45
|
62
|
|
kaushalpkk |
0:6b30097d9a45
|
63
|
int quadratureEncoder::getMoved() {
|
kaushalpkk |
0:6b30097d9a45
|
64
|
return _moved;
|
kaushalpkk |
0:6b30097d9a45
|
65
|
}
|
kaushalpkk |
0:6b30097d9a45
|
66
|
|
kaushalpkk |
0:6b30097d9a45
|
67
|
////////////////////////////////////////////////////////////////////////////////////////
|
kaushalpkk |
0:6b30097d9a45
|
68
|
char quadratureEncoder::saveState(char nowS) {
|
kaushalpkk |
0:6b30097d9a45
|
69
|
return nowS;
|
kaushalpkk |
0:6b30097d9a45
|
70
|
}
|
kaushalpkk |
0:6b30097d9a45
|
71
|
|
kaushalpkk |
0:6b30097d9a45
|
72
|
char quadratureEncoder::getBit(char bitP, char targB) {
|
kaushalpkk |
0:6b30097d9a45
|
73
|
targB = targB & _BV(bitP);
|
kaushalpkk |
0:6b30097d9a45
|
74
|
if (targB == 0)
|
kaushalpkk |
0:6b30097d9a45
|
75
|
return 0;
|
kaushalpkk |
0:6b30097d9a45
|
76
|
else
|
kaushalpkk |
0:6b30097d9a45
|
77
|
return 1;
|
kaushalpkk |
0:6b30097d9a45
|
78
|
}
|
kaushalpkk |
0:6b30097d9a45
|
79
|
|
kaushalpkk |
0:6b30097d9a45
|
80
|
|
kaushalpkk |
0:6b30097d9a45
|
81
|
/*
|
kaushalpkk |
0:6b30097d9a45
|
82
|
function: the following 8 functions are ISR called by rise and fall of 4 pins
|
kaushalpkk |
0:6b30097d9a45
|
83
|
input: none (interupt called)
|
kaushalpkk |
0:6b30097d9a45
|
84
|
output: none
|
kaushalpkk |
0:6b30097d9a45
|
85
|
functioning:interrupts are called when the pin change occours
|
kaushalpkk |
0:6b30097d9a45
|
86
|
sets or clears the respective bit
|
kaushalpkk |
0:6b30097d9a45
|
87
|
checks the previous state and increments respective direction
|
kaushalpkk |
0:6b30097d9a45
|
88
|
restores new state value into old state
|
kaushalpkk |
0:6b30097d9a45
|
89
|
increments a count value for processing */
|
kaushalpkk |
0:6b30097d9a45
|
90
|
void quadratureEncoder::ARise() {
|
kaushalpkk |
0:6b30097d9a45
|
91
|
sbi(_nowState,Abit);
|
kaushalpkk |
0:6b30097d9a45
|
92
|
if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==0)
|
kaushalpkk |
0:6b30097d9a45
|
93
|
_moved++;
|
kaushalpkk |
0:6b30097d9a45
|
94
|
else if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==1)
|
kaushalpkk |
0:6b30097d9a45
|
95
|
_moved--;
|
kaushalpkk |
0:6b30097d9a45
|
96
|
_oldState = saveState(_nowState);
|
kaushalpkk |
0:6b30097d9a45
|
97
|
_count++;
|
kaushalpkk |
0:6b30097d9a45
|
98
|
}
|
kaushalpkk |
0:6b30097d9a45
|
99
|
|
kaushalpkk |
0:6b30097d9a45
|
100
|
void quadratureEncoder::AFall() {
|
kaushalpkk |
0:6b30097d9a45
|
101
|
cbi(_nowState, Abit);
|
kaushalpkk |
0:6b30097d9a45
|
102
|
if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==1)
|
kaushalpkk |
0:6b30097d9a45
|
103
|
_moved++;
|
kaushalpkk |
0:6b30097d9a45
|
104
|
else if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==0)
|
kaushalpkk |
0:6b30097d9a45
|
105
|
_moved--;
|
kaushalpkk |
0:6b30097d9a45
|
106
|
_oldState = saveState(_nowState);
|
kaushalpkk |
0:6b30097d9a45
|
107
|
_count++;
|
kaushalpkk |
0:6b30097d9a45
|
108
|
}
|
kaushalpkk |
0:6b30097d9a45
|
109
|
|
kaushalpkk |
0:6b30097d9a45
|
110
|
void quadratureEncoder::BRise() {
|
kaushalpkk |
0:6b30097d9a45
|
111
|
sbi(_nowState,Bbit);
|
kaushalpkk |
0:6b30097d9a45
|
112
|
if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==0)
|
kaushalpkk |
0:6b30097d9a45
|
113
|
_moved++;
|
kaushalpkk |
0:6b30097d9a45
|
114
|
else if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==0)
|
kaushalpkk |
0:6b30097d9a45
|
115
|
_moved--;
|
kaushalpkk |
0:6b30097d9a45
|
116
|
_oldState = saveState(_nowState);
|
kaushalpkk |
0:6b30097d9a45
|
117
|
_count++;
|
kaushalpkk |
0:6b30097d9a45
|
118
|
}
|
kaushalpkk |
0:6b30097d9a45
|
119
|
|
kaushalpkk |
0:6b30097d9a45
|
120
|
void quadratureEncoder::BFall() {
|
kaushalpkk |
0:6b30097d9a45
|
121
|
cbi(_nowState, Bbit);
|
kaushalpkk |
0:6b30097d9a45
|
122
|
if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==1)
|
kaushalpkk |
0:6b30097d9a45
|
123
|
_moved++;
|
kaushalpkk |
0:6b30097d9a45
|
124
|
else if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==1)
|
kaushalpkk |
0:6b30097d9a45
|
125
|
_moved--;
|
kaushalpkk |
0:6b30097d9a45
|
126
|
_oldState = saveState(_nowState);
|
kaushalpkk |
0:6b30097d9a45
|
127
|
_count++;
|
kaushalpkk |
0:6b30097d9a45
|
128
|
}
|
kaushalpkk |
0:6b30097d9a45
|
129
|
|