Files
stepper_base/components/direction_controller/include/direction_controller.h

164 lines
5.0 KiB
C

/*
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include "driver/gpio.h"
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/* ========== Direction Definitions ========== */
/**
* @brief Direction definition entry
*/
typedef struct {
const char *name_cn; /* Chinese name */
const char *name_en; /* English abbreviation */
float angle; /* Angle relative to South */
} direction_controller_direction_t;
/**
* @brief Direction controller configuration
*/
typedef struct {
const direction_controller_direction_t *directions; /* Custom direction list, NULL to use default */
size_t direction_count; /* Number of directions */
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
*/
typedef enum {
DIRECTION_CONTROLLER_CMD_UNKNOWN = 0,
DIRECTION_CONTROLLER_CMD_HELP,
DIRECTION_CONTROLLER_CMD_POSITION,
DIRECTION_CONTROLLER_CMD_DIRECTION,
DIRECTION_CONTROLLER_CMD_CALIBRATE
} direction_controller_cmd_t;
/**
* @brief Initialize direction controller
*
* @param config Optional configuration, NULL uses defaults
*/
void direction_controller_init(const direction_controller_config_t *config);
/**
* @brief Parse a command string
*
* @param input Input string (null-terminated)
* @param out_direction Output matched direction when command type is DIRECTION_CONTROLLER_CMD_DIRECTION
* @return Parsed command type
*/
direction_controller_cmd_t direction_controller_parse_command(const char *input,
const direction_controller_direction_t **out_direction);
/**
* @brief Find direction by name
*
* @param input Direction name (Chinese or English abbreviation)
* @return Matched direction pointer, or NULL if not found
*/
const direction_controller_direction_t *direction_controller_find_direction(const char *input);
/**
* @brief Rotate to specified angle
*
* @param target_angle Target angle relative to South
* @return ESP_OK on success
*/
esp_err_t direction_controller_rotate_to_angle(float target_angle);
/**
* @brief Rotate to specified direction by name
*
* @param input Direction name (Chinese or English abbreviation)
* @return ESP_OK on success, ESP_ERR_NOT_FOUND if name is unknown
*/
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
*/
float direction_controller_get_current_angle(void);
/**
* @brief Set current angle (does not move motor)
*/
void direction_controller_set_current_angle(float angle);
/**
* @brief Get current direction if it matches a defined direction
*/
const direction_controller_direction_t *direction_controller_get_current_direction(void);
/**
* @brief Get direction count
*/
size_t direction_controller_get_direction_count(void);
/**
* @brief Get direction by index
*/
const direction_controller_direction_t *direction_controller_get_direction(size_t index);
/**
* @brief Get rotation speed (microseconds per step)
*/
int direction_controller_get_rotate_speed_us(void);
/**
* @brief Set rotation speed (microseconds per step)
*/
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