Motor is operated by keyboard commands, encoder reads out position but not yet speed
Dependencies: HIDScope
Dependents: Project_motor Project_motor Motor_poscontrol Conceptcontroller_v_1_0
Fork of Encoder by
encoder.h@1:2dd7853c911a, 2013-10-01 (annotated)
- Committer:
- vsluiter
- Date:
- Tue Oct 01 21:47:15 2013 +0000
- Revision:
- 1:2dd7853c911a
- Parent:
- 0:c90b36abcbf8
- Child:
- 2:0998a8fd7727
- Child:
- 3:dcb7bdc73882
Updated documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:c90b36abcbf8 | 1 | #ifndef _ENCODER_H_ |
vsluiter | 0:c90b36abcbf8 | 2 | #define _ENCODER_H_ |
vsluiter | 0:c90b36abcbf8 | 3 | |
vsluiter | 0:c90b36abcbf8 | 4 | #include "mbed.h" |
vsluiter | 1:2dd7853c911a | 5 | /** Encoder class. |
vsluiter | 1:2dd7853c911a | 6 | * Used to read out incremental position encoder. Decodes position in X2 configuration. |
vsluiter | 1:2dd7853c911a | 7 | * |
vsluiter | 1:2dd7853c911a | 8 | * Example: |
vsluiter | 1:2dd7853c911a | 9 | * @code |
vsluiter | 1:2dd7853c911a | 10 | * #include "mbed.h" |
vsluiter | 1:2dd7853c911a | 11 | * #include "Encoder.h" |
vsluiter | 1:2dd7853c911a | 12 | * |
vsluiter | 1:2dd7853c911a | 13 | * Encoder motor1(PTD0,PTC9); |
vsluiter | 1:2dd7853c911a | 14 | * Serial pc(USBTX,USBRX); |
vsluiter | 1:2dd7853c911a | 15 | * pc.baud(115200); |
vsluiter | 1:2dd7853c911a | 16 | * while(1) { |
vsluiter | 1:2dd7853c911a | 17 | * wait(0.2); |
vsluiter | 1:2dd7853c911a | 18 | * pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed()); |
vsluiter | 1:2dd7853c911a | 19 | * } |
vsluiter | 1:2dd7853c911a | 20 | * @endcode |
vsluiter | 1:2dd7853c911a | 21 | */ |
vsluiter | 0:c90b36abcbf8 | 22 | class Encoder |
vsluiter | 0:c90b36abcbf8 | 23 | { |
vsluiter | 0:c90b36abcbf8 | 24 | public: |
vsluiter | 1:2dd7853c911a | 25 | /** Create Encoder instance |
vsluiter | 1:2dd7853c911a | 26 | @param int_a Pin to be used as InterruptIn! Be careful, as not all pins on all platforms may be used as InterruptIn. |
vsluiter | 1:2dd7853c911a | 27 | @param int_b second encoder pin, used as DigitalIn. Can be any DigitalIn pin, not necessarily on InterruptIn location |
vsluiter | 1:2dd7853c911a | 28 | */ |
vsluiter | 0:c90b36abcbf8 | 29 | Encoder(PinName int_a, PinName int_b); |
vsluiter | 1:2dd7853c911a | 30 | /** Request position |
vsluiter | 1:2dd7853c911a | 31 | @returns current position in encoder counts |
vsluiter | 1:2dd7853c911a | 32 | */ |
vsluiter | 0:c90b36abcbf8 | 33 | int32_t getPosition(){return m_position;} |
vsluiter | 1:2dd7853c911a | 34 | /** Overwrite position |
vsluiter | 1:2dd7853c911a | 35 | @param pos position to be written |
vsluiter | 1:2dd7853c911a | 36 | */ |
vsluiter | 0:c90b36abcbf8 | 37 | void setPosition(int32_t pos){m_position = pos;} |
vsluiter | 1:2dd7853c911a | 38 | /** Request speed |
vsluiter | 1:2dd7853c911a | 39 | @returns current speed |
vsluiter | 1:2dd7853c911a | 40 | */ |
vsluiter | 0:c90b36abcbf8 | 41 | float getSpeed(){return m_speed;} |
vsluiter | 0:c90b36abcbf8 | 42 | private: |
vsluiter | 0:c90b36abcbf8 | 43 | void encoderFalling(void); |
vsluiter | 0:c90b36abcbf8 | 44 | void encoderRising(void); |
vsluiter | 0:c90b36abcbf8 | 45 | Timer EncoderTimer; |
vsluiter | 0:c90b36abcbf8 | 46 | Timeout EncoderTimeout; |
vsluiter | 0:c90b36abcbf8 | 47 | InterruptIn pin_a; |
vsluiter | 0:c90b36abcbf8 | 48 | DigitalIn pin_b; |
vsluiter | 0:c90b36abcbf8 | 49 | int32_t m_position; |
vsluiter | 0:c90b36abcbf8 | 50 | float m_speed; |
vsluiter | 0:c90b36abcbf8 | 51 | void timeouthandler(void); |
vsluiter | 0:c90b36abcbf8 | 52 | bool zero_speed; |
vsluiter | 0:c90b36abcbf8 | 53 | }; |
vsluiter | 0:c90b36abcbf8 | 54 | |
vsluiter | 0:c90b36abcbf8 | 55 | |
vsluiter | 0:c90b36abcbf8 | 56 | #endif //_ENCODER_H_ |