-
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "log.h" | ||
#include "ping.h" | ||
#include "system_impl.h" | ||
|
||
namespace mavsdk { | ||
|
||
Ping::Ping(SystemImpl& system_impl) : _system_impl(system_impl) | ||
{ | ||
_system_impl.register_mavlink_message_handler( | ||
MAVLINK_MSG_ID_PING, | ||
[this](const mavlink_message_t& message) { Ping::process_ping(message); }, | ||
this); | ||
} | ||
|
||
Ping::~Ping() | ||
{ | ||
_system_impl.unregister_all_mavlink_message_handlers(this); | ||
} | ||
|
||
void Ping::run_once() | ||
{ | ||
mavlink_message_t message; | ||
|
||
uint64_t now = static_cast<uint64_t>(_system_impl.get_time().elapsed_s() * 1e6); | ||
|
||
mavlink_msg_ping_pack( | ||
_system_impl.get_own_system_id(), | ||
_system_impl.get_own_component_id(), | ||
&message, | ||
now, | ||
_ping_sequence, | ||
0, | ||
0); // to all | ||
|
||
_system_impl.send_message(message); | ||
} | ||
|
||
void Ping::process_ping(const mavlink_message_t& message) | ||
{ | ||
mavlink_ping_t ping; | ||
mavlink_msg_ping_decode(&message, &ping); | ||
|
||
if (ping.target_system == 0 && ping.target_component == 0) { | ||
// Response to ping request. | ||
mavlink_message_t response_message; | ||
mavlink_msg_ping_pack( | ||
_system_impl.get_own_system_id(), | ||
_system_impl.get_own_component_id(), | ||
&response_message, | ||
ping.time_usec, | ||
ping.seq, | ||
message.sysid, | ||
message.compid); | ||
|
||
_system_impl.send_message(response_message); | ||
|
||
} else { | ||
// Answer from ping request. | ||
if (ping.seq != _ping_sequence) { | ||
LogWarn() << "Ignoring unknown ping sequence"; | ||
return; | ||
} | ||
|
||
if (message.compid != MAV_COMP_ID_AUTOPILOT1) { | ||
// We're currently only interested in the ping of the autopilot. | ||
return; | ||
} | ||
|
||
auto now_us = static_cast<uint64_t>(_system_impl.get_time().elapsed_s() * 1e6); | ||
_last_ping_time_us = now_us - ping.time_usec; | ||
LogDebug() << "Ping: " << _last_ping_time_us << " us"; | ||
} | ||
} | ||
|
||
} // namespace mavsdk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include "mavlink_include.h" | ||
#include <atomic> | ||
|
||
namespace mavsdk { | ||
|
||
class SystemImpl; | ||
|
||
class Ping { | ||
public: | ||
explicit Ping(SystemImpl& system_impl); | ||
~Ping(); | ||
|
||
void run_once(); | ||
double last_ping_time_s() const { return static_cast<double>(_last_ping_time_us) * 1e-6; } | ||
|
||
private: | ||
void process_ping(const mavlink_message_t& message); | ||
|
||
SystemImpl& _system_impl; | ||
uint32_t _ping_sequence{0}; | ||
std::atomic<uint64_t> _last_ping_time_us{0}; | ||
}; | ||
|
||
} // namespace mavsdk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters