Go to the documentation of this file.
19 #include <sensor_msgs/TimeReference.h>
20 #include <std_msgs/Duration.h>
21 #include <mavros_msgs/TimesyncStatus.h>
22 #include <rosgraph_msgs/Clock.h>
25 namespace std_plugins {
52 std::lock_guard<std::mutex> lock(
mutex);
67 void tick(int64_t rtt_ns, uint64_t remote_timestamp_ns, int64_t time_offset_ns)
69 std::lock_guard<std::mutex> lock(
mutex);
80 std::lock_guard<std::mutex> lock(
mutex);
86 std::lock_guard<std::mutex> lock(
mutex);
92 double freq = events / window;
98 stat.
summary(2,
"No events recorded.");
101 stat.
summary(1,
"Frequency too low.");
104 stat.
summary(1,
"Frequency too high.");
110 stat.
addf(
"Timesyncs since startup",
"%d",
count_);
111 stat.
addf(
"Frequency (Hz)",
"%f", freq);
115 stat.
addf(
"Estimated time offset (s)",
"%0.9f",
offset / 1e9);
156 PluginBase::initialize(uas_);
158 double conn_system_time_d;
159 double conn_timesync_d;
160 std::string ts_mode_str;
165 if (
nh.
getParam(
"conn/system_time_rate", conn_system_time_d) && conn_system_time_d != 0.0) {
169 if (
nh.
getParam(
"conn/timesync_rate", conn_timesync_d) && conn_timesync_d != 0.0) {
174 nh.
param<std::string>(
"time/timesync_mode", ts_mode_str,
"MAVLINK");
232 if (!conn_system_time.
isZero()) {
239 if (!conn_timesync.
isZero() && !(ts_mode == TSM::NONE || ts_mode == TSM::PASSTHROUGH)) {
296 void handle_system_time(
const mavlink::mavlink_message_t *msg, mavlink::common::msg::SYSTEM_TIME &mtime)
299 const bool fcu_time_valid = mtime.time_unix_usec > 1234567890ULL * 1000000;
301 if (fcu_time_valid) {
303 auto time_unix = boost::make_shared<sensor_msgs::TimeReference>();
305 mtime.time_unix_usec / 1000000,
306 (mtime.time_unix_usec % 1000000) * 1000);
309 time_unix->time_ref = time_ref;
314 auto clock = boost::make_shared<rosgraph_msgs::Clock>();
315 clock->clock = time_ref;
324 void handle_timesync(
const mavlink::mavlink_message_t *msg, mavlink::common::msg::TIMESYNC &tsync)
328 if (tsync.tc1 == 0) {
332 else if (tsync.tc1 > 0) {
344 mavlink::common::msg::SYSTEM_TIME mtime {};
345 mtime.time_unix_usec = time_unix_usec;
353 if (ts_mode == TSM::MAVLINK) {
355 }
else if (ts_mode == TSM::ONBOARD) {
366 mavlink::common::msg::TIMESYNC tsync {};
378 uint64_t rtt_ns = now_ns - local_time_ns;
381 uint64_t deviation = llabs(int64_t(
time_offset) - offset_ns);
391 ROS_ERROR_NAMED(
"time",
"TM : Time jump detected. Resetting time synchroniser.");
405 float p = 1.0f - expf(0.5
f * (1.0
f - 1.0
f / (1.0
f - progress)));
434 ROS_WARN_NAMED(
"time",
"TM : RTT too high for timesync: %0.2f ms.", rtt_ns / 1000000.0);
442 auto timesync_status = boost::make_shared<mavros_msgs::TimesyncStatus>();
445 timesync_status->remote_timestamp_ns = remote_time_ns;
446 timesync_status->observed_offset_ns = offset_ns;
447 timesync_status->estimated_offset_ns =
time_offset;
448 timesync_status->round_trip_time_ms = float(rtt_ns / 1000000.0);
495 struct timespec spec;
496 clock_gettime(CLOCK_MONOTONIC, &spec);
498 return spec.tv_sec * 1000000000ULL + spec.tv_nsec;
std::string time_ref_source
ros::WallTimer timesync_timer
std::vector< HandlerInfo > Subscriptions
Subscriptions vector.
timesync_mode get_timesync_mode(void)
void initialize(UAS &uas_) override
Plugin initializer.
int max_cons_high_deviation
PluginBase()
Plugin constructor Should not do anything before initialize()
bool getParam(const std::string &key, bool &b) const
void handle_timesync(const mavlink::mavlink_message_t *msg, mavlink::common::msg::TIMESYNC &tsync)
#define UAS_FCU(uasobjptr)
helper accessor to FCU link interface
void sys_time_cb(const ros::WallTimerEvent &event)
DiagnosticTask(const std::string name)
std::recursive_mutex mutex
void timesync_cb(const ros::WallTimerEvent &event)
ros::Publisher sim_time_pub
#define ROS_ERROR_NAMED(name,...)
std::vector< ros::Time > times_
void tick(int64_t rtt_ns, uint64_t remote_timestamp_ns, int64_t time_offset_ns)
void publish(const boost::shared_ptr< M > &message) const
Publisher advertise(AdvertiseOptions &ops)
void add_sample(int64_t offset_ns)
ros::Publisher timesync_status_pub
float filter_alpha_initial
std::vector< int > seq_nums_
void summary(const diagnostic_msgs::DiagnosticStatus &src)
ros::WallTimer sys_time_timer
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
void add_timesync_observation(int64_t offset_ns, uint64_t local_time_ns, uint64_t remote_time_ns)
std::string to_string(timesync_mode e)
#define UAS_DIAG(uasobjptr)
helper accessor to diagnostic updater
ros::Publisher time_ref_pub
void send_timesync_msg(uint64_t tc1, uint64_t ts1)
void handle_system_time(const mavlink::mavlink_message_t *msg, mavlink::common::msg::SYSTEM_TIME &mtime)
TimeSyncStatus(const std::string &name, size_t win_size)
#define ROS_WARN_THROTTLE_NAMED(period, name,...)
Subscriptions get_subscriptions() override
Return vector of MAVLink message subscriptions (handlers)
timesync_mode timesync_mode_from_str(const std::string &mode)
Retrieve timesync mode from name.
void set_timestamp(uint64_t remote_timestamp_ns)
MAVROS Plugin base class.
uint64_t get_monotonic_now(void)
#define ROS_WARN_NAMED(name,...)
void addf(const std::string &key, const char *format,...)
T param(const std::string ¶m_name, const T &default_val) const
#define ROS_INFO_STREAM_NAMED(name, args)
void run(diagnostic_updater::DiagnosticStatusWrapper &stat)
HandlerInfo make_handler(const mavlink::msgid_t id, void(_C::*fn)(const mavlink::mavlink_message_t *msg, const mavconn::Framing framing))
void set_time_offset(uint64_t offset_ns)
const size_t window_size_
float filter_beta_initial
utils::timesync_mode timesync_mode
WallTimer createWallTimer(WallDuration period, const WallTimerCallback &callback, bool oneshot=false, bool autostart=true) const
void set_timesync_mode(timesync_mode mode)
mavros
Author(s): Vladimir Ermakov
autogenerated on Tue May 6 2025 02:34:03