Drivers for the mini robot designed for Princeton's MAE 433 course.

Dependencies:   mbed-dsp mbed-rtos mbed

Dependents:   MAE433_Library_Tester RobotBalancerv2

Committer:
Electrotiger
Date:
Fri Jun 24 21:03:20 2016 +0000
Revision:
0:9afc272fa65f
First Commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Electrotiger 0:9afc272fa65f 1 /**
Electrotiger 0:9afc272fa65f 2 * @file QuadEnc.cpp
Electrotiger 0:9afc272fa65f 3 * @date June 2nd, 2016
Electrotiger 0:9afc272fa65f 4 * @author Weimen Li
Electrotiger 0:9afc272fa65f 5 */
Electrotiger 0:9afc272fa65f 6
Electrotiger 0:9afc272fa65f 7 #include "QuadEnc.hpp"
Electrotiger 0:9afc272fa65f 8
Electrotiger 0:9afc272fa65f 9 QuadEnc::QuadEnc(PinName ChannelA, PinName ChannelB, float CPR) :
Electrotiger 0:9afc272fa65f 10 count(0), bHasNewCount(false),
Electrotiger 0:9afc272fa65f 11 CPR(CPR), ChannelAIn(ChannelA), ChannelBIn(ChannelB), ChannelAInter(ChannelA), ChannelBInter(ChannelB),
Electrotiger 0:9afc272fa65f 12 prevCount(0), prevRevs(0), enc_val(0) {
Electrotiger 0:9afc272fa65f 13
Electrotiger 0:9afc272fa65f 14 // Configure Channel A interrupt to trigger on rise and fall.
Electrotiger 0:9afc272fa65f 15 ChannelAInter.rise(this, &QuadEnc::QuadEncISR);
Electrotiger 0:9afc272fa65f 16 ChannelAInter.fall(this, &QuadEnc::QuadEncISR);
Electrotiger 0:9afc272fa65f 17
Electrotiger 0:9afc272fa65f 18 // Configure Channel B interrupt to trigger on rise and fall.
Electrotiger 0:9afc272fa65f 19 ChannelBInter.rise(this, &QuadEnc::QuadEncISR);
Electrotiger 0:9afc272fa65f 20 ChannelBInter.fall(this, &QuadEnc::QuadEncISR);
Electrotiger 0:9afc272fa65f 21
Electrotiger 0:9afc272fa65f 22 }
Electrotiger 0:9afc272fa65f 23
Electrotiger 0:9afc272fa65f 24 QuadEnc::~QuadEnc() {
Electrotiger 0:9afc272fa65f 25
Electrotiger 0:9afc272fa65f 26 }
Electrotiger 0:9afc272fa65f 27
Electrotiger 0:9afc272fa65f 28 void QuadEnc::QuadEncISR() {
Electrotiger 0:9afc272fa65f 29 uint8_t QuadEncData = (ChannelAIn.read() << 1) | ChannelBIn.read();
Electrotiger 0:9afc272fa65f 30 const int8_t QuadEncLUT[16] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1,
Electrotiger 0:9afc272fa65f 31 -1, 0 };
Electrotiger 0:9afc272fa65f 32 enc_val = enc_val << 2;
Electrotiger 0:9afc272fa65f 33 enc_val = enc_val | QuadEncData;
Electrotiger 0:9afc272fa65f 34 count += QuadEncLUT[enc_val & 0b1111];
Electrotiger 0:9afc272fa65f 35 bHasNewCount = true;
Electrotiger 0:9afc272fa65f 36 }
Electrotiger 0:9afc272fa65f 37
Electrotiger 0:9afc272fa65f 38 bool QuadEnc::hasNewCount() {
Electrotiger 0:9afc272fa65f 39 bool returnVal = bHasNewCount;
Electrotiger 0:9afc272fa65f 40 bHasNewCount = false;
Electrotiger 0:9afc272fa65f 41 return returnVal;
Electrotiger 0:9afc272fa65f 42 }
Electrotiger 0:9afc272fa65f 43
Electrotiger 0:9afc272fa65f 44 float QuadEnc::getRevs() {
Electrotiger 0:9afc272fa65f 45 // Capture the current count to avoid corruption.
Electrotiger 0:9afc272fa65f 46 float currentCount = count;
Electrotiger 0:9afc272fa65f 47 if (prevCount != currentCount) {
Electrotiger 0:9afc272fa65f 48 prevRevs = currentCount / CPR;
Electrotiger 0:9afc272fa65f 49 }
Electrotiger 0:9afc272fa65f 50 return prevRevs;
Electrotiger 0:9afc272fa65f 51 }
Electrotiger 0:9afc272fa65f 52
Electrotiger 0:9afc272fa65f 53 void QuadEnc::reset() {
Electrotiger 0:9afc272fa65f 54 count = 0;
Electrotiger 0:9afc272fa65f 55 bHasNewCount = false;
Electrotiger 0:9afc272fa65f 56 }