Arduino ESC Tester — Adding DShot

The Arduino Servo/ESC Tester Series (Part 4)

Figure 1. Arduino & Tester Shield.

Synchronous vs Asynchronous Serial Protocols

Figure 2. Synchronous Serial Data (credit)
Figure 3. Asynchronous Serial Data (credit)

A Review of DShot

Figure 4. DShot vs OneShot (credit)
Figure 5. DShot Message Format ( credit)
Figure 6. DShot message on oscilloscope ( credit)
//    this typedef taken from src\main\drivers\pwm_output.h in the //    betaflight github page typedef enum { 
DSHOT_CMD_MOTOR_STOP = 0,
DSHOT_CMD_BEACON1,
DSHOT_CMD_BEACON2,
DSHOT_CMD_BEACON3,
DSHOT_CMD_BEACON4,
DSHOT_CMD_BEACON5,
DSHOT_CMD_ESC_INFO, // V2 includes settings
DSHOT_CMD_SPIN_DIRECTION_1,
DSHOT_CMD_SPIN_DIRECTION_2,
DSHOT_CMD_3D_MODE_OFF,
DSHOT_CMD_3D_MODE_ON,
DSHOT_CMD_SETTINGS_REQUEST, // Currently not implemented
DSHOT_CMD_SAVE_SETTINGS,
DSHOT_CMD_SPIN_DIRECTION_NORMAL = 20,
DSHOT_CMD_SPIN_DIRECTION_REVERSED = 21,
DSHOT_CMD_LED0_ON, // BLHeli32 only
DSHOT_CMD_LED1_ON, // BLHeli32 only
DSHOT_CMD_LED2_ON, // BLHeli32 only
DSHOT_CMD_LED3_ON, // BLHeli32 only
DSHOT_CMD_LED0_OFF, // BLHeli32 only
DSHOT_CMD_LED1_OFF, // BLHeli32 only
DSHOT_CMD_LED2_OFF, // BLHeli32 only
DSHOT_CMD_LED3_OFF, // BLHeli32 only
DSHOT_CMD_AUDIO_STREAM_MODE_ON_OFF = 30, // KISS audio Stream mode on/Off
DSHOT_CMD_SILENT_MODE_ON_OFF = 31, // KISS silent Mode on/Off
DSHOT_CMD_SIGNAL_LINE_TELEMETRY_DISABLE = 32,
DSHOT_CMD_SIGNAL_LINE_CONTINUOUS_ERPM_TELEMETRY = 33,
DSHOT_CMD_MAX = 47
} dshotCommands_e;
Figure 7. DShot Pulse Width according to mode ( credit)
Figure 8. DShot Sequence Chart ( credit)

DShot Compatible Flight Controllers & ESC’s

Generating DShot on an Arduino UNO

divider = 16 (clock speed in MHz) / 0.45 = 35.6
SPI.beginTransaction (SPISettings (4000000, MSBFIRST, SPI_MODE0));
DShot SPI Test Sketch
Figure 9. A DShot ON bit = 20 SPI bits ON & 7 SPI bits OFF = 0x07FF FF80
Figure 10. DShot ON (First 16 SPI Bits = 0x07FF)
Figure 11. DShot ON (Second 16 SPI Bits = 0xFF80)
Figure 12. DShot T1H
Figure 13. ATMega328P Block Diagram ( credit)
Figure 14. SPI Hardware Block Diagram in the ATMega328P ( credit)
Figure 15. DShot frame produced by library
Figure 16. DShot HIGH bit (T1H = W, T1L = 𝜏 — W).
Figure 17. DShot LOW bit (T0H = W, T0L = 𝜏 — W).

Calculating the DShot Checksum

// compute checksum
int csum = 0;
int csum_data = packet;
for (int i = 0; i < 3; i++) {
csum ^= csum_data; // xor data by nibbles
csum_data >>= 4;
}
csum &= 0xf;
// append checksum
packet = (packet << 4) | csum;
Figure 18. Calculating the DShot (4 bit) Checksum.

The Tester Code

#define DSHOT_PORT PORTB
static void initISR () { 
// TIMER 2 for interrupt frequency 1000 Hz:
cli(); // stop interrupts
TCCR2A = 0; // set entire TCCR2A register to 0
TCCR2B = 0; // same for TCCR2B
TCNT2 = 0; // initialize counter value to 0
// set compare match register for 1000 Hz increments
OCR2A = 249; // = 16000000 / (64 * 1000) - 1 (must be <256)
// turn on CTC mode
TCCR2B |= (1 << WGM21);
// Set CS22, CS21 and CS20 bits for 64 prescaler
TCCR2B |= (1 << CS22) | (0 << CS21) | (0 << CS20);
// enable timer compare interrupt
TIMSK2 |= (1 << OCIE2A);
sei(); // allow interrupts
}
ISR(TIMER2_COMPA_vect){
sendData();
}
#include <DShotTimer2.h>

DShot esc;
// 0x555 or 1365 Decimal (68% throttle)
uint16_t throttle = 0b10101010101;

void setup() {
esc.attach(9);
esc.setThrottle(0);
}

void loop() {
esc.setThrottle(throttle);
delay(10);
}
void DShot::setTimerActive(bool active){
timerActive = active;
}

Reefwing Software · iOS & Android Development · Robotics · #followback #iOS #Android #developer #indiedev #robotics #startup #arduino #raspberrypi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store