From 9c59bd7b2b304f737f582b987f5b59c3334107d0 Mon Sep 17 00:00:00 2001 From: zwsd Date: Tue, 2 Aug 2022 18:27:14 +0800 Subject: [PATCH] update --- .vscode/settings.json | 3 +- main/ble_spp_server_demo.c | 36 +++++-- main/ble_spp_server_demo.h | 4 + main/main.c | 244 ++++++++------------------------------------- main/motor_drive.c | 8 +- main/motor_drive.h | 5 +- main/port.c | 11 ++ main/port.h | 7 ++ 8 files changed, 101 insertions(+), 217 deletions(-) create mode 100644 main/port.c create mode 100644 main/port.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 695d5e0..a0c1166 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -34,6 +34,7 @@ "uart_types.h": "c", "esp_intr_alloc.h": "c", "motor_drive.h": "c", - "stdint.h": "c" + "stdint.h": "c", + "freertos.h": "c" }, } \ No newline at end of file diff --git a/main/ble_spp_server_demo.c b/main/ble_spp_server_demo.c index 4719d4c..12152cf 100644 --- a/main/ble_spp_server_demo.c +++ b/main/ble_spp_server_demo.c @@ -20,9 +20,10 @@ #include "freertos/event_groups.h" #include "freertos/task.h" #include "nvs_flash.h" +#include "port.h" #include "string.h" -static bleuart_t *s_ble_uart_init_struct; +static bleuart_t *s_module; static blerxcb_t s_cb; static uint16_t table_conn_id_m; static esp_gatt_if_t table_gatts_if_m; @@ -246,7 +247,7 @@ static uint8_t find_char_and_desr_index(uint16_t handle) { return error; } - +static void blerxprocess(uint8_t *rxmessage, size_t rxsize); static bool store_wr_buffer(esp_ble_gatts_cb_param_t *p_data) { temp_spp_recv_data_node_p1 = (spp_receive_data_node_t *)malloc(sizeof(spp_receive_data_node_t)); @@ -525,10 +526,7 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_ esp_log_buffer_char(GATTS_TABLE_TAG, (char *)(p_data->write.value), p_data->write.len); #else // uart_write_bytes(UART_NUM_0, (char *)(p_data->write.value), p_data->write.len); - if (s_ble_uart_init_struct->receive_data_processing_flag == false) { - s_cb(p_data->write.value, p_data->write.len); - } - + blerxprocess(p_data->write.value, p_data->write.len); #endif } else { // TODO: @@ -644,7 +642,7 @@ void ble_spp_server_demo_app_main(bleuart_t *bleuart) { esp_err_t ret; esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); - s_ble_uart_init_struct = bleuart; + s_module = bleuart; // Initialize NVS ret = nvs_flash_init(); @@ -688,3 +686,27 @@ void ble_spp_server_demo_app_main(bleuart_t *bleuart) { return; } + +void blerxprocess(uint8_t *rxmessage, size_t rxsize) { + if (s_module->has_rx_size + rxsize > s_module->rxbufsize - 10) { + return; + } + if (s_module->receive_data_processing_flag) { + return; + } + s_module->last_rx_ticket = port_get_ticket(); + + memcpy(&s_module->rxbuf[s_module->has_rx_size], rxmessage, rxsize); + s_module->has_rx_size = s_module->has_rx_size + rxsize; +} + +void bleuart_schedule() { + if (s_module->has_rx_size != 0 && port_haspassedms(s_module->last_rx_ticket) > 1000) { + s_module->receive_data_processing_flag = true; + + s_cb(s_module->rxbuf, s_module->has_rx_size); + + s_module->has_rx_size = 0; + s_module->receive_data_processing_flag = false; + } +} \ No newline at end of file diff --git a/main/ble_spp_server_demo.h b/main/ble_spp_server_demo.h index d67749e..779b648 100644 --- a/main/ble_spp_server_demo.h +++ b/main/ble_spp_server_demo.h @@ -70,8 +70,12 @@ typedef struct { bool receive_data_processing_flag; + int has_rx_size; + int maxClientNum; const char *bleName; + + int last_rx_ticket; } bleuart_t; /*接收完整JSON*/ typedef void (*blerxcb_t)(uint8_t *rx, size_t rxsize); diff --git a/main/main.c b/main/main.c index 64dc2b9..2fc1242 100644 --- a/main/main.c +++ b/main/main.c @@ -15,18 +15,10 @@ #include "motor_drive.h" #define MAIN_LOG_TAG "MAIN" - #define ble_uart_tx_size 128 #define ble_uart_rx_size 128 - static uint8_t bluetooth_tx_buffer[ble_uart_tx_size] = {0}; static uint8_t bluetooth_rx_buffer[ble_uart_rx_size] = {0}; - -uint8_t ble_rx_buffer_now_len = 0; -uint8_t ble_rx_buffer_off = 0; -uint32_t start_time = 0; -uint16_t modbus_uart_rx_off_before = 0; - bleuart_t ble_uart_init_struct = { .txbuf = bluetooth_tx_buffer, .txbufsize = sizeof(bluetooth_tx_buffer), @@ -37,215 +29,55 @@ bleuart_t ble_uart_init_struct = { .maxClientNum = 1, .bleName = "yimei_ble", }; - -static cJSON *json_tmp; -static double encoder_befor_num; -#define set_position "setPosition" -#define get_status "getStatus" -#define set_motor_current_size "setMotorCurrentSize" -#define set_motor_to_position "setMotorToPosition" -#define cmd_length_set_position 5 -#define cmd_length_get_status 2 -#define cmd_length_set_motor_current_size 2 -#define cmd_length_set_motor_to_position 5 -typedef struct { - char *order; //指令名称 - int index; // - int speedLevel; // - double position; //角度 - int direction; //旋转方向 - int code; //错误码 - char *info; //错误码信息 - char *deviceState; //设备状态 - int deviceException; //设备异常编号 - char *deviceExceptionInfo; //设备异常信息 - - bool cmd_flag; -} ble_uart_receive_data_t; - -ble_uart_receive_data_t ble_uart_receive_data = { - .order = "order", - .index = 0, - .speedLevel = 0, - .position = 0.0, - .direction = 0, - .code = 0, - .info = "noerror", - .deviceState = "init", - .deviceException = 0, - .deviceExceptionInfo = "noexception", - - .cmd_flag = false, -}; - motor_t ble_uart_motor_structer = {.uartNum = UART_NUM_1}; - -void buffer_all_init() { - ble_rx_buffer_now_len = 0; - memset(ble_uart_init_struct.rxbuf, 0, ble_uart_init_struct.rxbufsize); -} - -void blerxcb(uint8_t *rxmessage, size_t rxsize) { - // start_time = esp_log_timestamp(); - if ((rxsize + ble_rx_buffer_now_len) > ble_uart_init_struct.rxbufsize) { - ESP_LOGW("MAIN", "rx buffer overstep"); - buffer_all_init(); +void processrxjson(cJSON *rxjson) { + if (!cJSON_IsString(cJSON_GetObjectItem(rxjson, "order"))) { return; } - - for (int i = 0; i < rxsize; i++) { - ble_uart_init_struct.rxbuf[ble_rx_buffer_now_len++] = rxmessage[i]; - ble_rx_buffer_off++; - } -} - -void wait_ble_uart_receive_ms(uint16_t wait_time_ms) { - start_time = esp_log_timestamp(); - while ((esp_log_timestamp() - start_time) < wait_time_ms) { - } - modbus_uart_rx_off_before = ble_rx_buffer_off; -} - -bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) { - *json_tmp = cJSON_Parse((char *)ble_uart_init_struct.rxbuf); - if (*json_tmp == NULL) { - ESP_LOGE(MAIN_LOG_TAG, "parse rxbuffer null or redundant symbol ',','{' "); - return false; - } - return true; -} - -bool parse_json_to_struct(cJSON *ch) { - uint8_t cmd_length = 0; - while (ch != NULL) { - // ESP_LOGI(MAIN_LOG_TAG, "%s", ch->string); - if (strcmp(ch->string, "order") == 0) { - ble_uart_receive_data.order = ch->valuestring; - - if (strcmp(ch->valuestring, set_position) == 0) { - cmd_length = cmd_length_set_position; - } - if (strcmp(ch->valuestring, get_status) == 0) { - cmd_length = cmd_length_get_status; - } - if (strcmp(ch->valuestring, set_motor_current_size) == 0) { - cmd_length = cmd_length_set_motor_current_size; - } - if (strcmp(ch->valuestring, set_motor_to_position) == 0) { - cmd_length = cmd_length_set_motor_to_position; - } - - cmd_length--; + const char *order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order")); + + if (strcmp(order, "setPosition") == 0) { + // { "order": "setPosition", "index": 0, "speedLevel": 1, "position": 120.3, "direction": 0 } + int index = 0; + int speedLevel = 0; + float position = 0; + int direction = 0; + if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "position"))) { + position = cJSON_GetObjectItem(rxjson, "position")->valuedouble; } - if (strcmp(ch->string, "index") == 0) { - ble_uart_receive_data.index = ch->valueint; - cmd_length--; + if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) { + index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index")); } - if (strcmp(ch->string, "speedLevel") == 0) { - ble_uart_receive_data.speedLevel = ch->valueint; - cmd_length--; + if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "speedLevel"))) { + speedLevel = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "speedLevel")); } - if (strcmp(ch->string, "position") == 0) { - ble_uart_receive_data.position = ch->valuedouble; - cmd_length--; - } - if (strcmp(ch->string, "direction") == 0) { - ble_uart_receive_data.direction = ch->valueint; - cmd_length--; - } - ch = ch->next; - } - if (cmd_length == 0) { - ble_uart_receive_data.cmd_flag = true; - } else { - ESP_LOGE(MAIN_LOG_TAG, "JSON directive missing or exceeded"); - } - - return ble_uart_receive_data.cmd_flag; + if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "direction"))) { + direction = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "direction")); + } + motor_run_to_postion(1, position, index); + //组包,上报 + } else if (strcmp(order, "getStatus") == 0) { + + } } -void receipt_json_get_status() { - cJSON *pRoot = cJSON_CreateObject(); //创建一个对象 - if (!pRoot) { +void blerxcb(uint8_t *rxmessage, size_t rxsize) { + rxmessage[rxsize] = 0; + cJSON *rxmessage_json = cJSON_ParseWithLength((char *)rxmessage, rxsize); + if (rxmessage_json == NULL) { + ESP_LOGE(MAIN_LOG_TAG, "cJSON_Parse fail rxmessage_json == null,%s", cJSON_GetErrorPtr()); return; } - - cJSON_AddStringToObject(pRoot, "order", "receipt"); //添加一个节点 - cJSON_AddNumberToObject(pRoot, "index", ble_uart_receive_data.index); - cJSON_AddStringToObject(pRoot, "deviceState", ble_uart_receive_data.deviceState); - cJSON_AddNumberToObject(pRoot, "deviceException", ble_uart_receive_data.deviceException); - cJSON_AddStringToObject(pRoot, "deviceExceptionInfo", ble_uart_receive_data.deviceExceptionInfo); - cJSON_AddNumberToObject(pRoot, "position", ble_uart_receive_data.position); - - char *szJson = cJSON_Print(pRoot); - - if (szJson != NULL) { - ESP_LOGI(MAIN_LOG_TAG, "%s", szJson); - free(szJson); - } - - cJSON_Delete(pRoot); + processrxjson(rxmessage_json); + cJSON_Delete(rxmessage_json); } -void processing_uart_rx_data() { - if (ble_rx_buffer_off != 0) { - modbus_uart_rx_off_before = ble_rx_buffer_off; - - wait_ble_uart_receive_ms(200); - - if (ble_rx_buffer_off == modbus_uart_rx_off_before) { - ble_uart_init_struct.receive_data_processing_flag = true; - ESP_LOGI("MAIN", "%s", ble_uart_init_struct.rxbuf); - if (parse_rxbuffer_and_validation_data(&json_tmp)) { - if (parse_json_to_struct(json_tmp->child)) { - ESP_LOGI(MAIN_LOG_TAG, "order:%s ,index:%d speedLevel:%d position:%f direction:%d", // - ble_uart_receive_data.order, ble_uart_receive_data.index, ble_uart_receive_data.speedLevel, // - ble_uart_receive_data.position, ble_uart_receive_data.direction); - - if (strcmp(ble_uart_receive_data.order, set_position) == 0) { - // ble_uart_receive_data.auto_report_flag = true; - ESP_LOGI(MAIN_LOG_TAG, set_position); - - encoder_befor_num = motor_drive_read_encoder(); - if (encoder_befor_num >= 0) { - // ESP_LOGI(MAIN_LOG_TAG, "%.2lf", encoder_befor_num); - if (motor_drive_set_packages_ctr(ble_uart_receive_data.position, ble_uart_receive_data.direction) == 0) { - ets_delay_us(50000); - if (encoder_befor_num == motor_drive_read_encoder()) { - ESP_LOGW(MAIN_LOG_TAG, "motor no turning"); - } else { - ESP_LOGI(MAIN_LOG_TAG, "motor turning"); - } - } - } - - // receipt_json_set_position(); - } - if (strcmp(ble_uart_receive_data.order, get_status) == 0) { - ESP_LOGI(MAIN_LOG_TAG, get_status); - receipt_json_get_status(); - } - if (strcmp(ble_uart_receive_data.order, set_motor_current_size) == 0) { - ESP_LOGI(MAIN_LOG_TAG, set_motor_current_size); - motor_set_zero_point(); - // receipt_json_get_status(); - } - if (strcmp(ble_uart_receive_data.order, set_motor_to_position) == 0) { - ESP_LOGI(MAIN_LOG_TAG, set_motor_to_position); - motor_run_to_postion(ble_uart_receive_data.direction, ble_uart_receive_data.position, // - ble_uart_receive_data.speedLevel); - // receipt_json_get_status(); - } - } - } - } - - if (ble_uart_init_struct.receive_data_processing_flag == true) { - buffer_all_init(); - ble_rx_buffer_off = 0; - ble_uart_init_struct.receive_data_processing_flag = false; - } +void motor_on_event(motor_event_t event) { + if (event == kRunToPosition) { + //组包 + //上报 + //{ "order": "deviceStatusReport", "index": 0, "deviceState": "running", "position":13.9, "deviceException": 0, "deviceExceptionInfo": "", } } } @@ -253,8 +85,10 @@ void app_main(void) { ble_spp_server_demo_app_main(&ble_uart_init_struct); bleuart_reg_cb(blerxcb); motor_init(&ble_uart_motor_structer); + motor_reg_event_cb(motor_on_event); while (true) { - processing_uart_rx_data(); + bleuart_schedule(); + motor_module_schedule(); } } diff --git a/main/motor_drive.c b/main/motor_drive.c index e84a0a8..23829d4 100644 --- a/main/motor_drive.c +++ b/main/motor_drive.c @@ -37,6 +37,10 @@ void motor_init(motor_t *motor) { return; } + +static double motor_drive_read_encoder(); +static uint8_t motor_drive_set_packages_ctr(double position, int direction); + /** * @brief 设置电机零点位置,需电机转动前设置。 * @@ -158,7 +162,7 @@ void motor_reg_event_cb(motor_on_event_t onevent) { return; } * * @return double 编码器当前位置值返回 */ -double motor_drive_read_encoder() { +static double motor_drive_read_encoder() { size_t encoder_buffer_size = 5; uint8_t buffer[5] = {0X3E, 0X90, MOTOR_ID, 0X00, 0XCF}; uint16_t encoder_data = 0; @@ -190,7 +194,7 @@ double motor_drive_read_encoder() { * @param direction 方向 * @return uint8_t */ -uint8_t motor_drive_set_packages_ctr(double position, int direction) { +static uint8_t motor_drive_set_packages_ctr(double position, int direction) { int position_int = 0; uint8_t position_remainder = 0; uint8_t position_buffer_size = 5; //从第五位开始(低位) diff --git a/main/motor_drive.h b/main/motor_drive.h index af10511..dcfcbc2 100644 --- a/main/motor_drive.h +++ b/main/motor_drive.h @@ -15,5 +15,6 @@ void motor_set_zero_point(); uint32_t motor_get_position_degree(); void motor_run_to_postion(int rotation_direction, double position, int speed_level); void motor_reg_event_cb(motor_on_event_t onevent); -double motor_drive_read_encoder(); -uint8_t motor_drive_set_packages_ctr(double position, int direction); \ No newline at end of file + +void motor_module_schedule(); + diff --git a/main/port.c b/main/port.c new file mode 100644 index 0000000..66275a2 --- /dev/null +++ b/main/port.c @@ -0,0 +1,11 @@ +#include "port.h" + +#include "esp_log.h" + +uint32_t port_get_ticket() { return esp_log_timestamp(); } +uint32_t port_haspassedms(uint32_t ticket) { return port_get_ticket() - ticket; } +uint32_t port_delay_ms(uint32_t ms) { + uint32_t now = port_get_ticket(); + while (port_get_ticket() - now < ms) { + } +} diff --git a/main/port.h b/main/port.h new file mode 100644 index 0000000..f67ac73 --- /dev/null +++ b/main/port.h @@ -0,0 +1,7 @@ +#pragma once +#include +uint32_t port_get_ticket(); + +uint32_t port_haspassedms(uint32_t ticket); + +uint32_t port_delay_ms(uint32_t ms);