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.

181 lines
5.3 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
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_1
  6. #define tx_io_num 1
  7. #define rx_io_num 2
  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. size_t encoder_buffer_size = 5;
  37. uint8_t buffer[5] = {0X3E,0X90,0X01,0X00,0XCF};
  38. uint16_t encoder_data = 0;
  39. // Generate cmd
  40. // encoder_buffer_size = motor_drive_buffer_cmd_generate(buffer, 0x90, 4, 0X5A97FF00);
  41. // if (encoder_buffer_size == 0) {
  42. // ESP_LOGW(MOTOR_DRIVE, "generate_buffer_size null");
  43. // return -1;
  44. // }
  45. uart_flush(uart_num);
  46. // Send cmd
  47. uart_write_bytes(uart_num, (const char *)buffer, encoder_buffer_size);
  48. encoder_buffer_size = 0;
  49. memset(buffer, 0, sizeof(uint8_t) * 5);
  50. // Wait receive
  51. encoder_buffer_size = uart_read_bytes(uart_num, buffer, 12, 2000 / portTICK_RATE_MS);
  52. if (encoder_buffer_size != 12 || buffer[0] != 0X3E) {
  53. ESP_LOGW(MOTOR_DRIVE, "encoder size:%d,buffer[0] = 0X%x", encoder_buffer_size, buffer[0]);
  54. return -1;
  55. }
  56. // Parse receive
  57. motor_drive_buffer_cmd_parse(buffer);
  58. encoder_data = buffer[5] + (buffer[6] << 8);
  59. // parse motor usart
  60. return ((double)encoder_data/100);
  61. }
  62. void motor_drive_set_packages_data_max64bit(uint8_t cmd, uint8_t buffer_data_size, uint64_t buffer_data) {
  63. uint8_t buffer[20] = {0};
  64. motor_drive_buffer_cmd_generate(buffer, cmd, buffer_data_size, buffer_data);
  65. }
  66. void motor_drive_set_packages_ctr(double position) {
  67. int position_int = 0;
  68. uint8_t position_remainder = 0;
  69. uint8_t position_buffer_size = 5; //从第五位开始(低位)
  70. uint8_t checksum = 0;
  71. uint8_t buffer[10] = {0x3E, 0XA7, 0X01, 0X04, 0XEA, 0X00, 0X00, 0X00, 0X00, 0X00};
  72. uint8_t strbuffer[20] = {0};
  73. position_int = position * 100;
  74. ESP_LOGI(MOTOR_DRIVE, "%d", position_int);
  75. if (position_int != 0) {
  76. while ((position_int / 0XFF) > 0) {
  77. position_remainder = position_int & 0XFF;
  78. ESP_LOGI(MOTOR_DRIVE, "position_remainder :%d", position_remainder);
  79. buffer[position_buffer_size] = position_remainder;
  80. position_buffer_size += 1;
  81. position_int = position_int >> 8;
  82. checksum += position_remainder;
  83. }
  84. buffer[position_buffer_size] = position_int;
  85. checksum += position_int;
  86. checksum = checksum & 0XFF;
  87. buffer[9] = checksum;
  88. }
  89. for (int i = 0; i < 10; i++) {
  90. ESP_LOGI(MOTOR_DRIVE, "%d", buffer[i]);
  91. }
  92. motor_drive_hex_to_str((char *)buffer, 10, (char *)strbuffer);
  93. ESP_LOGI(MOTOR_DRIVE, "%s", strbuffer);
  94. }
  95. void motor_drive_hex_to_str(char *hex, int hex_len, char *str) {
  96. int i, pos = 0;
  97. for (i = 0; i < hex_len; i++)
  98. {
  99. sprintf(str + pos, "%02x", hex[i]);
  100. pos += 2;
  101. }
  102. }
  103. size_t motor_drive_buffer_cmd_generate(uint8_t *buffer, uint8_t cmd, uint8_t buffer_data_size, uint64_t buffer_data) {
  104. uint8_t i = 0;
  105. uint8_t checksum = 0;
  106. uint8_t buffer_data_uint8 = 0;
  107. uint8_t strbuffer[20] = {'\0'};
  108. uint8_t hex_to_str_size = buffer_data_size;
  109. if (buffer == NULL) {
  110. ESP_LOGW(MOTOR_DRIVE, "buffer nil ,init error");
  111. return 0;
  112. }
  113. buffer[0] = 0X3E;
  114. buffer[1] = cmd;
  115. buffer[2] = 0X1;
  116. buffer[3] = buffer_data_size;
  117. buffer[4] = (0X3E + cmd + 0X1 + buffer_data_size) % 0XFF;
  118. if (buffer_data_size > 0) {
  119. hex_to_str_size = buffer_data_size + 1;
  120. for (i = 0; i < buffer_data_size; i++) {
  121. buffer_data_uint8 = buffer_data;
  122. buffer_data = buffer_data >> 8;
  123. buffer[5 + i] = buffer_data_uint8;
  124. checksum += buffer_data_uint8;
  125. }
  126. buffer[5 + buffer_data_size] = (checksum % 0XFF);
  127. }
  128. motor_drive_hex_to_str((char *)buffer, (5 + hex_to_str_size), (char *)strbuffer);
  129. ESP_LOGI(MOTOR_DRIVE, "%s", strbuffer);
  130. motor_drive_buffer_cmd_parse(buffer);
  131. return (5 + hex_to_str_size);
  132. }
  133. void motor_drive_buffer_cmd_parse(uint8_t *buffer) {
  134. uint8_t i = 0;
  135. uint16_t temp_data_arr[(buffer[3] / 2)];
  136. ESP_LOGI(MOTOR_DRIVE, "=====buffer data size %d=====", buffer[3]);
  137. if (buffer == NULL) {
  138. ESP_LOGW(MOTOR_DRIVE, "cmd parse buffer null");
  139. return;
  140. }
  141. if (buffer[0] != 0X3E || buffer[2] != 0X01) {
  142. ESP_LOGW(MOTOR_DRIVE, "cmd parse buffer error");
  143. return;
  144. }
  145. for (i = 0; i < (buffer[3]); i += 2) {
  146. temp_data_arr[i] = buffer[5 + i] + (buffer[5 + i + 1] << 8);
  147. ESP_LOGI(MOTOR_DRIVE, "%d\n", temp_data_arr[i]);
  148. }
  149. }
  150. void motor_drive_set_motor_size() {}