This is a library for processing encoder

ENCODER_PROCESSOR.h

Committer:
benson516
Date:
2017-05-08
Revision:
0:6614a0ae9ae8

File content as of revision 0:6614a0ae9ae8:

/**
 * Note: This is a cross-platform version which is separated from hardware.
 * ---------------------------------------------------------------------------
 * This module is modified by Chun-Feng Huang for abstracting and more functionality.
 * Modified by: Chun-Feng Huang
 * E-mail: bens0516@gmail.com
 *
*/

//----------------------------------//
/**
 * @author Aaron Berk
 *
 * @section LICENSE
 *
 * Copyright (c) 2010 ARM Limited
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @section DESCRIPTION
 *
 * Quadrature Encoder Interface.
 *
 * A quadrature encoder consists of two code tracks on a disc which are 90
 * degrees out of phase. It can be used to determine how far a wheel has
 * rotated, relative to a known starting position.
 *
 * Only one code track changes at a time leading to a more robust system than
 * a single track, because any jitter around any edge won't cause a state
 * change as the other track will remain constant.
 *
 * Encoders can be a homebrew affair, consisting of infrared emitters/receivers
 * and paper code tracks consisting of alternating black and white sections;
 * alternatively, complete disk and PCB emitter/receiver encoder systems can
 * be bought, but the interface, regardless of implementation is the same.
 *
 *               +-----+     +-----+     +-----+
 * Channel A     |  ^  |     |     |     |     |
 *            ---+  ^  +-----+     +-----+     +-----
 *               ^  ^
 *               ^  +-----+     +-----+     +-----+
 * Channel B     ^  |     |     |     |     |     |
 *            ------+     +-----+     +-----+     +-----
 *               ^  ^
 *               ^  ^
 *               90deg
 *
 * The interface uses X2 encoding by default which calculates the pulse count
 * based on reading the current state after each rising and falling edge of
 * channel A.
 *
 *               +-----+     +-----+     +-----+
 * Channel A     |     |     |     |     |     |
 *            ---+     +-----+     +-----+     +-----
 *               ^     ^     ^     ^     ^
 *               ^  +-----+  ^  +-----+  ^  +-----+
 * Channel B     ^  |  ^  |  ^  |  ^  |  ^  |     |
 *            ------+  ^  +-----+  ^  +-----+     +--
 *               ^     ^     ^     ^     ^
 *               ^     ^     ^     ^     ^
 * Pulse count 0 1     2     3     4     5  ...
 *
 * This interface can also use X4 encoding which calculates the pulse count
 * based on reading the current state after each rising and falling edge of
 * either channel.
 *
 *               +-----+     +-----+     +-----+
 * Channel A     |     |     |     |     |     |
 *            ---+     +-----+     +-----+     +-----
 *               ^     ^     ^     ^     ^
 *               ^  +-----+  ^  +-----+  ^  +-----+
 * Channel B     ^  |  ^  |  ^  |  ^  |  ^  |     |
 *            ------+  ^  +-----+  ^  +-----+     +--
 *               ^  ^  ^  ^  ^  ^  ^  ^  ^  ^
 *               ^  ^  ^  ^  ^  ^  ^  ^  ^  ^
 * Pulse count 0 1  2  3  4  5  6  7  8  9  ...
 *
 * It defaults
 *
 * An optional index channel can be used which determines when a full
 * revolution has occured.
 *
 * If a 4 pules per revolution encoder was used, with X4 encoding,
 * the following would be observed.
 *
 *               +-----+     +-----+     +-----+
 * Channel A     |     |     |     |     |     |
 *            ---+     +-----+     +-----+     +-----
 *               ^     ^     ^     ^     ^
 *               ^  +-----+  ^  +-----+  ^  +-----+
 * Channel B     ^  |  ^  |  ^  |  ^  |  ^  |     |
 *            ------+  ^  +-----+  ^  +-----+     +--
 *               ^  ^  ^  ^  ^  ^  ^  ^  ^  ^
 *               ^  ^  ^  ^  ^  ^  ^  ^  ^  ^
 *               ^  ^  ^  +--+  ^  ^  +--+  ^
 *               ^  ^  ^  |  |  ^  ^  |  |  ^
 * Index      ------------+  +--------+  +-----------
 *               ^  ^  ^  ^  ^  ^  ^  ^  ^  ^
 * Pulse count 0 1  2  3  4  5  6  7  8  9  ...
 * Rev.  count 0          1           2
 *
 * Rotational position in degrees can be calculated by:
 *
 * (pulse count / X * N) * 360
 *
 * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number
 * of pulses per revolution.
 *
 * Linear position can be calculated by:
 *
 * (pulse count / X * N) * (1 / PPI)
 *
 * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of
 * pulses per revolution, and PPI is pulses per inch, or the equivalent for
 * any other unit of displacement. PPI can be calculated by taking the
 * circumference of the wheel or encoder disk and dividing it by the number
 * of pulses per revolution.
 */

