timestamp_corrector.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Roboception GmbH
3  * All rights reserved
4  *
5  * Author: Heiko Hirschmueller
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its contributors
18  * may be used to endorse or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "timestamp_corrector.h"
35 
36 #include <time.h>
37 
38 namespace rcgccam
39 {
41 {
42  tolerance_ = 2 * 10000000ll;
43  interval_ = 1000000000ll;
44 
45  last_ = 0;
46 
47  accuracy_ = -1;
48  offset_ = 0;
49 }
50 
52 {
53 }
54 
56 {
57  tolerance_ = 2 * tol_ns;
58 }
59 
61 {
62  interval_ = interval_ns;
63 }
64 
65 namespace
66 {
67 inline int64_t getClock(clockid_t id)
68 {
69  struct timespec ts;
70  clock_gettime(id, &ts);
71  return ts.tv_sec * 1000000000ll + ts.tv_nsec;
72 }
73 
74 } // namespace
75 
76 bool TimestampCorrector::determineOffset(const std::shared_ptr<GenApi::CNodeMapRef>& nodemap)
77 {
78  // do nothing if tolerance is negative
79 
80  if (tolerance_ < 0)
81  {
82  return true;
83  }
84 
85  // do nothing if last successful call is not long ago
86 
87  int64_t now_ns = getClock(CLOCK_MONOTONIC);
88 
89  if (accuracy_ > 0 && now_ns - last_ <= interval_)
90  {
91  return true;
92  }
93 
94  // determine offset of host and camera clock
95 
96  last_ = now_ns;
97 
98  int64_t before_ns = 0;
99  int64_t after_ns = 0;
100 
101  accuracy_ = tolerance_ + 1;
102 
103  int n = 3;
104  while (n > 0 && accuracy_ > tolerance_)
105  {
106  before_ns = getClock(CLOCK_REALTIME);
107  rcg::callCommand(nodemap, "TimestampLatch", true);
108  after_ns = getClock(CLOCK_REALTIME);
109 
110  accuracy_ = after_ns - before_ns;
111 
112  n--;
113  }
114 
115  if (accuracy_ <= tolerance_)
116  {
117  int64_t ts = rcg::getInteger(nodemap, "TimestampLatchValue");
118 
119  if (ts == 0)
120  {
121  ts = rcg::getInteger(nodemap, "Timestamp"); // fallback for Matrix Vision USB3 cameras
122  }
123 
124  offset_ = before_ns + (accuracy_ >> 1) - ts;
125 
126  return true;
127  }
128 
129  accuracy_ = -1;
130  offset_ = 0;
131 
132  return false;
133 }
134 
136 {
137  if (tolerance_ >= 0 && accuracy_ >= 0)
138  {
139  int64_t t = static_cast<int64_t>(time.toNSec());
140  time.fromNSec(static_cast<uint64_t>(t + offset_));
141 
142  return accuracy_;
143  }
144 
145  return -1;
146 }
147 
148 } // namespace rcgccam
time.h
rcgccam::TimestampCorrector::tolerance_
int64_t tolerance_
Definition: timestamp_corrector.h:96
timestamp_corrector.h
rcg::callCommand
bool callCommand(const std::shared_ptr< GenApi::CNodeMapRef > &nodemap, const char *name, bool exception=false)
rcgccam
Definition: camerainfolist.cc:40
rcgccam::TimestampCorrector::determineOffset
bool determineOffset(const std::shared_ptr< GenApi::CNodeMapRef > &nodemap)
Determine the offset between the system and camera clock.
Definition: timestamp_corrector.cc:76
rcgccam::TimestampCorrector::correct
int64_t correct(ros::Time &time)
Correct the given camera timestamp to system time.
Definition: timestamp_corrector.cc:135
rcgccam::TimestampCorrector::interval_
int64_t interval_
Definition: timestamp_corrector.h:97
rcgccam::TimestampCorrector::setMaximumTolerance
void setMaximumTolerance(int64_t tol_ns)
Set the maximum tolerance for the offset between system and camera clock.
Definition: timestamp_corrector.cc:55
rcgccam::TimestampCorrector::last_
int64_t last_
Definition: timestamp_corrector.h:99
rcgccam::TimestampCorrector::TimestampCorrector
TimestampCorrector()
Definition: timestamp_corrector.cc:40
rcgccam::TimestampCorrector::accuracy_
int64_t accuracy_
Definition: timestamp_corrector.h:101
TimeBase< Time, Duration >::fromNSec
Time & fromNSec(uint64_t t)
ros::Time
int64_t
__int64 int64_t
rcgccam::TimestampCorrector::setInterval
void setInterval(int64_t interval_ns)
Set minimum time between determination of offset.
Definition: timestamp_corrector.cc:60
TimeBase< Time, Duration >::toNSec
uint64_t toNSec() const
rcgccam::TimestampCorrector::~TimestampCorrector
~TimestampCorrector()
Definition: timestamp_corrector.cc:51
rcg::getInteger
int64_t getInteger(const std::shared_ptr< GenApi::CNodeMapRef > &nodemap, const char *name, int64_t *vmin=0, int64_t *vmax=0, bool exception=false, bool igncache=false)
rcgccam::TimestampCorrector::offset_
int64_t offset_
Definition: timestamp_corrector.h:102


rc_genicam_camera
Author(s): Heiko Hirschmueller
autogenerated on Wed Mar 2 2022 00:49:18