Program Listing for File conversions.hpp

Return to documentation for file (include/rp_utils/conversions.hpp)

#ifndef RP_UTILS_CONVERSIONS_HPP_
#define RP_UTILS_CONVERSIONS_HPP_

#include <rclcpp/duration.hpp>

namespace rp_utils
{

namespace time
{

template <typename T> rclcpp::Duration FloatingToDuration(T time_in_seconds)
{
    static_assert(std::is_floating_point<T>::value, "Template parameter must be a floating-point type");

    int32_t seconds = static_cast<int32_t>(time_in_seconds);
    int32_t nanoseconds = static_cast<int32_t>((time_in_seconds - seconds) * 1e9);

    return rclcpp::Duration(seconds, nanoseconds);
}

} // namespace time
} // namespace rp_utils

#endif // RP_UTILS_CONVERSIONS_HPP_