DSHOT150 algorithm using digital IO pins on the LPC-1768

Dependents:   DSHOT

Committer:
bwest32
Date:
Sat Apr 27 21:51:38 2019 +0000
Revision:
0:230008e7b5c8
First implementation of DSHOT150 on LPC-1768

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bwest32 0:230008e7b5c8 1 /*
bwest32 0:230008e7b5c8 2 Copyright (c) 2019 Blake West, Kevin Tseng, Tyler Brown, Mason Totri
bwest32 0:230008e7b5c8 3
bwest32 0:230008e7b5c8 4 Permission is hereby granted, free of charge, to any person obtaining a copy
bwest32 0:230008e7b5c8 5 of this software and associated documentation files (the "Software"), to deal
bwest32 0:230008e7b5c8 6 in the Software without restriction, including without limitation the rights
bwest32 0:230008e7b5c8 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
bwest32 0:230008e7b5c8 8 copies of the Software, and to permit persons to whom the Software is
bwest32 0:230008e7b5c8 9 furnished to do so, subject to the following conditions:
bwest32 0:230008e7b5c8 10
bwest32 0:230008e7b5c8 11 The above copyright notice and this permission notice shall be included in
bwest32 0:230008e7b5c8 12 all copies or substantial portions of the Software.
bwest32 0:230008e7b5c8 13
bwest32 0:230008e7b5c8 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bwest32 0:230008e7b5c8 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bwest32 0:230008e7b5c8 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
bwest32 0:230008e7b5c8 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bwest32 0:230008e7b5c8 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bwest32 0:230008e7b5c8 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
bwest32 0:230008e7b5c8 20 THE SOFTWARE.
bwest32 0:230008e7b5c8 21 */
bwest32 0:230008e7b5c8 22 #ifndef MBED_DSHOT15_H
bwest32 0:230008e7b5c8 23 #define MBED_DSHOT15_H
bwest32 0:230008e7b5c8 24 #include "mbed.h"
bwest32 0:230008e7b5c8 25 /** DSHOT150 converts a digital IO pin to act like a DSHOT pulse
bwest32 0:230008e7b5c8 26 * This is done through writing one's and zero's a specific amount
bwest32 0:230008e7b5c8 27 * to acheive the correct pulse width in the given duty cycle.
bwest32 0:230008e7b5c8 28 *
bwest32 0:230008e7b5c8 29 *
bwest32 0:230008e7b5c8 30 * Example
bwest32 0:230008e7b5c8 31 * @code
bwest32 0:230008e7b5c8 32 * #include "mbed.h"
bwest32 0:230008e7b5c8 33 * #include "DSHOT150.h"
bwest32 0:230008e7b5c8 34 *
bwest32 0:230008e7b5c8 35 * DSHOT150 motor( p21 );
bwest32 0:230008e7b5c8 36 *
bwest32 0:230008e7b5c8 37 * int main() {
bwest32 0:230008e7b5c8 38 *
bwest32 0:230008e7b5c8 39 * motor.arm();
bwest32 0:230008e7b5c8 40 * motor.get_tel( true );
bwest32 0:230008e7b5c8 41 * for( float i = 0; i < 1; i+=0.1){
bwest32 0:230008e7b5c8 42 * motor.throttle( i );
bwest32 0:230008e7b5c8 43 * }
bwest32 0:230008e7b5c8 44 *
bwest32 0:230008e7b5c8 45 *
bwest32 0:230008e7b5c8 46 * }
bwest32 0:230008e7b5c8 47 * @endcode
bwest32 0:230008e7b5c8 48 *
bwest32 0:230008e7b5c8 49 * This example will step the motor from 0 to 100% in 10% intervals
bwest32 0:230008e7b5c8 50 *
bwest32 0:230008e7b5c8 51 */
bwest32 0:230008e7b5c8 52 class DSHOT150
bwest32 0:230008e7b5c8 53 {
bwest32 0:230008e7b5c8 54 public:
bwest32 0:230008e7b5c8 55 DSHOT150(PinName pin); //Constructor that takes in
bwest32 0:230008e7b5c8 56 void throttle(float speed); //Throttle value as a percentage [0,1]
bwest32 0:230008e7b5c8 57 void get_tel(bool v); //Telemetry request function. Set to true if wanting to receive telemetry from the ESC
bwest32 0:230008e7b5c8 58 void arm(); //Arming comand for the motor
bwest32 0:230008e7b5c8 59
bwest32 0:230008e7b5c8 60 private:
bwest32 0:230008e7b5c8 61 void send_packet(); //Sends the packet
bwest32 0:230008e7b5c8 62 void check_sum(unsigned int v); //Calculates the check sum, and inserts it into the packet
bwest32 0:230008e7b5c8 63 void write_zero();
bwest32 0:230008e7b5c8 64 void write_one();
bwest32 0:230008e7b5c8 65 bool packet[16]; //Packet of data that is being sent
bwest32 0:230008e7b5c8 66 DigitalOut _pin; //Pin that is being used.
bwest32 0:230008e7b5c8 67 unsigned int tel;
bwest32 0:230008e7b5c8 68 };
bwest32 0:230008e7b5c8 69 #endif