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:
Thu Jun 30 23:52:51 2016 +0000
Revision:
8:da95e4ccbe4c
Parent:
5:e00cc0dab1c7
Decreased H-Bridge Period to Provide for Even Finer Control.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Electrotiger 0:9afc272fa65f 1 /**
Electrotiger 0:9afc272fa65f 2 * @class QuadEnc
Electrotiger 0:9afc272fa65f 3 * @date June 2nd, 2016
Electrotiger 0:9afc272fa65f 4 * @author Weimen Li
Electrotiger 0:9afc272fa65f 5 * @brief Header file for a class which encapsulates a quadrature encoder.
Electrotiger 5:e00cc0dab1c7 6 * @remark The algorith used to read from the encoder may be found at:
Electrotiger 5:e00cc0dab1c7 7 * http://makeatronics.blogspot.com/2013/02/efficiently-reading-quadrature-with.html
Electrotiger 0:9afc272fa65f 8 */
Electrotiger 0:9afc272fa65f 9
Electrotiger 0:9afc272fa65f 10 #ifndef QUADENC_H_
Electrotiger 0:9afc272fa65f 11 #define QUADENC_H_
Electrotiger 0:9afc272fa65f 12 #include "mbed.h"
Electrotiger 0:9afc272fa65f 13
Electrotiger 0:9afc272fa65f 14 class QuadEnc {
Electrotiger 0:9afc272fa65f 15 public:
Electrotiger 0:9afc272fa65f 16 /**
Electrotiger 0:9afc272fa65f 17 * @brief Constructor for the QuadEnc object.
Electrotiger 0:9afc272fa65f 18 * @param ChannelA The pin Channel A is connected to.
Electrotiger 0:9afc272fa65f 19 * @param ChannelB The pin Channel B is connected to.
Electrotiger 0:9afc272fa65f 20 * @param CPR The number of counts per revolution of the encoder.
Electrotiger 0:9afc272fa65f 21 */
Electrotiger 0:9afc272fa65f 22 QuadEnc(PinName ChannelA, PinName ChannelB, float CPR);
Electrotiger 0:9afc272fa65f 23 virtual ~QuadEnc();
Electrotiger 0:9afc272fa65f 24
Electrotiger 0:9afc272fa65f 25 /**
Electrotiger 0:9afc272fa65f 26 * @brief Returns true when a new encoder count has been seen.
Electrotiger 0:9afc272fa65f 27 * @returns Whether a new encoder count has been seen.
Electrotiger 0:9afc272fa65f 28 * @remark Sets the hasNewCount flag to false after being called.
Electrotiger 0:9afc272fa65f 29 */
Electrotiger 0:9afc272fa65f 30 bool hasNewCount();
Electrotiger 0:9afc272fa65f 31 /**
Electrotiger 0:9afc272fa65f 32 * @brief Returns the current encoder count.
Electrotiger 0:9afc272fa65f 33 * @returns The current encoder count.
Electrotiger 0:9afc272fa65f 34 * @remark Declared to be inline for greater performance.
Electrotiger 0:9afc272fa65f 35 */
Electrotiger 0:9afc272fa65f 36 inline int32_t getCount() {
Electrotiger 0:9afc272fa65f 37 // Generally, we would disable interrupts to avoid corruption here.
Electrotiger 0:9afc272fa65f 38 // However, the returned value is the word size of the processor, so
Electrotiger 0:9afc272fa65f 39 // It should be safe. Additionally, any corruption may only happen on the LSB.
Electrotiger 0:9afc272fa65f 40 return count;
Electrotiger 0:9afc272fa65f 41 }
Electrotiger 0:9afc272fa65f 42
Electrotiger 0:9afc272fa65f 43 /**
Electrotiger 0:9afc272fa65f 44 * @brief Returns the number of revolutions that have been counted.
Electrotiger 0:9afc272fa65f 45 * @returns The number of revolutions that have been counted.
Electrotiger 0:9afc272fa65f 46 */
Electrotiger 0:9afc272fa65f 47 float getRevs();
Electrotiger 0:9afc272fa65f 48
Electrotiger 0:9afc272fa65f 49 /**
Electrotiger 0:9afc272fa65f 50 * @brief Resets the internal state variables of the encoder.
Electrotiger 0:9afc272fa65f 51 * @returns void
Electrotiger 0:9afc272fa65f 52 */
Electrotiger 0:9afc272fa65f 53 void reset();
Electrotiger 0:9afc272fa65f 54 private:
Electrotiger 0:9afc272fa65f 55 /// The current encoder count since the program started.
Electrotiger 0:9afc272fa65f 56 volatile int32_t count;
Electrotiger 0:9afc272fa65f 57 /// Whether the encoder has a new count.
Electrotiger 0:9afc272fa65f 58 volatile bool bHasNewCount;
Electrotiger 0:9afc272fa65f 59 /// The number of counts per revolution.
Electrotiger 0:9afc272fa65f 60 const float CPR;
Electrotiger 0:9afc272fa65f 61
Electrotiger 0:9afc272fa65f 62 /// Digital Input for Channel A.
Electrotiger 0:9afc272fa65f 63 DigitalIn ChannelAIn;
Electrotiger 0:9afc272fa65f 64 /// Digital Input for Channel B.
Electrotiger 0:9afc272fa65f 65 DigitalIn ChannelBIn;
Electrotiger 0:9afc272fa65f 66
Electrotiger 0:9afc272fa65f 67 /// Interrupt Object for Channel A.
Electrotiger 0:9afc272fa65f 68 InterruptIn ChannelAInter;
Electrotiger 0:9afc272fa65f 69 /// Interrupt Object for Channel B.
Electrotiger 0:9afc272fa65f 70 InterruptIn ChannelBInter;
Electrotiger 0:9afc272fa65f 71
Electrotiger 0:9afc272fa65f 72 /// Member Function ISR to be called when a change is detected on the channel pins.
Electrotiger 0:9afc272fa65f 73 void QuadEncISR();
Electrotiger 0:9afc272fa65f 74
Electrotiger 0:9afc272fa65f 75 /// The previous encoder count.
Electrotiger 0:9afc272fa65f 76 int32_t prevCount;
Electrotiger 0:9afc272fa65f 77 /// The previous number of revolutions.
Electrotiger 0:9afc272fa65f 78 float prevRevs;
Electrotiger 0:9afc272fa65f 79 /// The raw encoder reading.
Electrotiger 0:9afc272fa65f 80 uint8_t enc_val;
Electrotiger 0:9afc272fa65f 81 };
Electrotiger 0:9afc272fa65f 82
Electrotiger 0:9afc272fa65f 83 #endif /* QUADENC_H_ */