Control project for the Lift-arm. Works with ROS Melodic
Dependencies: mbed Servo ros_lib_melodic ULN2003_StepperDriver Async_4pin_Stepper
src/encoder.h@3:4b6080e86761, 2021-05-31 (annotated)
- Committer:
- krogedal
- Date:
- Mon May 31 16:44:26 2021 +0000
- Revision:
- 3:4b6080e86761
- Parent:
- 1:7c355adbc977
Updated to work with ROS, reading from /cmd_vel
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krogedal | 0:441289ea4e29 | 1 | #ifndef KARBOT_ENCODER_H |
krogedal | 0:441289ea4e29 | 2 | #define KARBOT_ENCODER_H |
krogedal | 0:441289ea4e29 | 3 | |
krogedal | 0:441289ea4e29 | 4 | /* Karbot encoder class |
krogedal | 0:441289ea4e29 | 5 | * |
krogedal | 0:441289ea4e29 | 6 | * This class is based upon the QEI class by Aaron Berk and the encoder class |
krogedal | 0:441289ea4e29 | 7 | * I wrote during ESP in 2nd year |
krogedal | 0:441289ea4e29 | 8 | * |
krogedal | 0:441289ea4e29 | 9 | * Written by Simon Krogedal |
krogedal | 0:441289ea4e29 | 10 | * 27/05/21 |
krogedal | 0:441289ea4e29 | 11 | * Team 9 4th Year project |
krogedal | 0:441289ea4e29 | 12 | * |
krogedal | 0:441289ea4e29 | 13 | * for NUCLEO-F401RE |
krogedal | 0:441289ea4e29 | 14 | * |
krogedal | 0:441289ea4e29 | 15 | */ |
krogedal | 0:441289ea4e29 | 16 | |
krogedal | 0:441289ea4e29 | 17 | #include "mbed.h" |
krogedal | 0:441289ea4e29 | 18 | |
krogedal | 3:4b6080e86761 | 19 | |
krogedal | 0:441289ea4e29 | 20 | #define PREV_MASK 0x1 //Mask for the previous state in determining direction |
krogedal | 0:441289ea4e29 | 21 | //of rotation. |
krogedal | 0:441289ea4e29 | 22 | #define CURR_MASK 0x2 //Mask for the current state in determining direction |
krogedal | 0:441289ea4e29 | 23 | //of rotation. |
krogedal | 0:441289ea4e29 | 24 | #define INVALID 0x3 //XORing two states where both bits have changed. |
krogedal | 0:441289ea4e29 | 25 | |
krogedal | 0:441289ea4e29 | 26 | class encoder { |
krogedal | 0:441289ea4e29 | 27 | |
krogedal | 0:441289ea4e29 | 28 | private: |
krogedal | 0:441289ea4e29 | 29 | int tot_clicks, temp_tot; // clicks since last distance reset |
krogedal | 0:441289ea4e29 | 30 | double click_rate, click_store;// clickrate |
krogedal | 0:441289ea4e29 | 31 | bool c_d; // left or right bool |
krogedal | 0:441289ea4e29 | 32 | |
krogedal | 0:441289ea4e29 | 33 | /** |
krogedal | 0:441289ea4e29 | 34 | * Update the pulse count. |
krogedal | 0:441289ea4e29 | 35 | * |
krogedal | 0:441289ea4e29 | 36 | * Called on every rising/falling edge of channels A/B. |
krogedal | 0:441289ea4e29 | 37 | * |
krogedal | 0:441289ea4e29 | 38 | * Reads the state of the channels and determines whether a pulse forward |
krogedal | 0:441289ea4e29 | 39 | * or backward has occured, updating the count appropriately. |
krogedal | 0:441289ea4e29 | 40 | */ |
krogedal | 0:441289ea4e29 | 41 | void encode(void); |
krogedal | 0:441289ea4e29 | 42 | |
krogedal | 0:441289ea4e29 | 43 | InterruptIn channelA_; |
krogedal | 0:441289ea4e29 | 44 | InterruptIn channelB_; |
krogedal | 0:441289ea4e29 | 45 | |
krogedal | 0:441289ea4e29 | 46 | int pulsesPerRev_; |
krogedal | 0:441289ea4e29 | 47 | int prevState_; |
krogedal | 0:441289ea4e29 | 48 | int currState_; |
krogedal | 0:441289ea4e29 | 49 | |
krogedal | 0:441289ea4e29 | 50 | volatile int pulses_; |
krogedal | 0:441289ea4e29 | 51 | |
krogedal | 0:441289ea4e29 | 52 | protected: |
krogedal | 0:441289ea4e29 | 53 | |
krogedal | 0:441289ea4e29 | 54 | Ticker sampler; // ticker object to sample speed |
krogedal | 0:441289ea4e29 | 55 | double period, enc_const; // sampling period and wheel constant |
krogedal | 0:441289ea4e29 | 56 | void sample_func(void); // sample function |
krogedal | 0:441289ea4e29 | 57 | double getClicks(void); // returns clickrate |
krogedal | 0:441289ea4e29 | 58 | |
krogedal | 0:441289ea4e29 | 59 | public: |
krogedal | 0:441289ea4e29 | 60 | |
krogedal | 0:441289ea4e29 | 61 | // Constructor takes 3 encoder input pins, CPR, left or right bool, sampling period, and a wheel-size constant |
krogedal | 0:441289ea4e29 | 62 | encoder(PinName chanA, PinName chanB, int CPR, bool c, double p, double ec); |
krogedal | 0:441289ea4e29 | 63 | |
krogedal | 0:441289ea4e29 | 64 | double getSpeed(void); // returns wheel speed |
krogedal | 0:441289ea4e29 | 65 | double getDistance(void); // returns distance travelled |
krogedal | 0:441289ea4e29 | 66 | double tempDist(void); // returns distance travelled |
krogedal | 0:441289ea4e29 | 67 | void distRst(void); // resets distance |
krogedal | 0:441289ea4e29 | 68 | void tempRst(void); // resets distance |
krogedal | 0:441289ea4e29 | 69 | void start(void); // starts recording distance |
krogedal | 0:441289ea4e29 | 70 | void reset(void); // resets counting |
krogedal | 0:441289ea4e29 | 71 | |
krogedal | 0:441289ea4e29 | 72 | /** |
krogedal | 0:441289ea4e29 | 73 | * Read the state of the encoder. |
krogedal | 0:441289ea4e29 | 74 | * |
krogedal | 0:441289ea4e29 | 75 | * @return The current state of the encoder as a 2-bit number, where: |
krogedal | 0:441289ea4e29 | 76 | * bit 1 = The reading from channel B |
krogedal | 0:441289ea4e29 | 77 | * bit 2 = The reading from channel A |
krogedal | 0:441289ea4e29 | 78 | */ |
krogedal | 0:441289ea4e29 | 79 | int getCurrentState(void); |
krogedal | 0:441289ea4e29 | 80 | |
krogedal | 0:441289ea4e29 | 81 | /** |
krogedal | 0:441289ea4e29 | 82 | * Read the number of pulses recorded by the encoder. |
krogedal | 0:441289ea4e29 | 83 | * |
krogedal | 0:441289ea4e29 | 84 | * @return Number of pulses which have occured. |
krogedal | 0:441289ea4e29 | 85 | */ |
krogedal | 0:441289ea4e29 | 86 | int getPulses(void); |
krogedal | 0:441289ea4e29 | 87 | |
krogedal | 0:441289ea4e29 | 88 | }; |
krogedal | 0:441289ea4e29 | 89 | |
krogedal | 0:441289ea4e29 | 90 | #endif |