Zap'Em Blast'Em Robots
robots that play laser tag!
Loading...
Searching...
No Matches
encoder_driver.h File Reference

Header file for encoder structure and read/setup functions. More...

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

Go to the source code of this file.

Data Structures

struct  encoder_t
 Structure to hold encoder task configuration and state. More...

Functions

void setup_encoder (encoder_t *p_enc)
 Initializes the encoder by setting its zero reference.
void read_encoder (encoder_t *p_enc)
 Reads encoder ticks and applies wrap-around correction.

Detailed Description

Header file for encoder structure and read/setup functions.

Author
Andrew Carr and Kai De La Cruz

Created on: Jun 7, 2025

Function Documentation

◆ read_encoder()

void read_encoder ( encoder_t * p_enc)

Reads encoder ticks and applies wrap-around correction.

Updates cumulative tick count and stores the latest tick snapshot.

Parameters
p_encPointer to encoder_t instance to read from.

Reads encoder ticks and applies wrap-around correction.

Parameters
p_encPointer to an encoder_t instance.

Computes the difference between the current timer count and the zero reference, applies wrap-around correction based on the encoder’s auto-reload value (p_enc->ar), and updates:

  • last_ticks: snapshot of this read
  • ticks: cumulative total (signed)

Wrap-around logic: If the delta exceeds half the auto-reload range, it is adjusted by ±(ar+1) to account for rollover.

◆ setup_encoder()

void setup_encoder ( encoder_t * p_enc)

Initializes the encoder by setting its zero reference.

Parameters
p_encPointer to encoder_t instance to initialize.

Initializes the encoder by setting its zero reference.

Parameters
p_encPointer to an encoder_t instance.

Captures the current hardware timer counter as the zero reference, and clears both the cumulative tick count and the last-read snapshot.