sas
Modularised monitoring, logging, and control of robots.
Loading...
Searching...
No Matches
sas_common.hpp
1#pragma once
2/*
3# Copyright (c) 2016-2025 Murilo Marques Marinho
4#
5# This file is part of sas_common.
6#
7# sas_common is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Lesser General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# sas_common is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public License
18# along with sas_common. If not, see <https://www.gnu.org/licenses/>.
19#
20# ################################################################
21#
22# Author: Murilo M. Marinho, email: murilomarinho@ieee.org
23#
24# ################################################################
25# 2025.07.10
26# - Removed display_signal_handler_none_bug_info as no longer needed in Jazzy
27# - Added get_ros_optional_parameter
28*/
29
30
31#include <type_traits>
32#include <rclcpp/rclcpp.hpp>
33
34namespace sas
35{
36
37template<typename T>
44void get_ros_parameter(std::shared_ptr<rclcpp::Node>& node, const std::string& parameter_name, T& t)
45{
46 if(!node->has_parameter(parameter_name))
47 node->declare_parameter<T>(parameter_name);
48
49 if(!node->get_parameter(parameter_name,t))
50 {
51 throw std::runtime_error("::Error loading " + parameter_name);
52 }
53}
54
55template<typename T>
63void get_ros_optional_parameter(std::shared_ptr<rclcpp::Node>& node, const std::string& parameter_name, T& t, const T& default_value)
64{
65 if(!node->has_parameter(parameter_name))
66 node->declare_parameter<T>(parameter_name, default_value);
67 node->get_parameter(parameter_name,t);
68}
69
70template<typename T>
76std::string get_type_name(const std::vector<T>&)
77{
78 if constexpr(std::is_same_v<T,std::string>)
79 {
80 return "std::vector<std::string>";
81 }
82 if constexpr(std::is_same_v<T,int64_t>)
83 {
84 return "std::vector<int64_t>";
85 }
86 if constexpr(std::is_same_v<T,double>)
87 {
88 return "std::vector<double>";
89 }
90 if constexpr(std::is_same_v<T,bool>)
91 {
92 return "std::vector<bool>";
93 }
94 return "";
95}
96
97template<typename T>
111void get_ros_parameter(std::shared_ptr<rclcpp::Node>& node, const std::string& parameter_name, std::vector<T>& v)
112{
113 if(!node->has_parameter(parameter_name))
114 {
115 try
116 {
117 node->declare_parameter<std::vector<T>>(parameter_name);
118 }
119 catch (const rclcpp::exceptions::InvalidParameterTypeException& e)
120 {
121 RCLCPP_INFO_STREAM(
122 node->get_logger(),
123 "sas::get_ros_parameter: List "
124 << parameter_name
125 << " does not have expected type, a.k.a "
126 << get_type_name(v)
127 << ", in the parameter server, checking for empty list...");
128 node->declare_parameter<std::vector<std::string>>(parameter_name);
129 }
130 }
131
132 if(node->get_parameter(parameter_name).get_type() == rclcpp::PARAMETER_STRING_ARRAY)
133 {
134 auto v_checker = node->get_parameter(parameter_name).as_string_array();
135 if(v_checker.size() == 1 && v_checker.at(0) == "EMPTY_LIST")
136 {
137 RCLCPP_INFO_STREAM(
138 node->get_logger(),
139 "sas::get_ros_parameter: List "
140 << parameter_name
141 << " was loaded as an EMPTY list of type "
142 << get_type_name(v)
143 << ".");
144 v = std::vector<T>();
145 return;
146 }
147 else
148 {
149 if constexpr (not std::is_same_v<T,std::string>)
150 {
151 throw std::runtime_error("::Was this list, "
152 + parameter_name
153 + ", supposed to be empty? \n "
154 "It should be a list such as list:[\"EMPTY_LIST\"].");
155 }
156 }
157 }
158 if(!node->get_parameter(parameter_name,v))
159 {
160 throw std::runtime_error("::Unable to get parameter " + parameter_name);
161 }
162}
163
164}