修复了WS连接稳定性问题
This commit is contained in:
@@ -29,9 +29,11 @@ static float s_current_angle = 0.0f;
|
||||
static int s_rotate_speed_us = DOMAIN_DEFAULT_ROTATE_SPEED_US;
|
||||
static bool s_initialized;
|
||||
static SemaphoreHandle_t s_state_mutex;
|
||||
static SemaphoreHandle_t s_rotation_mutex;
|
||||
static portMUX_TYPE s_state_mutex_init_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
|
||||
static StaticSemaphore_t s_state_mutex_buf;
|
||||
static StaticSemaphore_t s_rotation_mutex_buf;
|
||||
#endif
|
||||
|
||||
static const domain_direction_axis_config_t s_default_axis_config = {
|
||||
@@ -94,6 +96,55 @@ static bool ensure_state_mutex(void)
|
||||
return (s_state_mutex != NULL);
|
||||
}
|
||||
|
||||
static bool ensure_rotation_mutex(void)
|
||||
{
|
||||
if (s_rotation_mutex != NULL) {
|
||||
return true;
|
||||
}
|
||||
#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
|
||||
portENTER_CRITICAL(&s_state_mutex_init_lock);
|
||||
if (s_rotation_mutex == NULL) {
|
||||
s_rotation_mutex = xSemaphoreCreateMutexStatic(&s_rotation_mutex_buf);
|
||||
}
|
||||
portEXIT_CRITICAL(&s_state_mutex_init_lock);
|
||||
#else
|
||||
SemaphoreHandle_t created_mutex = xSemaphoreCreateMutex();
|
||||
|
||||
portENTER_CRITICAL(&s_state_mutex_init_lock);
|
||||
if (s_rotation_mutex == NULL) {
|
||||
s_rotation_mutex = created_mutex;
|
||||
created_mutex = NULL;
|
||||
}
|
||||
portEXIT_CRITICAL(&s_state_mutex_init_lock);
|
||||
|
||||
if (created_mutex != NULL) {
|
||||
vSemaphoreDelete(created_mutex);
|
||||
}
|
||||
#endif
|
||||
return (s_rotation_mutex != NULL);
|
||||
}
|
||||
|
||||
static bool domain_direction_lock_rotation(int timeout_ms)
|
||||
{
|
||||
if (!ensure_rotation_mutex()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int effective_timeout_ms = timeout_ms > 0 ? timeout_ms : DOMAIN_ROTATE_TIMEOUT_MS;
|
||||
TickType_t timeout_ticks = pdMS_TO_TICKS(effective_timeout_ms);
|
||||
if (timeout_ticks == 0) {
|
||||
timeout_ticks = 1;
|
||||
}
|
||||
return (xSemaphoreTake(s_rotation_mutex, timeout_ticks) == pdTRUE);
|
||||
}
|
||||
|
||||
static void domain_direction_unlock_rotation(void)
|
||||
{
|
||||
if (s_rotation_mutex != NULL) {
|
||||
(void)xSemaphoreGive(s_rotation_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
bool domain_direction_lock_state(int timeout_ms)
|
||||
{
|
||||
if (!ensure_state_mutex()) {
|
||||
@@ -253,37 +304,62 @@ esp_err_t domain_rotate_to_angle_with_timeout(float target_angle, int timeout_ms
|
||||
TickType_t start_tick = xTaskGetTickCount();
|
||||
int effective_timeout_ms = timeout_ms > 0 ? timeout_ms : DOMAIN_ROTATE_TIMEOUT_MS;
|
||||
|
||||
if (!domain_direction_lock_state(effective_timeout_ms)) {
|
||||
if (!domain_direction_lock_rotation(effective_timeout_ms)) {
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
int state_timeout_ms = remaining_timeout_ms(start_tick, effective_timeout_ms);
|
||||
if (state_timeout_ms <= 0) {
|
||||
domain_direction_unlock_rotation();
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
if (!domain_direction_lock_state(state_timeout_ms)) {
|
||||
domain_direction_unlock_rotation();
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
if (!s_initialized) {
|
||||
domain_direction_unlock_state();
|
||||
domain_direction_unlock_rotation();
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
float normalized_target = domain_angle_normalize(target_angle);
|
||||
float rotation = domain_angle_shortest_rotation(s_current_angle, normalized_target);
|
||||
float current_angle = s_current_angle;
|
||||
int rotate_speed_us = s_rotate_speed_us;
|
||||
float rotation = domain_angle_shortest_rotation(current_angle, normalized_target);
|
||||
if (rotation == 0.0f) {
|
||||
domain_direction_unlock_state();
|
||||
domain_direction_unlock_rotation();
|
||||
return ESP_OK;
|
||||
}
|
||||
domain_direction_unlock_state();
|
||||
|
||||
int motor_timeout_ms = remaining_timeout_ms(start_tick, effective_timeout_ms);
|
||||
if (motor_timeout_ms <= 0) {
|
||||
domain_direction_unlock_state();
|
||||
domain_direction_unlock_rotation();
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
esp_err_t motor_err = domain_runtime_stepper_rotate_angle_with_accel_timeout(
|
||||
rotation,
|
||||
s_rotate_speed_us,
|
||||
rotate_speed_us,
|
||||
motor_timeout_ms);
|
||||
if (motor_err != ESP_OK) {
|
||||
domain_direction_unlock_state();
|
||||
domain_direction_unlock_rotation();
|
||||
return motor_err;
|
||||
}
|
||||
|
||||
s_current_angle = domain_angle_normalize(s_current_angle + rotation);
|
||||
state_timeout_ms = remaining_timeout_ms(start_tick, effective_timeout_ms);
|
||||
if (state_timeout_ms <= 0) {
|
||||
domain_direction_unlock_rotation();
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
if (!domain_direction_lock_state(state_timeout_ms)) {
|
||||
domain_direction_unlock_rotation();
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
s_current_angle = domain_angle_normalize(current_angle + rotation);
|
||||
domain_direction_unlock_state();
|
||||
domain_direction_unlock_rotation();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user