You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include "motor_drive.h"
  2. #include "driver/uart.h"
  3. #include "esp_log.h"
  4. #define MOTOR_DRIVE "MOTOR_DRIVE"
  5. #define uart_num UART_NUM_2
  6. #define tx_io_num 4
  7. #define rx_io_num 5
  8. #define buffer_size 128
  9. void motor_drive_uart_init() {
  10. uart_config_t uart_config = {
  11. .baud_rate = 115200,
  12. .data_bits = UART_DATA_8_BITS,
  13. .parity = UART_PARITY_DISABLE,
  14. .stop_bits = UART_STOP_BITS_1,
  15. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  16. .source_clk = UART_SCLK_APB,
  17. };
  18. ESP_ERROR_CHECK(uart_driver_install(uart_num, buffer_size * 2, 0, 0, NULL, 0));
  19. ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
  20. ESP_ERROR_CHECK(uart_set_pin(uart_num, tx_io_num, rx_io_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  21. }
  22. void motor_drive_turn(int direction, int speed_level, double position) {
  23. if ((direction > 2) || (direction < 0)) {
  24. ESP_LOGW(MOTOR_DRIVE, "Direction out of range");
  25. }
  26. if ((speed_level > 9) || (speed_level < 0)) {
  27. ESP_LOGW(MOTOR_DRIVE, "Speed level out of range");
  28. }
  29. if ((position > 360) || (position <= 0)) {
  30. ESP_LOGW(MOTOR_DRIVE, "Position out of range");
  31. }
  32. motor_drive_set_packages_ctr(position);
  33. uart_write_bytes(uart_num, "test", strlen("test"));
  34. }
  35. double motor_drive_read_encoder() {
  36. uint8_t i = 0;
  37. uint8_t buffer[20] = {0};
  38. // Generate cmd
  39. motor_drive_buffer_cmd_generate(buffer, 0x90, 4, 0X5A97FF00);
  40. // Send cmd
  41. // uart_write_bytes(UART_NUM_2, (const char*)test_str, strlen(test_str));
  42. // Wait receive
  43. // while (1) {
  44. // len = uart_read_bytes(ECHO_UART_PORT_NUM, data, BUF_SIZE, 20 / portTICK_RATE_MS);
  45. // if(len != 0 || timout){
  46. // break;
  47. // }
  48. // }
  49. // Parse receive
  50. // R_encoder = motor_drive_buffer_cmd_parse();
  51. // parse motor usart
  52. // return R_encoder;
  53. return 0.0;
  54. }
  55. void motor_drive_set_packages_data_max64bit(uint8_t cmd, uint8_t buffer_data_size, uint64_t buffer_data) {
  56. uint8_t buffer[20] = {0};
  57. motor_drive_buffer_cmd_generate(buffer, cmd, buffer_data_size, buffer_data);
  58. }
  59. void motor_drive_set_packages_ctr(double position) {
  60. int position_int = 0;
  61. uint8_t position_remainder = 0;
  62. uint8_t position_buffer_size = 5; //从第五位开始(低位)
  63. uint8_t checksum = 0;
  64. uint8_t buffer[10] = {0x3E, 0XA7, 0X01, 0X04, 0XEA, 0X00, 0X00, 0X00, 0X00, 0X00};
  65. uint8_t strbuffer[20] = {0};
  66. position_int = position * 100;
  67. if (position_int != 0) {
  68. while ((position_int / 256) > 0) {
  69. position_remainder = position_int % 256;
  70. buffer[position_buffer_size] = position_remainder;
  71. position_buffer_size += 1;
  72. position_int = position_int / 256;
  73. checksum += position_remainder;
  74. }
  75. buffer[position_buffer_size] = position_int;
  76. checksum += position_int;
  77. checksum %= 256;
  78. buffer[9] = checksum;
  79. }
  80. for (int i = 0; i < 10; i++) {
  81. ESP_LOGI(MOTOR_DRIVE, "%d", buffer[i]);
  82. }
  83. motor_drive_hex_to_str((char *)buffer, 10, (char *)strbuffer);
  84. ESP_LOGI(MOTOR_DRIVE, "%s", strbuffer);
  85. }
  86. void motor_drive_hex_to_str(char *hex, int hex_len, char *str) {
  87. int i, pos = 0;
  88. for (i = 0; i < hex_len; i++)
  89. {
  90. sprintf(str + pos, "%02x", hex[i]);
  91. pos += 2;
  92. }
  93. }
  94. void motor_drive_buffer_cmd_generate(uint8_t *buffer, uint8_t cmd, uint8_t buffer_data_size, uint64_t buffer_data) {
  95. uint8_t i = 0;
  96. uint8_t checksum = 0;
  97. uint8_t buffer_data_uint8 = 0;
  98. uint8_t strbuffer[20] = {'\0'};
  99. uint8_t hex_to_str_size = buffer_data_size;
  100. if (buffer == NULL) {
  101. ESP_LOGW(MOTOR_DRIVE, "buffer nil ,init error");
  102. return;
  103. }
  104. buffer[0] = 0X3E;
  105. buffer[1] = cmd;
  106. buffer[2] = 0X1;
  107. buffer[3] = buffer_data_size;
  108. buffer[4] = (0X3E + cmd + 0X1 + buffer_data_size) % 255;
  109. if (buffer_data_size > 0) {
  110. hex_to_str_size = buffer_data_size + 1;
  111. for (i = 0; i < buffer_data_size; i++) {
  112. buffer_data_uint8 = buffer_data;
  113. buffer_data = buffer_data >> 8;
  114. buffer[5 + i] = buffer_data_uint8;
  115. checksum += buffer_data_uint8;
  116. }
  117. buffer[5 + buffer_data_size] = (checksum % 255);
  118. }
  119. motor_drive_hex_to_str((char *)buffer, (5 + hex_to_str_size), (char *)strbuffer);
  120. ESP_LOGI(MOTOR_DRIVE, "%s", strbuffer);
  121. motor_drive_buffer_cmd_parse(buffer);
  122. }
  123. void motor_drive_buffer_cmd_parse(uint8_t *buffer) {
  124. uint8_t i = 0;
  125. uint16_t temp_data_arr[(buffer[3] / 2)];
  126. ESP_LOGI(MOTOR_DRIVE, "=====buffer data size %d=====", buffer[3]);
  127. if (buffer == NULL) {
  128. ESP_LOGW(MOTOR_DRIVE, "cmd parse buffer null");
  129. return;
  130. }
  131. if (buffer[0] != 0X3E || buffer[2] != 0X01) {
  132. ESP_LOGW(MOTOR_DRIVE, "cmd parse buffer error");
  133. return;
  134. }
  135. for (i = 0; i < (buffer[3]); i += 2) {
  136. temp_data_arr[i] = buffer[5 + i] + (buffer[5 + i + 1] << 8);
  137. ESP_LOGI(MOTOR_DRIVE, "%d\n", temp_data_arr[i]);
  138. }
  139. }