Skip to content

Commit d25c6c1

Browse files
authored
enhancement 🪄:: Sbus: Added serial connection monitoring and getters for additional data (#36)
* feat: Added check for serial connection loss for sbus and getters for additional sbus data * refactor: moved timeout vars to parent * docs: fixed examples and added examples * docs: add communication mointoring to toturial section
1 parent 546b167 commit d25c6c1

16 files changed

Lines changed: 380 additions & 9 deletions

File tree

‎Docs/source/example.rst‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,48 @@
33
Examples
44
========
55

6-
CRSF Basic Example
6+
IBUS Basic Example
77
------------------
88

9-
.. literalinclude:: ../../examples/espresiff/crsf_basic/crsf_basic.ino
9+
Atmel AVR
10+
^^^^^^^^^
11+
12+
.. literalinclude:: ../../examples/atmel/ibus_basic/ibus_basic.ino
13+
:language: cpp
14+
15+
RP2040
16+
^^^^^^
17+
18+
.. literalinclude:: ../../examples/rp2040/ibus_basic/ibus_basic.ino
19+
:language: cpp
20+
21+
22+
Crossfire Basic Example
23+
------------------
24+
25+
Atmel AVR
26+
^^^^^^^^^
27+
28+
.. literalinclude:: ../../examples/atmel/crsf_basic/crsf_basic.ino
29+
:language: cpp
30+
31+
RP2040
32+
^^^^^^
33+
34+
.. literalinclude:: ../../examples/rp2040/crsf_basic/crsf_basic.ino
1035
:language: cpp
1136

1237
SBUS Basic Example
1338
------------------
1439

15-
.. literalinclude:: ../../examples/espresiff/sbus_basic/sbus_basic.ino
16-
:language: cpp
40+
Atmel AVR
41+
^^^^^^^^^
42+
43+
.. literalinclude:: ../../examples/atmel/sbus_basic/sbus_basic.ino
44+
:language: cpp
45+
46+
RP2040
47+
^^^^^^
48+
49+
.. literalinclude:: ../../examples/rp2040/sbus_basic/sbus_basic.ino
50+
:language: cpp

‎Docs/source/tutorial.rst‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,81 @@ Crossfire
107107
mySerial.begin(CRSF_BAUDRATE);
108108
SerialIO *receiver = new crsf(mySerial);
109109
110+
Monitoring Communication
111+
========================
112+
You can monitor the communication status on certain protocols, to ensure reliability of the received channel data.
113+
114+
SBus
115+
----
116+
117+
Serial Communication
118+
^^^^^^^^^^^^^^^^^^^^
119+
You can monitor the serial communication using the `getSerialConnectionStatus()` method on the `receiver` instance.
120+
This method returns `true` if the serial communication is running without errors, otherwise it returns `false`.
121+
122+
.. code-block:: cpp
123+
124+
#include <SerialIO.h>
125+
126+
rc_channels_t channelData;
127+
sbus receiver(&Serial);
128+
129+
// within your setup routine
130+
receiver.begin();
131+
132+
// within your program loop
133+
receiver.processIncoming();
134+
receiver.getChannel(&channelData);
135+
136+
bool serialConnectionStatus = receiver.getSerialConnectionStatus();
137+
138+
if(!serialConnectionStatus) {
139+
// handle failsafe of channelData
140+
}
141+
142+
143+
Radio connection
144+
^^^^^^^^^^^^^^^^
145+
You can monitor the radio connection of the receiver using the `getFailsafe()` method on the `receiver` instance.
146+
The failsafe indicates when the receiver has lost connection to the transmitter.
147+
When the failsafe is activated, the channel data might be set to predefined values or hold the last known values.
148+
149+
You can also monitor the quality of the connection with `getFramelost()` method on the `receiver` instance.
150+
Usually lost frame indicates when a frame is lost between the transmitter an receiver.
151+
Failsafe activation requires that many frames ahs been lost in a row.
152+
153+
.. note::
154+
Some receivers might not provide any failsafe or frame loss information via SBus protocol.
155+
In that case, the methods will always return `false`.
156+
157+
.. code-block:: cpp
158+
159+
#include <SerialIO.h>
160+
161+
rc_channels_t channelData;
162+
sbus receiver(&Serial);
163+
164+
// within your setup routine
165+
receiver.begin();
166+
167+
// within your program loop
168+
receiver.processIncoming();
169+
receiver.getChannel(&channelData);
170+
171+
bool failsafe = receiver.getFailsafe();
172+
bool frameLost = receiver.getFramelost();
173+
174+
if(failsafe) {
175+
// handle failsafe of channelData
176+
}
177+
178+
if(frameLost) {
179+
// handle frame lost event
180+
}
181+
182+
110183
See Also
111-
^^^^^^^^
184+
========
112185
- :cpp:class:`SerialIO`
113186
- :cpp:class:`sbus`
114187
- :cpp:class:`crsf`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
compile:
2+
platforms:
3+
- uno
4+
- mega2560
5+
- leonardo
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*!
2+
* @file crsf_basic.ino
3+
*/
4+
#include <SerialIO.h>
5+
6+
rc_channels_t channelData;
7+
8+
crsf receiver(&Serial);
9+
10+
void setup() {
11+
receiver.begin();
12+
}
13+
14+
void loop() {
15+
// setup crsf receiver
16+
receiver.processIncoming();
17+
receiver.getChannel(&channelData);
18+
19+
// `channelData` now contains the latest RC channel values
20+
// You can use them by accessing the channelData e.g. channelData.channel1 to channelData.channel16
21+
}
22+

‎examples/atmel/ibus_basic/ibus_basic.ino‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ void setup() {
1414
void loop() {
1515
receiver.processIncoming();
1616
receiver.getChannel(&channelData);
17+
18+
// `channelData` now contains the latest RC channel values
19+
// You can use them by accessing the channelData e.g. channelData.channel1 to channelData.channel16
1720
}
1821

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
compile:
2+
platforms:
3+
- uno
4+
- mega2560
5+
- leonardo
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*!
2+
* @file sbus_basic.ino
3+
*/
4+
#include <SerialIO.h>
5+
6+
rc_channels_t channelData;
7+
8+
sbus receiver(&Serial);
9+
10+
void setup() {
11+
receiver.begin();
12+
}
13+
14+
void loop() {
15+
// setup sbus receiver
16+
receiver.processIncoming();
17+
receiver.getChannel(&channelData);
18+
19+
// `channelData` now contains the latest RC channel values
20+
// You can use them by accessing the channelData e.g. channelData.channel1 to channelData.channel16
21+
}
22+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
platforms:
2+
rpipico:
3+
board: rp2040:rp2040:rpipico
4+
package: rp2040:rp2040
5+
gcc:
6+
features:
7+
defines:
8+
- ARDUINO_ARCH_RP2040
9+
warnings:
10+
flags:
11+
12+
packages:
13+
rp2040:rp2040:
14+
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
15+
16+
compile:
17+
platforms:
18+
- rpipico
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*!
2+
* @file crsf_basic.ino
3+
*/
4+
/*
5+
6+
# Sample platformio.ini file:
7+
# ---------------------------
8+
[platformio]
9+
default_envs = ws-rp2040-zero
10+
11+
[env:ws-rp2040-zero]
12+
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
13+
board = waveshare_rp2040_pizero
14+
framework = arduino
15+
board_build.core = earlephilhower
16+
monitor_speed = 115200
17+
18+
lib_deps =
19+
https://github.com/Witty-Wizard/SerialIO
20+
*/
21+
22+
#include <SerialIO.h>
23+
24+
#define CRSF_TX_PIN 0
25+
#define CRSF_RX_PIN 1
26+
27+
rc_channels_t rcdata;
28+
// On RP2040 or Arduino ESP32 you need to specify the TX and RX pins
29+
crsf receiver(&Serial1, CRSF_RX_PIN, CRSF_TX_PIN,
30+
true); // RP2040 requires the TX_PIN so to not hang up the mcu
31+
32+
void setup() {
33+
// setup crsf receiver
34+
receiver.begin();
35+
36+
Serial.begin(115200);
37+
}
38+
39+
void loop() {
40+
static unsigned long last_millis = millis();
41+
42+
receiver.processIncoming();
43+
receiver.getChannel(&rcdata);
44+
45+
if (millis() > last_millis + 100) {
46+
Serial.printf("RC: %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d %4d",
47+
rcdata.channel1, rcdata.channel2, rcdata.channel3,
48+
rcdata.channel4, rcdata.channel5, rcdata.channel6,
49+
rcdata.channel7, rcdata.channel8, rcdata.channel9,
50+
rcdata.channel10, rcdata.channel11, rcdata.channel12,
51+
rcdata.channel13, rcdata.channel14);
52+
Serial.println();
53+
last_millis = millis();
54+
}
55+
}

‎examples/rp2040/ibus_basic/ibus_basic.ino‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ lib_deps =
2121

2222
#include <SerialIO.h>
2323

24-
#define SBUS_TX_PIN 0
25-
#define SBUS_RX_PIN 1
24+
#define IBUS_TX_PIN 0
25+
#define IBUS_RX_PIN 1
2626

2727
rc_channels_t rcdata;
28-
ibus receiver(&Serial1, SBUS_RX_PIN, SBUS_TX_PIN,
28+
// On RP2040 or Arduino ESP32 you need to specify the TX and RX pins
29+
ibus receiver(&Serial1, IBUS_RX_PIN, IBUS_TX_PIN,
2930
true); // RP2040 requires the TX_PIN so to not hang up the mcu
3031

3132
void setup() {
32-
// setup sbus receiver
33+
// setup ibus receiver
3334
receiver.begin();
3435

3536
Serial.begin(115200);

0 commit comments

Comments
 (0)