You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
 
4
+
5
+
# Sbus
6
+
This library communicates with SBUS receivers and servos and is compatible with Arduino and CMake build systems.
3
7
*[License](LICENSE.md)
4
8
*[Changelog](CHANGELOG.md)
5
9
@@ -16,7 +20,7 @@ The SBUS protocol uses an inverted serial logic with a baud rate of 100000, 8 da
16
20
* Bit 4: failsafe activated (0x10)
17
21
* Byte[24]: SBUS footer
18
22
19
-
Note that lost frame is indicated when a frame is lost between the transmitter and receiver. Failsafe activation typically requires that several frames are lost in a row and indicates that the receiver has moved into failsafe mode. Packets are sent approximately every 10 ms or 20 ms.
23
+
Note that lost frame is indicated when a frame is lost between the transmitter and receiver. Failsafe activation typically requires that many frames are lost in a row and indicates that the receiver has moved into failsafe mode. Packets are sent approximately every 10 ms or 20 ms.
20
24
21
25
**Note on CH17 and CH18:** Channel 17 and channel 18 are digital on/off channels. These are not universally available on all SBUS receivers and servos.
22
26
@@ -33,21 +37,49 @@ SBUS uses an inverted serial protocol, which is not commonly supported in Arduin
33
37
* STM32L476xx
34
38
* STM32L433xx
35
39
* STM32L432xx
40
+
* ESP32
36
41
37
42
For all other microcontrollers, you **must** use a serial inverter.
38
43
39
44
# Installation
45
+
46
+
## Arduino
40
47
Simply clone or download and extract the zipped library into your Arduino/libraries folder. The library is added as:
41
48
42
49
```C++
43
50
#include"sbus.h"
44
51
```
45
52
46
-
**AVR Boards:** you will also need to clone or download the [Embedded Template Library](https://github.com/ETLCPP/etl-arduino) into your Arduino/libraries folder. This provides *etl::array* functionality as an alternative to *std::array*.
53
+
## CMake
54
+
CMake is used to build this library, which is exported as a library target called *sbus*. The header is added as:
55
+
56
+
```C++
57
+
#include"sbus.h"
58
+
```
59
+
60
+
The library can be also be compiled stand-alone using the CMake idiom of creating a *build* directory and then, from within that directory issuing:
47
61
48
-
# Methods
62
+
```
63
+
cmake .. -DMCU=MK66FX1M0
64
+
make
65
+
```
49
66
50
-
## Receiving SBUS
67
+
This will build the library and example executable called *sbus_example*. The example executable source file is located at *examples/cmake/sbus_example.cc*. Notice that the *cmake* command includes a define specifying the microcontroller the code is being compiled for. This is required to correctly configure the code, CPU frequency, and compile/linker options. The available MCUs are:
68
+
* MK64FX512
69
+
* MK66FX1M0
70
+
* MKL26Z64
71
+
* IMXRT1062_T40
72
+
* IMXRT1062_T41
73
+
74
+
These are known to work with the same packages used in Teensy products. Also switching packages is known to work well, as long as it's only a package change.
75
+
76
+
The *sbus_example* target creates an executable for communicating with sbus receivers and servos. This target also has a *_hex* for creating the hex file and an *_upload* for using the [Teensy CLI Uploader](https://www.pjrc.com/teensy/loader_cli.html) to flash the Teensy. Please note that the CMake build tooling is expected to be run under Linux or WSL, instructions for setting up your build environment can be found in our [build-tools repo](https://github.com/bolderflight/build-tools).
77
+
78
+
# Namespace
79
+
This library is within the namespace *bfs*.
80
+
81
+
# SbusRx
82
+
This class is used for receiving SBUS data from an SBUS capable receiver.
51
83
52
84
**SbusRx(HardwareSerial *bus)** Creates an SbusRx object. A pointer to the Serial object corresponding to the serial port used is passed. The RX pin of the serial port will receive SBUS packets.
53
85
@@ -61,6 +93,8 @@ SbusRx sbus(&Serial1);
61
93
sbus.Begin();
62
94
```
63
95
96
+
**(ESP-32 ONLY) void Begin(const int8_t rxpin, const int8_t txpin)** Initialized SBUS communication, given the Serial RX and TX pins.
97
+
64
98
**bool Read()** Parses SBUS packets, returns true on successfully receiving an SBUS packet.
65
99
66
100
```C++
@@ -69,10 +103,25 @@ if (sbus.Read()) {
69
103
}
70
104
```
71
105
72
-
**std::array<uint16_t, 16> rx_channels()** Returns the array of received channel data.
106
+
**static constexpr int8_t NUM_CH()** A constant defining the number of SBUS channels (i.e. 16), useful for defining arrays to read the data into.
107
+
108
+
**(Non-AVR ONLY) std::array<uint16_t, 16> ch()** Returns the array of received channel data.
109
+
110
+
```C++
111
+
std::array<uint16_t, 16> sbus_data = sbus.ch();
112
+
```
113
+
114
+
**int8_t ch(int16_t * data, const int8_t len)** Copys the array of received channel data given a pointer to a destination, *data*, and length of the destination array *len*. Returns the number of channels copied on success or -1 on failure. Note that the maximum number of channels is the smaller of the *len* or *NUM_CH* (i.e. 16).
115
+
116
+
```C++
117
+
int16_t rx_ch[bfs::SbusRx::NUM_CH];
118
+
sbus.ch(rx_ch, bfs::SbusRx::NUM_CH);
119
+
```
120
+
121
+
**int16_t ch(const int8_t idx)** Returns received channel data given the channel index.
This class is used for transmitting SBUS data to SBUS capable servos.
103
153
104
154
**SbusTx(HardwareSerial *bus)** Creates an SbusTx object. A pointer to the Serial object corresponding to the serial port used is passed. The TX pin of the serial port will transmit SBUS packets.
105
155
@@ -113,12 +163,16 @@ SbusTx sbus(&Serial1);
113
163
sbus.Begin();
114
164
```
115
165
166
+
**(ESP-32 ONLY) void Begin(const int8_t rxpin, const int8_t txpin)** Initialized SBUS communication, given the Serial RX and TX pins.
167
+
116
168
**void Write()** Writes an SBUS packet. The packet is written immediately, you should regulate timing of sending packets to servos to maintain a frequency of approximately 100 Hz or 50 Hz, depending on the setup of the SBUS system.
117
169
118
170
```C++
119
171
sbus.Write();
120
172
```
121
173
174
+
**static constexpr int8_t NUM_CH()** A constant defining the number of SBUS channels (i.e. 16), useful for defining arrays to write the data from.
175
+
122
176
**void ch17(bool val)** Sets the value of channel 17 to be transmitted.
123
177
124
178
```C++
@@ -143,10 +197,24 @@ sbus.lost_frame(true);
143
197
sbus.failsafe(true);
144
198
```
145
199
146
-
**void tx_channels(const std::array<uint16_t, 16> &val)** Sets the channel data to be transmitted.
200
+
**(Non-AVR ONLY) void ch(const std::array<uint16_t, 16> &val)** Sets the channel data to be transmitted.
147
201
148
202
```C++
149
-
sbus.tx_channels(sbus_tx_data);
203
+
sbus.ch(sbus_tx_data);
204
+
```
205
+
206
+
**bool ch(const int8_t idx, const int16_t val)** Sets the channel data to be transmitted, given a channel index and corresponding value. Returns true on success and false on failure.
207
+
208
+
```C++
209
+
/* Set channel 3 to a value of 1200 */
210
+
sbus.ch(3, 1200);
211
+
```
212
+
213
+
**int8_t ch(int16_t const * const data, const int8_t len)** Sets the channel data to be transmitted given a pointer to an array of commands, *data*, and the array length, *len*. Returns the number of channels copied on success or -1 on failure. Note that the maximum number of channels is the smaller of the *len* or *NUM_CH* (i.e. 16).
214
+
215
+
```C++
216
+
int16_t cmd[bfs::SbusTx::NUM_CH()];
217
+
sbus.ch(cmd, bfs::SbusTx::NUM_CH());
150
218
```
151
219
152
220
**bool ch17()** Returns the value of channel 17 to be transmitted.
0 commit comments