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 Biorobotics Project

Committer:
MarkHeimgartner
Date:
Mon Oct 09 14:47:02 2017 +0000
Revision:
10:cea9b4827b28
Parent:
9:9d3da04c61db
works now with position in rad/s;

Who changed what in which revision?

UserRevisionLine numberNew 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 */
MarkHeimgartner 10:cea9b4827b28 38 int32_t getPosition()
MarkHeimgartner 10:cea9b4827b28 39 {
MarkHeimgartner 10:cea9b4827b28 40 return m_position;
MarkHeimgartner 10:cea9b4827b28 41 }
vsluiter 1:2dd7853c911a 42 /** Overwrite position
vsluiter 1:2dd7853c911a 43 @param pos position to be written
vsluiter 1:2dd7853c911a 44 */
vsluiter 0:c90b36abcbf8 45 void setPosition(int32_t pos){m_position = pos;}
vsluiter 1:2dd7853c911a 46 /** Request speed
vsluiter 1:2dd7853c911a 47 @returns current speed
vsluiter 1:2dd7853c911a 48 */
vera1 9:9d3da04c61db 49 float m_speed;
vsluiter 0:c90b36abcbf8 50 float getSpeed(){return m_speed;}
vsluiter 0:c90b36abcbf8 51 private:
vsluiter 0:c90b36abcbf8 52 void encoderFalling(void);
vsluiter 0:c90b36abcbf8 53 void encoderRising(void);
vsluiter 3:dcb7bdc73882 54 bool m_speed_enabled;
vsluiter 0:c90b36abcbf8 55 Timer EncoderTimer;
vsluiter 5:04c4a90c7a0a 56 Timeout EncoderTimeout;
vsluiter 0:c90b36abcbf8 57 InterruptIn pin_a;
vsluiter 0:c90b36abcbf8 58 DigitalIn pin_b;
vsluiter 0:c90b36abcbf8 59 int32_t m_position;
vera1 9:9d3da04c61db 60
vsluiter 0:c90b36abcbf8 61 void timeouthandler(void);
vsluiter 0:c90b36abcbf8 62 bool zero_speed;
vsluiter 0:c90b36abcbf8 63 };
vsluiter 0:c90b36abcbf8 64
vsluiter 0:c90b36abcbf8 65
vsluiter 0:c90b36abcbf8 66 #endif //_ENCODER_H_