ros2_control - foxy
Loading...
Searching...
No Matches
realtime_clock.h
1/*
2 * Copyright (c) 2013 hiDOF, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Willow Garage, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * Publishing ROS messages is difficult, as the publish function is
32 * not realtime safe. This class provides the proper locking so that
33 * you can call publish in realtime and a separate (non-realtime)
34 * thread will ensure that the message gets published over ROS.
35 *
36 * Author: Wim Meeussen
37 */
38
39#ifndef REALTIME_TOOLS__REALTIME_CLOCK_H_
40#define REALTIME_TOOLS__REALTIME_CLOCK_H_
41
42#include <mutex>
43#include <thread>
44
45#include "rclcpp/clock.hpp"
46#include "rclcpp/duration.hpp"
47#include "rclcpp/logger.hpp"
48#include "rclcpp/time.hpp"
49
50namespace realtime_tools
51{
52
54{
55public:
60
64 explicit RealtimeClock(rclcpp::Clock::SharedPtr clock);
65
69 RealtimeClock(rclcpp::Clock::SharedPtr clock, rclcpp::Logger logger);
70
72
77 [[deprecated]] rclcpp::Time getSystemTime(const rclcpp::Time & realtime_time = rclcpp::Time());
78
84 rclcpp::Time now(const rclcpp::Time & realtime_time = rclcpp::Time());
85
86private:
87 void loop();
88
89 rclcpp::Clock::SharedPtr clock_;
90 rclcpp::Logger logger_;
91 unsigned int lock_misses_ = 0;
92 rclcpp::Time system_time_;
93 rclcpp::Duration clock_offset_{0, 0u};
94
95 rclcpp::Time last_realtime_time_;
96 bool running_ = false;
97 bool initialized_ = false;
98 std::mutex mutex_;
99 std::thread thread_;
100}; // class
101
102} // namespace realtime_tools
103#endif // REALTIME_TOOLS__REALTIME_CLOCK_H_
Definition realtime_clock.h:54
RealtimeClock()
Definition realtime_clock.cpp:48
rclcpp::Time getSystemTime(const rclcpp::Time &realtime_time=rclcpp::Time())
rclcpp::Time now(const rclcpp::Time &realtime_time=rclcpp::Time())
Definition realtime_clock.cpp:69
Definition realtime_box.h:39