Generic Incremental Rotary Encoder

Generic incremental rotary encoder to measur RPM and rotation count. More...

Detailed Description

Generic incremental rotary encoder to measur RPM and rotation count.

Description

This is a driver for a generic incremental rotary encoder. These sensors are most often used with motors. An example of such an encoder can be found here

The sensor generates a fixed number of pulses per rotation on two output pins. These signals are phase-shifted by 90 degrees, enabling the detection of rotation direction.

These encoders typically have four wires:

Phase A should be connected to the pin configured as the interrupt pin or the QDEC A pin, while Phase B (the shifted signal) should be connected to the direction pin or QDEC B pin. If the Phase A and Phase B connections are swapped, the detected rotation direction will be reversed.

The driver provides functions to read the current RPM and the total number of revolutions (in hundredths) since the last measurement.

Configuration options are available via Kconfig to specify the number of pulses per rotation, the maximum RPM, and the gear reduction ratio (in tenths).

Backends

This driver supports two backends for reading the encoder signals:

To use either of the backends add the corresponding module to your application Makefile:

USEMODULE += inc_encoder_hardware

or

USEMODULE += inc_encoder_software

GPIO Interrupt Backend

The GPIO interrupt backend uses interrupts to count the number of rising edges on Phase A and determines the rotation direction based on the state of Phase B. The RPM calculation is done based on the delta time between the last two rising edges. If only one phase is connected, the driver will still count the pulses, but there will be no direction detection.

QDEC Backend

The QDEC backend uses the microcontroller's Quadrature Decoder peripheral to handle the counting and direction detection in hardware. The RPM calculation is done periodically based on the pulse count provided by the QDEC peripheral. If only one phase is connected, the QDEC peripheral will not count any pulses. The period for RPM calculation can be configured via Kconfig.

SAUL Interface

This driver implements the SAUL sensor interface. It provides two SAUL devices:

Note
After 327 revolutions without reading and resetting the revolution counter, the phydat value will overflow. Use the regular driver interface instead of SAUL if necessary.

Files

file  inc_encoder_constants.h
 Constants used in the incremental rotary encoder driver.
 
file  inc_encoder_params.h
 Default configuration for a generic incremental rotary encoder.
 
file  inc_encoder.h
 

Data Structures

struct  inc_encoder_params_t
 Device initialization parameters. More...
 
struct  inc_encoder_t
 Device descriptor for the driver. More...
 

Functions

int inc_encoder_init (inc_encoder_t *dev, const inc_encoder_params_t *params)
 Initialize the given device. More...
 
int inc_encoder_read_rpm (inc_encoder_t *dev, int32_t *rpm)
 Read the current RPM of the motor. More...
 
int inc_encoder_read_reset_ceti_revs (inc_encoder_t *dev, int32_t *pulse_counter)
 Read and reset number of revolutions since the last readout in hundredths. More...
 

Function Documentation

◆ inc_encoder_init()

int inc_encoder_init ( inc_encoder_t dev,
const inc_encoder_params_t params 
)

Initialize the given device.

Parameters
[in,out]devDevice descriptor of incremental rotary encoder
[in]paramsInitialization parameters
Return values
0on success
-EIOon failure setting up the pins
-EINVALon invalid qdec configuration

◆ inc_encoder_read_reset_ceti_revs()

int inc_encoder_read_reset_ceti_revs ( inc_encoder_t dev,
int32_t *  pulse_counter 
)

Read and reset number of revolutions since the last readout in hundredths.

Parameters
[in]devDevice descriptor of incremental rotary encoder
[out]pulse_counterNumber of revolutions since the last read in hundredths. Negative revolutions signal counter clock wise rotations.
Returns
0 on success

◆ inc_encoder_read_rpm()

int inc_encoder_read_rpm ( inc_encoder_t dev,
int32_t *  rpm 
)

Read the current RPM of the motor.

Parameters
[in]devDevice descriptor of incremental rotary encoder
[out]rpmRevolutions per minute. Negative RPM responds to counter clock wise rotation.
Returns
0 on success