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@9:9d3da04c61db, 2017-10-06 (annotated)
- Committer:
- vera1
- Date:
- Fri Oct 06 08:26:36 2017 +0000
- Revision:
- 9:9d3da04c61db
- Parent:
- 6:18b000b443af
- Child:
- 10:cea9b4827b28
motor velocity proportional to pot meter working
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 | 6:18b000b443af | 5 | |
vsluiter | 1:2dd7853c911a | 6 | /** Encoder class. |
vsluiter | 1:2dd7853c911a | 7 | * Used to read out incremental position encoder. Decodes position in X2 configuration. |
vsluiter | 1:2dd7853c911a | 8 | * |
vsluiter | 3:dcb7bdc73882 | 9 | * Speed estimation is very crude and computationally intensive. Turned off by default |
vsluiter | 3:dcb7bdc73882 | 10 | * |
vsluiter | 1:2dd7853c911a | 11 | * Example: |
vsluiter | 1:2dd7853c911a | 12 | * @code |
vsluiter | 1:2dd7853c911a | 13 | * #include "mbed.h" |
vsluiter | 1:2dd7853c911a | 14 | * #include "Encoder.h" |
vsluiter | 1:2dd7853c911a | 15 | * |
vsluiter | 3:dcb7bdc73882 | 16 | * Encoder motor1(PTD0,PTC9,true); |
vsluiter | 1:2dd7853c911a | 17 | * Serial pc(USBTX,USBRX); |
vsluiter | 1:2dd7853c911a | 18 | * pc.baud(115200); |
vsluiter | 1:2dd7853c911a | 19 | * while(1) { |
vsluiter | 1:2dd7853c911a | 20 | * wait(0.2); |
vsluiter | 1:2dd7853c911a | 21 | * pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed()); |
vsluiter | 1:2dd7853c911a | 22 | * } |
vsluiter | 1:2dd7853c911a | 23 | * @endcode |
vsluiter | 1:2dd7853c911a | 24 | */ |
vsluiter | 0:c90b36abcbf8 | 25 | class Encoder |
vsluiter | 0:c90b36abcbf8 | 26 | { |
vsluiter | 0:c90b36abcbf8 | 27 | public: |
vsluiter | 1:2dd7853c911a | 28 | /** Create Encoder instance |
vsluiter | 1:2dd7853c911a | 29 | @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 | 30 | @param int_b second encoder pin, used as DigitalIn. Can be any DigitalIn pin, not necessarily on InterruptIn location |
vsluiter | 6:18b000b443af | 31 | @param speed boolean value to determine whether speed calculation is done in interrupt routine. Default false (no speed calculation) |
vsluiter | 1:2dd7853c911a | 32 | */ |
vsluiter | 4:69be34f39c9f | 33 | |
vsluiter | 3:dcb7bdc73882 | 34 | Encoder(PinName int_a, PinName int_b, bool speed=false); |
vsluiter | 1:2dd7853c911a | 35 | /** Request position |
vsluiter | 1:2dd7853c911a | 36 | @returns current position in encoder counts |
vsluiter | 1:2dd7853c911a | 37 | */ |
vsluiter | 0:c90b36abcbf8 | 38 | int32_t getPosition(){return m_position;} |
vsluiter | 1:2dd7853c911a | 39 | /** Overwrite position |
vsluiter | 1:2dd7853c911a | 40 | @param pos position to be written |
vsluiter | 1:2dd7853c911a | 41 | */ |
vsluiter | 0:c90b36abcbf8 | 42 | void setPosition(int32_t pos){m_position = pos;} |
vsluiter | 1:2dd7853c911a | 43 | /** Request speed |
vsluiter | 1:2dd7853c911a | 44 | @returns current speed |
vsluiter | 1:2dd7853c911a | 45 | */ |
vera1 | 9:9d3da04c61db | 46 | float m_speed; |
vsluiter | 0:c90b36abcbf8 | 47 | float getSpeed(){return m_speed;} |
vsluiter | 0:c90b36abcbf8 | 48 | private: |
vsluiter | 0:c90b36abcbf8 | 49 | void encoderFalling(void); |
vsluiter | 0:c90b36abcbf8 | 50 | void encoderRising(void); |
vsluiter | 3:dcb7bdc73882 | 51 | bool m_speed_enabled; |
vsluiter | 0:c90b36abcbf8 | 52 | Timer EncoderTimer; |
vsluiter | 5:04c4a90c7a0a | 53 | Timeout EncoderTimeout; |
vsluiter | 0:c90b36abcbf8 | 54 | InterruptIn pin_a; |
vsluiter | 0:c90b36abcbf8 | 55 | DigitalIn pin_b; |
vsluiter | 0:c90b36abcbf8 | 56 | int32_t m_position; |
vera1 | 9:9d3da04c61db | 57 | |
vsluiter | 0:c90b36abcbf8 | 58 | void timeouthandler(void); |
vsluiter | 0:c90b36abcbf8 | 59 | bool zero_speed; |
vsluiter | 0:c90b36abcbf8 | 60 | }; |
vsluiter | 0:c90b36abcbf8 | 61 | |
vsluiter | 0:c90b36abcbf8 | 62 | |
vsluiter | 0:c90b36abcbf8 | 63 | #endif //_ENCODER_H_ |