dshot_esc_example_main.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "esp_log.h"
  9. #include "driver/rmt_tx.h"
  10. #include "dshot_esc_encoder.h"
  11. #if CONFIG_IDF_TARGET_ESP32H2
  12. #define DSHOT_ESC_RESOLUTION_HZ 32000000 // 32MHz resolution, DSHot protocol needs a relative high resolution
  13. #else
  14. #define DSHOT_ESC_RESOLUTION_HZ 40000000 // 40MHz resolution, DSHot protocol needs a relative high resolution
  15. #endif
  16. #define DSHOT_ESC_GPIO_NUM 0
  17. static const char *TAG = "example";
  18. void app_main(void)
  19. {
  20. ESP_LOGI(TAG, "Create RMT TX channel");
  21. rmt_channel_handle_t esc_chan = NULL;
  22. rmt_tx_channel_config_t tx_chan_config = {
  23. .clk_src = RMT_CLK_SRC_DEFAULT, // select a clock that can provide needed resolution
  24. .gpio_num = DSHOT_ESC_GPIO_NUM,
  25. .mem_block_symbols = 64,
  26. .resolution_hz = DSHOT_ESC_RESOLUTION_HZ,
  27. .trans_queue_depth = 10, // set the number of transactions that can be pending in the background
  28. };
  29. ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &esc_chan));
  30. ESP_LOGI(TAG, "Install Dshot ESC encoder");
  31. rmt_encoder_handle_t dshot_encoder = NULL;
  32. dshot_esc_encoder_config_t encoder_config = {
  33. .resolution = DSHOT_ESC_RESOLUTION_HZ,
  34. .baud_rate = 300000, // DSHOT300 protocol
  35. .post_delay_us = 50, // extra delay between each frame
  36. };
  37. ESP_ERROR_CHECK(rmt_new_dshot_esc_encoder(&encoder_config, &dshot_encoder));
  38. ESP_LOGI(TAG, "Enable RMT TX channel");
  39. ESP_ERROR_CHECK(rmt_enable(esc_chan));
  40. rmt_transmit_config_t tx_config = {
  41. .loop_count = -1, // infinite loop
  42. };
  43. dshot_esc_throttle_t throttle = {
  44. .throttle = 0,
  45. .telemetry_req = false, // telemetry is not supported in this example
  46. };
  47. ESP_LOGI(TAG, "Start ESC by sending zero throttle for a while...");
  48. ESP_ERROR_CHECK(rmt_transmit(esc_chan, dshot_encoder, &throttle, sizeof(throttle), &tx_config));
  49. vTaskDelay(pdMS_TO_TICKS(5000));
  50. ESP_LOGI(TAG, "Increase throttle, no telemetry");
  51. for (uint16_t thro = 100; thro < 1000; thro += 10) {
  52. throttle.throttle = thro;
  53. ESP_ERROR_CHECK(rmt_transmit(esc_chan, dshot_encoder, &throttle, sizeof(throttle), &tx_config));
  54. // the previous loop transfer is till undergoing, we need to stop it and restart,
  55. // so that the new throttle can be updated on the output
  56. ESP_ERROR_CHECK(rmt_disable(esc_chan));
  57. ESP_ERROR_CHECK(rmt_enable(esc_chan));
  58. vTaskDelay(pdMS_TO_TICKS(1000));
  59. }
  60. }