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

High-level routines for initializing and reading a rotary encoder. More...

#include "encoder_driver.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "stm32f4xx_hal.h"

Macros

#define STALL_THRESHOLD   5
#define STALL_COUNT   20
#define STALL_DELAY_MS   10

Functions

void setup_encoder (encoder_t *p_enc)
 Initialize encoder state.
void read_encoder (encoder_t *p_enc)
 Read and accumulate encoder ticks since last call.
void wait_for_stall (encoder_t *p_enc)
 Waits until the encoder motion stalls (no significant ticks change).

Detailed Description

High-level routines for initializing and reading a rotary encoder.

Created on: Jun 7, 2025 Author: Andrew Carr and Kai De La Cruz

Function Documentation

◆ read_encoder()

void read_encoder ( encoder_t * p_enc)

Read and accumulate encoder ticks since last call.

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)

Initialize encoder state.

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.

◆ wait_for_stall()

void wait_for_stall ( encoder_t * p_enc)

Waits until the encoder motion stalls (no significant ticks change).

Parameters
p_encPointer to encoder_t instance