ME 507 Romi++
Cylindrical Differential Drive Robot created for ME 507 at Cal Poly in Spring 2025
Loading...
Searching...
No Matches
encoder.h File Reference

Quadrature encoder interface for STM32 timers. More...

#include "time_utils.h"
#include <stm32f4xx_hal.h>

Go to the source code of this file.

Classes

struct  encoder_t
 Structure for tracking encoder state and motion data. More...

Functions

void encoder_init (encoder_t *encoder, TIM_HandleTypeDef *htim)
 Initializes an encoder structure with a given timer.
void encoder_update (encoder_t *encoder)
 Updates the encoder's internal position, delta, and speed.
int32_t encoder_getposition (encoder_t *encoder)
 Returns the current position (accumulated counts) of the encoder.
int32_t encoder_getdelta (encoder_t *encoder)
 Returns the most recent change in encoder count.
float encoder_getspeed (encoder_t *encoder)
 Returns the current speed in counts per second.
void encoder_reset (encoder_t *encoder)
 Resets encoder position and delta tracking.

Detailed Description

Quadrature encoder interface for STM32 timers.

Provides data structures and functions for tracking position, velocity, and delta counts using hardware timers configured in encoder mode.

Function Documentation

◆ encoder_getdelta()

int32_t encoder_getdelta ( encoder_t * encoder)

Returns the most recent change in encoder count.

Parameters
encoderPointer to encoder structure.
Returns
Signed 32-bit count delta since last update.

Returns the most recent change in encoder count.

Parameters
encoderPointer to encoder structure.
Returns
Count difference since the last update.

◆ encoder_getposition()

int32_t encoder_getposition ( encoder_t * encoder)

Returns the current position (accumulated counts) of the encoder.

Parameters
encoderPointer to encoder structure.
Returns
Signed 32-bit position count.

Returns the current position (accumulated counts) of the encoder.

Parameters
encoderPointer to encoder structure.
Returns
Current position in encoder counts.

◆ encoder_getspeed()

float encoder_getspeed ( encoder_t * encoder)

Returns the current speed in counts per second.

Parameters
encoderPointer to encoder structure.
Returns
Speed in CPS (counts per second).

Returns the current speed in counts per second.

Parameters
encoderPointer to encoder structure.
Returns
Speed in counts per second (CPS).

◆ encoder_init()

void encoder_init ( encoder_t * encoder,
TIM_HandleTypeDef * htim )

Initializes an encoder structure with a given timer.

Parameters
encoderPointer to encoder structure.
htimPointer to initialized TIM_HandleTypeDef configured for encoder mode.

Initializes an encoder structure with a given timer.

Parameters
encoderPointer to encoder structure to initialize.
htimPointer to a TIM_HandleTypeDef configured in encoder mode.

◆ encoder_reset()

void encoder_reset ( encoder_t * encoder)

Resets encoder position and delta tracking.

Parameters
encoderPointer to encoder structure.

Resets encoder position and delta tracking.

Parameters
encoderPointer to encoder structure.

◆ encoder_update()

void encoder_update ( encoder_t * encoder)

Updates the encoder's internal position, delta, and speed.

Parameters
encoderPointer to encoder structure.

Updates the encoder's internal position, delta, and speed.

Computes the count delta between updates and handles overflow. Speed is calculated in counts per second (CPS).

Parameters
encoderPointer to encoder structure to update.