Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions example/src/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ admittance_controller:
default_value: 1.0,
description: "derivative gain term"
}
fixed_string: {
type: string_fixed_25,
default_value: "string_value",
description: "test code generation for fixed sized string",
read_only: true
}
command_interfaces:
{
type: string_array,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ descriptor.read_only = {{parameter_read_only}};
auto parameter = rclcpp::ParameterType::PARAMETER_{{parameter_type}};
{% endif -%}
{%- if default_value|length %}
auto parameter = rclcpp::ParameterValue(params_.{{parameter_name}});
auto parameter = rclcpp::ParameterValue(params_.{{parameter_value}});
{% endif -%}
parameters_interface_->declare_parameter("{{parameter_name}}", parameter, descriptor);
{% endfilter -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include <string>
#include <type_traits>
#include <vector>
#include <string_view>
#include <array>


#include <fmt/core.h>
#include <fmt/format.h>
Expand All @@ -24,6 +27,24 @@ namespace {{namespace}} {
{%- endif -%}
{% endfilter -%}

template<size_t S>
class FixedSizeString {
public:
FixedSizeString() = default;
FixedSizeString(const std::string& str) {
size_t len = std::min(str.size(), S);
std::copy(str.cbegin(), str.cbegin()+len, data_.begin());
view_ = std::string_view(data_.data(), len);
}

operator std::string_view() const {
return view_;
}

private:
std::string_view view_;
std::array<char, S> data_;
};

{%- filter indent(width=4) %}
struct Params {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def is_mapped_parameter(param_name: str):
return param_name.__contains__("__map_")


@typechecked
def fixed_type(yaml_type: str):
tmp = yaml_type.split("_")
if len(tmp) < 3:
return False
return tmp[-2] == "fixed" and tmp[-1].isdigit()


@typechecked
def validate_type(defined_type: str, value):
type_translation = {
Expand Down Expand Up @@ -131,6 +139,13 @@ def str_to_str(s: Optional[str]):
return '"%s"' % s


@typechecked
def str_to_fixed_str(s: Optional[str], size: int):
if s is None:
return None
return f'FixedSizeString<{size}>("{s}")'


# cpp_type, val_to_cpp_str, parameter_conversion
@typechecked
def cpp_type_from_defined_type(yaml_type: str) -> str:
Expand All @@ -144,6 +159,8 @@ def cpp_type_from_defined_type(yaml_type: str) -> str:
cpp_type = "std::vector<bool>"
elif yaml_type == "string":
cpp_type = "std::string"
elif yaml_type.__contains__("string_fixed"):
cpp_type = "std::string_view"
elif yaml_type == "double":
cpp_type = "double"
elif yaml_type == "int":
Expand Down Expand Up @@ -193,6 +210,10 @@ def cpp_str_func_from_defined_type(yaml_type: str):
val_to_cpp_str = bool_to_str
elif yaml_type == "string":
val_to_cpp_str = str_to_str
elif yaml_type.__contains__("string_fixed"):
tmp = yaml_type.split("_")
size = int(tmp[-1])
val_to_cpp_str = lambda str_val: str_to_fixed_str(str_val, size)
elif yaml_type == "double":
val_to_cpp_str = float_to_str
elif yaml_type == "int":
Expand Down Expand Up @@ -233,7 +254,7 @@ def get_parameter_as_function_str(yaml_type: str) -> str:
parameter_conversion = "as_integer_array()"
elif yaml_type == "bool_array":
parameter_conversion = "as_bool_array()"
elif yaml_type == "string":
elif yaml_type == "string" or yaml_type.__contains__("string_fixed"):
parameter_conversion = "as_string()"
elif yaml_type == "double":
parameter_conversion = "as_double()"
Expand Down Expand Up @@ -314,6 +335,8 @@ def get_dynamic_parameter_map(yaml_parameter_name: str):


# Each template has a corresponding class with the str filling in the template with jinja


class DeclareParameter:
@typechecked
def __init__(
Expand All @@ -337,8 +360,14 @@ def __str__(self):
else:
default_value = ""

if fixed_type(self.parameter_type):
parameter_value = self.parameter_name + ".data()"
else:
parameter_value = self.parameter_name

data = {
"parameter_name": self.parameter_name,
"parameter_value": parameter_value,
"parameter_type": parameter_type,
"parameter_description": self.parameter_description,
"parameter_read_only": bool_to_str(self.parameter_read_only),
Expand Down Expand Up @@ -833,6 +862,7 @@ def parse_params(self, name, value, nested_name_list):
self.declare_parameter_sets.append(declare_parameter_set)

def parse_dict(self, name, root_map, nested_name):

if isinstance(root_map, dict) and isinstance(
next(iter(root_map.values())), dict
):
Expand Down