Krishna Kaushal Panduru / QuadratureEncoder
Committer:
kaushalpkk
Date:
Fri Jul 22 13:03:39 2011 +0000
Revision:
0:6b30097d9a45
Child:
1:52fba054be6c
needs documentation

Who changed what in which revision?

UserRevisionLine numberNew 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 0:6b30097d9a45 41 int outp = _count;
kaushalpkk 0:6b30097d9a45 42 _count = 0;
kaushalpkk 0:6b30097d9a45 43 return outp;
kaushalpkk 0:6b30097d9a45 44 }
kaushalpkk 0:6b30097d9a45 45
kaushalpkk 0:6b30097d9a45 46 void quadratureEncoder::resetCount() {
kaushalpkk 0:6b30097d9a45 47 _count = 0;
kaushalpkk 0:6b30097d9a45 48 }
kaushalpkk 0:6b30097d9a45 49
kaushalpkk 0:6b30097d9a45 50 void quadratureEncoder::setCount(int setCounter) {
kaushalpkk 0:6b30097d9a45 51 _count = setCounter;
kaushalpkk 0:6b30097d9a45 52 }
kaushalpkk 0:6b30097d9a45 53
kaushalpkk 0:6b30097d9a45 54
kaushalpkk 0:6b30097d9a45 55
kaushalpkk 0:6b30097d9a45 56 ////////////////////////////////////////////////////////////////////////////////////////
kaushalpkk 0:6b30097d9a45 57 void quadratureEncoder::resetMoved() {
kaushalpkk 0:6b30097d9a45 58 _moved = 0;
kaushalpkk 0:6b30097d9a45 59 }
kaushalpkk 0:6b30097d9a45 60
kaushalpkk 0:6b30097d9a45 61 void quadratureEncoder::setMoved(int setMover) {
kaushalpkk 0:6b30097d9a45 62 _moved = setMover;
kaushalpkk 0:6b30097d9a45 63 }
kaushalpkk 0:6b30097d9a45 64
kaushalpkk 0:6b30097d9a45 65 int quadratureEncoder::getMoved() {
kaushalpkk 0:6b30097d9a45 66 return _moved;
kaushalpkk 0:6b30097d9a45 67 }
kaushalpkk 0:6b30097d9a45 68
kaushalpkk 0:6b30097d9a45 69 ////////////////////////////////////////////////////////////////////////////////////////
kaushalpkk 0:6b30097d9a45 70 char quadratureEncoder::saveState(char nowS) {
kaushalpkk 0:6b30097d9a45 71 return nowS;
kaushalpkk 0:6b30097d9a45 72 }
kaushalpkk 0:6b30097d9a45 73
kaushalpkk 0:6b30097d9a45 74 char quadratureEncoder::getBit(char bitP, char targB) {
kaushalpkk 0:6b30097d9a45 75 targB = targB & _BV(bitP);
kaushalpkk 0:6b30097d9a45 76 if (targB == 0)
kaushalpkk 0:6b30097d9a45 77 return 0;
kaushalpkk 0:6b30097d9a45 78 else
kaushalpkk 0:6b30097d9a45 79 return 1;
kaushalpkk 0:6b30097d9a45 80 }
kaushalpkk 0:6b30097d9a45 81
kaushalpkk 0:6b30097d9a45 82
kaushalpkk 0:6b30097d9a45 83 /*
kaushalpkk 0:6b30097d9a45 84 function: the following 8 functions are ISR called by rise and fall of 4 pins
kaushalpkk 0:6b30097d9a45 85 input: none (interupt called)
kaushalpkk 0:6b30097d9a45 86 output: none
kaushalpkk 0:6b30097d9a45 87 functioning:interrupts are called when the pin change occours
kaushalpkk 0:6b30097d9a45 88 sets or clears the respective bit
kaushalpkk 0:6b30097d9a45 89 checks the previous state and increments respective direction
kaushalpkk 0:6b30097d9a45 90 restores new state value into old state
kaushalpkk 0:6b30097d9a45 91 increments a count value for processing */
kaushalpkk 0:6b30097d9a45 92 void quadratureEncoder::ARise() {
kaushalpkk 0:6b30097d9a45 93 sbi(_nowState,Abit);
kaushalpkk 0:6b30097d9a45 94 if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==0)
kaushalpkk 0:6b30097d9a45 95 _moved++;
kaushalpkk 0:6b30097d9a45 96 else if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==1)
kaushalpkk 0:6b30097d9a45 97 _moved--;
kaushalpkk 0:6b30097d9a45 98 _oldState = saveState(_nowState);
kaushalpkk 0:6b30097d9a45 99 _count++;
kaushalpkk 0:6b30097d9a45 100 }
kaushalpkk 0:6b30097d9a45 101
kaushalpkk 0:6b30097d9a45 102 void quadratureEncoder::AFall() {
kaushalpkk 0:6b30097d9a45 103 cbi(_nowState, Abit);
kaushalpkk 0:6b30097d9a45 104 if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==1)
kaushalpkk 0:6b30097d9a45 105 _moved++;
kaushalpkk 0:6b30097d9a45 106 else if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==0)
kaushalpkk 0:6b30097d9a45 107 _moved--;
kaushalpkk 0:6b30097d9a45 108 _oldState = saveState(_nowState);
kaushalpkk 0:6b30097d9a45 109 _count++;
kaushalpkk 0:6b30097d9a45 110 }
kaushalpkk 0:6b30097d9a45 111
kaushalpkk 0:6b30097d9a45 112 void quadratureEncoder::BRise() {
kaushalpkk 0:6b30097d9a45 113 sbi(_nowState,Bbit);
kaushalpkk 0:6b30097d9a45 114 if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==0)
kaushalpkk 0:6b30097d9a45 115 _moved++;
kaushalpkk 0:6b30097d9a45 116 else if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==0)
kaushalpkk 0:6b30097d9a45 117 _moved--;
kaushalpkk 0:6b30097d9a45 118 _oldState = saveState(_nowState);
kaushalpkk 0:6b30097d9a45 119 _count++;
kaushalpkk 0:6b30097d9a45 120 }
kaushalpkk 0:6b30097d9a45 121
kaushalpkk 0:6b30097d9a45 122 void quadratureEncoder::BFall() {
kaushalpkk 0:6b30097d9a45 123 cbi(_nowState, Bbit);
kaushalpkk 0:6b30097d9a45 124 if (getBit(Bbit,_oldState)==0 && getBit(Abit,_oldState)==1)
kaushalpkk 0:6b30097d9a45 125 _moved++;
kaushalpkk 0:6b30097d9a45 126 else if (getBit(Bbit,_oldState)==1 && getBit(Abit,_oldState)==1)
kaushalpkk 0:6b30097d9a45 127 _moved--;
kaushalpkk 0:6b30097d9a45 128 _oldState = saveState(_nowState);
kaushalpkk 0:6b30097d9a45 129 _count++;
kaushalpkk 0:6b30097d9a45 130 }
kaushalpkk 0:6b30097d9a45 131