Skip to content

Commit e8fe514

Browse files
authored
Migrate to using MAVSDK (#8)
* migrated to MAVSDK for mavlink, updated README, default install for simulation, general cleanup * update README and config defaults * remove mavlink-cpp submodule
1 parent 7131b5c commit e8fe514

11 files changed

Lines changed: 80 additions & 441 deletions

File tree

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
[submodule "libraries/mavlink-cpp"]
2-
path = libraries/mavlink-cpp
3-
url = https://github.com/dakejahl/mavlink-cpp.git
41
[submodule "libraries/bluez"]
52
path = libraries/bluez
63
url = https://github.com/bluez/bluez.git

CMakeLists.txt

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,9 @@ project(rid-transmitter VERSION 0.1 LANGUAGES C CXX)
77

88
add_compile_options(-Wall -Werror -Wunused -Wextra -Wno-address-of-packed-member)
99

10-
string(ASCII 27 Esc)
11-
set(White "${Esc}[m")
12-
set(Red "${Esc}[31m")
13-
set(Green "${Esc}[32m")
14-
set(Yellow "${Esc}[33m")
15-
set(Blue "${Esc}[34m")
16-
set(BoldGreen "${Esc}[1;32m")
17-
18-
# Determine git version
19-
exec_program(
20-
"git"
21-
${CMAKE_CURRENT_SOURCE_DIR}
22-
ARGS "describe"
23-
OUTPUT_VARIABLE GIT_TAG
24-
)
25-
26-
#...if not found get the SHA1 hash instead
27-
if(${GIT_TAG} MATCHES "fatal:.*")
28-
exec_program(
29-
"git"
30-
${CMAKE_CURRENT_SOURCE_DIR}
31-
ARGS "rev-parse HEAD"
32-
OUTPUT_VARIABLE GIT_VERSION
33-
)
34-
else()
35-
set(GIT_VERSION ${GIT_TAG})
36-
endif()
37-
38-
add_definitions(-DAPP_GIT_VERSION="${GIT_VERSION}")
39-
40-
message("-- Version: ${BoldGreen}${GIT_VERSION}${White}")
41-
42-
##### Build mavlink-cpp
43-
add_subdirectory(libraries/mavlink-cpp)
10+
# Assumes MAVSDK system wide install
11+
list(APPEND CMAKE_PREFIX_PATH "/usr/local/MAVSDK/install")
12+
find_package(MAVSDK REQUIRED)
4413

4514
# Include bluez
4615
find_package(PkgConfig REQUIRED)
@@ -52,7 +21,6 @@ include_directories(
5221
src/misc
5322
src/Bluetooth
5423
src/Transmitter
55-
src/Transmitter/parameters
5624
libraries/tomlplusplus/
5725
libraries/opendroneid-core-c/libopendroneid
5826
${BLUEZ_INCLUDE_DIRS}
@@ -65,11 +33,11 @@ add_executable(${PROJECT_NAME}
6533
src/Bluetooth/BluetoothLegacy.cpp
6634
src/Bluetooth/print_bt_features.c
6735
src/Transmitter/Transmitter.cpp
68-
src/Transmitter/parameters/parameters.cpp
6936
src/main.cpp
7037
)
7138

7239
target_link_libraries(${PROJECT_NAME}
7340
mavlink-cpp
41+
MAVSDK::mavsdk
7442
${BLUEZ_LIBRARIES}
7543
)

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# RemoteIDTransmitter
22

3+
Check out the YouTube video announcement here <br>
34
[![IMAGE ALT TEXT HERE](https://img.youtube.com/vi/VN_R9-af3zg/0.jpg)](https://www.youtube.com/watch?v=VN_R9-af3zg)
45

56
### Introduction
@@ -11,7 +12,7 @@ If you are new to RemoteID you will need to familiarize yourself with ASTM3411 a
1112

1213
---
1314
### Running the application
14-
Pre-requisites
15+
Install dependencies
1516
```
1617
sudo apt-get install -y \
1718
astyle \
@@ -23,11 +24,21 @@ Build
2324
```
2425
make
2526
```
26-
Run
27+
Install
2728
```
28-
sudo ./build/rid-transmitter --mavlink-url udp://0.0.0.0:14540
29+
make install
2930
```
30-
You must be root to use bluetooth. Or you can give the binary capabilities to use bluetooth
31+
After installing, you can run the application alongside the PX4 simulator.
32+
1. Edit the **~/.local/share/rid-transmitter/config.toml** and change the `connection_url`
33+
```
34+
connection_url = "udp://0.0.0.0:14540"
35+
```
36+
2. Run the application
37+
```
38+
rid-transmitter
39+
```
40+
#### Note
41+
You must be root to use bluetooth. You can give the binary capabilities to use bluetooth. The **install.sh** does this already.
3142
```
3243
sudo setcap 'cap_net_raw,cap_net_admin+eip' build/rid-transmitter
3344
```
@@ -42,11 +53,11 @@ sudo setcap 'cap_net_raw,cap_net_admin+eip' build/rid-transmitter
4253

4354
- We do not use message packs and instead send messages individually due to limitations with advertisement data packet size that varies between hardware.
4455

45-
- BlueZ cannot simultaneously broadcast standard and extended advertisement, so we must toggle between the two modes.
56+
- BlueZ cannot simultaneously broadcast standard and extended advertisement, so we rapidly toggle between both modes.
4657

47-
- The minimum advertising interval is 20ms per bluetooth spec, so we space advertisements 30ms apart.
58+
- The minimum bluetooth advertising interval is 20ms, so we space advertisements 30ms apart.
4859

49-
- We rely on the mavlink data to contain accurate information. We always publish the RemoteID messages and do not check that all the necessary data is present before transmitting.
60+
- We rely on the mavlink data to contain accurate information. We always transmit the RemoteID data and do not check the accurary of the data before transmitting.
5061

5162
- If things aren't working use `sudo btmon` to help debug.
5263

install.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$DEFAULT_XDG_CONF_HOME}"
77
export XDG_DATA_HOME="${XDG_DATA_HOME:-$DEFAULT_XDG_DATA_HOME}"
88
THIS_DIR="$(dirname "$(realpath "$BASH_SOURCE")")"
99

10-
sudo true
11-
1210
# Install dependencies
1311
sudo apt-get install -y astyle bluez bluez-tools libbluetooth-dev
1412

13+
pushd .
14+
cd "$THIS_DIR"
1515
make
16+
popd
1617

1718
# Setup project directory
1819
cp $THIS_DIR/build/rid-transmitter ~/.local/bin

libraries/mavlink-cpp

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Transmitter/Transmitter.cpp

Lines changed: 41 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
11
#include <Transmitter.hpp>
22
#include <unistd.h>
3-
#include <CircularBuffer.hpp>
3+
#include <mavsdk/log_callback.h>
44

55
namespace txr
66
{
77

8-
#if defined(DOCKER_BUILD)
9-
static char VERSION_FILE_NAME[] = "/data/version.txt";
10-
#else
11-
static char VERSION_FILE_NAME[] = "/tmp/rid-transmitter/version.txt";
12-
#endif
13-
148
Transmitter::Transmitter(const txr::Settings& settings)
159
: _settings(settings)
1610
{
17-
// If version has changed reset parameters to default
18-
std::string version(APP_GIT_VERSION);
19-
std::string saved_version(get_sw_version());
20-
21-
if (saved_version != version) {
22-
params::set_defaults();
23-
set_sw_version(version);
24-
LOG(GREEN_TEXT "New software version! Resetting parameters" NORMAL_TEXT);
25-
}
26-
27-
LOG(GREEN_TEXT "Version: " CYAN_TEXT "%s" NORMAL_TEXT, version.c_str());
28-
29-
// Load parameters from file system into RAM
30-
params::load();
11+
// Disable mavsdk noise
12+
mavsdk::log::subscribe([](...) {
13+
// https://mavsdk.mavlink.io/main/en/cpp/guide/logging.html
14+
return true;
15+
});
3116
}
3217

3318
bool Transmitter::start()
@@ -39,53 +24,68 @@ bool Transmitter::start()
3924
return false;
4025
}
4126

42-
//// Setup MAVLink
43-
_mavlink = std::make_shared<mavlink::Mavlink>(_settings.mavlink_settings);
27+
LOG("Waiting for MAVSDK connection: %s", _settings.mavsdk_connection_url.c_str());
4428

45-
// Provide PARAM_REQUEST_LIST and PARAM_SET callbacks for our application
46-
_mavlink->enable_parameters(
47-
std::bind(&Transmitter::mavlink_param_request_list_cb, this),
48-
std::bind(&Transmitter::mavlink_param_set_cb, this, std::placeholders::_1)
49-
);
29+
while (!wait_for_mavsdk_connection(3)) {
30+
if (_should_exit) {
31+
return false;
32+
}
33+
}
5034

51-
_mavlink->subscribe_to_message(MAVLINK_MSG_ID_HEARTBEAT, [this](const mavlink_message_t& message) {
35+
_mavlink->subscribe_message(MAVLINK_MSG_ID_HEARTBEAT, [this](const mavlink_message_t& message) {
5236
if (message.sysid == 1 && message.compid == 1) {
37+
// LOG("MAVLINK_MSG_ID_HEARTBEAT: %u / %u", message.sysid, message.compid);
5338
std::lock_guard<std::mutex> lock(_heartbeat_mutex);
5439
mavlink_msg_heartbeat_decode(&message, &_heartbeat_msg);
5540
}
5641
});
5742

58-
_mavlink->subscribe_to_message(MAVLINK_MSG_ID_OPEN_DRONE_ID_LOCATION, [this](const mavlink_message_t& message) {
43+
_mavlink->subscribe_message(MAVLINK_MSG_ID_OPEN_DRONE_ID_LOCATION, [this](const mavlink_message_t& message) {
5944
// LOG("MAVLINK_MSG_ID_OPEN_DRONE_ID_LOCATION: %u / %u", message.sysid, message.compid);
6045
std::lock_guard<std::mutex> lock(_location_mutex);
6146
mavlink_msg_open_drone_id_location_decode(&message, &_location_msg);
6247
});
6348

64-
_mavlink->subscribe_to_message(MAVLINK_MSG_ID_OPEN_DRONE_ID_SYSTEM, [this](const mavlink_message_t& message) {
49+
_mavlink->subscribe_message(MAVLINK_MSG_ID_OPEN_DRONE_ID_SYSTEM, [this](const mavlink_message_t& message) {
6550
// LOG("MAVLINK_MSG_ID_OPEN_DRONE_ID_SYSTEM: %u / %u", message.sysid, message.compid);
6651
std::lock_guard<std::mutex> lock(_system_mutex);
6752
mavlink_msg_open_drone_id_system_decode(&message, &_system_msg);
6853
});
6954

70-
auto result = _mavlink->start();
71-
72-
if (result != mavlink::ConnectionResult::Success) {
73-
LOG(RED_TEXT "Mavlink connection start failed" NORMAL_TEXT);
74-
return false;
75-
}
76-
7755
return true;
7856
}
7957

8058
void Transmitter::stop()
8159
{
82-
if (_mavlink.get()) _mavlink->stop();
83-
8460
if (_bluetooth.get()) _bluetooth->stop();
8561

8662
_should_exit.store(true);
8763
}
8864

65+
bool Transmitter::wait_for_mavsdk_connection(double timeout_s)
66+
{
67+
auto config = mavsdk::Mavsdk::Configuration(mavsdk::ComponentType::RemoteId);
68+
_mavsdk = std::make_shared<mavsdk::Mavsdk>(config);
69+
70+
auto result = _mavsdk->add_any_connection(_settings.mavsdk_connection_url);
71+
72+
if (result != mavsdk::ConnectionResult::Success) {
73+
return false;
74+
}
75+
76+
auto system = _mavsdk->first_autopilot(timeout_s);
77+
78+
if (!system) {
79+
return false;
80+
}
81+
82+
LOG("Connected to autopilot");
83+
_mavlink = std::make_shared<mavsdk::MavlinkPassthrough>(system.value());
84+
85+
return true;
86+
}
87+
88+
8989
void Transmitter::run_state_machine()
9090
{
9191
uint64_t loop_rate_ms = 200;
@@ -103,10 +103,6 @@ void Transmitter::run_state_machine()
103103
_bluetooth->enable_le_extended_advertising();
104104
}
105105

106-
if (params::updated()) {
107-
params::load();
108-
}
109-
110106
// Fill in the data from mavlink messages
111107
struct ODID_UAS_Data data = {};
112108
// Basic ID
@@ -211,68 +207,4 @@ void Transmitter::send_single_messages(struct ODID_UAS_Data* data)
211207
}
212208
}
213209

214-
std::vector<mavlink::Parameter> Transmitter::mavlink_param_request_list_cb()
215-
{
216-
std::vector<mavlink::Parameter> mavlink_parameters;
217-
auto params = params::parameters(); // Copy parameters from working set
218-
uint16_t count = 0;
219-
220-
for (auto& [name, value] : params) {
221-
mavlink::Parameter p = {
222-
.name = name,
223-
.float_value = value,
224-
.index = count++,
225-
.total_count = (uint16_t)params.size(),
226-
.type = MAV_PARAM_TYPE_REAL32 // We only use floats
227-
};
228-
229-
mavlink_parameters.emplace_back(p);
230-
}
231-
232-
return mavlink_parameters;
233-
}
234-
235-
bool Transmitter::mavlink_param_set_cb(mavlink::Parameter* param)
236-
{
237-
bool succes = params::set_parameter(param->name, param->float_value);
238-
239-
if (succes) {
240-
auto params = params::parameters();
241-
param->index = distance(params.begin(), params.find(param->name));
242-
}
243-
244-
return succes;
245-
}
246-
247-
const std::string Transmitter::get_sw_version()
248-
{
249-
std::string version;
250-
251-
std::ifstream infile(VERSION_FILE_NAME);
252-
std::stringstream is;
253-
254-
if (!is.good()) {
255-
return version;
256-
}
257-
258-
is << infile.rdbuf();
259-
infile.close();
260-
261-
std::string line;
262-
263-
if (std::getline(is, line)) {
264-
version = line;
265-
}
266-
267-
return version;
268-
}
269-
270-
void Transmitter::set_sw_version(const std::string& version)
271-
{
272-
std::ofstream outfile;
273-
outfile.open(VERSION_FILE_NAME, std::ofstream::out | std::ofstream::trunc);
274-
outfile << version << std::endl;
275-
outfile.close();
276-
}
277-
278-
} // end namespace txr
210+
} // end namespace txr

0 commit comments

Comments
 (0)