Skip to content

Commit 19c353b

Browse files
committed
Merge branch 'test/jtc' of github.com:TheDevMystic/ros2_controllers into test/jtc
2 parents 6a98061 + 185e9d1 commit 19c353b

File tree

1 file changed

+42
-69
lines changed

1 file changed

+42
-69
lines changed

joint_trajectory_controller/include/joint_trajectory_controller/interpolation_methods.hpp

Lines changed: 42 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,15 @@
1515
#ifndef JOINT_TRAJECTORY_CONTROLLER__INTERPOLATION_METHODS_HPP_
1616
#define JOINT_TRAJECTORY_CONTROLLER__INTERPOLATION_METHODS_HPP_
1717

18-
#include <cctype>
1918
#include <string>
2019
#include <unordered_map>
2120

21+
#include "hardware_interface/lexical_casts.hpp"
2222
#include "rclcpp/rclcpp.hpp"
2323

2424
namespace joint_trajectory_controller
2525
{
2626

27-
namespace
28-
{
29-
30-
/// Converts a string to lowercase for case-agnostic checking.
31-
inline std::string convert_to_lowercase(const std::string & str)
32-
{
33-
std::string s = str;
34-
for (char & c : s)
35-
{
36-
// C++ std requires the argument passed to std::tolower must be representable as
37-
// unsigned char or equal to EOF.
38-
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
39-
}
40-
return s;
41-
}
42-
43-
} // namespace
44-
4527
/// \brief Setup interpolation_methods' rclcpp::Logger instance.
4628
static const rclcpp::Logger LOGGER =
4729
rclcpp::get_logger("joint_trajectory_controller.interpolation_methods");
@@ -66,9 +48,9 @@ enum class InterpolationMethod
6648
/**
6749
* \brief Uses a variable-degree spline interpolation.
6850
* The degree of the spline is determined dynamically based on the number of
69-
* available deriviatives. This provides a smooth, continuous curve between data points.
51+
* available derivatives. This provides a smooth, continuous curve between data points.
7052
*
71-
* Based on available deriviatives, it uses following degree interpolation,
53+
* Based on available derivatives, it uses the following degree interpolation,
7254
* 1. Neither velocity nor acceleration is available: `Linear Interpolation`.
7355
* 2. Velocity is available, but acceleration is not available: `Cubic Spline Interpolation`.
7456
* 3. Both velocity and acceleration is available: `Quintic Spline Interpolation`.
@@ -78,57 +60,10 @@ enum class InterpolationMethod
7860

7961
/**
8062
* \brief The default interpolation method is set to `InterpolationMethod::VARIABLE_DEGREE_SPLINE`.
81-
* As, it provides most realistic, jerk-free and smooth motion.
63+
* As it provides the most realistic, jerk-free and smooth motion.
8264
*/
8365
const InterpolationMethod DEFAULT_INTERPOLATION = InterpolationMethod::VARIABLE_DEGREE_SPLINE;
8466

85-
/**
86-
* \brief Maps `InterpolationMethod` enum values to their string identifiers.
87-
* This constant map is used to look up the InterpolationMethod for a given
88-
* string (e.g., "splines" for `VARIABLE_DEGREE_SPLINE`).
89-
*/
90-
const std::unordered_map<std::string, InterpolationMethod> InterpolationMethodMap(
91-
{{"none", InterpolationMethod::NONE}, {"splines", InterpolationMethod::VARIABLE_DEGREE_SPLINE}});
92-
93-
/**
94-
* \brief Creates a `InterpolationMethod` enum class value from a string.
95-
* This function looks up `InterpolationMethodMap` for corresponding `InterpolationMethod` based
96-
* on interpolation_method string.
97-
*
98-
* \param[in] interpolation_method The given interpolation method string.
99-
*
100-
* \returns The corresponding InterpolationMethod.
101-
*
102-
* \note If interpolation_method does not have any corresponding InterpolationMethod
103-
* (i.e., "Unknown"), it defaults to `InterpolationMethod::VARIABLE_DEGREE_SPLINE`.
104-
*/
105-
[[nodiscard]] inline InterpolationMethod from_string(const std::string & interpolation_method)
106-
{
107-
// Convert to lowercase, so we have an case-agnostic checking,
108-
// (i.e., None and none, etc are treated same.)
109-
std::string method = convert_to_lowercase(interpolation_method);
110-
111-
// Iterator to InterpolationMethodMap
112-
const auto iterator = InterpolationMethodMap.find(method);
113-
114-
// If interpolation_method exists
115-
if (iterator != InterpolationMethodMap.end())
116-
{
117-
// Return corresponding `InterpolationMethod`
118-
return iterator->second;
119-
}
120-
// Default
121-
else
122-
{
123-
RCLCPP_WARN(
124-
LOGGER,
125-
"Unknown interpolation method parameter '%s' was given. Using the default: "
126-
"VARIABLE_DEGREE_SPLINE.",
127-
interpolation_method.c_str());
128-
return InterpolationMethod::VARIABLE_DEGREE_SPLINE;
129-
}
130-
}
131-
13267
/**
13368
* \brief Returns corresponding string value for the `InterpolationMethod`.
13469
* This function uses simple switch-case lookup to directly assign string value to
@@ -158,6 +93,44 @@ const std::unordered_map<std::string, InterpolationMethod> InterpolationMethodMa
15893
}
15994
}
16095

96+
/**
97+
* \brief Creates a `InterpolationMethod` enum class value from a string.
98+
* This function directly looks up for corresponding `InterpolationMethod` based
99+
* on interpolation_method string (case-agnostic).
100+
*
101+
* \param[in] interpolation_method The given interpolation method string.
102+
*
103+
* \returns The corresponding InterpolationMethod.
104+
*
105+
* \note If interpolation_method does not have any corresponding InterpolationMethod
106+
* (i.e., "Unknown"), it defaults to `DEFAULT_INTERPOLATION`.
107+
*/
108+
[[nodiscard]] inline InterpolationMethod from_string(const std::string & interpolation_method)
109+
{
110+
// Convert to lowercase, so we have a case-agnostic checking,
111+
// (i.e., "None, NONE, and none" are treated same.)
112+
std::string method = ::hardware_interface::to_lower_case(interpolation_method);
113+
114+
if (method == "none")
115+
{
116+
return InterpolationMethod::NONE;
117+
}
118+
else if (method == "splines")
119+
{
120+
return InterpolationMethod::VARIABLE_DEGREE_SPLINE;
121+
}
122+
// Default
123+
else
124+
{
125+
RCLCPP_WARN(
126+
LOGGER,
127+
"Unknown interpolation method parameter '%s' was given. Using the default: "
128+
"DEFAULT_INTERPOLATION (%s).",
129+
interpolation_method.c_str(), to_string(DEFAULT_INTERPOLATION).c_str());
130+
return DEFAULT_INTERPOLATION;
131+
}
132+
}
133+
161134
} // namespace interpolation_methods
162135
} // namespace joint_trajectory_controller
163136

0 commit comments

Comments
 (0)