Aruna
aruna::driver::Dshot Class Referenceabstract

#include <Dshot.h>

Inheritance diagram for aruna::driver::Dshot:
Collaboration diagram for aruna::driver::Dshot:

Public Member Functions

 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...
 

Protected Attributes

uint16_t T0H_ns
 
uint16_t T1H_ns
 
uint16_t T0L_ns
 
uint16_t T1L_ns
 

Private Member Functions

void add_CRC (uint16_t &bits)
 add CRC to end of bit stream. More...
 
uint16_t create_dshotFrame (uint16_t speed, uint8_t telemetry)
 craft dshot package More...
 
err_t _set (movement::axis_mask_t axisMask, int16_t speed) override
 Implementation of axis movement, this function is called from set(...). More...
 
err_t write_frame_continuous (uint16_t dshot_frame)
 Write set Dshot frame continuously on the bus. More...
 
virtual err_t _write_frame_continuous (uint16_t dshot_frame)=0
 Write set Dshot frame continuously on the bus. More...
 

Private Attributes

bool _continues_write = false
 
pthread_mutex_t write_mut
 
const uint32_t frequency
 
bool bidirectional
 

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...
 

Detailed Description

Definition at line 13 of file Dshot.h.

Constructor & Destructor Documentation

◆ Dshot()

Dshot::Dshot ( uint32_t  speed_hz = 150000,
bool  bidirectional = false 
)

Dshot parent object.

Parameters
speed_hzspeed of the protocol
bidirectionalset to true if the ESC is bidirectional.

Definition at line 13 of file Dshot.cpp.

13  : frequency(speed_hz), bidirectional(bidirectional) {
14 
15  uint32_t max_ns = (1.f / (float) speed_hz) * 1000000000;
16 
17  const static float T0H_percentage = 0.375;
18  const static float T1H_percentage = 0.75;
19  const static float T0L_percentage = 1 - T0H_percentage;
20  const static float T1L_percentage = 1 - T1H_percentage;
21 
22  T0H_ns = max_ns * T0H_percentage;
23  T1H_ns = max_ns * T1H_percentage;
24  T0L_ns = max_ns * T0L_percentage;
25  T1L_ns = max_ns * T1L_percentage;
26 
27  pthread_mutexattr_t swm_att;
28  pthread_mutexattr_init(&swm_att);
29  pthread_mutex_init(&write_mut, &swm_att);
30  pthread_mutexattr_destroy(&swm_att);
31 
32 }
bool bidirectional
Definition: Dshot.h:18
uint16_t T1H_ns
Definition: Dshot.h:58
const uint32_t frequency
Definition: Dshot.h:17
uint16_t T0L_ns
Definition: Dshot.h:59
pthread_mutex_t write_mut
Definition: Dshot.h:16
uint16_t T1L_ns
Definition: Dshot.h:60
uint16_t T0H_ns
Definition: Dshot.h:57

◆ ~Dshot()

Dshot::~Dshot ( )

Definition at line 116 of file Dshot.cpp.

116  {
117  pthread_mutex_destroy(&write_mut);
118 }
pthread_mutex_t write_mut
Definition: Dshot.h:16

Member Function Documentation

◆ _set()

aruna::err_t Dshot::_set ( movement::axis_mask_t  axisMask,
int16_t  speed 
)
overrideprivatevirtual

Implementation of axis movement, this function is called from set(...).

Only gets called when axisMask match axis of Actuator.

Parameters
axisMaskaxis (can be multiple) to set speed to.
speeddesired speed.
Returns
err_t::OK if successful, others when not...

Implements aruna::movement::Actuator.

Definition at line 35 of file Dshot.cpp.

35  {
36  return set_speed(speed);
37 }
err_t set_speed(int16_t speed)
Set speed, use negative values to rotate in the other direction if bidirectional is enabled...
Definition: Dshot.cpp:39
Here is the call graph for this function:

◆ _write_frame_continuous()

virtual err_t aruna::driver::Dshot::_write_frame_continuous ( uint16_t  dshot_frame)
privatepure virtual

Write set Dshot frame continuously on the bus.

Parameters
dshot_frameDhsot frame to send, already encoded in bits
Returns

Implemented in aruna::driver::ESP32_RMT_Dshot.

Here is the caller graph for this function:

◆ add_CRC()

void Dshot::add_CRC ( uint16_t &  bits)
private

add CRC to end of bit stream.

Parameters
bitsto add CRC on

Definition at line 62 of file Dshot.cpp.

62  {
63  uint8_t csum = 0;
64  uint16_t csum_data = bits >> 4;
65  for (uint8_t i = 0; i < 3; i++) {
66  csum ^= csum_data;
67  csum_data >>= 4;
68  }
69  csum &= 0xf;
70  bits |= csum;
71 }
Here is the caller graph for this function:

◆ arm_ESC()

void Dshot::arm_ESC ( )

Call the function after the constructor to arm the ESC.

set_speed() will not work without arming the ESC.

Definition at line 83 of file Dshot.cpp.

