A simple I2C library for ESCs using the Blue Robotics version of SimonK's TGY firmware (https://github.com/bluerobotics/tgy). This library supports the BlueESC, but also the Afro Nfet 30A. I2C is an alternative interface for micro-controllers, rather than using PWM (as a servo) -- this provides some additional features like temperature level, RPM, etc.

Dependents:   SimonK_I2C_ESC_Example

Example program here:

Import programSimonK_I2C_ESC_Example

Simple example for the SimonK_I2C_ESC. Tested on the FRDM K64F.

Additional guidance is provided in the Wiki

Committer:
azazeal88
Date:
Sat Apr 02 11:44:14 2016 +0000
Revision:
4:014606ea57a3
Parent:
3:d74e7df56379
Fixed RPM overflow issue because of timer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
azazeal88 3:d74e7df56379 1 //! SimonK I2C ESC Control Library
azazeal88 3:d74e7df56379 2 /*!
azazeal88 0:41ddae212f64 3 Title: Mbed I2C ESC Library
azazeal88 0:41ddae212f64 4 Description: This library provide methods to control I2C capable ESCs
azazeal88 0:41ddae212f64 5 using the Blue Robotics fork of the tgy firmware. It's designed for the
azazeal88 0:41ddae212f64 6 "BlueESC" but will work with an tgy compatible ESC (this code was tested on the Afro nfet 30A).
azazeal88 0:41ddae212f64 7 The code is designed for the Mbed boards.
azazeal88 0:41ddae212f64 8
azazeal88 0:41ddae212f64 9 Inspired by the Arduino I2C library provided by the blue robotics team (https://github.com/bluerobotics/Arduino_I2C_ESC).
azazeal88 0:41ddae212f64 10 -------------------------------
azazeal88 0:41ddae212f64 11 The MIT License (MIT)
azazeal88 0:41ddae212f64 12 Copyright (c) 2016 Daniel Knox.
azazeal88 0:41ddae212f64 13 Permission is hereby granted, free of charge, to any person obtaining a copy
azazeal88 0:41ddae212f64 14 of this software and associated documentation files (the "Software"), to deal
azazeal88 0:41ddae212f64 15 in the Software without restriction, including without limitation the rights
azazeal88 0:41ddae212f64 16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
azazeal88 0:41ddae212f64 17 copies of the Software, and to permit persons to whom the Software is
azazeal88 0:41ddae212f64 18 furnished to do so, subject to the following conditions:
azazeal88 0:41ddae212f64 19 The above copyright notice and this permission notice shall be included in
azazeal88 0:41ddae212f64 20 all copies or substantial portions of the Software.
azazeal88 0:41ddae212f64 21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
azazeal88 0:41ddae212f64 22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
azazeal88 0:41ddae212f64 23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
azazeal88 0:41ddae212f64 24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
azazeal88 0:41ddae212f64 25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
azazeal88 0:41ddae212f64 26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
azazeal88 0:41ddae212f64 27 THE SOFTWARE.
azazeal88 0:41ddae212f64 28 -------------------------------*/
azazeal88 0:41ddae212f64 29
azazeal88 0:41ddae212f64 30 #ifndef SimonK_I2C_ESC_H
azazeal88 0:41ddae212f64 31 #define SimonK_I2C_ESC_H
azazeal88 0:41ddae212f64 32
azazeal88 0:41ddae212f64 33 #include "SimonK_I2C_ESC.h"
azazeal88 0:41ddae212f64 34
azazeal88 0:41ddae212f64 35 #define MAX_ESCS 16
azazeal88 0:41ddae212f64 36
azazeal88 0:41ddae212f64 37 // THERMISTOR SPECIFICATIONS
azazeal88 0:41ddae212f64 38 // resistance at 25 degrees C
azazeal88 0:41ddae212f64 39 #define THERMISTORNOMINAL 10000
azazeal88 0:41ddae212f64 40 // temp. for nominal resistance (almost always 25 C)
azazeal88 0:41ddae212f64 41 #define TEMPERATURENOMINAL 25
azazeal88 0:41ddae212f64 42 // The beta coefficient of the thermistor (usually 3000-4000)
azazeal88 0:41ddae212f64 43 #define BCOEFFICIENT 3900
azazeal88 0:41ddae212f64 44 // the value of the 'other' resistor
azazeal88 0:41ddae212f64 45 #define SERIESRESISTOR 3300
azazeal88 0:41ddae212f64 46
azazeal88 0:41ddae212f64 47 #include "mbed.h"
azazeal88 0:41ddae212f64 48
azazeal88 0:41ddae212f64 49 class SimonK_I2C_ESC {
azazeal88 0:41ddae212f64 50 public:
azazeal88 2:8adc426cbe31 51 /** Constructor for ESC instance. Requires input of I2C address (ESC 0,1...16 is address
azazeal88 0:41ddae212f64 52 * 0x29,0x2A...0x39). Optionally accepts pole count for RPM measurements. T100 is 6 and
azazeal88 0:41ddae212f64 53 * T200 is 7. */
azazeal88 0:41ddae212f64 54 SimonK_I2C_ESC(I2C &_i2c, char address, char poleCount = 6);
azazeal88 0:41ddae212f64 55
azazeal88 2:8adc426cbe31 56 /** Sends updated throttle setting for the motor. Throttle input range is 16 bit
azazeal88 0:41ddae212f64 57 * (-32767 to +32767). */
azazeal88 0:41ddae212f64 58 void set(short throttle);
azazeal88 0:41ddae212f64 59
azazeal88 2:8adc426cbe31 60 /** Alternate function to set throttle using standard servo pulse range (1100-1900) */
azazeal88 0:41ddae212f64 61 void setPWM(short pwm);
azazeal88 0:41ddae212f64 62
azazeal88 2:8adc426cbe31 63 /** The update function reads new data from the ESC. */
azazeal88 0:41ddae212f64 64 void update();
azazeal88 0:41ddae212f64 65
azazeal88 2:8adc426cbe31 66 /** Returns true if the ESC is connected */
azazeal88 0:41ddae212f64 67 bool isAlive();
azazeal88 0:41ddae212f64 68
azazeal88 2:8adc426cbe31 69 /** Returns voltage measured by the ESC in volts */
azazeal88 0:41ddae212f64 70 float voltage();
azazeal88 0:41ddae212f64 71
azazeal88 2:8adc426cbe31 72 /** Returns current measured by the ESC in amps */
azazeal88 0:41ddae212f64 73 float current();
azazeal88 0:41ddae212f64 74
azazeal88 2:8adc426cbe31 75 /** Returns temperature measured by the ESC in degree Celsius */
azazeal88 0:41ddae212f64 76 float temperature();
azazeal88 0:41ddae212f64 77
azazeal88 2:8adc426cbe31 78 /** Returns RPM of the motor. Note that this measurement will be
azazeal88 0:41ddae212f64 79 * more accurate if the I2C data is read slowly. */
azazeal88 0:41ddae212f64 80 short rpm();
azazeal88 0:41ddae212f64 81
azazeal88 0:41ddae212f64 82 private:
azazeal88 0:41ddae212f64 83 I2C _i2c;
azazeal88 0:41ddae212f64 84 char _address;
azazeal88 0:41ddae212f64 85 unsigned short _voltage_raw, _current_raw, _temp_raw;
azazeal88 0:41ddae212f64 86 short _rpm;
azazeal88 4:014606ea57a3 87 int _rpmTimer;
azazeal88 0:41ddae212f64 88 Timer mbed_rpm_timer;
azazeal88 0:41ddae212f64 89 char _identifier;
azazeal88 0:41ddae212f64 90 char _poleCount;
azazeal88 0:41ddae212f64 91
azazeal88 0:41ddae212f64 92 void readBuffer(char buffer[]);
azazeal88 0:41ddae212f64 93
azazeal88 0:41ddae212f64 94 void readSensors(char address, unsigned short *rpm, unsigned short *vbat, unsigned short *temp, unsigned short *curr);
azazeal88 0:41ddae212f64 95 };
azazeal88 0:41ddae212f64 96
azazeal88 0:41ddae212f64 97 #endif