From d70c001a91114b3f9748325c4ec4bf66e31734ac Mon Sep 17 00:00:00 2001 From: zwsd Date: Thu, 4 Aug 2022 10:03:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=94=B5=E6=9C=BA=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E4=BB=A5=E5=8F=8A=E8=8E=B7=E5=8F=96=E7=94=B5=E6=9C=BA?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E7=8A=B6=E6=80=81=E5=9B=9E=E6=89=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.c | 39 ++++++++++++++++++++++++++++++++++++++- main/motor_drive.c | 6 +++--- main/motor_drive.h | 2 +- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/main/main.c b/main/main.c index fcdf336..ddca680 100644 --- a/main/main.c +++ b/main/main.c @@ -73,12 +73,39 @@ bleuart_t ble_uart_init_struct = { }; motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1}; +void order_receipt_setPosition(int index, uint8_t setPosition_code) { + uint8_t buffer[256] = {0}; + + ESP_LOGI(MAIN_LOG_TAG, "Command setPosition execution complete!"); + + if (setPosition_code == 0) { + sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"success\", \"index\": %d }", setPosition_code, index); + bleuart_notify_send(buffer, strlen((char *)buffer)); + } else { + sprintf((char *)buffer, "{ \"order\": \"receipt\", \"code\": %d, \"info\": \"error\", \"index\": %d }", setPosition_code, index); + bleuart_notify_send(buffer, strlen((char *)buffer)); + } +} + +void order_receipt_getStatus(int index) { + ESP_LOGI(MAIN_LOG_TAG, "Command getStatus execution complete!"); + uint8_t buffer[256] = {0}; + double motor_position = motor_get_position_degree(); + + sprintf((char *)buffer, + "{ \"order\": \"receipt\", \"index\": %d, \"deviceState\": \"running\", \"deviceException\": 0, \"deviceExceptionInfo\": " + ", \"position\":%lf }", + index, motor_position); + bleuart_notify_send(buffer, strlen((char *)buffer)); +} + void process_setPosition(cJSON *rxjson) { // { "order": "setPosition", "ble_uart_index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 } int index = 0; int speedLevel = 0; float position = 0; int direction = 0; + uint8_t setPosition_code; if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) { position = cJSON_GetObjectItem(rxjson, "position")->valuedouble; } @@ -92,7 +119,16 @@ void process_setPosition(cJSON *rxjson) { if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) { direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction")); } - motor_run_to_postion(1, position, 0); + setPosition_code = motor_run_to_postion(1, position, 0); + order_receipt_setPosition(index, setPosition_code); +} + +void process_getStatus(cJSON *rxjson) { + int index = 0; + if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) { + index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index")); + } + order_receipt_getStatus(index); } void processrxjson(cJSON *rxjson) { @@ -106,6 +142,7 @@ void processrxjson(cJSON *rxjson) { process_setPosition(rxjson); } else if (strcmp(order, "getStatus") == 0) { + process_getStatus(rxjson); } } diff --git a/main/motor_drive.c b/main/motor_drive.c index 3625032..e18ff6c 100644 --- a/main/motor_drive.c +++ b/main/motor_drive.c @@ -109,7 +109,7 @@ double motor_get_position_degree() { /** * @brief 电机转到多少度 */ -void motor_run_to_postion(int rotation_direction, double position, int speed_level) { +uint8_t motor_run_to_postion(int rotation_direction, double position, int speed_level) { int position_int = 0; uint8_t position_remainder = 0; uint8_t position_buffer_size = 6; //从第五位开始(低位) @@ -177,7 +177,7 @@ void motor_run_to_postion(int rotation_direction, double position, int speed_lev if (position_buffer_size != 13 || buffer[0] != 0X3E) { ESP_LOGW(MOTOR_DRIVE, "set motor size error ,buffer_size:%d,buffer[0] = 0X%x", position_buffer_size, buffer[0]); // bluetooth_active_notify((uint8_t *)notify_err, strlen(notify_err)); - return; + return 1; } motor_position = position; @@ -185,7 +185,7 @@ void motor_run_to_postion(int rotation_direction, double position, int speed_lev // Parse receive // motor_drive_buffer_cmd_parse(buffer); - return; + return 0; } /** diff --git a/main/motor_drive.h b/main/motor_drive.h index ea1286d..4aa82fc 100644 --- a/main/motor_drive.h +++ b/main/motor_drive.h @@ -14,7 +14,7 @@ typedef void (*motor_on_event_t)(motor_event_t event); void motor_init(motor_t* motor); void motor_set_zero_point(); double motor_get_position_degree(); -void motor_run_to_postion(int rotation_direction, double position, int speed_level); +uint8_t motor_run_to_postion(int rotation_direction, double position, int speed_level); void motor_reg_event_cb(motor_on_event_t onevent); void motor_module_schedule();