Motor control implemented

This commit is contained in:
2025-07-19 22:55:58 -06:00
parent fef8da2de2
commit 5a4c91fbd3
4 changed files with 316 additions and 17 deletions

View File

@ -12,6 +12,9 @@
#include "motor_control.h"
#include "sdkconfig.h"
// Uncomment this line to enable motor test mode with shorter intervals
// #define MOTOR_TEST_MODE
static const char *TAG = "MAIN";
// Application version
@ -76,6 +79,7 @@ static void mqtt_connected_callback(void)
"plant_watering/pump/+/speed",
"plant_watering/commands/test_pump/+",
"plant_watering/commands/emergency_stop",
"plant_watering/commands/test_mode",
"plant_watering/settings/+/+",
NULL
};
@ -161,17 +165,37 @@ static void mqtt_data_callback(const char* topic, const char* data, int data_len
// Parse JSON configuration here
} else if (strcmp(topic, "plant_watering/commands/test_pump/1") == 0) {
uint32_t duration = atoi(data);
if (duration > 0 && duration <= 10000) { // Max 10 seconds for test
if (duration > 0 && duration <= 30000) { // Max 30 seconds for test
ESP_LOGI(TAG, "Test pump 1 for %lu ms", duration);
motor_test_run(MOTOR_PUMP_1, duration);
} else {
ESP_LOGW(TAG, "Invalid test duration: %lu (max 30000ms)", duration);
}
} else if (strcmp(topic, "plant_watering/commands/test_pump/2") == 0) {
uint32_t duration = atoi(data);
if (duration > 0 && duration <= 10000) { // Max 10 seconds for test
if (duration > 0 && duration <= 30000) { // Max 30 seconds for test
ESP_LOGI(TAG, "Test pump 2 for %lu ms", duration);
motor_test_run(MOTOR_PUMP_2, duration);
} else {
ESP_LOGW(TAG, "Invalid test duration: %lu (max 30000ms)", duration);
}
} else if (strcmp(topic, "plant_watering/commands/emergency_stop") == 0) {
ESP_LOGW(TAG, "Emergency stop command received!");
motor_emergency_stop();
} else if (strcmp(topic, "plant_watering/commands/test_mode") == 0) {
if (strncmp(data, "on", data_len) == 0) {
ESP_LOGW(TAG, "Enabling test mode - short intervals");
motor_set_min_interval(MOTOR_PUMP_1, 5000); // 5 seconds
motor_set_min_interval(MOTOR_PUMP_2, 5000);
motor_set_max_runtime(MOTOR_PUMP_1, 30000); // 30 seconds
motor_set_max_runtime(MOTOR_PUMP_2, 30000);
} else if (strncmp(data, "off", data_len) == 0) {
ESP_LOGI(TAG, "Disabling test mode - normal intervals");
motor_set_min_interval(MOTOR_PUMP_1, CONFIG_WATERING_MIN_INTERVAL_MS);
motor_set_min_interval(MOTOR_PUMP_2, CONFIG_WATERING_MIN_INTERVAL_MS);
motor_set_max_runtime(MOTOR_PUMP_1, CONFIG_WATERING_MAX_DURATION_MS);
motor_set_max_runtime(MOTOR_PUMP_2, CONFIG_WATERING_MAX_DURATION_MS);
}
} else if (strncmp(topic, "plant_watering/settings/pump/", 29) == 0) {
// Parse settings commands like:
// plant_watering/settings/pump/1/max_runtime
@ -373,11 +397,21 @@ void app_main(void)
motor_register_state_callback(motor_state_change_callback);
motor_register_error_callback(motor_error_callback);
// Configure motor safety limits from Kconfig
motor_set_max_runtime(MOTOR_PUMP_1, CONFIG_WATERING_MAX_DURATION_MS);
motor_set_max_runtime(MOTOR_PUMP_2, CONFIG_WATERING_MAX_DURATION_MS);
motor_set_min_interval(MOTOR_PUMP_1, CONFIG_WATERING_MIN_INTERVAL_MS);
motor_set_min_interval(MOTOR_PUMP_2, CONFIG_WATERING_MIN_INTERVAL_MS);
// Configure motor safety limits
#ifdef MOTOR_TEST_MODE
// Use shorter limits for testing
ESP_LOGI(TAG, "MOTOR TEST MODE - Using short intervals for testing");
motor_set_max_runtime(MOTOR_PUMP_1, 30000); // 30 seconds max
motor_set_max_runtime(MOTOR_PUMP_2, 30000);
motor_set_min_interval(MOTOR_PUMP_1, 5000); // 5 seconds for testing
motor_set_min_interval(MOTOR_PUMP_2, 5000);
#else
// Use production values from Kconfig
motor_set_max_runtime(MOTOR_PUMP_1, CONFIG_WATERING_MAX_DURATION_MS);
motor_set_max_runtime(MOTOR_PUMP_2, CONFIG_WATERING_MAX_DURATION_MS);
motor_set_min_interval(MOTOR_PUMP_1, CONFIG_WATERING_MIN_INTERVAL_MS);
motor_set_min_interval(MOTOR_PUMP_2, CONFIG_WATERING_MIN_INTERVAL_MS);
#endif
// Start WiFi connection
esp_err_t ret = wifi_manager_start();