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

Handle reading pulses from encoder. More...

#include <QEI.h>

Inheritance diagram for QEI:
[legend]

Public Types

enum  Encoding { X2_ENCODING , X4_ENCODING }
 
typedef enum QEI::Encoding Encoding
 

Public Member Functions

 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...
 

Private Member Functions

void encode (void)
 Update the pulse count. More...
 
void index (void)
 Called on every rising edge of channel index to update revolution count by one. More...
 

Private Attributes

Encoding encoding_
 
InterruptIn channelA_
 
InterruptIn channelB_
 
InterruptIn index_
 
int pulsesPerRev_
 
int prevState_
 
int currState_
 
volatile int invalid_
 
volatile int pulses_
 
volatile int revolutions_
 

Detailed Description

Handle reading pulses from encoder.

Member Typedef Documentation

◆ Encoding

Member Enumeration Documentation

◆ Encoding

Enumerator
X2_ENCODING 
X4_ENCODING 

Constructor & Destructor Documentation

◆ QEI()

QEI::QEI ( PinName  channelA,
PinName  channelB,
PinName  index,
int  pulsesPerRev,
Encoding  encoding = X2_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.

Parameters
channelAmbed pin for channel A input.
channelBmbed pin for channel B input.
indexmbed pin for optional index channel input, (pass NC if not needed).
pulsesPerRevNumber of pulses in one revolution.
encodingThe 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.

Member Function Documentation

◆ reset()

void QEI::reset ( void  )

Reset the encoder.

Sets the pulses and revolutions count to zero.

◆ getCurrentState()

int QEI::getCurrentState ( void  )

Read the state of the encoder.

Returns
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

◆ getInvalidCount()

int QEI::getInvalidCount ( void  )

Read the number of invalid counts.

Note
Reading this resets the counter!
Returns
the number of times the gray-code transition was violated.

◆ getPulses()

int QEI::getPulses ( void  )

Read the number of pulses recorded by the encoder.

Returns
Number of pulses which have occured.

◆ getRevolutions()

int QEI::getRevolutions ( void  )

Read the number of revolutions recorded by the encoder on the index channel.

Returns
Number of revolutions which have occured on the index channel.

◆ setPulses()

void QEI::setPulses ( int  newCount)

Set the current pulse count in case you want to init it.

Parameters
[in]newCountis the count to set it to.

◆ setRevolutions()

void QEI::setRevolutions ( int  newRevs)

Set the current revolution count in case you want to init it.

Parameters
[in]newRevsis the count to set it to.

◆ encode()

void QEI::encode ( void  )
private

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.

◆ index()

void QEI::index ( void  )
private

Called on every rising edge of channel index to update revolution count by one.

Member Data Documentation

◆ encoding_

Encoding QEI::encoding_
private

◆ channelA_

InterruptIn QEI::channelA_
private

◆ channelB_

InterruptIn QEI::channelB_
private

◆ index_

InterruptIn QEI::index_
private

◆ pulsesPerRev_

int QEI::pulsesPerRev_
private

◆ prevState_

int QEI::prevState_
private

◆ currState_

int QEI::currState_
private

◆ invalid_

volatile int QEI::invalid_
private

◆ pulses_

volatile int QEI::pulses_
private

◆ revolutions_

volatile int QEI::revolutions_
private

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