device_state_estimator.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 
32 ros::Time ClockState::stampToTime(const uint64_t stamp) const
33 {
34  const double from_origin = (stamp_ + static_cast<int64_t>(stamp - stamp_) / gain_) * DEVICE_TIMESTAMP_RESOLUTION;
35  return origin_ + ros::Duration(from_origin);
36 }
37 
39 {
40  const double from_origin = (t - origin_).toSec();
41  const double interval = interval_.toSec();
42  const int n = std::lround(from_origin / interval);
43  return origin_ + ros::Duration(interval * n);
44 }
45 
47 {
48  const double r = std::fmod(t.toSec(), DEVICE_TIMESTAMP_RESOLUTION);
49  const double t0 = std::min(t_min_, t_max_);
50  const double t1 = std::max(t_min_, t_max_);
51  return t0 - TOLERANCE < r && r < t1 + TOLERANCE;
52 }
53 
55 {
56  const double frac = (t_min_ + t_max_) / 2;
57  double t_integral = std::floor(t.toSec() / DEVICE_TIMESTAMP_RESOLUTION) * DEVICE_TIMESTAMP_RESOLUTION;
58  if (std::fmod(t.toSec(), DEVICE_TIMESTAMP_RESOLUTION) < frac)
59  {
60  t_integral -= DEVICE_TIMESTAMP_RESOLUTION;
61  }
62  return ros::Time(t_integral + frac);
63 }
64 
66 {
67  recent_clocks_.push_back(clock);
68  if (recent_clocks_.size() >= CLOCK_SAMPLES)
69  {
70  recent_clocks_.pop_front();
71  }
72 
73  if (recent_clocks_.size() <= 1)
74  {
75  // Initialized=false since clock gain is not yet estimated
76  clock_ = ClockState(clock.origin_, 1, clock.stamp_, false);
77  scip2::logger::debug() << "initial origin: " << clock.origin_ << std::endl;
78  return true;
79  }
80 
81  std::vector<ClockState> clocks;
82  for (size_t i = 1; i < recent_clocks_.size(); ++i)
83  {
84  for (size_t j = 0; j < i; ++j)
85  {
86  const double t_diff =
87  (recent_clocks_[i].t_estim_ - recent_clocks_[j].t_estim_).toSec();
88  const double origin_diff =
89  (recent_clocks_[i].origin_ - recent_clocks_[j].origin_).toSec();
90  clocks.emplace_back(
91  recent_clocks_[i].origin_,
92  (t_diff - origin_diff) / t_diff,
93  recent_clocks_[i].stamp_);
94  }
95  }
96  std::sort(clocks.begin(), clocks.end());
97  clock_ = clocks[clocks.size() / 2];
98 
100  << "origin: " << clock_.origin_
101  << ", gain: " << clock_.gain_
102  << std::endl;
103 
104  return true;
105 }
106 
107 } // namespace device_state_estimator
108 } // namespace urg_stamped
urg_stamped::device_state_estimator::ScanState::interval_
ros::Duration interval_
Definition: device_state_estimator.h:174
urg_stamped::device_state_estimator::ClockSample::origin_
ros::Time origin_
Definition: device_state_estimator.h:54
urg_stamped::device_state_estimator::ClockState::stamp_
uint64_t stamp_
Definition: device_state_estimator.h:62
urg_stamped::device_state_estimator::ClockEstimator::recent_clocks_
std::deque< ClockSample > recent_clocks_
Definition: device_state_estimator.h:225
logger.h
urg_stamped::device_state_estimator::ClockSample
Definition: device_state_estimator.h:49
urg_stamped::device_state_estimator::ClockState::origin_
ros::Time origin_
Definition: device_state_estimator.h:60
time.h
TimeBase< Time, Duration >::toSec
double toSec() const
urg_stamped::device_state_estimator::ClockState::gain_
double gain_
Definition: device_state_estimator.h:61
urg_stamped::device_state_estimator::ScanState::fit
ros::Time fit(const ros::Time &t) const
Definition: device_state_estimator.cpp:38
urg_stamped::device_state_estimator::OriginFracPart::TOLERANCE
static constexpr double TOLERANCE
Definition: device_state_estimator.h:114
urg_stamped::device_state_estimator::ClockSample::stamp_
uint64_t stamp_
Definition: device_state_estimator.h:53
urg_stamped::device_state_estimator::OriginFracPart::compensate
ros::Time compensate(const ros::Time &t) const
Definition: device_state_estimator.cpp:54
urg_stamped::device_state_estimator::OriginFracPart::t_min_
const double t_min_
Definition: device_state_estimator.h:112
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
urg_stamped::device_state_estimator::OriginFracPart::t_max_
const double t_max_
Definition: device_state_estimator.h:113
urg_stamped::device_state_estimator::OriginFracPart::isOnOverflow
bool isOnOverflow(const ros::Time &t) const
Definition: device_state_estimator.cpp:46
ros::Time
urg_stamped::device_state_estimator::ClockEstimator::CLOCK_SAMPLES
static constexpr int CLOCK_SAMPLES
Definition: device_state_estimator.h:221
device_state_estimator.h
DurationBase< Duration >::toSec
double toSec() const
urg_stamped
Definition: device_state_estimator.h:35
urg_stamped::device_state_estimator::ScanState::origin_
ros::Time origin_
Definition: device_state_estimator.h:173
ros::Duration
urg_stamped::device_state_estimator::ClockState
Definition: device_state_estimator.h:57
urg_stamped::device_state_estimator::ClockEstimator::clock_
ClockState clock_
Definition: device_state_estimator.h:223
urg_stamped::device_state_estimator::ClockState::stampToTime
ros::Time stampToTime(const uint64_t stamp) const
Definition: device_state_estimator.cpp:32
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