Control project for the Lift-arm. Works with ROS Melodic
Dependencies: mbed Servo ros_lib_melodic ULN2003_StepperDriver Async_4pin_Stepper
src/encoder.h@0:441289ea4e29, 2021-05-27 (annotated)
- Committer:
- krogedal
- Date:
- Thu May 27 18:36:23 2021 +0000
- Revision:
- 0:441289ea4e29
- Child:
- 1:7c355adbc977
Set up basic motor control
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 | 0:441289ea4e29 | 19 | #define PREV_MASK 0x1 //Mask for the previous state in determining direction |
krogedal | 0:441289ea4e29 | 20 | //of rotation. |
krogedal | 0:441289ea4e29 | 21 | #define CURR_MASK 0x2 //Mask for the current state in determining direction |
krogedal | 0:441289ea4e29 | 22 | //of rotation. |
krogedal | 0:441289ea4e29 | 23 | #define INVALID 0x3 //XORing two states where both bits have changed. |
krogedal | 0:441289ea4e29 | 24 | |
krogedal | 0:441289ea4e29 | 25 | |
krogedal | 0:441289ea4e29 | 26 | |
krogedal | 0:441289ea4e29 | 27 | class encoder { |
krogedal | 0:441289ea4e29 | 28 | |
krogedal | 0:441289ea4e29 | 29 | private: |
krogedal | 0:441289ea4e29 | 30 | int tot_clicks, temp_tot; // clicks since last distance reset |
krogedal | 0:441289ea4e29 | 31 | double click_rate, click_store;// clickrate |
krogedal | 0:441289ea4e29 | 32 | bool c_d; // left or right bool |
krogedal | 0:441289ea4e29 | 33 | |
krogedal | 0:441289ea4e29 | 34 | /** |
krogedal | 0:441289ea4e29 | 35 | * Update the pulse count. |
krogedal | 0:441289ea4e29 | 36 | * |
krogedal | 0:441289ea4e29 | 37 | * Called on every rising/falling edge of channels A/B. |
krogedal | 0:441289ea4e29 | 38 | * |
krogedal | 0:441289ea4e29 | 39 | * Reads the state of the channels and determines whether a pulse forward |
krogedal | 0:441289ea4e29 | 40 | * or backward has occured, updating the count appropriately. |
krogedal | 0:441289ea4e29 | 41 | */ |
krogedal | 0:441289ea4e29 | 42 | void encode(void); |
krogedal | 0:441289ea4e29 | 43 | |
krogedal | 0:441289ea4e29 | 44 | InterruptIn channelA_; |
krogedal | 0:441289ea4e29 | 45 | InterruptIn channelB_; |
krogedal | 0:441289ea4e29 | 46 | InterruptIn index_; |
krogedal | 0:441289ea4e29 | 47 | |
krogedal | 0:441289ea4e29 | 48 | int pulsesPerRev_; |
krogedal | 0:441289ea4e29 | 49 | int prevState_; |
krogedal | 0:441289ea4e29 | 50 | int currState_; |
krogedal | 0:441289ea4e29 | 51 | |
krogedal | 0:441289ea4e29 | 52 | volatile int pulses_; |
krogedal | 0:441289ea4e29 | 53 | |
krogedal | 0:441289ea4e29 | 54 | protected: |
krogedal | 0:441289ea4e29 | 55 | |
krogedal | 0:441289ea4e29 | 56 | Ticker sampler; // ticker object to sample speed |
krogedal | 0:441289ea4e29 | 57 | double period, enc_const; // sampling period and wheel constant |
krogedal | 0:441289ea4e29 | 58 | void sample_func(void); // sample function |
krogedal | 0:441289ea4e29 | 59 | double getClicks(void); // returns clickrate |
krogedal | 0:441289ea4e29 | 60 | |
krogedal | 0:441289ea4e29 | 61 | public: |
krogedal | 0:441289ea4e29 | 62 | |
krogedal | 0:441289ea4e29 | 63 | // Constructor takes 3 encoder input pins, CPR, left or right bool, sampling period, and a wheel-size constant |
krogedal | 0:441289ea4e29 | 64 | encoder(PinName chanA, PinName chanB, int CPR, bool c, double p, double ec); |
krogedal | 0:441289ea4e29 | 65 | |
krogedal | 0:441289ea4e29 | 66 | double getSpeed(void); // returns wheel speed |
krogedal | 0:441289ea4e29 | 67 | double getDistance(void); // returns distance travelled |
krogedal | 0:441289ea4e29 | 68 | double tempDist(void); // returns distance travelled |
krogedal | 0:441289ea4e29 | 69 | void distRst(void); // resets distance |
krogedal | 0:441289ea4e29 | 70 | void tempRst(void); // resets distance |
krogedal | 0:441289ea4e29 | 71 | void start(void); // starts recording distance |
krogedal | 0:441289ea4e29 | 72 | void reset(void); // resets counting |
krogedal | 0:441289ea4e29 | 73 | |
krogedal | 0:441289ea4e29 | 74 | /** |
krogedal | 0:441289ea4e29 | 75 | * Read the state of the encoder. |
krogedal | 0:441289ea4e29 | 76 | * |
krogedal | 0:441289ea4e29 | 77 | * @return The current state of the encoder as a 2-bit number, where: |
krogedal | 0:441289ea4e29 | 78 | * bit 1 = The reading from channel B |
krogedal | 0:441289ea4e29 | 79 | * bit 2 = The reading from channel A |
krogedal | 0:441289ea4e29 | 80 | */ |
krogedal | 0:441289ea4e29 | 81 | int getCurrentState(void); |
krogedal | 0:441289ea4e29 | 82 | |
krogedal | 0:441289ea4e29 | 83 | /** |
krogedal | 0:441289ea4e29 | 84 | * Read the number of pulses recorded by the encoder. |
krogedal | 0:441289ea4e29 | 85 | * |
krogedal | 0:441289ea4e29 | 86 | * @return Number of pulses which have occured. |
krogedal | 0:441289ea4e29 | 87 | */ |
krogedal | 0:441289ea4e29 | 88 | int getPulses(void); |
krogedal | 0:441289ea4e29 | 89 | |
krogedal | 0:441289ea4e29 | 90 | }; |
krogedal | 0:441289ea4e29 | 91 | |
krogedal | 0:441289ea4e29 | 92 | #endif |