EEEN21000
GROUP 30 Line Following Buggy
Loading...
Searching...
No Matches
Encoder Class Reference

Encoder class with helper functions, derived from QEI. More...

#include <encoder.h>

Inheritance diagram for Encoder:
[legend]
Collaboration diagram for Encoder:
[legend]

Public Member Functions

 Encoder (PinName A, PinName B)
 Constructor. More...
 
void start ()
 Start the routine to measure pulses periodically. More...
 
int getCounter () const
 Get current running sum of pulses. More...
 
void resetCounter ()
 Reset the running sum of pulses used for getDistance(). More...
 
double getVelocity () const
 Get current velocity. More...
 
double getDistance () const
 Get total distance travelled. More...
 
double getPPS () const
 Get current pulses per unit second (PPS). More...
 
- Public Member Functions inherited from QEI
 QEI (PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding=X2_ENCODING)
 Constructor. More...
 
void reset (void)
 Reset the encoder. More...
 
int getCurrentState (void)
 Read the state of the encoder. More...
 
int getInvalidCount (void)
 Read the number of invalid counts. More...
 
int getPulses (void)
 Read the number of pulses recorded by the encoder. More...
 
int getRevolutions (void)
 Read the number of revolutions recorded by the encoder on the index channel. More...
 
void setPulses (int newCount)
 Set the current pulse count in case you want to init it. More...
 
void setRevolutions (int newRevs)
 Set the current revolution count in case you want to init it. More...
 

Static Public Member Functions

static double getAverageAngular (const Encoder &right_encoder, const Encoder &left_encoder)
 Get average of angular velocity between two wheels. More...
 
static double getAverageVelocity (const Encoder &right_encoder, const Encoder &left_encoder)
 Get average of linear velocity between two wheels. More...
 
static double getAverageDistance (const Encoder &right_encoder, const Encoder &left_encoder)
 Get average of total distance between two wheels. More...
 

Static Public Attributes

static constexpr double CIRCUMFERENCE = DIAMETER * M_PI
 Circumference of the wheel. More...
 
static constexpr double FULL_ROTATION_SIDE_PIVOT = 2.0 * M_PI * WHEEL_DISTANCE
 Distance covered in full rotation when turning only using one wheel. More...
 
static constexpr double FULL_ROTATION_CENTER_PIVOT = M_PI * WHEEL_DISTANCE
 Distance covered in full rotation when turning using both wheels. More...
 

Private Member Functions

void m_calculateSpeed (const double &time_elapsed)
 Calculate the velocity of the wheel. More...
 
void m_stop ()
 Stop the timer and get current pulse. More...
 

Private Attributes

volatile int m_start_pulse
 Counter for previous pulse. More...
 
volatile int m_end_pulse
 Counter for latest pulse. More...
 
volatile int m_pulse_width
 Counter for pulse difference. More...
 
volatile int m_pulse_counter
 Counter for total pulses from start. More...
 
volatile double m_pulse_per_sec
 Pulse per unit second. More...
 
volatile double m_velocity
 Velocity. More...
 
Timer m_t
 Timer for high-precision clocking. More...
 
Timeout m_to
 Timeout to interrupt every POLLING_PERIOD. More...
 

Additional Inherited Members

- Public Types inherited from QEI
enum  Encoding { X2_ENCODING , X4_ENCODING }
 
typedef enum QEI::Encoding Encoding
 

Detailed Description

Encoder class with helper functions, derived from QEI.

This class call a function periodically to measure the speed of wheel. It features return values in terms of pulse per second, distance, and velocity.

Constructor & Destructor Documentation

◆ Encoder()

Encoder::Encoder ( PinName  A,
PinName  B 
)

Constructor.

Construct a new Encoder object.

Parameters
AChannel A pin.
BChannel B pin.

Member Function Documentation

◆ start()

void Encoder::start ( )

Start the routine to measure pulses periodically.

Start timer and get current pulse. Timeout for every POLLING_PERIOD.

◆ getCounter()

int Encoder::getCounter ( ) const

Get current running sum of pulses.

Returns
int Total number of pulses after start.

