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.

146 lines
4.1 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
  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 buffer[20] = {0};
  37. // Generate cmd
  38. motor_drive_buffer_cmd_generate(buffer, 0x90, 0, 0);
  39. // Send cmd
  40. // uart_write_bytes(UART_NUM_2, (const char*)test_str, strlen(test_str));
  41. // Wait receive
  42. // while (1) {
  43. // len = uart_read_bytes(ECHO_UART_PORT_NUM, data, BUF_SIZE, 20 / portTICK_RATE_MS);
  44. // if(len != 0 || timout){
  45. // break;
  46. // }
  47. // }
  48. // Parse receive
  49. // R_encoder = motor_drive_buffer_cmd_parse();
  50. // parse motor usart
  51. // return R_encoder;
  52. return 0.0;
  53. }
  54. void motor_drive_set_packages_data_max64bit(uint8_t cmd, uint8_t buffer_data_size, uint64_t buffer_data) {
  55. uint8_t buffer[20] = {0};
  56. motor_drive_buffer_cmd_generate(buffer, cmd, buffer_data_size, buffer_data);
  57. }
  58. void motor_drive_set_packages_ctr(double position) {
  59. int position_int = 0;
  60. uint8_t position_remainder = 0;
  61. uint8_t position_buffer_size = 5; //从第五位开始(低位)
  62. uint8_t checksum = 0;
  63. uint8_t buffer[10] = {0x3E, 0XA7, 0X01, 0X04, 0XEA, 0X00, 0X00, 0X00, 0X00, 0X00};
  64. uint8_t strbuffer[20] = {0};
  65. position_int = position * 100;
  66. if (position_int != 0) {
  67. while ((position_int / 256) > 0) {
  68. position_remainder = position_int % 256;
  69. buffer[position_buffer_size] = position_remainder;
  70. position_buffer_size += 1;
  71. position_int = position_int / 256;
  72. checksum += position_remainder;
  73. }
  74. buffer[position_buffer_size] = position_int;
  75. checksum += position_int;
  76. checksum %= 256;
  77. buffer[9] = checksum;
  78. }
  79. for (int i = 0; i < 10; i++) {
  80. ESP_LOGI(MOTOR_DRIVE, "%d", buffer[i]);
  81. }
  82. motor_drive_hex_to_str((char *)buffer, 10, (char *)strbuffer);
  83. ESP_LOGI(MOTOR_DRIVE, "%s", strbuffer);
  84. }
  85. void motor_drive_hex_to_str(char *hex, int hex_len, char *str) {
  86. int i, pos = 0;
  87. for (i = 0; i < hex_len; i++)
  88. {
  89. sprintf(str + pos, "%02x", hex[i]);
  90. pos += 2;
  91. }
  92. }
  93. void motor_drive_buffer_cmd_generate(uint8_t *buffer, uint8_t cmd, uint8_t buffer_data_size, uint64_t buffer_data) {
  94. uint8_t i = 0;
  95. uint8_t checksum = 0;
  96. uint8_t buffer_data_uint8 = 0;
  97. uint8_t strbuffer[20] = {'\0'};
  98. uint8_t hex_to_str_size = buffer_data_size;
  99. if (buffer == NULL) {
  100. ESP_LOGW(MOTOR_DRIVE, "buffer nil ,init error");
  101. return;
  102. }
  103. buffer[0] = 0X3E;
  104. buffer[1] = cmd;
  105. buffer[2] = 0X1;
  106. buffer[3] = buffer_data_size;
  107. buffer[4] = (0X3E + cmd + 0X1 + buffer_data_size) % 255;
  108. if (buffer_data_size > 0) {
  109. hex_to_str_size = buffer_data_size + 1;
  110. for (i = 0; i < buffer_data_size; i++) {
  111. buffer_data_uint8 = buffer_data;
  112. buffer_data = buffer_data >> 8;
  113. buffer[5 + i] = buffer_data_uint8;
  114. checksum += buffer_data_uint8;
  115. }
  116. buffer[5 + buffer_data_size] = (checksum % 255);
  117. }
  118. motor_drive_hex_to_str((char *)buffer, (5 + hex_to_str_size), (char *)strbuffer);
  119. ESP_LOGI(MOTOR_DRIVE, "%s", strbuffer);
  120. }
  121. void motor_drive_buffer_cmd_parse(uint8_t *buffer) {}