83  {
84 
85 // arm ESC
86  uint16_t start_value = 48;
87  uint8_t step = 50;
88  uint8_t amount_of_steps = 40;
89 
90 // up hill
91  for (int i = 0; i < amount_of_steps; ++i) {
92  write_frame_continuous(create_dshotFrame((step * i) + start_value, 0));
93  usleep(1);
94  }
95 // down hill
96  for (int i = amount_of_steps - 1; i > -1; --i) {
97  write_frame_continuous(create_dshotFrame((step * i) + start_value, 0));
98  usleep(1);
99  }
100 // stay low for a little while
101  write_frame_continuous(create_dshotFrame(start_value, 0));
102  usleep(150);
103 
104  set_speed(0);
105 
106 }
err_t write_frame_continuous(uint16_t dshot_frame)
Write set Dshot frame continuously on the bus.
Definition: Dshot.cpp:108
err_t set_speed(int16_t speed)
Set speed, use negative values to rotate in the other direction if bidirectional is enabled...
Definition: Dshot.cpp:39
uint16_t create_dshotFrame(uint16_t speed, uint8_t telemetry)
craft dshot package
Definition: Dshot.cpp:75
Here is the call graph for this function:

◆ create_dshotFrame()

uint16_t Dshot::create_dshotFrame ( uint16_t  speed,
uint8_t  telemetry 
)
private

craft dshot package

Parameters
speedkeep between 48-2047
telemetry0 or 1
Returns
dshot bit package with CRC

Definition at line 75 of file Dshot.cpp.

75  {
76  uint16_t bit_frame = 0;
77  bit_frame = speed << 5;
78  bit_frame |= telemetry << 4;
79  add_CRC(bit_frame);
80  return bit_frame;
81 }
void add_CRC(uint16_t &bits)
add CRC to end of bit stream.
Definition: Dshot.cpp:62
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_bidirectional()

bool Dshot::get_bidirectional ( )

Is this Dshot driver birectional (can spin both ways)

Returns
true if is birectional, false if not.

Definition at line 124 of file Dshot.cpp.

124  {
125  return bidirectional;
126 }
bool bidirectional
Definition: Dshot.h:18

◆ get_frequency()

uint32_t Dshot::get_frequency ( )

get frequency of the driver

Returns
frequency in hertz.

Definition at line 120 of file Dshot.cpp.

120  {
121  return frequency;
122 }
const uint32_t frequency
Definition: Dshot.h:17

◆ set_bidirectional()

void Dshot::set_bidirectional ( bool  is_bidirectional)

Set bidirectional property of the driver.

Parameters
is_bidirectionaltrue is bidirectional, false if single directional.

Definition at line 128 of file Dshot.cpp.

128  {
129  bidirectional = is_bidirectional;
130 }
bool bidirectional
Definition: Dshot.h:18

◆ set_speed()

err_t Dshot::set_speed ( int16_t  speed)

Set speed, use negative values to rotate in the other direction if bidirectional is enabled.

Parameters
speedspeed
Returns

Definition at line 39 of file Dshot.cpp.

39  {
40  const static uint16_t MAX_VALUE = 2047;
41  const static uint16_t MIN_VALUE = 48;
42  const static uint16_t MID_VALUE = (MAX_VALUE + MIN_VALUE) / 2;
43  const static uint8_t telemetry = 0;
44 
45  uint16_t max, min = 0;
46  if (bidirectional) {
47  max = speed < 0 ? MID_VALUE : MAX_VALUE;
48  min = speed < 0 ? MIN_VALUE : MID_VALUE;
49  } else {
50  max = MAX_VALUE;
51  min = MIN_VALUE;
52  }
53  uint16_t adjusted_speed = (uint16_t) convert_range(abs(speed), max, min);
54  uint16_t bit_frame = create_dshotFrame(adjusted_speed, telemetry);
55 
56 
57  return write_frame_continuous(bit_frame);
58 }
bool bidirectional
Definition: Dshot.h:18
static double convert_range(uint16_t input, float range_max=100.f, float range_min=0.f)
Convert uint16 to a new range.
Definition: Actuator.cpp:46
err_t write_frame_continuous(uint16_t dshot_frame)
Write set Dshot frame continuously on the bus.
Definition: Dshot.cpp:108
uint16_t create_dshotFrame(uint16_t speed, uint8_t telemetry)
craft dshot package
Definition: Dshot.cpp:75
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_frame_continuous()

err_t Dshot::write_frame_continuous ( uint16_t  dshot_frame)
private

Write set Dshot frame continuously on the bus.

Parameters
dshot_framedhsot frame to send, already encoded in bits
Returns

Definition at line 108 of file Dshot.cpp.

108  {
109  err_t e;
110  pthread_mutex_lock(&write_mut);
111  e = _write_frame_continuous(dshot_frame);
112  pthread_mutex_unlock(&write_mut);
113  return e;
114 }
virtual err_t _write_frame_continuous(uint16_t dshot_frame)=0
Write set Dshot frame continuously on the bus.
pthread_mutex_t write_mut
Definition: Dshot.h:16
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _continues_write

bool aruna::driver::Dshot::_continues_write = false
private

Definition at line 15 of file Dshot.h.

◆ bidirectional

bool aruna::driver::Dshot::bidirectional
private

Definition at line 18 of file Dshot.h.

◆ frequency

const uint32_t aruna::driver::Dshot::frequency
private

Definition at line 17 of file Dshot.h.

◆ T0H_ns

uint16_t aruna::driver::Dshot::T0H_ns
protected

Definition at line 57 of file Dshot.h.

◆ T0L_ns

uint16_t aruna::driver::Dshot::T0L_ns
protected

Definition at line 59 of file Dshot.h.

◆ T1H_ns

uint16_t aruna::driver::Dshot::T1H_ns
protected

Definition at line 58 of file Dshot.h.

◆ T1L_ns

uint16_t aruna::driver::Dshot::T1L_ns
protected

Definition at line 60 of file Dshot.h.

◆ write_mut

pthread_mutex_t aruna::driver::Dshot::write_mut
private

Definition at line 16 of file Dshot.h.


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