rampering! fixed ramp times but works
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
This commit is contained in:
parent
1bdfc64972
commit
c57f25af03
2 changed files with 144 additions and 17 deletions
|
@ -80,6 +80,7 @@ void app_main(void) {
|
||||||
sprintf(name, "%s-%02X", TAG, mac[5]);
|
sprintf(name, "%s-%02X", TAG, mac[5]);
|
||||||
|
|
||||||
uint8_t tank_levels[] = { 100, 100, 100, 100 };
|
uint8_t tank_levels[] = { 100, 100, 100, 100 };
|
||||||
|
uint16_t pump_ramp_ms[] = { 500, 500, 500, 500 };
|
||||||
|
|
||||||
cfglr_handle_t cfglr_handle = {
|
cfglr_handle_t cfglr_handle = {
|
||||||
.namespace = "configulator",
|
.namespace = "configulator",
|
||||||
|
@ -91,6 +92,7 @@ void app_main(void) {
|
||||||
CFGLR_ELEMENT_STR("device_name", 32, name, CFGLR_SIGNALER_IDF_EVENT()),
|
CFGLR_ELEMENT_STR("device_name", 32, name, CFGLR_SIGNALER_IDF_EVENT()),
|
||||||
CFGLR_ELEMENT_BIN("tank_levels", 4, tank_levels, 4, CFGLR_SIGNALER_IDF_EVENT()),
|
CFGLR_ELEMENT_BIN("tank_levels", 4, tank_levels, 4, CFGLR_SIGNALER_IDF_EVENT()),
|
||||||
CFGLR_ELEMENT_BIN("pump_pins", 4, pump_gpio_map, 4, CFGLR_SIGNALER_IDF_EVENT()),
|
CFGLR_ELEMENT_BIN("pump_pins", 4, pump_gpio_map, 4, CFGLR_SIGNALER_IDF_EVENT()),
|
||||||
|
CFGLR_ELEMENT_BIN("pump_ramp_ms", sizeof(uint16_t) * 4, pump_ramp_ms, sizeof(uint16_t) * 4, CFGLR_SIGNALER_IDF_EVENT()),
|
||||||
{ NULL },
|
{ NULL },
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
159
main/pumps.c
159
main/pumps.c
|
@ -1,8 +1,10 @@
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/timers.h"
|
#include "freertos/timers.h"
|
||||||
|
#include "freertos/queue.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
|
#include "driver/ledc.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
@ -12,12 +14,42 @@
|
||||||
|
|
||||||
uint8_t running = 0;
|
uint8_t running = 0;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
PUMP_STATE_IDLE = 0,
|
||||||
|
PUMP_STATE_RAMPING_UP,
|
||||||
|
PUMP_STATE_RUNNING,
|
||||||
|
PUMP_STATE_RAMPING_DOWN,
|
||||||
|
} pump_states_e;
|
||||||
|
|
||||||
TimerHandle_t pump_timers[PUMPS];
|
TimerHandle_t pump_timers[PUMPS];
|
||||||
|
QueueHandle_t pump_step_queue;
|
||||||
|
|
||||||
|
static pump_states_e pump_states[PUMPS] = { PUMP_STATE_IDLE };
|
||||||
static uint8_t pumps_duration[PUMPS] = {0};
|
static uint8_t pumps_duration[PUMPS] = {0};
|
||||||
static uint8_t pumps_enabled[PUMPS] = {0};
|
static uint8_t pumps_enabled[PUMPS] = {0};
|
||||||
static uint8_t pumps_running[PUMPS] = {0};
|
static uint8_t pumps_running[PUMPS] = {0};
|
||||||
static uint8_t safety = 1;
|
static uint8_t safety = 1;
|
||||||
|
|
||||||
|
#define RAMP_UP_TIME (500)
|
||||||
|
#define RAMP_DOWN_TIME (250)
|
||||||
|
|
||||||
|
#define LEDC_HS_TIMER LEDC_TIMER_0
|
||||||
|
#define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE
|
||||||
|
#define LEDC_LS_TIMER LEDC_TIMER_1
|
||||||
|
#define LEDC_LS_MODE LEDC_LOW_SPEED_MODE
|
||||||
|
|
||||||
|
#define LEDC_CONFIG(CHANNEL_NO, GPIO) (ledc_channel_config_t){\
|
||||||
|
.channel = CHANNEL_NO,\
|
||||||
|
.duty = 0,\
|
||||||
|
.gpio_num = GPIO,\
|
||||||
|
.speed_mode = LEDC_LOW_SPEED_MODE,\
|
||||||
|
.hpoint = 0,\
|
||||||
|
.timer_sel = LEDC_TIMER_1,\
|
||||||
|
.flags.output_invert = 0,\
|
||||||
|
}
|
||||||
|
|
||||||
|
ledc_channel_config_t ledc_motor_channels[4] = {};
|
||||||
|
|
||||||
void pumps_update_config() {
|
void pumps_update_config() {
|
||||||
ESP_LOGI(TAG, "writing config");
|
ESP_LOGI(TAG, "writing config");
|
||||||
|
|
||||||
|
@ -79,9 +111,7 @@ void pump_timer_done(TimerHandle_t timer) {
|
||||||
uint8_t idx = (pcTimerGetName(timer) - 0x48);
|
uint8_t idx = (pcTimerGetName(timer) - 0x48);
|
||||||
ESP_LOGD(TAG, "pump done: %d", idx);
|
ESP_LOGD(TAG, "pump done: %d", idx);
|
||||||
|
|
||||||
running--;
|
xQueueSend(pump_step_queue, &idx, portMAX_DELAY);
|
||||||
|
|
||||||
pumps_set_enabled(idx, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void pumps_stop() {
|
void pumps_stop() {
|
||||||
|
@ -96,24 +126,80 @@ void pumps_stop() {
|
||||||
void pumps_run() {
|
void pumps_run() {
|
||||||
if(running > 0) return;
|
if(running > 0) return;
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Running pumps");
|
||||||
|
|
||||||
for(uint8_t i = 0; i < PUMPS; i++) {
|
for(uint8_t i = 0; i < PUMPS; i++) {
|
||||||
if(pumps_duration[i] == 0) continue;
|
//if(pumps_duration[i] == 0 || pumps_enabled[i] != 1) continue;
|
||||||
|
|
||||||
|
if(pump_states[i] != PUMP_STATE_IDLE) {
|
||||||
|
ESP_LOGW(TAG, "Attempting to start pump in non-IDLE state. idx: %d state: %d", i, pump_states[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
ledc_set_fade_with_time(ledc_motor_channels[i].speed_mode, ledc_motor_channels[i].channel, 8192, RAMP_UP_TIME);
|
||||||
|
ledc_fade_start(ledc_motor_channels[i].speed_mode, ledc_motor_channels[i].channel, LEDC_FADE_NO_WAIT);
|
||||||
|
|
||||||
|
pump_states[i] = PUMP_STATE_RAMPING_UP;
|
||||||
|
|
||||||
running++;
|
running++;
|
||||||
|
ESP_LOGI(TAG, "Pump %d started", i);
|
||||||
pump_timers[i] = xTimerCreate((const char *)(0x48 + i), (pumps_duration[i] * 250 / portTICK_PERIOD_MS), pdFALSE, (void*)0, pump_timer_done);
|
|
||||||
xTimerStart(pump_timers[i], 0);
|
|
||||||
|
|
||||||
pumps_set_enabled(i, 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void pump_step_task() {
|
||||||
|
uint8_t idx;
|
||||||
|
BaseType_t awoke = pdFALSE;
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Starting pump stepping task");
|
||||||
|
|
||||||
|
while(xQueueReceive(pump_step_queue, &idx, &awoke)) {
|
||||||
|
ESP_LOGI(TAG, "step %d complete for %d", pump_states[idx], idx);
|
||||||
|
|
||||||
|
if(pump_states[idx] == PUMP_STATE_RAMPING_UP) {
|
||||||
|
uint16_t pump_time = pumps_duration[idx] * 250 - (RAMP_UP_TIME + RAMP_DOWN_TIME) / 2;
|
||||||
|
|
||||||
|
if(RAMP_UP_TIME + RAMP_DOWN_TIME / 2 > pumps_duration[idx] * 250) {
|
||||||
|
ESP_LOGW(TAG, "Pump %d total ramp time great that duration, running for 1 Tick", idx);
|
||||||
|
|
||||||
|
pump_time = 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Running pump %d for %dms", idx, pump_time);
|
||||||
|
|
||||||
|
xTimerChangePeriod(pump_timers[idx], pump_time / portTICK_PERIOD_MS, portMAX_DELAY);
|
||||||
|
xTimerStart(pump_timers[idx], portMAX_DELAY);
|
||||||
|
|
||||||
|
pump_states[idx] = PUMP_STATE_RUNNING;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
} else if(pump_states[idx] == PUMP_STATE_RUNNING) {
|
||||||
|
ledc_set_fade_with_time(ledc_motor_channels[idx].speed_mode, ledc_motor_channels[idx].channel, 0, RAMP_UP_TIME);
|
||||||
|
ledc_fade_start(ledc_motor_channels[idx].speed_mode, ledc_motor_channels[idx].channel, LEDC_FADE_NO_WAIT);
|
||||||
|
|
||||||
|
pump_states[idx] = PUMP_STATE_RAMPING_DOWN;
|
||||||
|
continue;
|
||||||
|
} else if(pump_states[idx] == PUMP_STATE_RAMPING_DOWN) {
|
||||||
|
ESP_LOGI(TAG, "Pump cycles complete: %d", idx);
|
||||||
|
pump_states[idx] = PUMP_STATE_IDLE;
|
||||||
|
|
||||||
|
running--;
|
||||||
|
} else {
|
||||||
|
ESP_LOGE(TAG, "Pump step fired with unknown state: %d", pump_states[idx]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static IRAM_ATTR bool ledc_fade_end_event(const ledc_cb_param_t *param, void *parg) {
|
||||||
|
BaseType_t awoke = pdFALSE;
|
||||||
|
|
||||||
|
xQueueSendFromISR(pump_step_queue, parg, &awoke);
|
||||||
|
|
||||||
|
return (awoke == pdTRUE);
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t pumps_io_init() {
|
uint8_t pumps_io_init() {
|
||||||
gpio_config_t io_conf;
|
gpio_config_t io_conf;
|
||||||
|
|
||||||
// put the output high before initializing
|
for(uint8_t i = 0 ; i < PUMPS; i++) {
|
||||||
// to prevent relays from clacking
|
|
||||||
for(uint8_t i = 0 ; i < 4; i++) {
|
|
||||||
gpio_set_level(pump_gpio_map[i], 0);
|
gpio_set_level(pump_gpio_map[i], 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,15 +213,12 @@ uint8_t pumps_io_init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t pumps_init() {
|
uint8_t pumps_init() {
|
||||||
|
|
||||||
// zero out pump enabled, duration
|
// zero out pump enabled, duration
|
||||||
memset(&pumps_enabled, 0, sizeof(uint8_t) * PUMPS);
|
memset(&pumps_enabled, 0, sizeof(uint8_t) * PUMPS);
|
||||||
memset(&pumps_duration, 5, sizeof(uint8_t) * PUMPS);
|
memset(&pumps_duration, 5, sizeof(uint8_t) * PUMPS);
|
||||||
|
|
||||||
for(uint8_t i = 0; i < PUMPS; i++) {
|
// this should neve go above 4, but just in case?
|
||||||
gpio_set_level(pump_gpio_map[i], 0);
|
pump_step_queue = xQueueCreate(PUMPS * 4, sizeof(uint8_t));
|
||||||
pump_timers[i] = xTimerCreate((const char *)(0x48 + i), (pumps_duration[i] * 1000 / portTICK_PERIOD_MS), pdFALSE, (void*)0, pump_timer_done);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size = sizeof(uint8_t) * PUMPS;
|
size_t size = sizeof(uint8_t) * PUMPS;
|
||||||
if(!nvs_get_blob(config_handle, "pumps_duration", &pumps_duration, &size)) {
|
if(!nvs_get_blob(config_handle, "pumps_duration", &pumps_duration, &size)) {
|
||||||
|
@ -144,8 +227,50 @@ uint8_t pumps_init() {
|
||||||
nvs_set_blob(config_handle, "pumps_duration", &pumps_duration, size);
|
nvs_set_blob(config_handle, "pumps_duration", &pumps_duration, size);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ledc_timer_config_t ledc_timer = {
|
||||||
|
.duty_resolution = LEDC_TIMER_13_BIT, // resolution of PWM duty
|
||||||
|
.freq_hz = 4000, // frequency of PWM signal
|
||||||
|
.speed_mode = LEDC_LS_MODE, // timer mode
|
||||||
|
.timer_num = LEDC_LS_TIMER, // timer index
|
||||||
|
.clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock
|
||||||
|
};
|
||||||
|
|
||||||
|
ledc_timer_config(&ledc_timer);
|
||||||
|
|
||||||
|
ledc_timer.speed_mode = LEDC_HS_MODE;
|
||||||
|
ledc_timer.timer_num = LEDC_HS_TIMER;
|
||||||
|
|
||||||
|
ledc_timer_config(&ledc_timer);
|
||||||
|
|
||||||
|
for(uint8_t i = 0; i < PUMPS; i++) {
|
||||||
|
gpio_set_level(pump_gpio_map[i], 0);
|
||||||
|
|
||||||
|
pump_timers[i] = xTimerCreate((const char *)(0x48 + i), 1, pdFALSE, (void*)0, pump_timer_done);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(uint8_t i = 0; i < PUMPS; i++) {
|
||||||
|
ledc_motor_channels[i] = LEDC_CONFIG(i, pump_gpio_map[i]);
|
||||||
|
ledc_channel_config(&ledc_motor_channels[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
ledc_cbs_t callbacks = {
|
||||||
|
.fade_cb = ledc_fade_end_event
|
||||||
|
};
|
||||||
|
|
||||||
|
ledc_fade_func_install(0);
|
||||||
|
|
||||||
|
uint8_t *idx;
|
||||||
|
|
||||||
|
for(uint8_t i = 0; i < PUMPS; i++) {
|
||||||
|
idx = malloc(1);
|
||||||
|
memcpy(idx, &i, 1);
|
||||||
|
ledc_cb_register(ledc_motor_channels[i].speed_mode, ledc_motor_channels[i].channel, &callbacks, idx);
|
||||||
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "pumps_enabled: %d %d %d %d", pumps_enabled[0], pumps_enabled[1], pumps_enabled[2], pumps_enabled[3]);
|
ESP_LOGI(TAG, "pumps_enabled: %d %d %d %d", pumps_enabled[0], pumps_enabled[1], pumps_enabled[2], pumps_enabled[3]);
|
||||||
ESP_LOGI(TAG, "pumps_duration: %d %d %d %d", pumps_duration[0], pumps_duration[1], pumps_duration[2], pumps_duration[3]);
|
ESP_LOGI(TAG, "pumps_duration: %d %d %d %d", pumps_duration[0], pumps_duration[1], pumps_duration[2], pumps_duration[3]);
|
||||||
|
|
||||||
|
xTaskCreate(pump_step_task, "ramp", 4024, NULL, tskIDLE_PRIORITY + 4, NULL);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue