Browse Source

已经可以读取电机编码器的值,修复了处理数据慢问题。

master
zwsd 3 years ago
parent
commit
52006b379d
  1. 6
      .vscode/settings.json
  2. 45
      main/main.c
  3. 57
      main/motor_drive.c
  4. 5
      main/motor_drive.h

6
.vscode/settings.json

@ -30,6 +30,10 @@
"functional": "c", "functional": "c",
"tuple": "c", "tuple": "c",
"type_traits": "c", "type_traits": "c",
"utility": "c"
"utility": "c",
"uart_types.h": "c",
"esp_intr_alloc.h": "c",
"motor_drive.h": "c",
"stdint.h": "c"
}, },
} }

45
main/main.c

@ -2,6 +2,7 @@
#include "cJSON_Utils.h" #include "cJSON_Utils.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/timer.h" #include "driver/timer.h"
#include "driver/uart.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_system.h" #include "esp_system.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
@ -38,7 +39,7 @@ bleuart_t ble_uart_init_struct = {
}; };
static cJSON *json_tmp; static cJSON *json_tmp;
// static double encoder_befor_num;
static double encoder_befor_num;
#define set_position "setPosition" #define set_position "setPosition"
#define get_status "getStatus" #define get_status "getStatus"
#define set_motor_current_size "setMotorCurrentSize" #define set_motor_current_size "setMotorCurrentSize"
@ -77,28 +78,32 @@ ble_uart_receive_data_t ble_uart_receive_data = {
.cmd_flag = false, .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) { 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) { if ((rxsize + ble_rx_buffer_now_len) > ble_uart_init_struct.rxbufsize) {
ESP_LOGW("MAIN", "rx buffer overstep"); ESP_LOGW("MAIN", "rx buffer overstep");
buffer_all_init();
return; return;
} }
for (int i = 0; i < rxsize; i++) { for (int i = 0; i < rxsize; i++) {
ble_uart_init_struct.rxbuf[ble_rx_buffer_now_len++] = rxmessage[i]; ble_uart_init_struct.rxbuf[ble_rx_buffer_now_len++] = rxmessage[i];
ble_rx_buffer_off++; ble_rx_buffer_off++;
start_time = esp_log_timestamp();
} }
} }
void buffer_all_init() {
ble_rx_buffer_now_len = 0;
memset(ble_uart_init_struct.rxbuf, 0, ble_uart_init_struct.rxbufsize);
}
void wait_ble_uart_receive_ms(uint16_t wait_time_ms) { void wait_ble_uart_receive_ms(uint16_t wait_time_ms) {
start_time = esp_log_timestamp(); start_time = esp_log_timestamp();
while ((esp_log_timestamp() - start_time) < wait_time_ms) { 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) { bool parse_rxbuffer_and_validation_data(cJSON **json_tmp) {
@ -164,21 +169,24 @@ void processing_uart_rx_data() {
if (ble_rx_buffer_off != 0) { if (ble_rx_buffer_off != 0) {
modbus_uart_rx_off_before = ble_rx_buffer_off; modbus_uart_rx_off_before = ble_rx_buffer_off;
wait_ble_uart_receive_ms(100);
wait_ble_uart_receive_ms(200);
if (ble_rx_buffer_off == modbus_uart_rx_off_before) { if (ble_rx_buffer_off == modbus_uart_rx_off_before) {
ble_uart_init_struct.receive_data_processing_flag = true; ble_uart_init_struct.receive_data_processing_flag = true;
ESP_LOGI("MAIN", "%s", ble_uart_init_struct.rxbuf); ESP_LOGI("MAIN", "%s", ble_uart_init_struct.rxbuf);
if (parse_rxbuffer_and_validation_data(&json_tmp)) { if (parse_rxbuffer_and_validation_data(&json_tmp)) {
if (parse_json_to_struct(json_tmp->child)) { 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,
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); 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) {
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) { // if (motor_drive_set_packages_ctr(ble_uart_receive_data.position, ble_uart_receive_data.direction) == 0) {
// ets_delay_us(50000); // ets_delay_us(50000);
// if (encoder_befor_num == motor_drive_read_encoder()) { // if (encoder_befor_num == motor_drive_read_encoder()) {
@ -187,10 +195,10 @@ void processing_uart_rx_data() {
// ESP_LOGI(MAIN_LOG_TAG, "motor turning"); // ESP_LOGI(MAIN_LOG_TAG, "motor turning");
// } // }
// } // }
// }
}
// // receipt_json_set_position();
// }
// receipt_json_set_position();
}
// if (strcmp(ble_uart_receive_data.order, get_status) == 0) { // if (strcmp(ble_uart_receive_data.order, get_status) == 0) {
// ESP_LOGI(MAIN_LOG_TAG, get_status); // ESP_LOGI(MAIN_LOG_TAG, get_status);
// receipt_json_get_status(); // receipt_json_get_status();
@ -212,8 +220,8 @@ void processing_uart_rx_data() {
if (ble_uart_init_struct.receive_data_processing_flag == true) { if (ble_uart_init_struct.receive_data_processing_flag == true) {
buffer_all_init(); buffer_all_init();
ble_uart_init_struct.receive_data_processing_flag = false;
ble_rx_buffer_off = 0; ble_rx_buffer_off = 0;
ble_uart_init_struct.receive_data_processing_flag = false;
} }
} }
} }
@ -221,6 +229,7 @@ void processing_uart_rx_data() {
void app_main(void) { void app_main(void) {
ble_spp_server_demo_app_main(&ble_uart_init_struct); ble_spp_server_demo_app_main(&ble_uart_init_struct);
bleuart_reg_cb(blerxcb); bleuart_reg_cb(blerxcb);
motor_init(&ble_uart_motor_structer);
while (true) { while (true) {
processing_uart_rx_data(); processing_uart_rx_data();

57
main/motor_drive.c

@ -1,6 +1,36 @@
#include "motor_drive.h" #include "motor_drive.h"
void motor_init(motor_t* motor) { return; }
#include "driver/uart.h"
#include "esp_log.h"
#define MOTOR_DRIVE "MOTOR_DRIVE"
#define uart_num UART_NUM_1
#define tx_io_num 23
#define rx_io_num 22
#define buffer_size 128
#define uart_read_time_ms 200
#define MOTOR_ID 0X01
static motor_t *motor_init_structer;
void motor_init(motor_t *motor) {
motor_init_structer = motor;
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
ESP_ERROR_CHECK(uart_driver_install(uart_num, buffer_size * 2, 0, 0, NULL, 0));
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(uart_num, tx_io_num, rx_io_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
return;
}
void motor_set_zero_point() { return; } void motor_set_zero_point() { return; }
/** /**
* @brief 0->360 * @brief 0->360
@ -13,3 +43,28 @@ uint32_t motor_get_position_degree() { return 0; }
*/ */
void motor_run_to_postion(int potion) { return; } void motor_run_to_postion(int potion) { return; }
void motor_reg_event_cb(motor_on_event_t onevent) { return; } void motor_reg_event_cb(motor_on_event_t onevent) { return; }
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;
// Send cmd
uart_flush(uart_num);
uart_write_bytes(uart_num, (const char *)buffer, encoder_buffer_size);
encoder_buffer_size = 0;
memset(buffer, 0, sizeof(uint8_t) * 5);
// Wait receive
encoder_buffer_size = uart_read_bytes(uart_num, buffer, 12, uart_read_time_ms / portTICK_RATE_MS);
if (encoder_buffer_size != 12 || buffer[0] != 0X3E) {
ESP_LOGW(MOTOR_DRIVE, "encoder size:%d,buffer[0] = 0X%x", encoder_buffer_size, buffer[0]);
return -1.0;
}
encoder_data = buffer[5] + (buffer[6] << 8);
return ((double)encoder_data / 100.0);
}

5
main/motor_drive.h

@ -1,9 +1,9 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <string.h>
typedef struct { typedef struct {
int mark;
// int uartNum;
int uartNum;
} motor_t; } motor_t;
typedef enum { typedef enum {
kRunToPosition, kRunToPosition,
@ -23,3 +23,4 @@ uint32_t motor_get_position_degree();
*/ */
void motor_run_to_postion(int potion); void motor_run_to_postion(int potion);
void motor_reg_event_cb(motor_on_event_t onevent); void motor_reg_event_cb(motor_on_event_t onevent);
double motor_drive_read_encoder();
Loading…
Cancel
Save