Aruna
aruna::driver::ESP32_RMT_Dshot Class Reference

#include <ESP32_RMT_Dshot.h>

Inheritance diagram for aruna::driver::ESP32_RMT_Dshot:
Collaboration diagram for aruna::driver::ESP32_RMT_Dshot:

Public Member Functions

 ESP32_RMT_Dshot (rmt_channel_t channel, gpio_num_t gpio_port)
 Dshot150 using RMT hardware. More...
 
 ~ESP32_RMT_Dshot ()
 
- Public Member Functions inherited from aruna::driver::Dshot
 Dshot (uint32_t speed_hz=150000, bool bidirectional=false)
 Dshot parent object. More...
 
uint32_t get_frequency ()
 get frequency of the driver More...
 
err_t set_speed (int16_t speed)
 Set speed, use negative values to rotate in the other direction if bidirectional is enabled. More...
 
bool get_bidirectional ()
 Is this Dshot driver birectional (can spin both ways) More...
 
void set_bidirectional (bool is_bidirectional)
 Set bidirectional property of the driver. More...
 
void arm_ESC ()
 Call the function after the constructor to arm the ESC. More...
 
 ~Dshot ()
 
- Public Member Functions inherited from aruna::movement::Actuator
 Actuator (axis_mask_t axis=axis_mask_t::NONE)
 Actuator object, used by the movement module for vehicle movement. More...
 
virtual ~Actuator ()
 
err_t set_axis (axis_mask_t new_axis)
 Set the axis that this Actuator is capable in moving in. More...
 
axis_mask_t get_axis ()
 get the movement modes that this driver supports. More...
 
err_t set (axis_mask_t axisMask, int16_t speed)
 Set the speed of the motors directly. More...
 
uint16_t get_speed ()
 Get the current speed of Actuator. More...
 

Private Member Functions

void bits_to_dshotFrame (uint16_t bits, rmt_item32_t *frame_buffer)
 convert bits to rmt frame More...
 
void update_task ()
 updates ESC with current dshot buffer, blocks CPU More...
 
err_t _write_frame_continuous (uint16_t dshot_frame) override
 Write set Dshot frame continuously on the bus. More...
 
err_t single_write (uint16_t dshot_frame)
 Write a single Dshot frame to the bus. More...
 

Static Private Member Functions

static void * _update_task (void *_this)
 static function to help pthead More...
 

Private Attributes

uint16_t T0H_ticks: 15
 
uint16_t T1H_ticks: 15
 
uint16_t T0L_ticks: 15
 
uint16_t T1L_ticks: 15
 
pthread_mutex_t dshot_frame_lock
 
pthread_t update_handler
 
rmt_config_t driver_config
 
uint16_t dshot_frame = 0
 

Additional Inherited Members

- Public Attributes inherited from aruna::movement::Actuator
err_t startup_error = err_t::NOT_STARTED
 error when constructing gets put here, read before usage. More...
 
- Static Protected Member Functions inherited from aruna::movement::Actuator
static double convert_range (uint16_t input, float range_max=100.f, float range_min=0.f)
 Convert uint16 to a new range. More...
 
- Protected Attributes inherited from aruna::driver::Dshot
uint16_t T0H_ns
 
uint16_t T1H_ns
 
uint16_t T0L_ns
 
uint16_t T1L_ns
 

Detailed Description

Definition at line 15 of file ESP32_RMT_Dshot.h.

Constructor & Destructor Documentation

◆ ESP32_RMT_Dshot()

ESP32_RMT_Dshot::ESP32_RMT_Dshot ( rmt_channel_t  channel,
gpio_num_t  gpio_port 
)

Dshot150 using RMT hardware.

Parameters
channel,rmtchannel
gpio_port,gpioport to send Dshot over.

Definition at line 9 of file ESP32_RMT_Dshot.cpp.

9  {
10  const static uint32_t PERF_CLK_HZ = 80000000;
11 // TODO get the clock speed dynamicly
12  const static uint8_t clk_div = 1;
13 
14 // config driver
15  driver_config.channel = channel;
16  driver_config.gpio_num = gpio_port;
17  driver_config.rmt_mode = RMT_MODE_TX;
18  driver_config.clk_div = clk_div;
19  driver_config.mem_block_num = 1;
20  driver_config.tx_config.loop_en = false;
21  driver_config.tx_config.carrier_en = false;
22  driver_config.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
23  driver_config.tx_config.idle_output_en = false;
24 
25 // calculate ticks
26  const static float ticks_per_second_in_nanosecond = ((float) PERF_CLK_HZ / (float) clk_div) / 1000000000;
27 
28  T0H_ticks = ticks_per_second_in_nanosecond * T0H_ns;
29  T1H_ticks = ticks_per_second_in_nanosecond * T1H_ns;
30  T0L_ticks = ticks_per_second_in_nanosecond * T0L_ns;
31  T1L_ticks = ticks_per_second_in_nanosecond * T1L_ns;
32 
33  err_t rval;
34  esp_err_t eerr;
35  ESP_ERROR_CHECK(rmt_config(&driver_config));
36  eerr = rmt_driver_install(driver_config.channel, 0, 0);
37 // TODO make generic esp_err_t to aruna::err_t function?
38  switch (eerr) {
39  case ESP_OK:
40  rval = err_t::OK;
41  break;
42  case ESP_ERR_INVALID_ARG:
44  break;
45  case ESP_FAIL:
46  default:
47  rval = err_t::FAIL;
48  break;
49  }
50  // start thread
51  int p_mut, p_cre = 0;
52  p_mut = pthread_mutex_init(&dshot_frame_lock, NULL);
53  p_cre = pthread_create(&update_handler, NULL, _update_task, this);
54  if (p_mut || p_cre)
55  rval = err_t::TASK_FAILED;
56  startup_error = rval;
57 }
static void * _update_task(void *_this)
static function to help pthead
uint16_t T1H_ns
Definition: Dshot.h:58
uint16_t T0L_ns
Definition: Dshot.h:59
uint16_t T1L_ns
Definition: Dshot.h:60
uint16_t T0H_ns
Definition: Dshot.h:57
err_t startup_error
error when constructing gets put here, read before usage.
Definition: Actuator.h:45
Here is the call graph for this function:

