forked from bolderflight/sbus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example to read 10 analog inputs, linearly map them to SBUS…
… servo commands, and then send those commands to the servos. An interrupt is used to control packet timing.
- Loading branch information
1 parent
fb8f06b
commit effbebb
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
SBUS_example.ino | ||
Brian R Taylor | ||
[email protected] | ||
2017-01-13 | ||
Copyright (c) 2016 Bolder Flight Systems | ||
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. | ||
*/ | ||
|
||
// This example reads 10 analog inputs, linearly maps them to SBUS | ||
// servo commands and sends the command to the servos. In this case | ||
// an interrupt is used to control packet timing. | ||
|
||
#include <TimerOne.h> | ||
#include "SBUS.h" | ||
|
||
// a SBUS object, which is on Teensy hardware | ||
// serial port 1 | ||
SBUS x8r(1); | ||
|
||
// analog read values, 16 bit counts | ||
uint16_t ain[10]; | ||
|
||
void setup() { | ||
|
||
// serial to display the channel commands for debugging | ||
Serial.begin(115200); | ||
|
||
// begin the SBUS communication | ||
x8r.begin(); | ||
|
||
// setup the analog read resolution to 16 bits | ||
analogReadResolution(16); | ||
|
||
// setup an interrupt to send packets every 9 ms | ||
Timer1.initialize(9000); | ||
Timer1.attachInterrupt(sendSBUS); | ||
} | ||
|
||
void loop() { | ||
|
||
} | ||
|
||
/* reads analog inputs and sends an SBUS packet */ | ||
void sendSBUS() { | ||
float scaleFactor = 1639.0f / 65535.0f; | ||
float bias = 172.0f; | ||
uint16_t channels[16]; | ||
|
||
// read the analog inputs | ||
for(uint8_t i = 14; i < 24; i++) { | ||
ain[i-14] = analogRead(i); | ||
} | ||
|
||
// linearly map the analog measurements (0-65535) | ||
// to the SBUS commands (172-1811) | ||
for(uint8_t i = 0; i < 10; i++) { | ||
channels[i] = (uint16_t)(((float)ain[i]) * scaleFactor + bias); | ||
Serial.print(channels[i]); // print the channel command (172-1811) | ||
Serial.print("\t"); | ||
} | ||
Serial.println(); | ||
|
||
// write the SBUS packet to an SBUS compatible servo | ||
x8r.write(&channels[0]); | ||
} | ||
|