Compare commits
6 Commits
79a66c19d7
...
ec1b25d489
| Author | SHA1 | Date | |
|---|---|---|---|
|
ec1b25d489
|
|||
|
4eb1bb03a0
|
|||
|
7d597f3725
|
|||
|
71b632737e
|
|||
|
374647732c
|
|||
|
ae33204a87
|
@@ -263,6 +263,18 @@ void relay_chn_update_state(relay_chn_ctl_t *chn_ctl, relay_chn_state_t new_stat
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void relay_chn_execute_idle(relay_chn_ctl_t *chn_ctl);
|
||||||
|
|
||||||
|
static void relay_chn_start_timer_or_idle(relay_chn_ctl_t *chn_ctl, esp_timer_handle_t timer, uint32_t time_ms, const char* timer_name)
|
||||||
|
{
|
||||||
|
if (relay_chn_start_esp_timer_once(timer, time_ms) != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "Failed to start %s timer for ch %d", timer_name, chn_ctl->id);
|
||||||
|
// Attempt to go to a safe state.
|
||||||
|
// relay_chn_execute_idle is safe to call, it stops timers and sets state.
|
||||||
|
relay_chn_execute_idle(chn_ctl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The command issuer function.
|
* @brief The command issuer function.
|
||||||
*
|
*
|
||||||
@@ -327,21 +339,29 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
|
|||||||
// If the last run command is different from the current command, calculate the time passed
|
// If the last run command is different from the current command, calculate the time passed
|
||||||
// since the last run command stopped and decide whether to run the command immediately or wait
|
// since the last run command stopped and decide whether to run the command immediately or wait
|
||||||
uint32_t last_run_cmd_time_ms = relay_chn_run_info_get_last_run_cmd_time_ms(chn_ctl->run_info);
|
uint32_t last_run_cmd_time_ms = relay_chn_run_info_get_last_run_cmd_time_ms(chn_ctl->run_info);
|
||||||
uint32_t inertia_time_passed_ms = (uint32_t) (esp_timer_get_time() / 1000) - last_run_cmd_time_ms;
|
uint32_t current_time_ms = (uint32_t)(esp_timer_get_time() / 1000);
|
||||||
|
if (current_time_ms < last_run_cmd_time_ms) { // Timer overflow
|
||||||
|
// If timer overflowed, it's been a long time. Run immediately.
|
||||||
|
relay_chn_dispatch_cmd(chn_ctl, cmd);
|
||||||
|
} else {
|
||||||
|
uint32_t inertia_time_passed_ms = current_time_ms - last_run_cmd_time_ms;
|
||||||
|
if (inertia_time_passed_ms < CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS) {
|
||||||
uint32_t inertia_time_ms = CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS - inertia_time_passed_ms;
|
uint32_t inertia_time_ms = CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS - inertia_time_passed_ms;
|
||||||
if (inertia_time_ms > 0) {
|
|
||||||
chn_ctl->pending_cmd = cmd;
|
chn_ctl->pending_cmd = cmd;
|
||||||
relay_chn_state_t new_state = cmd == RELAY_CHN_CMD_FORWARD
|
relay_chn_state_t new_state = cmd == RELAY_CHN_CMD_FORWARD
|
||||||
? RELAY_CHN_STATE_FORWARD_PENDING : RELAY_CHN_STATE_REVERSE_PENDING;
|
? RELAY_CHN_STATE_FORWARD_PENDING : RELAY_CHN_STATE_REVERSE_PENDING;
|
||||||
relay_chn_update_state(chn_ctl, new_state);
|
relay_chn_update_state(chn_ctl, new_state);
|
||||||
// If the time passed is less than the opposite inertia time, wait for the remaining time
|
// If the time passed is less than the opposite inertia time, wait for the remaining time
|
||||||
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, inertia_time_ms);
|
if (relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, inertia_time_ms) != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "Failed to start inertia timer for ch %d", chn_ctl->id);
|
||||||
|
relay_chn_execute_idle(chn_ctl);
|
||||||
}
|
}
|
||||||
else {
|
} else {
|
||||||
// If the time passed is more than the opposite inertia time, run the command immediately
|
// If the time passed is more than the opposite inertia time, run the command immediately
|
||||||
relay_chn_dispatch_cmd(chn_ctl, cmd);
|
relay_chn_dispatch_cmd(chn_ctl, cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case RELAY_CHN_STATE_FORWARD:
|
case RELAY_CHN_STATE_FORWARD:
|
||||||
@@ -366,7 +386,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
|
|||||||
relay_chn_state_t new_state = cmd == RELAY_CHN_CMD_FORWARD
|
relay_chn_state_t new_state = cmd == RELAY_CHN_CMD_FORWARD
|
||||||
? RELAY_CHN_STATE_FORWARD_PENDING : RELAY_CHN_STATE_REVERSE_PENDING;
|
? RELAY_CHN_STATE_FORWARD_PENDING : RELAY_CHN_STATE_REVERSE_PENDING;
|
||||||
relay_chn_update_state(chn_ctl, new_state);
|
relay_chn_update_state(chn_ctl, new_state);
|
||||||
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
|
relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "inertia");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_TILTING
|
#if CONFIG_RELAY_CHN_ENABLE_TILTING
|
||||||
@@ -434,13 +454,20 @@ static void relay_chn_execute_stop(relay_chn_ctl_t *chn_ctl)
|
|||||||
|
|
||||||
// If there is any pending command, cancel it since the STOP command is issued right after it
|
// If there is any pending command, cancel it since the STOP command is issued right after it
|
||||||
chn_ctl->pending_cmd = RELAY_CHN_CMD_NONE;
|
chn_ctl->pending_cmd = RELAY_CHN_CMD_NONE;
|
||||||
// Invalidate the channel's timer if it is active
|
|
||||||
esp_timer_stop(chn_ctl->inertia_timer);
|
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
||||||
esp_timer_stop(chn_ctl->run_limit_timer);
|
esp_timer_stop(chn_ctl->run_limit_timer);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (previous_state == RELAY_CHN_STATE_FORWARD_PENDING || previous_state == RELAY_CHN_STATE_REVERSE_PENDING) {
|
||||||
|
chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE;
|
||||||
|
// Do nothing more and let the timer set channel idle when it expires
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalidate the channel's timer if it is active
|
||||||
|
esp_timer_stop(chn_ctl->inertia_timer);
|
||||||
|
|
||||||
// Save the last run time only if the previous state was either STATE FORWARD
|
// Save the last run time only if the previous state was either STATE FORWARD
|
||||||
// or STATE_REVERSE. Then schedule a free command.
|
// or STATE_REVERSE. Then schedule a free command.
|
||||||
if (previous_state == RELAY_CHN_STATE_FORWARD || previous_state == RELAY_CHN_STATE_REVERSE) {
|
if (previous_state == RELAY_CHN_STATE_FORWARD || previous_state == RELAY_CHN_STATE_REVERSE) {
|
||||||
@@ -448,7 +475,7 @@ static void relay_chn_execute_stop(relay_chn_ctl_t *chn_ctl)
|
|||||||
relay_chn_run_info_set_last_run_cmd_time_ms(chn_ctl->run_info, (uint32_t)(esp_timer_get_time() / 1000));
|
relay_chn_run_info_set_last_run_cmd_time_ms(chn_ctl->run_info, (uint32_t)(esp_timer_get_time() / 1000));
|
||||||
// Schedule a free command for the channel
|
// Schedule a free command for the channel
|
||||||
chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE;
|
chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE;
|
||||||
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
|
relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "idle");
|
||||||
} else {
|
} else {
|
||||||
// If the channel was not running one of the run or fwd, issue a free command immediately
|
// If the channel was not running one of the run or fwd, issue a free command immediately
|
||||||
// relay_chn_dispatch_cmd(chn_ctl, RELAY_CHN_CMD_IDLE);
|
// relay_chn_dispatch_cmd(chn_ctl, RELAY_CHN_CMD_IDLE);
|
||||||
@@ -466,7 +493,7 @@ static void relay_chn_execute_forward(relay_chn_ctl_t *chn_ctl)
|
|||||||
relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_FORWARD);
|
relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_FORWARD);
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
||||||
relay_chn_start_esp_timer_once(chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000);
|
relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000, "run limit");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -480,7 +507,7 @@ static void relay_chn_execute_reverse(relay_chn_ctl_t *chn_ctl)
|
|||||||
relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_REVERSE);
|
relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_REVERSE);
|
||||||
|
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
||||||
relay_chn_start_esp_timer_once(chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000);
|
relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000, "run limit");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,7 +516,7 @@ static void relay_chn_execute_flip(relay_chn_ctl_t *chn_ctl)
|
|||||||
relay_chn_output_flip(chn_ctl->output);
|
relay_chn_output_flip(chn_ctl->output);
|
||||||
// Set an inertia on the channel to prevent any immediate movement
|
// Set an inertia on the channel to prevent any immediate movement
|
||||||
chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE;
|
chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE;
|
||||||
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
|
relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "flip inertia");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dispatch relay channel command
|
// Dispatch relay channel command
|
||||||
|
|||||||
@@ -91,6 +91,18 @@ static uint32_t relay_chn_tilt_get_required_timing_before_tilting(relay_chn_tilt
|
|||||||
return CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS - inertia_time_passed_ms;
|
return CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS - inertia_time_passed_ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void relay_chn_tilt_execute_stop(relay_chn_tilt_ctl_t *tilt_ctl);
|
||||||
|
|
||||||
|
static void relay_chn_tilt_start_timer_or_stop(relay_chn_tilt_ctl_t *tilt_ctl, esp_timer_handle_t timer, uint32_t time_ms, const char* timer_name)
|
||||||
|
{
|
||||||
|
if (relay_chn_start_esp_timer_once(timer, time_ms) != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "Failed to start %s timer for ch %d", timer_name, tilt_ctl->chn_ctl->id);
|
||||||
|
// Attempt to go to a safe state for tilt.
|
||||||
|
// relay_chn_tilt_execute_stop is safe to call, it stops timers and sets state.
|
||||||
|
relay_chn_tilt_execute_stop(tilt_ctl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Issue a tilt command to a specific relay channel.
|
// Issue a tilt command to a specific relay channel.
|
||||||
static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_tilt_cmd_t cmd)
|
static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_tilt_cmd_t cmd)
|
||||||
{
|
{
|
||||||
@@ -136,7 +148,7 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t
|
|||||||
} else {
|
} else {
|
||||||
// Channel needs timing before running tilting action, schedule it
|
// Channel needs timing before running tilting action, schedule it
|
||||||
tilt_ctl->step = RELAY_CHN_TILT_STEP_PENDING;
|
tilt_ctl->step = RELAY_CHN_TILT_STEP_PENDING;
|
||||||
relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, req_timing_ms);
|
relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, req_timing_ms, "pending tilt");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -147,7 +159,7 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t
|
|||||||
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
|
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
|
||||||
// Schedule for tilting
|
// Schedule for tilting
|
||||||
tilt_ctl->step = RELAY_CHN_TILT_STEP_PENDING;
|
tilt_ctl->step = RELAY_CHN_TILT_STEP_PENDING;
|
||||||
relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
|
relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "tilt inertia");
|
||||||
} else if (cmd == RELAY_CHN_TILT_CMD_REVERSE) {
|
} else if (cmd == RELAY_CHN_TILT_CMD_REVERSE) {
|
||||||
// Stop the running channel first
|
// Stop the running channel first
|
||||||
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
|
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
|
||||||
@@ -162,7 +174,7 @@ static void relay_chn_tilt_issue_cmd(relay_chn_tilt_ctl_t *tilt_ctl, relay_chn_t
|
|||||||
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
|
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
|
||||||
// Schedule for tilting
|
// Schedule for tilting
|
||||||
tilt_ctl->step = RELAY_CHN_TILT_STEP_PENDING;
|
tilt_ctl->step = RELAY_CHN_TILT_STEP_PENDING;
|
||||||
relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
|
relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "tilt inertia");
|
||||||
} else if (cmd == RELAY_CHN_TILT_CMD_FORWARD) {
|
} else if (cmd == RELAY_CHN_TILT_CMD_FORWARD) {
|
||||||
// Stop the running channel first
|
// Stop the running channel first
|
||||||
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
|
relay_chn_dispatch_cmd(tilt_ctl->chn_ctl, RELAY_CHN_CMD_STOP);
|
||||||
@@ -507,7 +519,10 @@ static void relay_chn_tilt_execute_stop(relay_chn_tilt_ctl_t *tilt_ctl)
|
|||||||
|
|
||||||
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
#if CONFIG_RELAY_CHN_ENABLE_NVS
|
||||||
// Start the flush debounce timer
|
// Start the flush debounce timer
|
||||||
relay_chn_start_esp_timer_once(tilt_ctl->flush_timer, RELAY_CHN_TILT_FLUSH_DEBOUNCE_MS);
|
if (relay_chn_start_esp_timer_once(tilt_ctl->flush_timer, RELAY_CHN_TILT_FLUSH_DEBOUNCE_MS) != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "Failed to start tilt flush timer for ch %d", tilt_ctl->chn_ctl->id);
|
||||||
|
// This is not a critical failure, just log it. The count will be saved on next stop.
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -516,11 +531,11 @@ static void relay_chn_tilt_execute_forward(relay_chn_tilt_ctl_t *tilt_ctl)
|
|||||||
if (relay_chn_output_reverse(tilt_ctl->chn_ctl->output) != ESP_OK) {
|
if (relay_chn_output_reverse(tilt_ctl->chn_ctl->output) != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "relay_chn_tilt_execute_forward: Failed to output reverse for relay channel #%d!", tilt_ctl->chn_ctl->id);
|
ESP_LOGE(TAG, "relay_chn_tilt_execute_forward: Failed to output reverse for relay channel #%d!", tilt_ctl->chn_ctl->id);
|
||||||
// Stop tilting because of the error
|
// Stop tilting because of the error
|
||||||
relay_chn_tilt_dispatch_cmd(tilt_ctl, RELAY_CHN_TILT_CMD_STOP);
|
relay_chn_tilt_execute_stop(tilt_ctl);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Set the move time timer
|
// Set the move time timer
|
||||||
relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.move_time_ms);
|
relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.move_time_ms, "tilt move");
|
||||||
// Set to pause step
|
// Set to pause step
|
||||||
tilt_ctl->step = RELAY_CHN_TILT_STEP_PAUSE;
|
tilt_ctl->step = RELAY_CHN_TILT_STEP_PAUSE;
|
||||||
}
|
}
|
||||||
@@ -530,11 +545,11 @@ static void relay_chn_tilt_execute_reverse(relay_chn_tilt_ctl_t *tilt_ctl)
|
|||||||
if (relay_chn_output_forward(tilt_ctl->chn_ctl->output) != ESP_OK) {
|
if (relay_chn_output_forward(tilt_ctl->chn_ctl->output) != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "relay_chn_tilt_execute_reverse: Failed to output forward for relay channel #%d!", tilt_ctl->chn_ctl->id);
|
ESP_LOGE(TAG, "relay_chn_tilt_execute_reverse: Failed to output forward for relay channel #%d!", tilt_ctl->chn_ctl->id);
|
||||||
// Stop tilting because of the error
|
// Stop tilting because of the error
|
||||||
relay_chn_tilt_dispatch_cmd(tilt_ctl, RELAY_CHN_TILT_CMD_STOP);
|
relay_chn_tilt_execute_stop(tilt_ctl);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Set the move time timer
|
// Set the move time timer
|
||||||
relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.move_time_ms);
|
relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.move_time_ms, "tilt move");
|
||||||
// Set to pause step
|
// Set to pause step
|
||||||
tilt_ctl->step = RELAY_CHN_TILT_STEP_PAUSE;
|
tilt_ctl->step = RELAY_CHN_TILT_STEP_PAUSE;
|
||||||
}
|
}
|
||||||
@@ -545,7 +560,7 @@ static void relay_chn_tilt_execute_pause(relay_chn_tilt_ctl_t *tilt_ctl)
|
|||||||
if (relay_chn_output_stop(tilt_ctl->chn_ctl->output) != ESP_OK) {
|
if (relay_chn_output_stop(tilt_ctl->chn_ctl->output) != ESP_OK) {
|
||||||
ESP_LOGE(TAG, "relay_chn_tilt_execute_pause: Failed to output stop for relay channel #%d!", tilt_ctl->chn_ctl->id);
|
ESP_LOGE(TAG, "relay_chn_tilt_execute_pause: Failed to output stop for relay channel #%d!", tilt_ctl->chn_ctl->id);
|
||||||
// Stop tilting because of the error
|
// Stop tilting because of the error
|
||||||
relay_chn_tilt_dispatch_cmd(tilt_ctl, RELAY_CHN_TILT_CMD_STOP);
|
relay_chn_tilt_execute_stop(tilt_ctl);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -553,12 +568,12 @@ static void relay_chn_tilt_execute_pause(relay_chn_tilt_ctl_t *tilt_ctl)
|
|||||||
if (relay_chn_tilt_count_update(tilt_ctl) == 0) {
|
if (relay_chn_tilt_count_update(tilt_ctl) == 0) {
|
||||||
ESP_LOGD(TAG, "relay_chn_tilt_execute_pause: Relay channel cannot tilt anymore");
|
ESP_LOGD(TAG, "relay_chn_tilt_execute_pause: Relay channel cannot tilt anymore");
|
||||||
// Stop tilting since the tilting limit has been reached
|
// Stop tilting since the tilting limit has been reached
|
||||||
relay_chn_tilt_dispatch_cmd(tilt_ctl, RELAY_CHN_TILT_CMD_STOP);
|
relay_chn_tilt_execute_stop(tilt_ctl);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the pause time timer
|
// Set the pause time timer
|
||||||
relay_chn_start_esp_timer_once(tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.pause_time_ms);
|
relay_chn_tilt_start_timer_or_stop(tilt_ctl, tilt_ctl->tilt_timer, tilt_ctl->tilt_timing.pause_time_ms, "tilt pause");
|
||||||
// Set to move step
|
// Set to move step
|
||||||
tilt_ctl->step = RELAY_CHN_TILT_STEP_MOVE;
|
tilt_ctl->step = RELAY_CHN_TILT_STEP_MOVE;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -450,4 +450,33 @@ TEST_CASE("Test run limit persistence across stop/start", "[relay_chn][run_limit
|
|||||||
TEST_ASSERT_EQUAL(TEST_RUN_LIMIT_SEC, relay_chn_get_run_limit(i));
|
TEST_ASSERT_EQUAL(TEST_RUN_LIMIT_SEC, relay_chn_get_run_limit(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
|
|
||||||
|
TEST_CASE("Run limit functions handle invalid channel ID", "[relay_chn][run_limit]")
|
||||||
|
{
|
||||||
|
const uint8_t invalid_ch = CONFIG_RELAY_CHN_COUNT + 5;
|
||||||
|
const uint16_t original_limit = relay_chn_get_run_limit(0);
|
||||||
|
|
||||||
|
// get_run_limit with invalid ID should return 0
|
||||||
|
TEST_ASSERT_EQUAL(0, relay_chn_get_run_limit(invalid_ch));
|
||||||
|
|
||||||
|
// set_run_limit with invalid ID should not crash or affect other channels
|
||||||
|
relay_chn_set_run_limit(invalid_ch, 999);
|
||||||
|
TEST_ASSERT_EQUAL(original_limit, relay_chn_get_run_limit(0));
|
||||||
|
}
|
||||||
|
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
|
||||||
|
|
||||||
|
TEST_CASE("relay_chn_destroy allows clean-up and re-creation", "[relay_chn][core]")
|
||||||
|
{
|
||||||
|
relay_chn_run_forward_all();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
|
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
relay_chn_destroy();
|
||||||
|
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -256,3 +256,59 @@ TEST_CASE("Test run limit persistence across stop/start", "[relay_chn][run_limit
|
|||||||
TEST_ASSERT_EQUAL(TEST_RUN_LIMIT_SEC, relay_chn_get_run_limit());
|
TEST_ASSERT_EQUAL(TEST_RUN_LIMIT_SEC, relay_chn_get_run_limit());
|
||||||
}
|
}
|
||||||
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
|
#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1
|
||||||
|
|
||||||
|
TEST_CASE("relay_chn_get_state_str returns correct strings", "[relay_chn][core]")
|
||||||
|
{
|
||||||
|
// This test is a bit contrived as it's hard to force every state
|
||||||
|
// without complex sequences. We will test the most common ones.
|
||||||
|
TEST_ASSERT_EQUAL_STRING("IDLE", relay_chn_get_state_str());
|
||||||
|
|
||||||
|
relay_chn_run_forward();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
|
TEST_ASSERT_EQUAL_STRING("FORWARD", relay_chn_get_state_str());
|
||||||
|
|
||||||
|
relay_chn_run_reverse();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
|
TEST_ASSERT_EQUAL_STRING("REVERSE_PENDING", relay_chn_get_state_str());
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS));
|
||||||
|
TEST_ASSERT_EQUAL_STRING("REVERSE", relay_chn_get_state_str());
|
||||||
|
|
||||||
|
relay_chn_stop();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
|
TEST_ASSERT_EQUAL_STRING("STOPPED", relay_chn_get_state_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Stop command interrupts pending commands", "[relay_chn][core][inertia]")
|
||||||
|
{
|
||||||
|
// 1. Start running forward
|
||||||
|
relay_chn_run_forward();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state());
|
||||||
|
|
||||||
|
// 2. Issue a reverse command, which will make the state REVERSE_PENDING
|
||||||
|
relay_chn_run_reverse();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE_PENDING, relay_chn_get_state());
|
||||||
|
|
||||||
|
// 3. Before the inertia timer fires, issue a stop command
|
||||||
|
relay_chn_stop();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state());
|
||||||
|
|
||||||
|
// 4. Wait for more than the inertia period
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
|
||||||
|
// The channel should transition to IDLE, not REVERSE, because stop cancelled the pending command.
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("relay_chn_destroy allows clean-up and re-creation", "[relay_chn][core]")
|
||||||
|
{
|
||||||
|
relay_chn_run_forward();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state());
|
||||||
|
|
||||||
|
relay_chn_destroy();
|
||||||
|
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state());
|
||||||
|
}
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ TEST_CASE("tilt_auto_all tilts channels based on last run direction", "[relay_ch
|
|||||||
TEST_ASSERT_GREATER_OR_EQUAL_MESSAGE(2, CONFIG_RELAY_CHN_COUNT, "Test requires at least 2 channels");
|
TEST_ASSERT_GREATER_OR_EQUAL_MESSAGE(2, CONFIG_RELAY_CHN_COUNT, "Test requires at least 2 channels");
|
||||||
|
|
||||||
// 1. Prepare channel 0 with last run FORWARD and channel 1 with last run REVERSE
|
// 1. Prepare channel 0 with last run FORWARD and channel 1 with last run REVERSE
|
||||||
prepare_channels_for_tilt_with_mixed_runs(0, RELAY_CHN_CMD_FORWARD);
|
prepare_channels_for_tilt_with_mixed_runs();
|
||||||
|
|
||||||
// 2. Issue auto tilt command to all channels
|
// 2. Issue auto tilt command to all channels
|
||||||
relay_chn_tilt_auto_all();
|
relay_chn_tilt_auto_all();
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ void prepare_channel_for_tilt(int initial_cmd) {
|
|||||||
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state());
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST_CASE: Test transition from running forward to tilt forward
|
// Test transition from running forward to tilt forward
|
||||||
// Scenario: RELAY_CHN_STATE_FORWARD -> (relay_chn_tilt_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_FORWARD
|
// Scenario: RELAY_CHN_STATE_FORWARD -> (relay_chn_tilt_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_FORWARD
|
||||||
TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][tilt][inertia]")
|
TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][tilt][inertia]")
|
||||||
{
|
{
|
||||||
@@ -53,7 +53,7 @@ TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][ti
|
|||||||
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state());
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST_CASE: Test transition from running reverse to tilt reverse
|
// Test transition from running reverse to tilt reverse
|
||||||
// Scenario: RELAY_CHN_STATE_REVERSE -> (relay_chn_tilt_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_REVERSE
|
// Scenario: RELAY_CHN_STATE_REVERSE -> (relay_chn_tilt_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_REVERSE
|
||||||
TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][tilt][inertia]")
|
TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][tilt][inertia]")
|
||||||
{
|
{
|
||||||
@@ -74,7 +74,7 @@ TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][ti
|
|||||||
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state());
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST_CASE: Test transition from FREE state to tilt forward (now with preparation)
|
// Test transition from FREE state to tilt forward (now with preparation)
|
||||||
// Scenario: RELAY_CHN_STATE_IDLE -> (prepare) -> RELAY_CHN_STATE_IDLE -> (relay_chn_tilt_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_FORWARD
|
// Scenario: RELAY_CHN_STATE_IDLE -> (prepare) -> RELAY_CHN_STATE_IDLE -> (relay_chn_tilt_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_FORWARD
|
||||||
TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn][tilt][inertia]")
|
TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn][tilt][inertia]")
|
||||||
{
|
{
|
||||||
@@ -89,7 +89,7 @@ TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn
|
|||||||
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state());
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST_CASE: Test transition from FREE state to tilt reverse (now with preparation)
|
// Test transition from FREE state to tilt reverse (now with preparation)
|
||||||
// Scenario: RELAY_CHN_STATE_IDLE -> (prepare) -> RELAY_CHN_STATE_IDLE -> (relay_chn_tilt_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_REVERSE
|
// Scenario: RELAY_CHN_STATE_IDLE -> (prepare) -> RELAY_CHN_STATE_IDLE -> (relay_chn_tilt_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_REVERSE
|
||||||
TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn][tilt][inertia]")
|
TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn][tilt][inertia]")
|
||||||
{
|
{
|
||||||
@@ -103,7 +103,7 @@ TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn
|
|||||||
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state());
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST_CASE: Test transition from tilt forward to run forward (inertia expected for run)
|
// Test transition from tilt forward to run forward (inertia expected for run)
|
||||||
// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_run_forward) -> RELAY_CHN_STATE_FORWARD
|
// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_run_forward) -> RELAY_CHN_STATE_FORWARD
|
||||||
TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][tilt][inertia]")
|
TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][tilt][inertia]")
|
||||||
{
|
{
|
||||||
@@ -121,7 +121,7 @@ TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][ti
|
|||||||
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state());
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST_CASE: Test transition from tilt reverse to run reverse (no inertia expected for run)
|
// Test transition from tilt reverse to run reverse (no inertia expected for run)
|
||||||
// Scenario: RELAY_CHN_STATE_TILT_REVERSE -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_REVERSE
|
// Scenario: RELAY_CHN_STATE_TILT_REVERSE -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_REVERSE
|
||||||
TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][tilt][inertia]")
|
TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][tilt][inertia]")
|
||||||
{
|
{
|
||||||
@@ -138,7 +138,7 @@ TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][ti
|
|||||||
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state());
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST_CASE: Test transition from tilt forward to run reverse (without inertia)
|
// Test transition from tilt forward to run reverse (without inertia)
|
||||||
// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_REVERSE
|
// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_REVERSE
|
||||||
TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn][tilt][inertia]")
|
TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn][tilt][inertia]")
|
||||||
{
|
{
|
||||||
@@ -154,7 +154,7 @@ TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn]
|
|||||||
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state());
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST_CASE: Test stopping from a tilt state (no inertia for stop command itself)
|
// Test stopping from a tilt state (no inertia for stop command itself)
|
||||||
// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_stop) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_IDLE
|
// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_stop) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_IDLE
|
||||||
TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_chn][tilt][inertia]")
|
TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_chn][tilt][inertia]")
|
||||||
{
|
{
|
||||||
@@ -265,3 +265,35 @@ TEST_CASE("run command during TILT state transitions correctly", "[relay_chn][ti
|
|||||||
relay_chn_state_t state = relay_chn_get_state();
|
relay_chn_state_t state = relay_chn_get_state();
|
||||||
TEST_ASSERT(state == RELAY_CHN_STATE_REVERSE || state == RELAY_CHN_STATE_REVERSE_PENDING);
|
TEST_ASSERT(state == RELAY_CHN_STATE_REVERSE || state == RELAY_CHN_STATE_REVERSE_PENDING);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test run command during active tilt cycle (move/pause)
|
||||||
|
TEST_CASE("run command during active tilt cycle stops tilt", "[relay_chn][tilt][interrupt]")
|
||||||
|
{
|
||||||
|
// Set a known sensitivity for predictable timing.
|
||||||
|
// For sensitivity=50, move_time=30ms, pause_time=270ms.
|
||||||
|
relay_chn_tilt_set_sensitivity(50);
|
||||||
|
const uint32_t move_time_ms = 30;
|
||||||
|
|
||||||
|
// --- Test interrupting during MOVE step ---
|
||||||
|
prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD);
|
||||||
|
relay_chn_tilt_forward();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(move_time_ms / 2)); // Wait for half of the move time
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state());
|
||||||
|
|
||||||
|
// Interrupt with run_reverse while in the MOVE part of the cycle
|
||||||
|
relay_chn_run_reverse();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS));
|
||||||
|
// Should stop tilting and go to REVERSE immediately (no inertia from TILT_FORWARD)
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state());
|
||||||
|
|
||||||
|
// --- Test interrupting during PAUSE step ---
|
||||||
|
prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD);
|
||||||
|
relay_chn_tilt_forward();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(move_time_ms + TEST_DELAY_MARGIN_MS)); // Wait past MOVE, into PAUSE
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state());
|
||||||
|
|
||||||
|
// Interrupt with run_forward while in the PAUSE part of the cycle
|
||||||
|
relay_chn_run_forward();
|
||||||
|
// Should stop tilting and go to FORWARD_PENDING (inertia from TILT_FORWARD)
|
||||||
|
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD_PENDING, relay_chn_get_state());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user