Skip to content

Commit

Permalink
Changed channels and lostFrames to uint16_t data types to more accura…
Browse files Browse the repository at this point in the history
…tely match the data type being used. Modified the scale and bias for the readCal() function to be single precision floats instead of doubles. Changed the example and README to match the code changes.
  • Loading branch information
flybrianfly committed Sep 21, 2016
1 parent 91d6d74 commit 56325b3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,39 @@ This should be called in your setup function. It initializes the serial communic
x8r.begin();
```

**bool read(int16_t* channels, uint8_t* failsafe, int* lostFrames)**
*read(int16_t* channels, uint8_t* failsafe, int* lostFrames)* reads data from the SBUS receiver and parses the SBUS packet. When a complete packet is received, *read(int16_t* channels, uint8_t* failsafe, int* lostFrames)* returns *true* and the *channels[0-15]*, *failsafe*, and *lost frames* data is available. Note that *lost frames* is a counter that increments once each time a lost frame flag is read in the SBUS packet. For example, placing the following code in the loop function will print the value of *channel 0* every time a valid SBUS packet is received.
**bool read(uint16_t* channels, uint8_t* failsafe, uint16_t* lostFrames)**
*read(uint16_t* channels, uint8_t* failsafe, uint16_t* lostFrames)* reads data from the SBUS receiver and parses the SBUS packet. When a complete packet is received, *read(uint16_t* channels, uint8_t* failsafe, uint16_t* lostFrames)* returns *true* and the *channels[0-15]*, *failsafe*, and *lost frames* data is available. Note that *lost frames* is a counter that increments once each time a lost frame flag is read in the SBUS packet. For example, placing the following code in the loop function will print the value of *channel 0* every time a valid SBUS packet is received.

```C++
int16_t channels[16];
uint16_t channels[16];
uint8_t failSafe;
int lostFrames = 0;
uint16_t lostFrames = 0;

if(x8r.read(&channels[0], &failSafe, &lostFrames)){
Serial.println(channels[0]);
}
```

**bool readCal(float* calChannels, uint8_t* failsafe, int* lostFrames)**
*readCal(float* calChannels, uint8_t* failsafe, int* lostFrames)* reads data from the SBUS receiver and parses the SBUS packet. The data from *channels[0-15]* is calibrated to a +/- 1.0 float value assuming a linear relationship based on the minimum and maximum value (172 and 1811 using FrSky set to 0-100%). When a complete packet is received, *readCal(float* calChannels, uint8_t* failsafe, int* lostFrames)* returns *true* and the *calChannels[0-15]*, *failsafe*, and *lost frames* data is available. Note that *lost frames* is a counter that increments once each time a lost frame flag is read in the SBUS packet. For example, placing the following code in the loop function will print the calibrated value of *channel 0* every time a valid SBUS packet is received.
**bool readCal(float* calChannels, uint8_t* failsafe, uint16_t* lostFrames)**
*readCal(float* calChannels, uint8_t* failsafe, uint16_t* lostFrames)* reads data from the SBUS receiver and parses the SBUS packet. The data from *channels[0-15]* is calibrated to a +/- 1.0 float value assuming a linear relationship based on the minimum and maximum value (172 and 1811 using FrSky set to 0-100%). When a complete packet is received, *readCal(float* calChannels, uint8_t* failsafe, uint16_t* lostFrames)* returns *true* and the *calChannels[0-15]*, *failsafe*, and *lost frames* data is available. Note that *lost frames* is a counter that increments once each time a lost frame flag is read in the SBUS packet. For example, placing the following code in the loop function will print the calibrated value of *channel 0* every time a valid SBUS packet is received.

```C++
float channels[16];
uint8_t failSafe;
int lostFrames = 0;
uint16_t lostFrames = 0;

