Wheel control software for satellite microcontroller running the motors on the Karbor
Dependencies: mbed ros_lib_melodic
Diff: src/encoder.h
- Revision:
- 6:ed47deb76adf
- Parent:
- 5:44b2454a5eea
--- a/src/encoder.h Tue Jun 08 15:04:33 2021 +0000 +++ b/src/encoder.h Tue Jun 08 20:55:08 2021 +0000 @@ -22,12 +22,10 @@ /** encoder Class * - * Class for measeuring quadrature magnetic encoders requiring internal pullups + * Class for measuring quadrature magnetic encoders requiring internal pullups * * @author Simon Krogedal * - * Written by Simon Krogedal - * * 27/05/2021 * * Team 9 4th Year project @@ -40,56 +38,26 @@ */ class encoder { - private: - int tot_clicks, temp_tot; // clicks since last distance reset - double click_rate, click_store;// clickrate - bool c_d; // left or right bool - - /** - * Update the pulse count. - * - * Called on every rising/falling edge of channels A/B. - * - * Reads the state of the channels and determines whether a pulse forward - * or backward has occured, updating the count appropriately. - */ - void encode(void); - - InterruptIn channelA_; - InterruptIn channelB_; + public: - int pulsesPerRev_; - int prevState_; - int currState_; - - volatile int pulses_; - - protected: - - Ticker sampler; /// ticker object to sample speed - double period, enc_const; /// sampling period and wheel constant - - /// Sample function called by ticker object - void sample_func(void); - - /** Get wheel speed - * @returns Wheel speed in clicks/second + /** Direction enumerator */ - double getClicks(void); /// returns clickrate - - public: + typedef enum { + Left, // Left side motor, CCW rotation is forward + Right // Right side motor, CW rotation is forward + } Side; /** Create an instance * * @param chanA Encoder channel A pin * @param chanB Encoder channel B pin * @param CPR Encoder clicks per revolution - * @param c Boolean flag to invert speeds, used to correct for left and right motors - * @param p Sampling period of the wheel speed + * @param side Left side or right side motor, defines whether counter-clockwise rotation is forwards (left) or backwards (right) (when looking at the robot from outside) + * @param Period Sampling period of the wheel speed * @note Too short samling period gives a noisy reading, too long and dynamics are lost - * @param diameter Wheel diameter + * @param diameter Wheel diameter in meters */ - encoder(PinName chanA, PinName chanB, int CPR, bool c, double p, double diameter); + encoder(PinName chanA, PinName chanB, int CPR, Side side, double Period, double diameter); /** Get wheel speed * @returns Wheel speed in meters/second @@ -134,6 +102,45 @@ */ int getPulses(void); + private: + int tot_clicks, temp_tot; // clicks since last distance reset + double click_rate, click_store;// clickrate + bool c_d; // left or right bool + + /** + * Update the pulse count. + * + * Called on every rising/falling edge of channels A/B. + * + * Reads the state of the channels and determines whether a pulse forward + * or backward has occured, updating the count appropriately. + */ + void encode(void); + + InterruptIn channelA_; + InterruptIn channelB_; + + int pulsesPerRev_; + int prevState_; + int currState_; + + volatile int pulses_; + + Side side_; + + protected: + + Ticker sampler; /// ticker object to sample speed + double period, enc_const; /// sampling period and wheel constant + + /// Sample function called by ticker object + void sample_func(void); + + /** Get wheel speed + * @returns Wheel speed in clicks/second + */ + double getClicks(void); + }; #endif \ No newline at end of file