Use FlixPeriph library for SBUS

This commit is contained in:
Oleg Kalachev 2024-03-17 02:29:37 +03:00
parent f782f647cb
commit 646fa46f6b
6 changed files with 19 additions and 16 deletions

View File

@ -15,7 +15,6 @@ dependencies .dependencies:
arduino-cli core update-index --config-file arduino-cli.yaml arduino-cli core update-index --config-file arduino-cli.yaml
arduino-cli core install esp32:esp32@2.0.11 --config-file arduino-cli.yaml arduino-cli core install esp32:esp32@2.0.11 --config-file arduino-cli.yaml
arduino-cli lib update-index arduino-cli lib update-index
arduino-cli lib install "Bolder Flight Systems SBUS"@1.0.1
arduino-cli lib install "FlixPeriph" arduino-cli lib install "FlixPeriph"
arduino-cli lib install "MAVLink"@2.0.1 arduino-cli lib install "MAVLink"@2.0.1
touch .dependencies touch .dependencies

View File

@ -81,9 +81,8 @@ Use USB remote control or QGroundControl mobile app (with *Virtual Joystick* set
1. Install [Arduino IDE](https://www.arduino.cc/en/software) (version 2 is recommended). 1. Install [Arduino IDE](https://www.arduino.cc/en/software) (version 2 is recommended).
2. Install ESP32 core using [Boards Manager](https://docs.arduino.cc/learn/starting-guide/cores). 2. Install ESP32 core using [Boards Manager](https://docs.arduino.cc/learn/starting-guide/cores).
3. Install the following libraries using [Library Manager](https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-installing-a-library) (**versions are significant**): 3. Install the following libraries using [Library Manager](https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-installing-a-library):
* `Bolder Flight Systems SBUS`, version 1.0.1. * `FlixPeriph`.
* `FlixPeriph`, version 1.0.0.
* `MAVLink`, version 2.0.1. * `MAVLink`, version 2.0.1.
4. Clone the project using git or [download the source code as a ZIP archive](https://codeload.github.com/okalachev/flix/zip/refs/heads/master). 4. Clone the project using git or [download the source code as a ZIP archive](https://codeload.github.com/okalachev/flix/zip/refs/heads/master).
5. Open the downloaded Arduino sketch `flix/flix.ino` in Arduino IDE. 5. Open the downloaded Arduino sketch `flix/flix.ino` in Arduino IDE.

View File

@ -26,7 +26,7 @@
float t = NAN; // current step time, s float t = NAN; // current step time, s
float dt; // time delta from previous step, s float dt; // time delta from previous step, s
float loopFreq; // loop frequency, Hz float loopFreq; // loop frequency, Hz
uint16_t channels[16]; // raw rc channels int16_t channels[16]; // raw rc channels
float controls[RC_CHANNELS]; // normalized controls in range [-1..1] ([0..1] for throttle) float controls[RC_CHANNELS]; // normalized controls in range [-1..1] ([0..1] for throttle)
Vector rates; // angular rates, rad/s Vector rates; // angular rates, rad/s
Vector acc; // accelerometer data, m/s/s Vector acc; // accelerometer data, m/s/s

View File

@ -5,7 +5,7 @@
#include <SBUS.h> #include <SBUS.h>
// NOTE: use `cr` command and replace with the actual values // NOTE: use 'cr' command to calibrate the RC and put the values here
int channelNeutral[] = {995, 883, 200, 972, 512, 512}; int channelNeutral[] = {995, 883, 200, 972, 512, 512};
int channelMax[] = {1651, 1540, 1713, 1630, 1472, 1472}; int channelMax[] = {1651, 1540, 1713, 1630, 1472, 1472};
@ -14,14 +14,12 @@ SBUS RC(Serial2);
void setupRC() { void setupRC() {
Serial.println("Setup RC"); Serial.println("Setup RC");
RC.begin(); RC.begin();
Serial2.setRxInvert(true); // SBUS uses inverted signal
} }
void readRC() { void readRC() {
bool failsafe, lostFrame; if (RC.read()) {
if (RC.read(channels, &failsafe, &lostFrame)) { SBUSData data = RC.data();
if (failsafe) { return; } // TODO: memcpy(channels, data.ch, sizeof(channels)); // copy channels data
if (lostFrame) { return; }
normalizeRC(); normalizeRC();
} }
} }

View File

@ -5,13 +5,20 @@
#include "joystick.h" #include "joystick.h"
struct SBUSData {
int16_t ch[16];
};
class SBUS { class SBUS {
public: public:
SBUS(HardwareSerial& bus) {}; SBUS(HardwareSerial& bus) {};
void begin() {}; void begin() {};
bool read(int16_t *channels, bool *failsafe, bool *lostFrame) { // NOTE: on the hardware channels is uint16_t bool read() { return joystickGet(); };
*failsafe = false; SBUSData data() {
*lostFrame = false; SBUSData data;
return joystickGet(); for (uint8_t i = 0; i < 16; i++) {
data.ch[i] = channels[i];
}
return data;
}; };
}; };

View File

@ -23,7 +23,7 @@ float t = NAN;
float dt; float dt;
float loopFreq; float loopFreq;
float motors[4]; float motors[4];
int16_t channels[16]; // raw rc channels WARNING: unsigned on hardware int16_t channels[16]; // raw rc channels
float controls[RC_CHANNELS]; float controls[RC_CHANNELS];
Vector acc; Vector acc;
Vector rates; Vector rates;