Files
PlantWater/main/motor_control.h

97 lines
3.4 KiB
C

#ifndef MOTOR_CONTROL_H
#define MOTOR_CONTROL_H
#include <stdbool.h>
#include "esp_err.h"
// TB6612FNG GPIO assignments
#define MOTOR_AIN1_GPIO 4 // Pump 1 Direction
#define MOTOR_AIN2_GPIO 5 // Pump 1 Direction
#define MOTOR_BIN1_GPIO 6 // Pump 2 Direction
#define MOTOR_BIN2_GPIO 7 // Pump 2 Direction
#define MOTOR_PWMA_GPIO 8 // Pump 1 Speed (PWM)
#define MOTOR_PWMB_GPIO 9 // Pump 2 Speed (PWM)
#define MOTOR_STBY_GPIO 10 // Standby (Active Low)
// PWM Configuration
#define MOTOR_PWM_FREQ_HZ 5000 // 5kHz PWM frequency
#define MOTOR_PWM_RESOLUTION 8 // 8-bit resolution (0-255)
#define MOTOR_PWM_MAX_DUTY 255 // Maximum duty cycle
// Safety Configuration
#define MOTOR_DEFAULT_SPEED 80 // Default pump speed (%)
#define MOTOR_MIN_SPEED 20 // Minimum pump speed (%)
#define MOTOR_MAX_RUNTIME_MS 30000 // Maximum runtime (30 seconds)
#define MOTOR_MIN_INTERVAL_MS 300000 // Minimum interval between runs (5 minutes)
#define MOTOR_SOFT_START_TIME_MS 500 // Soft start ramp time
// Motor IDs
typedef enum {
MOTOR_PUMP_1 = 1,
MOTOR_PUMP_2 = 2,
MOTOR_PUMP_MAX
} motor_id_t;
// Motor states
typedef enum {
MOTOR_STATE_STOPPED = 0,
MOTOR_STATE_RUNNING,
MOTOR_STATE_ERROR,
MOTOR_STATE_COOLDOWN
} motor_state_t;
// Motor direction (pumps are unidirectional, but driver supports both)
typedef enum {
MOTOR_DIR_FORWARD = 0,
MOTOR_DIR_REVERSE
} motor_dir_t;
// Motor runtime statistics
typedef struct {
uint32_t total_runtime_ms; // Total runtime in milliseconds
uint32_t last_run_duration_ms; // Last run duration
int64_t last_run_timestamp; // Timestamp of last run
uint32_t run_count; // Total number of runs
uint32_t error_count; // Total number of errors
} motor_stats_t;
// Callbacks
typedef void (*motor_state_callback_t)(motor_id_t id, motor_state_t state);
typedef void (*motor_error_callback_t)(motor_id_t id, const char* error);
// Motor control functions
esp_err_t motor_control_init(void);
esp_err_t motor_control_deinit(void);
// Basic control
esp_err_t motor_start(motor_id_t id, uint8_t speed_percent);
esp_err_t motor_stop(motor_id_t id);
esp_err_t motor_stop_all(void);
esp_err_t motor_emergency_stop(void);
// Advanced control
esp_err_t motor_start_timed(motor_id_t id, uint8_t speed_percent, uint32_t duration_ms);
esp_err_t motor_pulse(motor_id_t id, uint8_t speed_percent, uint32_t on_time_ms, uint32_t off_time_ms, uint32_t cycles);
esp_err_t motor_set_speed(motor_id_t id, uint8_t speed_percent);
// Status and configuration
motor_state_t motor_get_state(motor_id_t id);
bool motor_is_running(motor_id_t id);
bool motor_is_cooldown(motor_id_t id);
uint32_t motor_get_runtime_ms(motor_id_t id);
esp_err_t motor_get_stats(motor_id_t id, motor_stats_t *stats);
// Safety configuration
esp_err_t motor_set_max_runtime(motor_id_t id, uint32_t max_runtime_ms);
esp_err_t motor_set_min_interval(motor_id_t id, uint32_t min_interval_ms);
esp_err_t motor_set_speed_limits(motor_id_t id, uint8_t min_speed, uint8_t max_speed);
// Callbacks
void motor_register_state_callback(motor_state_callback_t callback);
void motor_register_error_callback(motor_error_callback_t callback);
// Calibration and testing
esp_err_t motor_test_run(motor_id_t id, uint32_t duration_ms);
esp_err_t motor_calibrate_flow(motor_id_t id);
#endif // MOTOR_CONTROL_H