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

Dependencies:   mbed ros_lib_melodic

src/encoder.h

Committer:
krogedal
Date:
2021-05-27
Revision:
0:441289ea4e29
Child:
1:7c355adbc977

File content as of revision 0:441289ea4e29:

#ifndef KARBOT_ENCODER_H
#define KARBOT_ENCODER_H

/* Karbot encoder class
 *
 * This class is based upon the QEI class by Aaron Berk and the encoder class
 * I wrote during ESP in 2nd year
 *
 * Written by Simon Krogedal
 * 27/05/21
 * Team 9 4th Year project
 * 
 * for NUCLEO-F401RE
 * 
 */
 
#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.



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_;
        InterruptIn index_;
    
        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
        void sample_func(void);             // sample function
        double getClicks(void);             // returns clickrate
        
    public:
    
        // Constructor takes 3 encoder input pins, CPR, left or right bool, sampling period, and a wheel-size constant
        encoder(PinName chanA, PinName chanB, int CPR, bool c, double p, double ec);
        
        double getSpeed(void);      // returns wheel speed
        double getDistance(void);   // returns distance travelled
        double tempDist(void);      // returns distance travelled
        void distRst(void);         // resets distance
        void tempRst(void);         // resets distance
        void start(void);           // starts recording distance
        void reset(void);           // resets counting
        
        /**
         * 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