Skip to content

Commit

Permalink
Merge branch 'interface' into 'main'
Browse files Browse the repository at this point in the history
Update away from Inceptor and Effector interfaces.

See merge request bolderflight/software/sbus!19
  • Loading branch information
flybrianfly committed Nov 18, 2021
2 parents 79a1bbe + 4f955e5 commit fbdfad3
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 339 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v5.0.0
-

## v4.0.5
- Updated to effector v6.1.3, which adds a check to see whether a channel was configured.

Expand Down
26 changes: 1 addition & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if (DEFINED MCU)
endif()
# Project information
project(Sbus
VERSION 4.0.5
VERSION 5.0.0
DESCRIPTION "SBUS encoder and decoder"
LANGUAGES C CXX
)
Expand All @@ -21,27 +21,6 @@ if (DEFINED MCU)
GIT_TAG v2.0.5
)
FetchContent_MakeAvailable(core)
# Fetch inceptor
FetchContent_Declare(
inceptor
GIT_REPOSITORY https://github.com/bolderflight/inceptor.git
GIT_TAG v2.2.0
)
FetchContent_MakeAvailable(inceptor)
# Fetch effector
FetchContent_Declare(
effector
GIT_REPOSITORY https://github.com/bolderflight/effector.git
GIT_TAG v6.1.3
)
FetchContent_MakeAvailable(effector)
# Fetch polytools
FetchContent_Declare(
polytools
GIT_REPOSITORY https://github.com/bolderflight/polytools.git
GIT_TAG v3.0.2
)
FetchContent_MakeAvailable(polytools)
# Add the library target
add_library(sbus
src/sbus/sbus.cc
Expand All @@ -51,9 +30,6 @@ if (DEFINED MCU)
target_link_libraries(sbus
PUBLIC
core
inceptor
effector
polytools
)
# Setup include directories
target_include_directories(sbus PUBLIC
Expand Down
171 changes: 108 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,93 +53,138 @@ The *sbus_example* target creates an executable for communicating with sbus rece
# Namespace
This library is within the namespace *bfs*

# SbusRx
This driver conforms to the [Inceptor interface](https://github.com/bolderflight/inceptor); please refer to those documents for information on the *InceptorConfig* and *InceptorData* structs.

**bool Init(const InceptorConfig &ref)** Initializes communication with the SBUS receiver. Returns true on successfully initializing communication with the receiver. Note that often receivers will not transmit data until they've connected with an SBUS transmitter, so it might be necessary to turn on the transmitter before this method is called.

```C++
/* SBUS object, reading SBUS */
bfs::SbusRx sbus_rx;
/* RX Config */
bfs::InceptorConfig rx_config = {
.hw = &Serial2,
.throttle = {
.ch = 0,
.num_coef = 2,
.poly_coef = {0.0012203, -1.2098841}
}
};
if (!sbus_rx.Init(rx_config)) {
Serial.println("Unable to establish communication with SBUS receiver");
while (1) {}
}
# Receiving SBUS

**SbusRx** Creates an SbusRx object.

```C++
SbusRx sbus;
```

**bool Read(InceptorData * const data)** Reads data from the SBUS receiver adn passes the data to the *InceptorData* struct. Returns true on successfully receiving new data.
**void Init(HardwareSerial *uart)** Initializes SBUS communication. 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.

```C++
/* Sbus RX data */
bfs::InceptorData data;
if (sbus_rx.Read(&data)) {
sbus.Init(&Serial2);
```

**bool Read()** Parses SBUS packets, returns true on successfully receiving an SBUS packet.

```C++
if (sbus.Read()) {
// Do something with the received data
}
```

# SbusTx
This driver conforms to the [Effector interface](https://github.com/bolderflight/effector); please refer to those documents for information on the *EffectorConfig* and struct.

**bool Init(const EffectorConfig &ref)** Initializes communication on the SBUS. Returns true on success.

```C++
/* SBUS object, writing SBUS */
bfs::SbusTx<16> sbus_tx;
/* TX Config */
bfs::EffectorConfig<16> tx_config = {
.hw = &Serial2,
.effectors = {
{
.type = bfs::SERVO,
.ch = 1,
.min = -20,
.max = 20,
.failsafe = 0,
.num_coef = 2,
.poly_coef = {819.50, 991.50}
}
}
};
if (!sbus_tx.Init(tx_config)) {
Serial.println("Unable to init SBUS transmitter");
while (1) {}
}
**int8_t NUM_CH** Returns the number of SBUS channels (i.e. 16).

**std::array<int16_t, NUM_CH> ch()** Returns the array of received channel data.

```C++
std::array<int16_t, bfs::SbusRx::NUM_CH> sbus_data = sbus.ch();
```

**bool ch17()** Returns the value of channel 17.

```C++
bool ch17 = sbus.ch17();
```

**bool ch18()** Returns the value of channel 18.

```C++
bool ch18 = sbus.ch18();
```

**bool lost_frame()** Returns true if a frame has been lost.

```C++
bool lost_frame = sbus.lost_frame();
```

**bool failsafe()** Returns true if the receiver has entered failsafe mode.

```C++
bool failsafe = sbus.failsafe();
```

**void Cmd(std::span<float> cmds)** Issues angle commands, which are converted to SBUS commands and stored.
# Writing SBUS

**SbusTx** 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.

```C++
cmds[0] = data.throttle;
sbus_tx.Cmd(cmds);
SbusTx sbus;
```

**void Write()** Sends the stored SBUS commands to the servos. This method should be called every 10ms to 20ms.
**void Init(HardwareSerial &ast;uart)** Initializes SBUS communication. 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.

```C++
sbus_tx.Write();
sbus.Init(&Serial1);
```

**void EnableMotors()** Enables motors to output commands.
**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.

```C++
sbus_tx.EnableMotors();
sbus.Write();
```

**void DisableMotors()** Disables motors from outputting commands, the failsafe command is sent instead.
**void ch17(bool val)** Sets the value of channel 17 to be transmitted.

**void EnableServos()** Enables servos to output commands.
```C++
sbus.ch17(true);
```

**void ch18(bool val)** Sets the value of channel 18 to be transmitted.

```C++
sbus_tx.EnableServos();
sbus.ch18(true);
```

**void DisableServos()** Disables servos from outputting commands, the failsafe command is sent instead.
**void lost_frame(bool val)** Sets whether to transmit the lost frame flag.

```C++
sbus.lost_frame(true);
```

**void failsafe(bool val)** Sets whether to transmit the failsafe flag.

```C++
sbus.failsafe(true);
```

**int8_t NUM_CH** Returns the number of SBUS channels (i.e. 16).

**void ch(const std::array<int16_t, 16> &val)** Sets the channel data to be transmitted.

```C++
sbus.ch(sbus_tx_data);
```

**bool ch17()** Returns the value of channel 17 to be transmitted.

```C++
bool ch17 = sbus.ch17();
```

**bool ch18()** Returns the value of channel 18 to be transmitted.

```C++
bool ch18 = sbus.ch18();
```

**bool lost_frame()** Returns the lost frame flag value to be transmitted.

```C++
bool lost_frame = sbus.lost_frame();
```

**bool failsafe()** Returns the failsafe flag value to be transmitted.

```C++
bool failsafe = sbus.failsafe();
```

**std::array<int16_t, 16> ch()** Returns the array of channel data to be transmitted.

```C++
std::array<int16_t, 16> sbus_tx_data = sbus.ch();
```
63 changes: 21 additions & 42 deletions examples/sbus_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,57 +27,36 @@

/* SBUS object, reading SBUS */
bfs::SbusRx sbus_rx;

/* SBUS object, writing SBUS */
bfs::SbusTx<16> sbus_tx;

/* Sbus RX data */
bfs::InceptorData data;
bfs::SbusTx sbus_tx;

int main() {
/* Serial to display data */
Serial.begin(115200);
while(!Serial) {}
/* RX Config */
bfs::InceptorConfig rx_config = {
.hw = &Serial2,
.throttle = {
.ch = 0,
.num_coef = 2,
.poly_coef = {0.0012203, -1.2098841}
}
};
if (!sbus_rx.Init(rx_config)) {
Serial.println("Unable to establish communication with SBUS receiver");
while (1) {}
}
Serial.print("Initializing SBUS receiver...");
while (!sbus_rx.Init(&Serial2)) {}
Serial.println("done.");
/* TX Config */
bfs::EffectorConfig<16> tx_config = {
.hw = &Serial2,
.effectors = {
{
.type = bfs::SERVO,
.ch = 1,
.min = -20,
.max = 20,
.failsafe = 0,
.num_coef = 2,
.poly_coef = {819.50, 991.50}
}
}
};
if (!sbus_tx.Init(tx_config)) {
Serial.println("Unable to init SBUS transmitter");
while (1) {}
}
sbus_tx.EnableMotors();
sbus_tx.EnableServos();
std::array<float, 1> cmds;
sbus_tx.Init(&Serial2);
while (1) {
if (sbus_rx.Read(&data)) {
Serial.println(data.throttle);
cmds[0] = data.throttle;
sbus_tx.Cmd(cmds);
if (sbus_rx.Read()) {
/* Print the SBUS data */
Serial.print(sbus_rx.lost_frame());
Serial.print("\t");
Serial.print(sbus_rx.failsafe());
Serial.print("\t");
std::array<int16_t, bfs::SbusRx::NUM_CH> data = sbus_rx.ch();
for (std::size_t i = 0; i < data.size(); i++) {
Serial.print(data[i]);
Serial.print("\t");
}
Serial.print(sbus_rx.ch17());
Serial.print("\t");
Serial.println(sbus_rx.ch18());
/* Write the SBUS data */
sbus_tx.ch(data);
sbus_tx.Write();
}
}
Expand Down
Loading

0 comments on commit fbdfad3

Please sign in to comment.