Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-dsp mbed-rtos mbed
Dependents: MAE433_Library_Tester RobotBalancerv2
QuadEnc.hpp
00001 /** 00002 * @class QuadEnc 00003 * @date June 2nd, 2016 00004 * @author Weimen Li 00005 * @brief Header file for a class which encapsulates a quadrature encoder. 00006 * @remark The algorith used to read from the encoder may be found at: 00007 * http://makeatronics.blogspot.com/2013/02/efficiently-reading-quadrature-with.html 00008 */ 00009 00010 #ifndef QUADENC_H_ 00011 #define QUADENC_H_ 00012 #include "mbed.h" 00013 00014 class QuadEnc { 00015 public: 00016 /** 00017 * @brief Constructor for the QuadEnc object. 00018 * @param ChannelA The pin Channel A is connected to. 00019 * @param ChannelB The pin Channel B is connected to. 00020 * @param CPR The number of counts per revolution of the encoder. 00021 */ 00022 QuadEnc(PinName ChannelA, PinName ChannelB, float CPR); 00023 virtual ~QuadEnc(); 00024 00025 /** 00026 * @brief Returns true when a new encoder count has been seen. 00027 * @returns Whether a new encoder count has been seen. 00028 * @remark Sets the hasNewCount flag to false after being called. 00029 */ 00030 bool hasNewCount(); 00031 /** 00032 * @brief Returns the current encoder count. 00033 * @returns The current encoder count. 00034 * @remark Declared to be inline for greater performance. 00035 */ 00036 inline int32_t getCount() { 00037 // Generally, we would disable interrupts to avoid corruption here. 00038 // However, the returned value is the word size of the processor, so 00039 // It should be safe. Additionally, any corruption may only happen on the LSB. 00040 return count; 00041 } 00042 00043 /** 00044 * @brief Returns the number of revolutions that have been counted. 00045 * @returns The number of revolutions that have been counted. 00046 */ 00047 float getRevs(); 00048 00049 /** 00050 * @brief Resets the internal state variables of the encoder. 00051 * @returns void 00052 */ 00053 void reset(); 00054 private: 00055 /// The current encoder count since the program started. 00056 volatile int32_t count; 00057 /// Whether the encoder has a new count. 00058 volatile bool bHasNewCount; 00059 /// The number of counts per revolution. 00060 const float CPR; 00061 00062 /// Digital Input for Channel A. 00063 DigitalIn ChannelAIn; 00064 /// Digital Input for Channel B. 00065 DigitalIn ChannelBIn; 00066 00067 /// Interrupt Object for Channel A. 00068 InterruptIn ChannelAInter; 00069 /// Interrupt Object for Channel B. 00070 InterruptIn ChannelBInter; 00071 00072 /// Member Function ISR to be called when a change is detected on the channel pins. 00073 void QuadEncISR(); 00074 00075 /// The previous encoder count. 00076 int32_t prevCount; 00077 /// The previous number of revolutions. 00078 float prevRevs; 00079 /// The raw encoder reading. 00080 uint8_t enc_val; 00081 }; 00082 00083 #endif /* QUADENC_H_ */
Generated on Tue Jul 12 2022 16:20:51 by
1.7.2