feat: add calibration switch support

This commit is contained in:
2026-01-30 17:08:40 +08:00
parent 512c64ecff
commit a709cdb56a
3 changed files with 228 additions and 7 deletions

View File

@@ -8,6 +8,7 @@
#include <stdbool.h>
#include <stddef.h>
#include "driver/gpio.h"
#include "esp_err.h"
#ifdef __cplusplus
@@ -33,8 +34,25 @@ typedef struct {
float initial_angle; /* Initial angle relative to South */
int rotate_speed_us; /* Rotation speed (microseconds per step) */
bool init_stepper_gpio; /* Initialize stepper GPIO in init */
const struct direction_controller_calibration_t *calibration; /* Optional calibration config */
} direction_controller_config_t;
/**
* @brief Calibration switch configuration
*/
typedef struct direction_controller_calibration_t {
bool enabled; /* Enable calibration */
gpio_num_t gpio_num; /* Switch GPIO */
int active_level; /* Active logic level (0 or 1) */
bool pullup_en; /* Enable internal pull-up (if supported) */
bool pulldown_en; /* Enable internal pull-down (if supported) */
float calibration_angle; /* Angle to set when switch is active */
float step_angle_deg; /* Step angle per search iteration */
float max_sweep_deg; /* Maximum total sweep angle */
bool search_cw; /* Search direction: true=clockwise, false=counterclockwise */
int settle_delay_ms; /* Delay after each step to allow switch settle */
} direction_controller_calibration_t;
/**
* @brief Parsed command types
*/
@@ -42,7 +60,8 @@ typedef enum {
DIRECTION_CONTROLLER_CMD_UNKNOWN = 0,
DIRECTION_CONTROLLER_CMD_HELP,
DIRECTION_CONTROLLER_CMD_POSITION,
DIRECTION_CONTROLLER_CMD_DIRECTION
DIRECTION_CONTROLLER_CMD_DIRECTION,
DIRECTION_CONTROLLER_CMD_CALIBRATE
} direction_controller_cmd_t;
/**
@@ -86,6 +105,14 @@ esp_err_t direction_controller_rotate_to_angle(float target_angle);
*/
esp_err_t direction_controller_rotate_to_direction(const char *input);
/**
* @brief Calibrate direction using a switch
*
* @param config Optional calibration configuration, NULL uses config set in init
* @return ESP_OK on success
*/
esp_err_t direction_controller_calibrate(const direction_controller_calibration_t *config);
/**
* @brief Get current angle
*/
@@ -121,6 +148,16 @@ int direction_controller_get_rotate_speed_us(void);
*/
void direction_controller_set_rotate_speed_us(int rotate_speed_us);
/**
* @brief Set calibration configuration
*/
void direction_controller_set_calibration(const direction_controller_calibration_t *config);
/**
* @brief Get calibration configuration (NULL if not configured)
*/
const direction_controller_calibration_t *direction_controller_get_calibration(void);
#ifdef __cplusplus
}
#endif