if(x8r.readCal(&channels[0], &failSafe, &lostFrames)){
Serial.println(channels[0]);
}
```

**void write(int16_t* channels)**
*write(int16_t* channels)* writes the SBUS packet to SBUS capable servos given position commands from *channels[0-15]*. Note that this function simply creates and sends the SBUS packet, but does not handle timing (i.e. the time between sending subsequent SBUS packets). This timing must be handled by the calling function. For example, placing the following code in the loop function will create and send the SBUS packet to servos every time a valid SBUS packet is received.
**void write(uint16_t* channels)**
*write(uint16_t* channels)* writes the SBUS packet to SBUS capable servos given position commands from *channels[0-15]*. Note that this function simply creates and sends the SBUS packet, but does not handle timing (i.e. the time between sending subsequent SBUS packets). This timing must be handled by the calling function. For example, placing the following code in the loop function will create and send the SBUS packet to servos every time a valid SBUS packet is received.

```C++
int16_t channels[16];
uint16_t channels[16];
uint8_t failSafe;
int lostFrames = 0;
uint16_t lostFrames = 0;

// look for a good SBUS packet from the receiver
if(x8r.read(&channels[0], &failSafe, &lostFrames)){
Expand Down
10 changes: 5 additions & 5 deletions SBUS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SBUS.cpp
Brian R Taylor
[email protected]
2016-09-19
2016-09-21
Copyright (c) 2016 Bolder Flight Systems
Expand Down Expand Up @@ -72,8 +72,8 @@ void SBUS::begin(){
}

/* read the SBUS data and calibrate it to +/- 1 */
bool SBUS::readCal(float* calChannels, uint8_t* failsafe, int* lostFrames){
int16_t channels[16];
bool SBUS::readCal(float* calChannels, uint8_t* failsafe, uint16_t* lostFrames){
uint16_t channels[16];

// read the SBUS data
if(read(&channels[0],failsafe,lostFrames)){
Expand All @@ -94,7 +94,7 @@ bool SBUS::readCal(float* calChannels, uint8_t* failsafe, int* lostFrames){
}

/* read the SBUS data */
bool SBUS::read(int16_t* channels, uint8_t* failsafe, int* lostFrames){
bool SBUS::read(uint16_t* channels, uint8_t* failsafe, uint16_t* lostFrames){

// parse the SBUS packet
if(parse()){
Expand Down Expand Up @@ -182,7 +182,7 @@ bool SBUS::parse(){
}

/* write SBUS packets */
void SBUS::write(int16_t* channels){
void SBUS::write(uint16_t* channels){
uint8_t packet[25];

/* assemble the SBUS packet */
Expand Down
12 changes: 6 additions & 6 deletions SBUS.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SBUS.h
Brian R Taylor
[email protected]
2016-09-02
2016-09-21
Copyright (c) 2016 Bolder Flight Systems
Expand Down Expand Up @@ -31,14 +31,14 @@ class SBUS{
public:
SBUS(int bus);
void begin();
bool read(int16_t* channels, uint8_t* failsafe, int* lostFrames);
bool readCal(float* calChannels, uint8_t* failsafe, int* lostFrames);
void write(int16_t* channels);
bool read(uint16_t* channels, uint8_t* failsafe, uint16_t* lostFrames);
bool readCal(float* calChannels, uint8_t* failsafe, uint16_t* lostFrames);
void write(uint16_t* channels);
private:
int _bus;
int _fpos;
const double _sbusScale = 0.0012202562538133;
const double _sbusBias = -1.20988407565589;
const float _sbusScale = 0.00122025625f;
const float _sbusBias = -1.2098840f;
static const uint8_t _sbusHeader = 0x0F;
static const uint8_t _sbusFooter = 0x00;
static const uint8_t _sbusLostFrame = 0x20;
Expand Down
6 changes: 3 additions & 3 deletions examples/SBUS_example/SBUS_example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SBUS_example.ino
Brian R Taylor
[email protected]
2016-07-12
2016-09-21
Copyright (c) 2016 Bolder Flight Systems
Expand Down Expand Up @@ -40,9 +40,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO
SBUS x8r(1);

// channel, fail safe, and lost frames data
int16_t channels[16];
uint16_t channels[16];
uint8_t failSafe;
int lostFrames = 0;
uint16_t lostFrames = 0;

void setup() {
// begin the SBUS communication
Expand Down

0 comments on commit 56325b3

Please sign in to comment.