device_time_origin.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020-2021 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 #ifndef URG_STAMPED_DEVICE_TIME_ORIGIN_H
18 #define URG_STAMPED_DEVICE_TIME_ORIGIN_H
19 
20 #include <ros/ros.h>
21 
22 #include <cmath>
23 
24 namespace urg_stamped
25 {
26 namespace device_time_origin
27 {
29 {
30 public:
32  double gain_;
33 
34  inline DriftedTime()
35  : gain_(1.0)
36  {
37  }
38  inline DriftedTime(const ros::Time origin, const float gain)
39  : origin_(origin)
40  , gain_(gain)
41  {
42  }
43 };
44 
45 namespace estimator
46 {
48  const boost::posix_time::ptime& time_request,
49  const boost::posix_time::ptime& time_response,
50  const uint64_t& device_timestamp)
51 {
52  const ros::Duration delay =
53  ros::Time::fromBoost(time_response) -
54  ros::Time::fromBoost(time_request);
55  const ros::Time time_at_device_timestamp = ros::Time::fromBoost(time_request) + delay * 0.5;
56 
57  return time_at_device_timestamp - ros::Duration().fromNSec(device_timestamp * 1e6);
58 }
60  const boost::posix_time::ptime& time_response,
61  const uint64_t& device_timestamp,
62  const ros::Duration& communication_delay,
63  ros::Time& time_at_device_timestamp)
64 {
65  time_at_device_timestamp = ros::Time::fromBoost(time_response) - communication_delay * 0.5;
66 
67  return time_at_device_timestamp - ros::Duration().fromNSec(device_timestamp * 1e6);
68 }
69 } // namespace estimator
70 
71 namespace jump_detector
72 {
73 inline bool detectTimeJump(
74  const ros::Time& last_device_time_origin,
75  const ros::Time& current_device_time_origin,
76  const double allowed_device_time_origin_diff)
77 {
78  if (last_device_time_origin == ros::Time(0))
79  return false;
80 
81  const ros::Duration origin_diff = last_device_time_origin - current_device_time_origin;
82  return std::abs(origin_diff.toSec()) > allowed_device_time_origin_diff;
83 }
84 } // namespace jump_detector
85 } // namespace device_time_origin
86 } // namespace urg_stamped
87 
88 #endif // URG_STAMPED_DEVICE_TIME_ORIGIN_H
DriftedTime(const ros::Time origin, const float gain)
ros::Time estimateOriginByAverage(const boost::posix_time::ptime &time_request, const boost::posix_time::ptime &time_response, const uint64_t &device_timestamp)
bool detectTimeJump(const ros::Time &last_device_time_origin, const ros::Time &current_device_time_origin, const double allowed_device_time_origin_diff)
ros::Time estimateOrigin(const boost::posix_time::ptime &time_response, const uint64_t &device_timestamp, const ros::Duration &communication_delay, ros::Time &time_at_device_timestamp)
Duration & fromNSec(int64_t t)
static Time fromBoost(const boost::posix_time::ptime &t)


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Tue May 11 2021 02:14:05