moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
rclpy_time_typecaster.hpp
Go to the documentation of this file.
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2022, Shobin Vinod
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Author: Shobin Vinod */
36
37#include <cstdint>
38#include <pybind11/pybind11.h>
39#include <rclcpp/rclcpp.hpp>
40#include <rclcpp/serialization.hpp>
41#include <rclcpp/duration.hpp>
42
43namespace py = pybind11;
44
45namespace pybind11
46{
47namespace detail
48{
49template <>
50struct type_caster<rclcpp::Time>
51{
52 PYBIND11_TYPE_CASTER(rclcpp::Time, _("rclcpp::Time"));
53
54 // convert from rclpy::Time to rclcpp::Time
55 bool load(py::handle src, bool)
56 {
57 if (src.is_none())
58 return false;
59
60 // check to validate if the object is a rclcpp::Time object
61 if (!py::hasattr(src, "nanoseconds") || !py::hasattr(src, "clock_type"))
62 {
63 return false;
64 }
65
66 // Extract the value for constructing the rclcpp::Time object
67 int64_t nanoseconds = src.attr("nanoseconds").cast<int64_t>();
68 int clock_type = src.attr("clock_type").cast<int>();
69
70 // Construct the rclcpp::Time object
71 value = rclcpp::Time(nanoseconds, static_cast<rcl_clock_type_t>(clock_type));
72 return true;
73 }
74
75 // convert from rclcpp::Time to rclpy::Time
76 static py::handle cast(const rclcpp::Time& src, return_value_policy /* policy */, py::handle /* parent */)
77 {
78 py::module rclpy_time = py::module::import("rclpy.time");
79 py::object Time = rclpy_time.attr("Time");
80
81 int64_t nanoseconds = src.nanoseconds();
82 int clock_type = static_cast<int>(src.get_clock_type());
83
84 return Time(py::arg("nanoseconds") = nanoseconds,
85 py::arg("clock_type") = clock_type)
86 .release(); // release the ownership of the object
87 }
88};
89} // namespace detail
90} // namespace pybind11
static py::handle cast(const rclcpp::Time &src, return_value_policy, py::handle)
PYBIND11_TYPE_CASTER(rclcpp::Time, _("rclcpp::Time"))