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: Encoder MODSERIAL mbed
Fork of P_controller_motor by
encoder.h@3:eee8d5461256, 2015-09-17 (annotated)
- Committer:
- ThomasBNL
- Date:
- Thu Sep 17 10:45:54 2015 +0000
- Revision:
- 3:eee8d5461256
Poging toevoegen encoder
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ThomasBNL | 3:eee8d5461256 | 1 | #ifndef _ENCODER_H_ |
| ThomasBNL | 3:eee8d5461256 | 2 | #define _ENCODER_H_ |
| ThomasBNL | 3:eee8d5461256 | 3 | |
| ThomasBNL | 3:eee8d5461256 | 4 | #include "mbed.h" |
| ThomasBNL | 3:eee8d5461256 | 5 | |
| ThomasBNL | 3:eee8d5461256 | 6 | /** Encoder class. |
| ThomasBNL | 3:eee8d5461256 | 7 | * Used to read out incremental position encoder. Decodes position in X2 configuration. |
| ThomasBNL | 3:eee8d5461256 | 8 | * |
| ThomasBNL | 3:eee8d5461256 | 9 | * Speed estimation is very crude and computationally intensive. Turned off by default |
| ThomasBNL | 3:eee8d5461256 | 10 | * |
| ThomasBNL | 3:eee8d5461256 | 11 | * Example: |
| ThomasBNL | 3:eee8d5461256 | 12 | * @code |
| ThomasBNL | 3:eee8d5461256 | 13 | * #include "mbed.h" |
| ThomasBNL | 3:eee8d5461256 | 14 | * #include "Encoder.h" |
| ThomasBNL | 3:eee8d5461256 | 15 | * |
| ThomasBNL | 3:eee8d5461256 | 16 | * Encoder motor1(PTD0,PTC9,true); |
| ThomasBNL | 3:eee8d5461256 | 17 | * Serial pc(USBTX,USBRX); |
| ThomasBNL | 3:eee8d5461256 | 18 | * pc.baud(115200); |
| ThomasBNL | 3:eee8d5461256 | 19 | * while(1) { |
| ThomasBNL | 3:eee8d5461256 | 20 | * wait(0.2); |
| ThomasBNL | 3:eee8d5461256 | 21 | * pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed()); |
| ThomasBNL | 3:eee8d5461256 | 22 | * } |
| ThomasBNL | 3:eee8d5461256 | 23 | * @endcode |
| ThomasBNL | 3:eee8d5461256 | 24 | */ |
| ThomasBNL | 3:eee8d5461256 | 25 | class Encoder |
| ThomasBNL | 3:eee8d5461256 | 26 | { |
| ThomasBNL | 3:eee8d5461256 | 27 | public: |
| ThomasBNL | 3:eee8d5461256 | 28 | /** Create Encoder instance |
| ThomasBNL | 3:eee8d5461256 | 29 | @param int_a Pin to be used as InterruptIn! Be careful, as not all pins on all platforms may be used as InterruptIn. |
| ThomasBNL | 3:eee8d5461256 | 30 | @param int_b second encoder pin, used as DigitalIn. Can be any DigitalIn pin, not necessarily on InterruptIn location |
| ThomasBNL | 3:eee8d5461256 | 31 | @param speed boolean value to determine whether speed calculation is done in interrupt routine. Default false (no speed calculation) |
| ThomasBNL | 3:eee8d5461256 | 32 | */ |
| ThomasBNL | 3:eee8d5461256 | 33 | |
| ThomasBNL | 3:eee8d5461256 | 34 | Encoder(PinName int_a, PinName int_b, bool speed=false); |
| ThomasBNL | 3:eee8d5461256 | 35 | /** Request position |
| ThomasBNL | 3:eee8d5461256 | 36 | @returns current position in encoder counts |
| ThomasBNL | 3:eee8d5461256 | 37 | */ |
| ThomasBNL | 3:eee8d5461256 | 38 | int32_t getPosition(){return m_position;} |
| ThomasBNL | 3:eee8d5461256 | 39 | /** Overwrite position |
| ThomasBNL | 3:eee8d5461256 | 40 | @param pos position to be written |
| ThomasBNL | 3:eee8d5461256 | 41 | */ |
| ThomasBNL | 3:eee8d5461256 | 42 | void setPosition(int32_t pos){m_position = pos;} |
| ThomasBNL | 3:eee8d5461256 | 43 | /** Request speed |
| ThomasBNL | 3:eee8d5461256 | 44 | @returns current speed |
| ThomasBNL | 3:eee8d5461256 | 45 | */ |
| ThomasBNL | 3:eee8d5461256 | 46 | float getSpeed(){return m_speed;} |
| ThomasBNL | 3:eee8d5461256 | 47 | private: |
| ThomasBNL | 3:eee8d5461256 | 48 | void encoderFalling(void); |
| ThomasBNL | 3:eee8d5461256 | 49 | void encoderRising(void); |
| ThomasBNL | 3:eee8d5461256 | 50 | bool m_speed_enabled; |
| ThomasBNL | 3:eee8d5461256 | 51 | Timer EncoderTimer; |
| ThomasBNL | 3:eee8d5461256 | 52 | Timeout EncoderTimeout; |
| ThomasBNL | 3:eee8d5461256 | 53 | InterruptIn pin_a; |
| ThomasBNL | 3:eee8d5461256 | 54 | DigitalIn pin_b; |
| ThomasBNL | 3:eee8d5461256 | 55 | int32_t m_position; |
| ThomasBNL | 3:eee8d5461256 | 56 | float m_speed; |
| ThomasBNL | 3:eee8d5461256 | 57 | void timeouthandler(void); |
| ThomasBNL | 3:eee8d5461256 | 58 | bool zero_speed; |
| ThomasBNL | 3:eee8d5461256 | 59 | }; |
| ThomasBNL | 3:eee8d5461256 | 60 | |
| ThomasBNL | 3:eee8d5461256 | 61 | |
| ThomasBNL | 3:eee8d5461256 | 62 | #endif //_ENCODER_H_ |