◆ resetCounter()

void Encoder::resetCounter ( )

Reset the running sum of pulses used for getDistance().

◆ getVelocity()

double Encoder::getVelocity ( ) const

Get current velocity.

Returns
double Velocity of the wheel.

◆ getDistance()

double Encoder::getDistance ( ) const

Get total distance travelled.

The distance can be calculated from known values of COUNTS_PER_REV and CIRCUMFERENCE which is defined by \(distance = \frac{pulse\ counter\ \cdot\ cirmcumference}{2\ \cdot\ CPR}\\\)

Returns
double Total distance (metre) travelled by the wheel.

◆ getPPS()

double Encoder::getPPS ( ) const

Get current pulses per unit second (PPS).

Returns
double PPS of the wheel.

◆ getAverageAngular()

static double Encoder::getAverageAngular ( const Encoder right_encoder,
const Encoder left_encoder 
)
static

Get average of angular velocity between two wheels.

+ve indicates tilting to the right. -ve indicates tilting to the left.

Parameters
right_encoderRight Wheel encoder
left_encoderLeft Wheel encoder
Returns
double Angular velocity (deg/s) of the buggy.

◆ getAverageVelocity()

static double Encoder::getAverageVelocity ( const Encoder right_encoder,
const Encoder left_encoder 
)
static

Get average of linear velocity between two wheels.

+ve indicates forward direction. -ve indicates reverse direction.

Parameters
right_encoderRight Wheel encoder.
left_encoderLeft Wheel encoder.
Returns
double Linear velocity (m/s) of the buggy.

◆ getAverageDistance()

static double Encoder::getAverageDistance ( const Encoder right_encoder,
const Encoder left_encoder 
)
static

Get average of total distance between two wheels.

+ve indicates forward direction. -ve indicates reverse direction.

Parameters
right_encoderRight Wheel encoder.
left_encoderLeft Wheel encoder.
Returns
double Average distance of the buggy.

◆ m_calculateSpeed()

void Encoder::m_calculateSpeed ( const double &  time_elapsed)
private

Calculate the velocity of the wheel.

The velocity can be calculated from known values of COUNTS_PER_REV CPR and CIRCUMFERENCE which is defined by \(velocity = \frac{pulse\ width\ \cdot\ cirmcumference}{2\ \cdot\ CPR \ \cdot \ time}\\\)

Parameters
time_elapsedThe elapsed time between two pulse count.

◆ m_stop()

void Encoder::m_stop ( )
private

Stop the timer and get current pulse.

Member Data Documentation

◆ m_start_pulse

volatile int Encoder::m_start_pulse
private

Counter for previous pulse.

◆ m_end_pulse

volatile int Encoder::m_end_pulse
private

Counter for latest pulse.

◆ m_pulse_width

volatile int Encoder::m_pulse_width
private

Counter for pulse difference.

◆ m_pulse_counter

volatile int Encoder::m_pulse_counter
private

Counter for total pulses from start.

◆ m_pulse_per_sec

volatile double Encoder::m_pulse_per_sec
private

Pulse per unit second.

◆ m_velocity

volatile double Encoder::m_velocity
private

Velocity.

◆ m_t

Timer Encoder::m_t
private

Timer for high-precision clocking.

◆ m_to

Timeout Encoder::m_to
private

Timeout to interrupt every POLLING_PERIOD.

◆ CIRCUMFERENCE

constexpr double Encoder::CIRCUMFERENCE = DIAMETER * M_PI
staticconstexpr

Circumference of the wheel.

◆ FULL_ROTATION_SIDE_PIVOT

constexpr double Encoder::FULL_ROTATION_SIDE_PIVOT = 2.0 * M_PI * WHEEL_DISTANCE
staticconstexpr

Distance covered in full rotation when turning only using one wheel.

◆ FULL_ROTATION_CENTER_PIVOT

constexpr double Encoder::FULL_ROTATION_CENTER_PIVOT = M_PI * WHEEL_DISTANCE
staticconstexpr

Distance covered in full rotation when turning using both wheels.


The documentation for this class was generated from the following file: