mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-08 07:50:35 +08:00
msg ROS2 compatibility, microdds_client improvements (timesync, reduced code size, added topics, etc), fastrtps purge
- update all msgs to be directly compatible with ROS2 - microdds_client improvements - timesync - reduced code size - add to most default builds if we can afford it - lots of other little changes - purge fastrtps (I tried to save this multiple times, but kept hitting roadblocks)
This commit is contained in:
@@ -72,82 +72,7 @@ MavlinkTimesync::handle_message(const mavlink_message_t *msg)
|
||||
|
||||
} else if (tsync.tc1 > 0) { // Message originating from this system, compute time offset from it
|
||||
|
||||
// Calculate time offset between this system and the remote system, assuming RTT for
|
||||
// the timesync packet is roughly equal both ways.
|
||||
int64_t offset_us = (int64_t)((tsync.ts1 / 1000ULL) + now - (tsync.tc1 / 1000ULL) * 2) / 2 ;
|
||||
|
||||
// Calculate the round trip time (RTT) it took the timesync packet to bounce back to us from remote system
|
||||
uint64_t rtt_us = now - (tsync.ts1 / 1000ULL);
|
||||
|
||||
// Calculate the difference of this sample from the current estimate
|
||||
uint64_t deviation = llabs((int64_t)_time_offset - offset_us);
|
||||
|
||||
if (rtt_us < MAX_RTT_SAMPLE) { // Only use samples with low RTT
|
||||
|
||||
if (sync_converged() && (deviation > MAX_DEVIATION_SAMPLE)) {
|
||||
|
||||
// Increment the counter if we have a good estimate and are getting samples far from the estimate
|
||||
_high_deviation_count++;
|
||||
|
||||
// We reset the filter if we received 5 consecutive samples which violate our present estimate.
|
||||
// This is most likely due to a time jump on the offboard system.
|
||||
if (_high_deviation_count > MAX_CONSECUTIVE_HIGH_DEVIATION) {
|
||||
PX4_ERR("[timesync] Time jump detected. Resetting time synchroniser.");
|
||||
// Reset the filter
|
||||
reset_filter();
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// Filter gain scheduling
|
||||
if (!sync_converged()) {
|
||||
// Interpolate with a sigmoid function
|
||||
double progress = (double)_sequence / (double)CONVERGENCE_WINDOW;
|
||||
double p = 1.0 - exp(0.5 * (1.0 - 1.0 / (1.0 - progress)));
|
||||
_filter_alpha = p * ALPHA_GAIN_FINAL + (1.0 - p) * ALPHA_GAIN_INITIAL;
|
||||
_filter_beta = p * BETA_GAIN_FINAL + (1.0 - p) * BETA_GAIN_INITIAL;
|
||||
|
||||
} else {
|
||||
_filter_alpha = ALPHA_GAIN_FINAL;
|
||||
_filter_beta = BETA_GAIN_FINAL;
|
||||
}
|
||||
|
||||
// Perform filter update
|
||||
add_sample(offset_us);
|
||||
|
||||
// Increment sequence counter after filter update
|
||||
_sequence++;
|
||||
|
||||
// Reset high deviation count after filter update
|
||||
_high_deviation_count = 0;
|
||||
|
||||
// Reset high RTT count after filter update
|
||||
_high_rtt_count = 0;
|
||||
}
|
||||
|
||||
} else {
|
||||
// Increment counter if round trip time is too high for accurate timesync
|
||||
_high_rtt_count++;
|
||||
|
||||
if (_high_rtt_count > MAX_CONSECUTIVE_HIGH_RTT) {
|
||||
PX4_WARN("[timesync] RTT too high for timesync: %llu ms (sender: %i)", rtt_us / 1000ULL, msg->compid);
|
||||
// Reset counter to rate-limit warnings
|
||||
_high_rtt_count = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Publish status message
|
||||
timesync_status_s tsync_status{};
|
||||
|
||||
tsync_status.timestamp = hrt_absolute_time();
|
||||
tsync_status.source_protocol = timesync_status_s::SOURCE_PROTOCOL_MAVLINK;
|
||||
tsync_status.remote_timestamp = tsync.tc1 / 1000ULL;
|
||||
tsync_status.observed_offset = offset_us;
|
||||
tsync_status.estimated_offset = (int64_t)_time_offset;
|
||||
tsync_status.round_trip_time = rtt_us;
|
||||
|
||||
_timesync_status_pub.publish(tsync_status);
|
||||
_timesync.update(now, tsync.tc1, tsync.ts1);
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -181,58 +106,3 @@ MavlinkTimesync::handle_message(const mavlink_message_t *msg)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t
|
||||
MavlinkTimesync::sync_stamp(uint64_t usec)
|
||||
{
|
||||
// Only return synchronised stamp if we have converged to a good value
|
||||
if (sync_converged()) {
|
||||
return usec + (int64_t)_time_offset;
|
||||
|
||||
} else {
|
||||
return hrt_absolute_time();
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
MavlinkTimesync::sync_converged()
|
||||
{
|
||||
return _sequence >= CONVERGENCE_WINDOW;
|
||||
}
|
||||
|
||||
void
|
||||
MavlinkTimesync::add_sample(int64_t offset_us)
|
||||
{
|
||||
/* Online exponential smoothing filter. The derivative of the estimate is also
|
||||
* estimated in order to produce an estimate without steady state lag:
|
||||
* https://en.wikipedia.org/wiki/Exponential_smoothing#Double_exponential_smoothing
|
||||
*/
|
||||
|
||||
double time_offset_prev = _time_offset;
|
||||
|
||||
if (_sequence == 0) { // First offset sample
|
||||
_time_offset = offset_us;
|
||||
|
||||
} else {
|
||||
// Update the clock offset estimate
|
||||
_time_offset = _filter_alpha * offset_us + (1.0 - _filter_alpha) * (_time_offset + _time_skew);
|
||||
|
||||
// Update the clock skew estimate
|
||||
_time_skew = _filter_beta * (_time_offset - time_offset_prev) + (1.0 - _filter_beta) * _time_skew;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
MavlinkTimesync::reset_filter()
|
||||
{
|
||||
// Do a full reset of all statistics and parameters
|
||||
_sequence = 0;
|
||||
_time_offset = 0.0;
|
||||
_time_skew = 0.0;
|
||||
_filter_alpha = ALPHA_GAIN_INITIAL;
|
||||
_filter_beta = BETA_GAIN_INITIAL;
|
||||
_high_deviation_count = 0;
|
||||
_high_rtt_count = 0;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user