Wheel control software for satellite microcontroller running the motors on the Karbor

Dependencies:   mbed ros_lib_melodic

src/encoder.h

Committer:
krogedal
Date:
2021-06-08
Revision:
5:44b2454a5eea
Parent:
3:4b6080e86761
Child:
6:ed47deb76adf

File content as of revision 5:44b2454a5eea:

/**
 * @file encoder.h
 * This class is based upon the QEI class by Aaron Berk and the encoder class I wrote during ESP in 2nd year.
 *
 * @author Simon Krogedal
 *
 * @version 0.1
 */

#ifndef KARBOT_ENCODER_H
#define KARBOT_ENCODER_H


#include "mbed.h"
 
 
#define PREV_MASK 0x1 //Mask for the previous state in determining direction
//of rotation.
#define CURR_MASK 0x2 //Mask for the current state in determining direction
//of rotation.
#define INVALID   0x3 //XORing two states where both bits have changed.

/** encoder Class
 *
 * Class for measeuring quadrature magnetic encoders requiring internal pullups
 *
 * @author Simon Krogedal
 *
 * Written by Simon Krogedal
 *
 * 27/05/2021
 *
 * Team 9 4th Year project
 * 
 *
 * for NUCLEO-F401RE
 * 
 * @version 0.2
 *
 */
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_;
    
        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
         */
        double getClicks(void);             /// returns clickrate
        
    public:
    
        /** 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
         * @note Too short samling period gives a noisy reading, too long and dynamics are lost
         * @param diameter Wheel diameter
         */
        encoder(PinName chanA, PinName chanB, int CPR, bool c, double p, double diameter);
        
        /** Get wheel speed
         * @returns Wheel speed in meters/second
         */
        double getSpeed(void);
        
        /** Get distance travelled
         * @returns Distance travelled in meters
         */
        double getDistance(void);
        
        /** Get distance travelled
         * @returns Distance travelled in meters since last reset
         */
        double tempDist(void);
        
        /// Reset distance measurement
        void distRst(void);
        
        /// Reset distance measurement
        void tempRst(void);
        
        /// Start encoder reading
        void start(void);
        
        /// Stop encoder reading
        void reset(void);
        
        /**
         * Read the state of the encoder.
         *
         * @return The current state of the encoder as a 2-bit number, where:
         *         bit 1 = The reading from channel B
         *         bit 2 = The reading from channel A
         */
        int getCurrentState(void);
    
        /**
         * Read the number of pulses recorded by the encoder.
         *
         * @return Number of pulses which have occured.
         */
        int getPulses(void);
    
};

#endif