clock_estimator_uust2.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2024 The urg_stamped Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <algorithm>
18 #include <cmath>
19 #include <cstdint>
20 #include <vector>
21 
22 #include <ros/time.h>
23 
25 #include <scip2/logger.h>
26 
27 namespace urg_stamped
28 {
29 namespace device_state_estimator
30 {
31 
33 {
34  sync_samples_.clear();
36  cnt_samples_ = 0;
37 }
38 
40  const ros::Time& t_req, const ros::Time& t_res, const uint64_t device_wall_stamp)
41 {
42  cnt_samples_++;
43  const SyncSampleUUST2 s(t_req, t_res, device_wall_stamp);
44 
46  {
47  sync_samples_.push_back(s);
48  }
49  if (s.delay_ < best_delay_)
50  {
51  best_delay_ = s.delay_;
52  }
53  if (comm_delay_.min_.isZero() || comm_delay_.min_ > s.delay_)
54  {
55  comm_delay_.min_ = s.delay_;
56  }
57 }
58 
60 {
61  return cnt_samples_ >= MAX_SYNC_ATTEMPTS ||
62  (sync_samples_.size() >= MIN_SYNC_SAMPLES &&
64 }
65 
67 {
68  if (sync_samples_.size() < MIN_SYNC_SAMPLES)
69  {
71  << "Not enough number of usable sync responses. "
72  << "Communication delay may be too large"
73  << std::endl;
74  return false;
75  }
76 
78  {
79  // UUST2 sets a device timestamp of sometime between command receive time
80  // and 5ms response timer. So, the timestamp can be matched only when
81  // it's returned within DEVICE_TIMESTAMP_RESOLUTION.
83  << "No sync response with small delay. "
84  << "Communication delay may be too large"
85  << std::endl;
86  return false;
87  }
88 
89  ros::Time min_t_origin = sync_samples_.front().t_origin_;
90  for (const auto& s : sync_samples_)
91  {
92  if (s.t_origin_ < min_t_origin)
93  {
94  min_t_origin = s.t_origin_;
95  }
96  }
97 
98  std::vector<SyncSampleUUST2> sorted_samples;
99  for (auto& s : sync_samples_)
100  {
101  const double origin_compensate =
102  std::round((s.t_origin_ - min_t_origin).toSec() / DEVICE_TIMESTAMP_RESOLUTION) * DEVICE_TIMESTAMP_RESOLUTION;
103 
104  s.t_origin_ -= ros::Duration(origin_compensate);
105  sorted_samples.push_back(s);
106 
107  SyncSampleUUST2 s2 = s;
109  sorted_samples.push_back(s2);
110  }
111 
112  std::sort(sorted_samples.begin(), sorted_samples.end());
113  const SyncSampleUUST2 med = sorted_samples[sorted_samples.size() / 2];
114 
115  comm_delay_.sigma_ = ros::Duration(0.005); // Not used by UST scan estimator
116 
118  << "delay: " << med.delay_
119  << ", device timestamp: " << med.device_wall_stamp_
120  << std::endl;
121 
122  const ClockSample clock =
123  {
124  .t_estim_ = med.t_process_,
125  .stamp_ = med.device_wall_stamp_,
126  .origin_ = med.t_origin_,
127  };
128  return pushClockSample(clock);
129 }
130 
131 } // namespace device_state_estimator
132 } // namespace urg_stamped
133 
logger.h
urg_stamped::device_state_estimator::ClockEstimator::comm_delay_
CommDelay comm_delay_
Definition: device_state_estimator.h:224
s
XmlRpcServer s
urg_stamped::device_state_estimator::ClockSample
Definition: device_state_estimator.h:49
time.h
urg_stamped::device_state_estimator::ClockEstimatorUUST2::SyncSampleUUST2::t_frac_
ros::Duration t_frac_
Definition: device_state_estimator.h:351
urg_stamped::device_state_estimator::ClockEstimatorUUST2::cnt_samples_
int cnt_samples_
Definition: device_state_estimator.h:368
urg_stamped::device_state_estimator::ClockEstimatorUUST2::hasEnoughSyncSamples
bool hasEnoughSyncSamples() const override
Definition: clock_estimator_uust2.cpp:59
urg_stamped::device_state_estimator::CommDelay::min_
ros::Duration min_
Definition: device_state_estimator.h:45
urg_stamped::device_state_estimator::ClockEstimatorUUST2::MAX_SYNC_ATTEMPTS
static constexpr int MAX_SYNC_ATTEMPTS
Definition: device_state_estimator.h:343
urg_stamped::device_state_estimator::SyncSample::device_wall_stamp_
uint64_t device_wall_stamp_
Definition: device_state_estimator.h:93
urg_stamped::device_state_estimator::SyncSample::t_process_
ros::Time t_process_
Definition: device_state_estimator.h:96
urg_stamped::device_state_estimator::SyncSample::delay_
ros::Duration delay_
Definition: device_state_estimator.h:95
urg_stamped::device_state_estimator::ClockEstimatorUUST2::SyncSampleUUST2
Definition: device_state_estimator.h:347
urg_stamped::device_state_estimator::ClockSample::t_estim_
ros::Time t_estim_
Definition: device_state_estimator.h:52
urg_stamped::device_state_estimator::ClockEstimatorUUST2::startSync
void startSync() override
Definition: clock_estimator_uust2.cpp:32
urg_stamped::device_state_estimator::ClockEstimatorUUST2::best_delay_
ros::Duration best_delay_
Definition: device_state_estimator.h:367
urg_stamped::device_state_estimator::ClockEstimator::pushClockSample
bool pushClockSample(const ClockSample &clock)
Definition: device_state_estimator.cpp:65
scip2::logger::debug
std::ostream & debug()
Definition: logger.cpp:93
ros::Time
urg_stamped::device_state_estimator::ClockEstimatorUUST2::ACCEPTABLE_SAMPLE_DELAY
static constexpr double ACCEPTABLE_SAMPLE_DELAY
Definition: device_state_estimator.h:345
urg_stamped::device_state_estimator::ClockEstimatorUUST2::MIN_SYNC_SAMPLES
static constexpr int MIN_SYNC_SAMPLES
Definition: device_state_estimator.h:342
device_state_estimator.h
urg_stamped::device_state_estimator::ClockEstimatorUUST2::SyncSampleUUST2::t_origin_
ros::Time t_origin_
Definition: device_state_estimator.h:350
urg_stamped::device_state_estimator::CommDelay::sigma_
ros::Duration sigma_
Definition: device_state_estimator.h:46
urg_stamped::device_state_estimator::ClockEstimatorUUST2::RESPONSE_TIMER_INTERVAL
static constexpr double RESPONSE_TIMER_INTERVAL
Definition: device_state_estimator.h:344
urg_stamped::device_state_estimator::ClockEstimatorUUST2::pushSyncSample
void pushSyncSample(const ros::Time &t_req, const ros::Time &t_res, const uint64_t device_wall_stamp) override
Definition: clock_estimator_uust2.cpp:39
urg_stamped
Definition: device_state_estimator.h:35
scip2::logger::error
std::ostream & error()
Definition: logger.cpp:105
urg_stamped::device_state_estimator::ClockEstimatorUUST2::finishSync
bool finishSync() override
Definition: clock_estimator_uust2.cpp:66
urg_stamped::device_state_estimator::ClockEstimatorUUST2::sync_samples_
std::vector< SyncSampleUUST2 > sync_samples_
Definition: device_state_estimator.h:366
ros::Duration
DurationBase< Duration >::isZero
bool isZero() const
urg_stamped::device_state_estimator::DEVICE_TIMESTAMP_RESOLUTION
static constexpr double DEVICE_TIMESTAMP_RESOLUTION
Definition: device_state_estimator.h:40


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Wed Dec 18 2024 03:10:57