Browse Source

update

master
zwsd 3 years ago
parent
commit
9c59bd7b2b
  1. 3
      .vscode/settings.json
  2. 36
      main/ble_spp_server_demo.c
  3. 4
      main/ble_spp_server_demo.h
  4. 236
      main/main.c
  5. 8
      main/motor_drive.c
  6. 5
      main/motor_drive.h
  7. 11
      main/port.c
  8. 7
      main/port.h

3
.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"
},
}

36
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;
}
}

4
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);

236
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;
}
const char *order = cJSON_GetStringValue(cJSON_GetObjectItem(rxjson, "order"));
for (int i = 0; i < rxsize; i++) {
ble_uart_init_struct.rxbuf[ble_rx_buffer_now_len++] = rxmessage[i];
ble_rx_buffer_off++;
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 (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "index"))) {
index = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "index"));
}
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;
if (cJSON_IsNumber(cJSON_GetObjectItem(rxjson, "speedLevel"))) {
speedLevel = cJSON_GetNumberValue(cJSON_GetObjectItem(rxjson, "speedLevel"));
}
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--;
}
if (strcmp(ch->string, "index") == 0) {
ble_uart_receive_data.index = ch->valueint;
cmd_length--;
}
if (strcmp(ch->string, "speedLevel") == 0) {
ble_uart_receive_data.speedLevel = ch->valueint;
cmd_length--;
}
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 (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) {
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;
}
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();
}
}

8
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; //()

5
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);
void motor_module_schedule();

11
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) {
}
}

7
main/port.h

@ -0,0 +1,7 @@
#pragma once
#include <stdint.h>
uint32_t port_get_ticket();
uint32_t port_haspassedms(uint32_t ticket);
uint32_t port_delay_ms(uint32_t ms);
Loading…
Cancel
Save