test_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 <gtest/gtest.h>
18 
19 #include <vector>
20 
21 #include <ros/time.h>
22 
24 
25 namespace urg_stamped
26 {
27 namespace device_state_estimator
28 {
29 
30 TEST(ClockState, StampToTime)
31 {
32  {
33  SCOPED_TRACE("ClockGain 1.5");
34  ClockState s;
35 
36  s.stamp_ = 1000;
37  s.origin_ = ros::Time(100);
38  s.gain_ = 1.0 / 1.5;
39 
40  ASSERT_EQ(ros::Time(99.5), s.stampToTime(0));
41  ASSERT_EQ(ros::Time(101.0), s.stampToTime(1000));
42  ASSERT_EQ(ros::Time(102.5), s.stampToTime(2000));
43  }
44  {
45  SCOPED_TRACE("ClockGain 1.0");
46  ClockState s;
47 
48  s.stamp_ = 1000;
49  s.origin_ = ros::Time(100);
50  s.gain_ = 1.0;
51 
52  ASSERT_EQ(ros::Time(100.0), s.stampToTime(0));
53  ASSERT_EQ(ros::Time(101.0), s.stampToTime(1000));
54  ASSERT_EQ(ros::Time(102.0), s.stampToTime(2000));
55  }
56  {
57  SCOPED_TRACE("ClockGain 0.5");
58  ClockState s;
59 
60  s.stamp_ = 1000;
61  s.origin_ = ros::Time(100);
62  s.gain_ = 1.0 / 0.5;
63 
64  ASSERT_EQ(ros::Time(100.5), s.stampToTime(0));
65  ASSERT_EQ(ros::Time(101.0), s.stampToTime(1000));
66  ASSERT_EQ(ros::Time(101.5), s.stampToTime(2000));
67  }
68 }
69 
70 TEST(ClockEstimatorUUST1, FindMinDelay)
71 {
73 
74  est.startSync();
75  est.pushSyncSample(ros::Time(100.0001), ros::Time(100.00015), 100);
76  est.pushSyncSample(ros::Time(101.0002), ros::Time(101.00023), 1100);
77  est.pushSyncSample(ros::Time(102.0003), ros::Time(102.00031), 2100); // Minimal delay
78  est.pushSyncSample(ros::Time(103.0004), ros::Time(103.00046), 3100);
79  est.pushSyncSample(ros::Time(104.0005), ros::Time(104.00054), 4100);
80  est.pushSyncSample(ros::Time(105.0006), ros::Time(105.00062), 5100); // Second minimal delay
81 
82  {
83  SCOPED_TRACE("Without OriginFracPart");
84  const auto it = est.findMinDelay(OriginFracPart());
85  ASSERT_EQ(ros::Time(102.0003), it->t_req_);
86  ASSERT_EQ(ros::Time(102.00031), it->t_res_);
87  ASSERT_EQ(2100, it->device_wall_stamp_);
88  }
89 
90  {
91  SCOPED_TRACE("With OriginFracPart");
92  const auto it = est.findMinDelay(OriginFracPart(0.00029, 0.00031));
93  ASSERT_EQ(ros::Time(105.0006), it->t_req_);
94  ASSERT_EQ(ros::Time(105.00062), it->t_res_);
95  ASSERT_EQ(5100, it->device_wall_stamp_);
96  }
97 
98  est.finishSync();
99  est.startSync();
100 
101  {
102  SCOPED_TRACE("No samples");
103  ASSERT_EQ(est.sync_samples_.end(), est.findMinDelay(OriginFracPart()))
104  << "must return end iterator if no samples are pushSyncSampleed";
105  }
106 }
107 
108 TEST(ClockEstimatorUUST1, RawClockOrigin)
109 {
111  for (double d = 0; d < 0.003; d += 0.00025)
112  {
113  SCOPED_TRACE("Offset " + std::to_string(d));
114  est.startSync();
115  for (double t = 0; t < 0.11; t += 0.0101)
116  {
117  const uint64_t ts = (t + d) * 1000;
118  est.pushSyncSample(ros::Time(1 + t), ros::Time(1 + t + 0.0001), ts);
119  }
120  est.finishSync();
121  ASSERT_NEAR(est.recent_clocks_.back().origin_.toSec(), 1.000 - d, 0.0002);
122  }
123 }
124 
126 {
127  const std::vector<double> gains =
128  {
129  0.999,
130  1.000,
131  1.001,
132  };
133 
134  for (const double gain : gains)
135  {
137  SCOPED_TRACE("Gain " + std::to_string(gain));
138 
139  for (double t0 = 0; t0 < 50; t0 += 10)
140  {
141  SCOPED_TRACE("T " + std::to_string(t0));
142  est.startSync();
143  for (double t = t0; t < t0 + 0.11; t += 0.0101)
144  {
145  const uint64_t ts = t * gain * 1000;
146  est.pushSyncSample(ros::Time(1 + t), ros::Time(1 + t + 0.0001), ts);
147  }
148  est.finishSync();
149 
150  if (t0 > 0)
151  {
152  ASSERT_NEAR(est.getClockState().gain_, gain, 0.0001);
153  }
154  }
155  }
156 }
157 
158 TEST(ClockEstimatorUUST1, InitialClockState)
159 {
161  const double t0 = 100;
162 
163  est.startSync();
164  for (double t = 10; t < 10.2; t += 0.0101)
165  {
166  est.pushSyncSample(ros::Time(t0 + t), ros::Time(t0 + t + 0.00001), t * 1000);
167  }
168  est.finishSync();
169 
170  ASSERT_NEAR(
171  t0 + 31.0000,
172  est.getClockState().stampToTime(31000).toSec(),
173  0.0001);
174  ASSERT_NEAR(
175  t0 + 51.0000,
176  est.getClockState().stampToTime(51000).toSec(),
177  0.0001);
178 }
179 
180 TEST(ScanEstimatorUTM, PushScanSampleRaw)
181 {
183  ScanEstimatorUTM est(clock_est, ros::Duration(0.1));
184  const double t0 = 100;
185 
186  clock_est->startSync();
187  for (double t = 10; t < 10.2; t += 0.0101)
188  {
189  clock_est->pushSyncSample(ros::Time(t0 + t), ros::Time(t0 + t + 0.00001), t * 1000);
190  }
191  clock_est->finishSync();
192  clock_est->startSync();
193  for (double t = 20; t < 20.2; t += 0.0101)
194  {
195  clock_est->pushSyncSample(ros::Time(t0 + t), ros::Time(t0 + t + 0.00001), t * 1000);
196  }
197  clock_est->finishSync();
198 
199  const auto clock = clock_est->getClockState();
200  est.estimateScanTime(
201  ros::Time(t0 + 30.0200), clock.stampToTime(30000)); // minimal scan stamp to send delay: 20ms
202  est.estimateScanTime(
203  ros::Time(t0 + 30.1206), clock.stampToTime(30100));
204  est.estimateScanTime(
205  ros::Time(t0 + 30.2203), clock.stampToTime(30200));
206 
207  ASSERT_NEAR(
208  t0 + 31.0000,
209  est.estimateScanTime(ros::Time(t0 + 31.0200), clock.stampToTime(31000)).first.toSec(),
210  0.0001);
211  ASSERT_NEAR(
212  t0 + 32.0005,
213  est.estimateScanTime(ros::Time(t0 + 32.0205), clock.stampToTime(32000)).first.toSec(),
214  0.0001);
215 }
216 
217 } // namespace device_state_estimator
218 } // namespace urg_stamped
219 
220 int main(int argc, char** argv)
221 {
222  testing::InitGoogleTest(&argc, argv);
223 
224  return RUN_ALL_TESTS();
225 }
urg_stamped::device_state_estimator::TEST
TEST(ClockState, StampToTime)
Definition: test_device_state_estimator.cpp:30
urg_stamped::device_state_estimator::ClockEstimator::recent_clocks_
std::deque< ClockSample > recent_clocks_
Definition: device_state_estimator.h:225
s
XmlRpcServer s
time.h
urg_stamped::device_state_estimator::OriginFracPart
Definition: device_state_estimator.h:108
urg_stamped::device_state_estimator::ClockEstimatorUUST1::sync_samples_
std::vector< SyncSampleUUST1 > sync_samples_
Definition: device_state_estimator.h:308
urg_stamped::device_state_estimator::ClockEstimator::Ptr
std::shared_ptr< ClockEstimator > Ptr
Definition: device_state_estimator.h:200
TimeBase< Time, Duration >::toSec
double toSec() const
urg_stamped::device_state_estimator::ClockEstimatorUUST1::finishSync
bool finishSync() override
Definition: clock_estimator_uust1.cpp:73
urg_stamped::device_state_estimator::ClockState::gain_
double gain_
Definition: device_state_estimator.h:61
main
int main(int argc, char **argv)
Definition: test_device_state_estimator.cpp:220
urg_stamped::device_state_estimator::ClockEstimator::getClockState
ClockState getClockState() const
Definition: device_state_estimator.h:211
urg_stamped::device_state_estimator::ScanEstimatorUTM
Definition: device_state_estimator.h:371
urg_stamped::device_state_estimator::ClockEstimatorUUST1::findMinDelay
std::vector< SyncSampleUUST1 >::const_iterator findMinDelay(const OriginFracPart &overflow_range) const
Definition: clock_estimator_uust1.cpp:116
d
d
urg_stamped::device_state_estimator::ClockEstimatorUUST1::pushSyncSample
void pushSyncSample(const ros::Time &t_req, const ros::Time &t_res, const uint64_t device_wall_stamp) override
Definition: clock_estimator_uust1.cpp:38
urg_stamped::device_state_estimator::ClockEstimatorUUST1
Definition: device_state_estimator.h:272
urg_stamped::device_state_estimator::ScanEstimatorUTM::estimateScanTime
std::pair< ros::Time, bool > estimateScanTime(const ros::Time &t_recv, const ros::Time &t_stamp)
Definition: scan_estimator_utm.cpp:86
ros::Time
urg_stamped::device_state_estimator::ClockEstimatorUUST1::startSync
void startSync() override
Definition: clock_estimator_uust1.cpp:32
device_state_estimator.h
urg_stamped
Definition: device_state_estimator.h:35
ros::Duration
urg_stamped::device_state_estimator::ClockState
Definition: device_state_estimator.h:57
urg_stamped::device_state_estimator::ClockState::stampToTime
ros::Time stampToTime(const uint64_t stamp) const
Definition: device_state_estimator.cpp:32


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