Browse Source

控制指令

devtest
zwsd 3 years ago
parent
commit
aca6089dec
  1. 20
      main/main.c
  2. 68
      main/motor_drive.c
  3. 3
      main/motor_drive.h

20
main/main.c

@ -57,16 +57,18 @@ void app_main(void) {
// char* test_str = "This is a test string.\n";
while (true) {
bluetooth_gatts_try_process_data();
if (s_bluetooth_processer.auto_report_flag) {
receipt_json_get_status();
s_bluetooth_processer.auto_report_flag = false;
}
// bluetooth_gatts_try_process_data();
// if (s_bluetooth_processer.auto_report_flag) {
// receipt_json_get_status();
// s_bluetooth_processer.auto_report_flag = false;
// }
if ((motor_drive_read_encoder() == s_bluetooth_processer.position) && s_bluetooth_processer.motor_drive_turn_flag == true) {
ESP_LOGI("test", "info log ok\n");
s_bluetooth_processer.motor_drive_turn_flag = false;
}
// if ((motor_drive_read_encoder() == s_bluetooth_processer.position) && s_bluetooth_processer.motor_drive_turn_flag == true) {
// ESP_LOGI("test", "info log ok\n");
// s_bluetooth_processer.motor_drive_turn_flag = false;
// }
motor_drive_set_packages_ctr(35.68);
// uart_write_bytes(UART_NUM_2, (const char*)test_str, strlen(test_str));
// ets_delay_us(1000000);

68
main/motor_drive.c

@ -1,10 +1,30 @@
#include "motor_drive.h"
#include "driver/uart.h"
#include "esp_log.h"
#define MOTOR_DRIVE "MOTOR_DRIVE"
void motor_drive_uart_init() {}
#define uart_num UART_NUM_2
#define tx_io_num 4
#define rx_io_num 5
#define buffer_size 128
void motor_drive_uart_init() {
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));
}
void motor_drive_turn(int direction, int speed_level, double position) {
if ((direction > 2) || (direction < 0)) {
@ -16,8 +36,52 @@ void motor_drive_turn(int direction, int speed_level, double position) {
if ((position > 360) || (position <= 0)) {
ESP_LOGW(MOTOR_DRIVE, "Position out of range");
}
motor_drive_set_packages_ctr(position);
uart_write_bytes(uart_num, "test", strlen("test"));
}
double motor_drive_read_encoder() { return 0.0; }
void motor_drive_set_packages() {}
void motor_drive_set_packages_ctr(double position) {
int position_int = 0;
uint8_t position_remainder = 0;
uint8_t position_buffer_size = 5; //()
uint8_t checksum = 0;
uint8_t buffer[10] = {0x3E, 0XA7, 0X01, 0X04, 0XEA, 0X00, 0X00, 0X00, 0X00, 0X00};
uint8_t strbuffer[20] = {0};
position_int = position * 100;
if (position_int != 0) {
while ((position_int / 256) > 0) {
position_remainder = position_int % 256;
buffer[position_buffer_size] = position_remainder;
position_buffer_size += 1;
position_int = position_int / 256;
checksum += position_remainder;
}
buffer[position_buffer_size] = position_int;
checksum += position_int;
checksum %= 256;
buffer[9] = checksum;
}
for (int i = 0; i < 10; i++) {
ESP_LOGI(MOTOR_DRIVE, "%d", buffer[i]);
}
motor_drive_hex_to_str((char *)buffer, 10, (char *)strbuffer);
ESP_LOGI(MOTOR_DRIVE, "%s", strbuffer);
}
double motor_drive_read_encoder() { return 0.0; }
void motor_drive_hex_to_str(char *hex, int hex_len, char *str) {
int i, pos = 0;
for (i = 0; i < hex_len; i++)
{
sprintf(str + pos, "%02x", hex[i]);
pos += 2;
}
}

3
main/motor_drive.h

@ -39,3 +39,6 @@ typedef enum {
void motor_drive_uart_init();
void motor_drive_turn(int direction, int speed_level, double position);
double motor_drive_read_encoder();
void motor_drive_set_packages();
void motor_drive_set_packages_ctr(double position);
void motor_drive_hex_to_str(char *hex, int hex_len, char *str);
Loading…
Cancel
Save