-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_autopilot_version.cpp
95 lines (78 loc) · 2.72 KB
/
send_autopilot_version.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <mavsdk/mavsdk.h>
#include <mavsdk/plugins/info/info.h>
#include <mavsdk/plugins/telemetry/telemetry.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <csignal>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <mutex>
#include <sstream>
#include <thread>
using mavsdk::ConnectionResult;
using mavsdk::Mavsdk;
using mavsdk::Info;
using mavsdk::System;
using mavsdk::Telemetry;
void usage(std::string bin_name) {
std::cout << "Usage : " << bin_name << " <connection_url>" << std::endl
<< "Connection URL format should be :" << std::endl
<< " For TCP : tcp://[server_host][:server_port]" << std::endl
<< " For UDP : udp://[bind_host][:bind_port]" << std::endl
<< " For Serial : serial:///path/to/serial/dev[:baudrate]" << std::endl
<< "For example, to connect to the simulator use URL: udp://:14540" << std::endl;
}
static std::atomic_bool g_is_shutdown(false);
void signal_handler(int /*sig*/) { g_is_shutdown.store(true); }
int main(int argc, char** argv) {
signal(SIGINT, signal_handler);
if (argc != 2) {
usage(argv[0]);
return 1;
}
const std::string connection_url = argv[1];
Mavsdk mavsdk;
mavsdk.set_configuration(Mavsdk::Configuration::CompanionComputer);
const ConnectionResult connection_result = mavsdk.add_any_connection(connection_url);
if (connection_result != ConnectionResult::SUCCESS) {
std::cerr << "Connection failed: " << connection_result_str(connection_result) << std::endl;
return 1;
}
std::cout << "send_autopilot_version: Waiting to discover system..." << std::endl;
std::atomic<System*> system(nullptr);
std::atomic<uint64_t> uid1;
mavsdk.register_on_discover([&system, &mavsdk, &uid1](uint64_t uuid_param) {
System* nullptr_ob = nullptr;
uid1.store(uuid_param);
if (system.compare_exchange_strong(/*expected=*/nullptr_ob, /*desired=*/&mavsdk.system(uid1))) {
std::cout << "Discovered system with UID1: " << uid1 << ", hex " << uid1
<< std::endl;
}
});
// Try forever to get a connection
for (int64_t i = 0; !g_is_shutdown.load(); ++i) {
std::this_thread::sleep_for(std::chrono::seconds(1));
if (system.load() != nullptr) {
break;
}
if (i % 60 == 0) {
std::cout << "send_autopilot_version: Still waiting to discover system" << std::endl;
}
}
Info info(*system);
while (!g_is_shutdown.load()) {
// Spin loop at 20Hz
std::this_thread::sleep_for(std::chrono::milliseconds(50));
info.send_autopilot_version();
// std::cout << "send autopilot version" << std::endl;
}
return 0;
}