From 7f93e6d4fa1a9edea8652c94c4ed610106eb6418 Mon Sep 17 00:00:00 2001 From: Tyler Lentz Date: Sat, 13 Apr 2024 19:57:56 +0000 Subject: [PATCH 1/5] add /connection handler func --- include/network/gcs_routes.hpp | 9 +++++++++ include/network/mavlink.hpp | 1 + protos | 2 +- src/network/gcs.cpp | 1 + src/network/gcs_routes.cpp | 25 +++++++++++++++++++++++++ src/network/mavlink.cpp | 4 ++++ 6 files changed, 41 insertions(+), 1 deletion(-) diff --git a/include/network/gcs_routes.hpp b/include/network/gcs_routes.hpp index 58fef3d1..986bf82e 100644 --- a/include/network/gcs_routes.hpp +++ b/include/network/gcs_routes.hpp @@ -17,6 +17,15 @@ #include "ticks/tick.hpp" #include "ticks/path_gen.hpp" +/* + * GET /connection + * --- + * Returns information about the connection status of the OBC + * + * 200 OK: Successfully retrieved data + */ +DEF_GCS_HANDLE(Get, connection); + /* * GET /tick * --- diff --git a/include/network/mavlink.hpp b/include/network/mavlink.hpp index bfeeed79..81195da8 100644 --- a/include/network/mavlink.hpp +++ b/include/network/mavlink.hpp @@ -64,6 +64,7 @@ class MavlinkClient { double airspeed_m_s(); double heading_deg(); mavsdk::Telemetry::FlightMode flight_mode(); + mavsdk::Telemetry::RcStatus get_conn_status(); private: mavsdk::Mavsdk mavsdk; diff --git a/protos b/protos index b7d3e9b7..abe95730 160000 --- a/protos +++ b/protos @@ -1 +1 @@ -Subproject commit b7d3e9b74b38046602d4631072e6777b0b59b273 +Subproject commit abe95730634b146f39fb5127ee96bac511e9cbd5 diff --git a/src/network/gcs.cpp b/src/network/gcs.cpp index c890ff49..fccf1480 100644 --- a/src/network/gcs.cpp +++ b/src/network/gcs.cpp @@ -46,6 +46,7 @@ GCSServer::~GCSServer() { } void GCSServer::_bindHandlers() { + BIND_HANDLER(Get, connection); BIND_HANDLER(Get, tick); BIND_HANDLER(Get, mission); BIND_HANDLER(Post, mission); diff --git a/src/network/gcs_routes.cpp b/src/network/gcs_routes.cpp index b2825f72..bbf82ecf 100644 --- a/src/network/gcs_routes.cpp +++ b/src/network/gcs_routes.cpp @@ -16,6 +16,8 @@ #include "ticks/path_gen.hpp" #include "ticks/path_validate.hpp" +using namespace std::chrono_literals; + /* * This file defines all of the GCS handler functions for every route * the gcs server is listening on. @@ -34,6 +36,29 @@ * the LOG_RESPONSE macro will handle it for you. */ +DEF_GCS_HANDLE(Get, connection) { + LOG_REQUEST("GET", "/connection"); + + auto lost_airdrop_conns = state->getAirdrop()->getLostConnections(3s); + auto mav_conn = state->getMav()->get_conn_status(); + // TODO: query the camera status + bool camera_good = false; + + OBCConnInfo info; + for (auto const& [bottle_index, ms_since_last_heartbeat] : lost_airdrop_conns) { + info.add_dropped_bottle_idx(bottle_index); + info.add_ms_since_ad_heartbeat(ms_since_last_heartbeat.count()); + } + info.set_mav_rc_good(mav_conn.is_available); + info.set_mav_rc_strength(mav_conn.signal_strength_percent); + info.set_camera_good(camera_good); + + std::string output; + google::protobuf::util::MessageToJsonString(info, &output); + + LOG_RESPONSE(INFO, "Returning conn info", OK, output.c_str(), mime::json); +} + DEF_GCS_HANDLE(Get, tick) { LOG_REQUEST("GET", "/tick"); diff --git a/src/network/mavlink.cpp b/src/network/mavlink.cpp index 05d89d67..4a929e54 100644 --- a/src/network/mavlink.cpp +++ b/src/network/mavlink.cpp @@ -276,3 +276,7 @@ mavsdk::Telemetry::FlightMode MavlinkClient::flight_mode() { Lock lock(this->data_mut); return this->data.flight_mode; } + +mavsdk::Telemetry::RcStatus MavlinkClient::get_conn_status() { + return this->telemetry->rc_status(); +} \ No newline at end of file From 8b86de093e13f3f17e2f60a9a788f75317244ecf Mon Sep 17 00:00:00 2001 From: Tyler Lentz Date: Wed, 17 Apr 2024 21:28:56 +0000 Subject: [PATCH 2/5] add err msg if gcs server fails to start up --- .devcontainer/devcontainer.json | 5 ++++- include/network/gcs_routes.hpp | 2 +- src/network/gcs.cpp | 6 ++++-- src/network/gcs_routes.cpp | 4 ++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 2f83d5b7..a118c206 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,11 +1,14 @@ // See this page for reference of options: https://containers.dev/implementors/json_reference { "name": "Existing Dockerfile", - "image": "ghcr.io/tritonuas/obcpp:x86", + "image": "ghcr.io/tritonuas/obcpp:main", // enable when need to connect over USB to pixhawk // also: need to run obcpp with sudo or add tuas user to dialout group with // `sudo usermod -aG dialout tuas && newgrp && bash` // "runArgs": ["--device=/dev/ttyACM0"], + "runArgs": [ + "--network=host" + ], "customizations": { "vscode": { diff --git a/include/network/gcs_routes.hpp b/include/network/gcs_routes.hpp index 986bf82e..f81a86f1 100644 --- a/include/network/gcs_routes.hpp +++ b/include/network/gcs_routes.hpp @@ -24,7 +24,7 @@ * * 200 OK: Successfully retrieved data */ -DEF_GCS_HANDLE(Get, connection); +DEF_GCS_HANDLE(Get, connections); /* * GET /tick diff --git a/src/network/gcs.cpp b/src/network/gcs.cpp index fccf1480..956ac1d6 100644 --- a/src/network/gcs.cpp +++ b/src/network/gcs.cpp @@ -33,7 +33,9 @@ GCSServer::GCSServer(uint16_t port, std::shared_ptr state) this->server_thread = std::thread([this, port]() { loguru::set_thread_name("gcs server"); LOG_F(INFO, "Starting GCS HTTP server on port %d", port); - this->server.listen("0.0.0.0", port); + if (!this->server.listen("0.0.0.0", port)) { + LOG_F(ERROR, "ERROR: GCS server stopped!"); + } LOG_F(INFO, "GCS Server stopped on port %d", port); }); } @@ -46,7 +48,7 @@ GCSServer::~GCSServer() { } void GCSServer::_bindHandlers() { - BIND_HANDLER(Get, connection); + BIND_HANDLER(Get, connections); BIND_HANDLER(Get, tick); BIND_HANDLER(Get, mission); BIND_HANDLER(Post, mission); diff --git a/src/network/gcs_routes.cpp b/src/network/gcs_routes.cpp index bbf82ecf..6c3f78b7 100644 --- a/src/network/gcs_routes.cpp +++ b/src/network/gcs_routes.cpp @@ -36,8 +36,8 @@ using namespace std::chrono_literals; * the LOG_RESPONSE macro will handle it for you. */ -DEF_GCS_HANDLE(Get, connection) { - LOG_REQUEST("GET", "/connection"); +DEF_GCS_HANDLE(Get, connections) { + LOG_REQUEST("GET", "/connections"); auto lost_airdrop_conns = state->getAirdrop()->getLostConnections(3s); auto mav_conn = state->getMav()->get_conn_status(); From 7c1f2563c42b2ff741d0e9c30ee7f9d1a5308df9 Mon Sep 17 00:00:00 2001 From: Tyler Lentz Date: Wed, 17 Apr 2024 23:02:42 +0000 Subject: [PATCH 3/5] implement connection route --- .devcontainer/devcontainer.json | 6 +++--- protos | 2 +- src/network/gcs.cpp | 4 ++++ src/network/gcs_routes.cpp | 22 ++++++++++++++++++++-- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index a118c206..48852230 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -6,9 +6,9 @@ // also: need to run obcpp with sudo or add tuas user to dialout group with // `sudo usermod -aG dialout tuas && newgrp && bash` // "runArgs": ["--device=/dev/ttyACM0"], - "runArgs": [ - "--network=host" - ], + // "runArgs": [ + // "--network=host" + // ], "customizations": { "vscode": { diff --git a/protos b/protos index abe95730..c5841043 160000 --- a/protos +++ b/protos @@ -1 +1 @@ -Subproject commit abe95730634b146f39fb5127ee96bac511e9cbd5 +Subproject commit c5841043ba805c065997232b5a6ae281873d9969 diff --git a/src/network/gcs.cpp b/src/network/gcs.cpp index 956ac1d6..970c7c3c 100644 --- a/src/network/gcs.cpp +++ b/src/network/gcs.cpp @@ -48,6 +48,10 @@ GCSServer::~GCSServer() { } void GCSServer::_bindHandlers() { + this->server.Get("/", [](const httplib::Request& request, httplib::Response& response) { + response.status = 200; + response.set_content("Fort-nite", "text/plain"); + }); BIND_HANDLER(Get, connections); BIND_HANDLER(Get, tick); BIND_HANDLER(Get, mission); diff --git a/src/network/gcs_routes.cpp b/src/network/gcs_routes.cpp index 6c3f78b7..d2b29ad3 100644 --- a/src/network/gcs_routes.cpp +++ b/src/network/gcs_routes.cpp @@ -39,8 +39,26 @@ using namespace std::chrono_literals; DEF_GCS_HANDLE(Get, connections) { LOG_REQUEST("GET", "/connections"); - auto lost_airdrop_conns = state->getAirdrop()->getLostConnections(3s); - auto mav_conn = state->getMav()->get_conn_status(); + std::list> lost_airdrop_conns; + if (state->getAirdrop() == nullptr) { + lost_airdrop_conns.push_back({BottleDropIndex::A, 99999ms}); + lost_airdrop_conns.push_back({BottleDropIndex::B, 99999ms}); + lost_airdrop_conns.push_back({BottleDropIndex::C, 99999ms}); + lost_airdrop_conns.push_back({BottleDropIndex::D, 99999ms}); + lost_airdrop_conns.push_back({BottleDropIndex::E, 99999ms}); + } else { + lost_airdrop_conns = state->getAirdrop()->getLostConnections(3s); + } + + mavsdk::Telemetry::RcStatus mav_conn; + + if (state->getMav() == nullptr) { + mav_conn.is_available = false; + mav_conn.signal_strength_percent = 0.1; + } else { + mav_conn = state->getMav()->get_conn_status(); + } + // TODO: query the camera status bool camera_good = false; From cdd6ebd1d4fce8afd3652919853ec8ce08fdfa72 Mon Sep 17 00:00:00 2001 From: Tyler Lentz Date: Wed, 17 Apr 2024 23:05:42 +0000 Subject: [PATCH 4/5] fix lint --- src/network/gcs_routes.cpp | 2 +- src/network/mavlink.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/network/gcs_routes.cpp b/src/network/gcs_routes.cpp index d2b29ad3..b74a78db 100644 --- a/src/network/gcs_routes.cpp +++ b/src/network/gcs_routes.cpp @@ -16,7 +16,7 @@ #include "ticks/path_gen.hpp" #include "ticks/path_validate.hpp" -using namespace std::chrono_literals; +using namespace std::chrono_literals; // NOLINT /* * This file defines all of the GCS handler functions for every route diff --git a/src/network/mavlink.cpp b/src/network/mavlink.cpp index 8f66e314..ea2297bb 100644 --- a/src/network/mavlink.cpp +++ b/src/network/mavlink.cpp @@ -279,4 +279,4 @@ mavsdk::Telemetry::FlightMode MavlinkClient::flight_mode() { mavsdk::Telemetry::RcStatus MavlinkClient::get_conn_status() { return this->telemetry->rc_status(); -} \ No newline at end of file +} From 05b3b15a8213ac80001432b250bbf94fc2c29212 Mon Sep 17 00:00:00 2001 From: Tyler Lentz Date: Wed, 17 Apr 2024 23:11:06 +0000 Subject: [PATCH 5/5] fix default value not being 0.0 for rcStrength --- include/network/gcs_macros.hpp | 5 +++++ src/network/gcs_routes.cpp | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/network/gcs_macros.hpp b/include/network/gcs_macros.hpp index ae1d1a19..11303615 100644 --- a/include/network/gcs_macros.hpp +++ b/include/network/gcs_macros.hpp @@ -107,6 +107,11 @@ if (request.has_header("User-Agent")) \ LOG_F(INFO, "User-Agent: %s", request.get_header_value("User-Agent").c_str()); +#define LOG_REQUEST_TRACE(method, route) \ + VLOG_SCOPE_F(TRACE, "%s %s", method, route); \ + if (request.has_header("User-Agent")) \ + VLOG_F(TRACE, "User-Agent: %s", request.get_header_value("User-Agent").c_str()); + // One of the LOG_RESPONSE logging functions should be used to both log and // set the HTTP response #define LOG_RESPONSE_5(LOG_LEVEL, msg, response_code, body, mime) \ diff --git a/src/network/gcs_routes.cpp b/src/network/gcs_routes.cpp index b74a78db..fe619c67 100644 --- a/src/network/gcs_routes.cpp +++ b/src/network/gcs_routes.cpp @@ -37,7 +37,7 @@ using namespace std::chrono_literals; // NOLINT */ DEF_GCS_HANDLE(Get, connections) { - LOG_REQUEST("GET", "/connections"); + LOG_REQUEST_TRACE("GET", "/connections"); std::list> lost_airdrop_conns; if (state->getAirdrop() == nullptr) { @@ -54,7 +54,7 @@ DEF_GCS_HANDLE(Get, connections) { if (state->getMav() == nullptr) { mav_conn.is_available = false; - mav_conn.signal_strength_percent = 0.1; + mav_conn.signal_strength_percent = 0.0; } else { mav_conn = state->getMav()->get_conn_status(); } @@ -74,7 +74,7 @@ DEF_GCS_HANDLE(Get, connections) { std::string output; google::protobuf::util::MessageToJsonString(info, &output); - LOG_RESPONSE(INFO, "Returning conn info", OK, output.c_str(), mime::json); + LOG_RESPONSE(TRACE, "Returning conn info", OK, output.c_str(), mime::json); } DEF_GCS_HANDLE(Get, tick) {