//////////////////////////////
#ifndef ENCODER_PROCESSOR_H
#define ENCODER_PROCESSOR_H
//
#include <vector>

using std::vector;




#define PREV_MASK 0x1 // Mask for the previous state in determining direction of rotation. (0b01)
#define CURR_MASK 0x2 // Mask for the current state in determining direction of rotation. (0b10)
#define INVALID   0x3 // Indicating that both phase have changed. (0b11)

// ENCODER_PROCESSOR
////////////////////////
class ENCODER_PROCESSOR {

public:

    double Ts;

    //
    bool is_initiated; // The state of A and B should be read at first time.


    volatile int pulses_;

    int delta_count;
    vector<int> MA_window;
    size_t idx_MA_array;
    size_t size_MA_window;


    typedef enum Encoding {

        X2_ENCODING,
        X4_ENCODING

    } Encoding;

    /**
     * Constructor.
     *
     * Reads the current values on channel A and channel B to determine the
     * initial state.
     *
     * Attaches the encode function to the rise/fall interrupt edges of
     * channels A and B to perform X4 encoding.
     *
     * Attaches the index function to the rise interrupt edge of channel index
     * (if it is used) to count revolutions.
     *
     * @param channelA mbed pin for channel A input.
     * @param channelB mbed pin for channel B input.
     * @param index    mbed pin for optional index channel input,
     *                 (pass NC if not needed).
     * @param pulsesPerRev Number of pulses in one revolution.
     * @param encoding The encoding to use. Uses X2 encoding by default. X2
     *                 encoding uses interrupts on the rising and falling edges
     *                 of only channel A where as X4 uses them on both
     *                 channels.
     */
    ENCODER_PROCESSOR(int pulsesPerRevolution_in, int size_MA_window_in, double sampling_time_in, Encoding encoding = X2_ENCODING);

    // Process control
    void reset(void);

    // Main functions to be call at proper interupts
    //---------------------------------------------//
    // Call-back function for both pin A and pin B interupt (rise and fall)
    void IntrCB_pulseUpdate(int phase_A, int phase_B);
    // (Un-necessary) Call-back function for index-pin interupt
    void IntrCB_indexUpdate(void);
    //---------------------------------------------//


    //---------------------------------------------//
    // Iterate at each timer interupt
    void iterateOnce(void);
    //---------------------------------------------//


    // Get states
    int getEncoderState(void); // Get the combined AB status, A B
    int getPulses(void); // Get the total count since last reset
    int getRevolutions(void); // Get the number of revolution since last reset

    // Get results
    // Rotational speed
    double getAngularSpeed(void); // rad/s
    double getAngularSpeed_deg_s(void); // deg/s
    // Angle
    double getAngle(bool is_ranged); // rad, if is_ranged, return 0~2*PI
    double getAngle_deg(bool is_ranged); // deg, if is_ranged, return 0~360

private:

    // Encoding type
    Encoding encoding_;

    //
    int          pulsesPerRevolution;
    size_t       encoderState_pre;
    size_t       encoderState_now; // (chanA << 1) | (chanB) ,  A B

    // Unit transformation
    // Rotational speed
    double       count_2_rad_s;
    double       count_2_deg_s;
    // Angle
    double       count_2_rad;
    double       count_2_deg;


//    volatile int pulses_;
    volatile int revolutions_;

};

#endif /* ENCODER_PROCESSOR_H */