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: HIDScope
Dependents: Project_motor Project_motor Motor_poscontrol Conceptcontroller_v_1_0
Fork of Encoder by
encoder.h
00001 #ifndef _ENCODER_H_ 00002 #define _ENCODER_H_ 00003 00004 #include "mbed.h" 00005 00006 /** Encoder class. 00007 * Used to read out incremental position encoder. Decodes position in X2 configuration. 00008 * 00009 * Speed estimation is very crude and computationally intensive. Turned off by default 00010 * 00011 * Example: 00012 * @code 00013 * #include "mbed.h" 00014 * #include "Encoder.h" 00015 * 00016 * Encoder motor1(PTD0,PTC9,true); 00017 * Serial pc(USBTX,USBRX); 00018 * pc.baud(115200); 00019 * while(1) { 00020 * wait(0.2); 00021 * pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed()); 00022 * } 00023 * @endcode 00024 */ 00025 class Encoder 00026 { 00027 public: 00028 /** Create Encoder instance 00029 @param int_a Pin to be used as InterruptIn! Be careful, as not all pins on all platforms may be used as InterruptIn. 00030 @param int_b second encoder pin, used as DigitalIn. Can be any DigitalIn pin, not necessarily on InterruptIn location 00031 @param speed boolean value to determine whether speed calculation is done in interrupt routine. Default false (no speed calculation) 00032 */ 00033 00034 Encoder(PinName int_a, PinName int_b, bool speed=false); 00035 /** Request position 00036 @returns current position in encoder counts 00037 */ 00038 int32_t getPosition() 00039 { 00040 return m_position; 00041 } 00042 /** Overwrite position 00043 @param pos position to be written 00044 */ 00045 void setPosition(int32_t pos){m_position = pos;} 00046 /** Request speed 00047 @returns current speed 00048 */ 00049 float m_speed; 00050 float getSpeed(){return m_speed;} 00051 private: 00052 void encoderFalling(void); 00053 void encoderRising(void); 00054 bool m_speed_enabled; 00055 Timer EncoderTimer; 00056 Timeout EncoderTimeout; 00057 InterruptIn pin_a; 00058 DigitalIn pin_b; 00059 int32_t m_position; 00060 00061 void timeouthandler(void); 00062 bool zero_speed; 00063 }; 00064 00065 00066 #endif //_ENCODER_H_
Generated on Sat Jul 16 2022 13:53:12 by
1.7.2
