Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable timesync by default #1169

Merged
merged 1 commit into from Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/core/system.cpp
Expand Up @@ -57,4 +57,9 @@ void System::register_component_discovered_callback(discover_callback_t callback
return _system_impl->register_component_discovered_callback(callback);
}

void System::enable_timesync()
{
_system_impl->enable_timesync();
}

} // namespace mavsdk
5 changes: 5 additions & 0 deletions src/core/system.h
Expand Up @@ -97,6 +97,11 @@ class System {
*/
void register_component_discovered_callback(discover_callback_t callback) const;

/**
* @brief Enable time synchronization using the TIMESYNC messages.
*/
void enable_timesync();

/**
* @brief Copy constructor (object is not copyable).
*/
Expand Down
5 changes: 5 additions & 0 deletions src/core/system_impl.cpp
Expand Up @@ -108,6 +108,11 @@ void SystemImpl::unregister_timeout_handler(const void* cookie)
_parent.timeout_handler.remove(cookie);
}

void SystemImpl::enable_timesync()
{
_timesync.enable();
}

void SystemImpl::process_mavlink_message(mavlink_message_t& message)
{
// This is a low level interface where incoming messages can be tampered
Expand Down
3 changes: 2 additions & 1 deletion src/core/system_impl.h
Expand Up @@ -53,6 +53,8 @@ class SystemImpl : public Sender {
MavsdkImpl& parent, uint8_t system_id, uint8_t component_id, bool connected);
~SystemImpl();

void enable_timesync();

void process_mavlink_message(mavlink_message_t& message);

typedef std::function<void(const mavlink_message_t&)> mavlink_message_handler_t;
Expand Down Expand Up @@ -231,7 +233,6 @@ class SystemImpl : public Sender {
const SystemImpl& operator=(const SystemImpl&) = delete;

private:
// Helper methods added to increase readablity
static bool is_autopilot(uint8_t comp_id);
static bool is_camera(uint8_t comp_id);

Expand Down
21 changes: 14 additions & 7 deletions src/core/timesync.cpp
Expand Up @@ -6,21 +6,28 @@

namespace mavsdk {

Timesync::Timesync(SystemImpl& parent) : _parent(parent)
{
using namespace std::placeholders; // for `_1`

_parent.register_mavlink_message_handler(
MAVLINK_MSG_ID_TIMESYNC, std::bind(&Timesync::process_timesync, this, _1), this);
}
Timesync::Timesync(SystemImpl& parent) : _parent(parent) {}

Timesync::~Timesync()
{
_parent.unregister_all_mavlink_message_handlers(this);
}

void Timesync::enable()
{
_is_enabled = true;
_parent.register_mavlink_message_handler(
MAVLINK_MSG_ID_TIMESYNC,
std::bind(&Timesync::process_timesync, this, std::placeholders::_1),
this);
}

void Timesync::do_work()
{
if (!_is_enabled) {
return;
}

if (_parent.get_time().elapsed_since_s(_last_time) >= _TIMESYNC_SEND_INTERVAL_S) {
if (_parent.is_connected()) {
uint64_t now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
Expand Down
4 changes: 3 additions & 1 deletion src/core/timesync.h
Expand Up @@ -12,6 +12,7 @@ class Timesync {
Timesync(SystemImpl& parent);
~Timesync();

void enable();
void do_work();

Timesync(const Timesync&) = delete;
Expand All @@ -30,6 +31,7 @@ class Timesync {
static constexpr uint64_t _MAX_CONS_HIGH_RTT = 5;
static constexpr uint64_t _MAX_RTT_SAMPLE_MS = 10;
uint64_t _high_rtt_count{};
bool _autopilot_timesync_acquired = false;
bool _autopilot_timesync_acquired{false};
bool _is_enabled{false};
};
} // namespace mavsdk