DSHOT150 algorithm using digital IO pins on the LPC-1768

Dependents:   DSHOT

Files at this revision

API Documentation at this revision

Comitter:
bwest32
Date:
Sat Apr 27 21:51:38 2019 +0000
Commit message:
First implementation of DSHOT150 on LPC-1768

Changed in this revision

DSHOT150.cpp Show annotated file Show diff for this revision Revisions of this file
DSHOT150.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 230008e7b5c8 DSHOT150.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DSHOT150.cpp	Sat Apr 27 21:51:38 2019 +0000
@@ -0,0 +1,87 @@
+#include "DSHOT150.h"
+#include "mbed.h"
+DSHOT150::DSHOT150(PinName pin) : _pin(pin)
+{
+    _pin = 0;
+    tel = 0;
+}
+void DSHOT150::write_zero()
+{
+    int i = 0;
+    while (i<19) {
+        _pin.write(1);
+        i++;
+    }
+    while (i<51) {
+        _pin.write(0);
+        i++;
+    }
+}
+void DSHOT150::write_one()
+{
+    int i = 0;
+    while (i<40) {
+        _pin.write(1);
+        i++;
+    }
+    while (i<51) {
+        _pin.write(0);
+        i++;
+    }
+}
+void DSHOT150::check_sum(unsigned int v)
+{
+    v = v<<1;
+    v = v|tel;
+    uint8_t cs;
+    for( int i = 0; i < 3; ++i){
+        cs ^= v;
+        v>>=4;
+    }
+    cs&=0xF;
+    packet[15] = cs&0x1;
+    packet[14] = cs&0x2;
+    packet[13] = cs&0x4;
+    packet[12] = cs&0x8;
+}
+void DSHOT150::get_tel(bool v)
+{
+    tel = v == true? 1 : 0;
+}
+void DSHOT150::send_packet()
+{
+    for(int j = 0; j < 1000; ++j) {
+        for(int i = 0; i < 16; ++i) {
+            if(packet[i])
+                write_one();
+            else
+                write_zero();
+        }
+        wait_us(500);
+    }
+}
+void DSHOT150::arm(){
+    throttle(0.25);
+    throttle(0);
+}
+void DSHOT150::throttle(float speed)
+{
+    unsigned int val;
+    speed = speed > 1 ? 1 : speed; //Bound checking and restricitng of the input
+    speed = speed < 0 ? 0 : speed; //Anything below 0 is converted to zero
+                                   //Anything above 1 is converted to one 
+                                   
+    val = (unsigned int)(speed * 2000); //Scale the throttle value. 0 - 48 are reserved for the motor
+    val +=48;                           //Throttle of zero starts at 48
+    
+    check_sum(val); //Calculate the checksum and insert it into the packet
+    
+    for(int i = 10; i >= 0; --i) {  //Insert the throttle bits into the packet
+        packet[i] = val&0x1;
+        val = val>>1;
+    }
+    
+    packet[11] = tel;   //Set the telemetry bit in the packet
+    
+    send_packet();
+}
\ No newline at end of file
diff -r 000000000000 -r 230008e7b5c8 DSHOT150.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DSHOT150.h	Sat Apr 27 21:51:38 2019 +0000
@@ -0,0 +1,69 @@
+/*
+    Copyright (c) 2019 Blake West, Kevin Tseng, Tyler Brown, Mason Totri
+ 
+    Permission is hereby granted, free of charge, to any person obtaining a copy
+    of this software and associated documentation files (the "Software"), to deal
+    in the Software without restriction, including without limitation the rights
+    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+    copies of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+ 
+    The above copyright notice and this permission notice shall be included in
+    all copies or substantial portions of the Software.
+ 
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+    THE SOFTWARE.
+*/
+#ifndef MBED_DSHOT15_H
+#define MBED_DSHOT15_H
+#include "mbed.h"
+/** DSHOT150 converts a digital IO pin to act like a DSHOT pulse
+ * This is done through writing one's and zero's a specific amount 
+ * to acheive the correct pulse width in the given duty cycle.
+ *
+ *
+ * Example
+ * @code
+ * #include "mbed.h"
+ * #include "DSHOT150.h"
+ *
+ * DSHOT150 motor( p21 );
+ *
+ * int main() {
+ *
+ *      motor.arm();
+ *      motor.get_tel( true );
+ *      for( float i = 0; i < 1; i+=0.1){
+ *          motor.throttle( i );
+ *      }
+ *
+ *
+ *  }
+ *  @endcode
+ *
+ *  This example will step the motor from 0 to 100% in 10% intervals
+ *
+ */
+class DSHOT150
+{
+public:
+    DSHOT150(PinName pin); //Constructor that takes in 
+    void throttle(float speed); //Throttle value as a percentage [0,1]
+    void get_tel(bool v); //Telemetry request function. Set to true if wanting to receive telemetry from the ESC
+    void arm(); //Arming comand for the motor
+    
+private:
+    void send_packet(); //Sends the packet
+    void check_sum(unsigned int v); //Calculates the check sum, and inserts it into the packet
+    void write_zero(); 
+    void write_one();
+    bool packet[16]; //Packet of data that is being sent
+    DigitalOut _pin; //Pin that is being used.
+    unsigned int tel;
+};
+#endif
\ No newline at end of file