◆ ~ESP32_RMT_Dshot()

ESP32_RMT_Dshot::~ESP32_RMT_Dshot ( )

Definition at line 59 of file ESP32_RMT_Dshot.cpp.

59  {
60  rmt_driver_uninstall(driver_config.channel);
61  pthread_cancel(update_handler);
62  pthread_mutex_destroy(&dshot_frame_lock);
63 }

Member Function Documentation

◆ _update_task()

void * ESP32_RMT_Dshot::_update_task ( void *  _this)
staticprivate

static function to help pthead

Parameters
_thisDshot object
Returns
0

Definition at line 76 of file ESP32_RMT_Dshot.cpp.

76  {
77  static_cast<ESP32_RMT_Dshot *>(_this)->update_task();
78  return 0;
79 }
void update_task()
updates ESC with current dshot buffer, blocks CPU
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _write_frame_continuous()

err_t ESP32_RMT_Dshot::_write_frame_continuous ( uint16_t  dshot_frame)
overrideprivatevirtual

Write set Dshot frame continuously on the bus.

Parameters
dshot_frameDhsot frame to send, already encoded in bits
Returns

Implements aruna::driver::Dshot.

Definition at line 81 of file ESP32_RMT_Dshot.cpp.

81  {
82  pthread_mutex_lock(&dshot_frame_lock);
83  this->dshot_frame = dshot_frame;
84  pthread_mutex_unlock(&dshot_frame_lock);
85  return err_t::OK;
86 }

◆ bits_to_dshotFrame()

void aruna::driver::ESP32_RMT_Dshot::bits_to_dshotFrame ( uint16_t  bits,
rmt_item32_t *  frame_buffer 
)
private

convert bits to rmt frame

Parameters
bitsdshot package (16bit with speed, telemetry and CRC)
frame_bufferarray of 17 (to include 0 terminator)

◆ single_write()

err_t ESP32_RMT_Dshot::single_write ( uint16_t  dshot_frame)
private

Write a single Dshot frame to the bus.

Parameters
dshot_frameframe to be written
Returns
always err_t::OK

Definition at line 88 of file ESP32_RMT_Dshot.cpp.

88  {
89  const static size_t rmt_size = 17;
90  static rmt_item32_t rmt_frame[rmt_size];
91 
92  for (uint8_t i = 0; i < rmt_size - 1; ++i) {
93  uint16_t i_bit = (dshot_frame >> (rmt_size - 2 - i)) & 0b1;
94  uint16_t on_time = i_bit ? T1H_ticks : T0H_ticks;
95  uint16_t off_time = i_bit ? T1L_ticks : T0L_ticks;
96 
97  rmt_frame[i] = {{{on_time, 1, off_time, 0}}};
98  }
99 // clear at the end
100  rmt_frame[rmt_size - 1] = {{{0, 1, 0, 0}}};
101 
102  ESP_ERROR_CHECK(rmt_write_items(driver_config.channel, rmt_frame, rmt_size, true));
103 // TODO return error.
104  return err_t::OK;
105 }
Here is the caller graph for this function:

◆ update_task()

void ESP32_RMT_Dshot::update_task ( )
private

updates ESC with current dshot buffer, blocks CPU

Definition at line 65 of file ESP32_RMT_Dshot.cpp.

65  {
66 
67  while (true) {
68  pthread_mutex_lock(&dshot_frame_lock);
70  pthread_mutex_unlock(&dshot_frame_lock);
71  usleep(1);
72  }
73 
74 }
err_t single_write(uint16_t dshot_frame)
Write a single Dshot frame to the bus.
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ driver_config

rmt_config_t aruna::driver::ESP32_RMT_Dshot::driver_config
private

Definition at line 27 of file ESP32_RMT_Dshot.h.

◆ dshot_frame

uint16_t aruna::driver::ESP32_RMT_Dshot::dshot_frame = 0
private

Definition at line 28 of file ESP32_RMT_Dshot.h.

◆ dshot_frame_lock

pthread_mutex_t aruna::driver::ESP32_RMT_Dshot::dshot_frame_lock
private

Definition at line 24 of file ESP32_RMT_Dshot.h.

◆ T0H_ticks

uint16_t aruna::driver::ESP32_RMT_Dshot::T0H_ticks
private

Definition at line 18 of file ESP32_RMT_Dshot.h.

◆ T0L_ticks

uint16_t aruna::driver::ESP32_RMT_Dshot::T0L_ticks
private

Definition at line 20 of file ESP32_RMT_Dshot.h.

◆ T1H_ticks

uint16_t aruna::driver::ESP32_RMT_Dshot::T1H_ticks
private

Definition at line 19 of file ESP32_RMT_Dshot.h.

◆ T1L_ticks

uint16_t aruna::driver::ESP32_RMT_Dshot::T1L_ticks
private

Definition at line 21 of file ESP32_RMT_Dshot.h.

◆ update_handler

pthread_t aruna::driver::ESP32_RMT_Dshot::update_handler
private

Definition at line 25 of file ESP32_RMT_Dshot.h.


The documentation for this class was generated from the following files: