Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read only parameters #495

Merged
merged 48 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
73553ec
in progress broken test_time_source
sloretz Jun 8, 2018
88a74ff
style
sloretz Jun 8, 2018
bc8457d
test undeclared params
sloretz Jun 8, 2018
a6fc8de
Only get parameter if it is set
sloretz Jun 8, 2018
7e19756
doc fixup
wjwwood Oct 18, 2018
2597f46
use override rather than virtual in places
wjwwood Oct 18, 2018
347449d
rename ParameterInfo_t to ParameterInfo and just use struct, no typedef
wjwwood Oct 18, 2018
304ba62
add method to access ParameterValue within a Parameter
wjwwood Oct 18, 2018
6e5aae9
enable get<Parameter> and get<ParameterValue> on Parameter class
wjwwood Oct 18, 2018
38c7670
avoid const pass by value
wjwwood Oct 18, 2018
c11654e
match type of enum in C++ to type used in message definition
wjwwood Oct 18, 2018
b3325aa
fixup after rebase
wjwwood Jan 30, 2019
189c02e
more fixup after rebase
wjwwood Jan 30, 2019
85b0377
replace create_parameter with declare_parameter
wjwwood Feb 1, 2019
598325d
provide implementation for templated declare_parameter method
wjwwood Feb 1, 2019
6b14049
style
wjwwood Feb 4, 2019
46c207f
do not use const reference when it's a primitive (like bool)
wjwwood Feb 22, 2019
8e28200
typo
wjwwood Feb 22, 2019
5273ad9
follow to bool change that wasn't staged
wjwwood Feb 23, 2019
06ddfc4
fixup tests
wjwwood Feb 23, 2019
04f812c
added lots of docs, alternative API signatures, and some of the tests
wjwwood Apr 10, 2019
206ffe3
more tests and associated fixes
wjwwood Apr 11, 2019
89018de
address documentation feedback
wjwwood Apr 12, 2019
9e03b08
fixup previously added tests
wjwwood Apr 15, 2019
ac1b6a6
add tests and fixes for describe_parameter(s) and get_parameter_types
wjwwood Apr 15, 2019
73b4c28
remove old parameter tests
wjwwood Apr 16, 2019
78a94d9
use const reference where possible
wjwwood Apr 16, 2019
bf1d149
address comments
wjwwood Apr 16, 2019
00da0a6
fix tests for deprecated methods
wjwwood Apr 16, 2019
570c7c2
address feedback
wjwwood Apr 17, 2019
3ef385d
significantly improve the reliability of the time_source tests
wjwwood Apr 18, 2019
01362ff
uncrustify, cpplint, and cppcheck fixes
wjwwood Apr 18, 2019
c48db2d
Revert "significantly improve the reliability of the time_source tests"
wjwwood Apr 18, 2019
4673ce9
Merge remote-tracking branch 'origin/master' into read_only_parameters
wjwwood Apr 18, 2019
ce9af32
only declare use_sim_time parameter if not already declared
wjwwood Apr 18, 2019
fb78acd
fixup rclcpp_lifecycle
wjwwood Apr 19, 2019
b7e4c0d
fixup tests
wjwwood Apr 19, 2019
efb3f85
add missing namespace scope which fails on Windows
wjwwood Apr 19, 2019
4ba4eeb
extend deprecation warning suppression to support Windows too
wjwwood Apr 19, 2019
cb72e58
fix compiler warnings and missing visibility macro
wjwwood Apr 19, 2019
7e79bba
remove commented left over tests
wjwwood Apr 19, 2019
933ac09
fix compiler warning on Windows
wjwwood Apr 19, 2019
444de25
suppress deprecation warning on include of file in Windows
wjwwood Apr 19, 2019
df7b036
avoid potential loss of data warning converting int64_t to int
wjwwood Apr 19, 2019
ea3285f
trying to fix more loss of data warnings
wjwwood Apr 20, 2019
68a4afe
fix test_node
wjwwood Apr 22, 2019
d9ad096
add option to automatically declare parameters from initial parameter…
wjwwood Apr 23, 2019
e29cdf1
remove redundant conditional
wjwwood Apr 23, 2019
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
27 changes: 24 additions & 3 deletions rclcpp/include/rclcpp/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,35 @@ class EventNotRegisteredError : public std::runtime_error
class InvalidParametersException : public std::runtime_error
{
public:
// Inherit constructors from runtime_error;
// Inherit constructors from runtime_error.
using std::runtime_error::runtime_error;
};

/// Throwing if passed parameter value is invalid.
/// Thrown if passed parameter value is invalid.
class InvalidParameterValueException : public std::runtime_error
{
// Inherit constructors from runtime_error;
// Inherit constructors from runtime_error.
using std::runtime_error::runtime_error;
};

/// Thrown if parameter is already declared.
class ParameterAlreadyDeclaredException : public std::runtime_error
{
// Inherit constructors from runtime_error.
using std::runtime_error::runtime_error;
};

/// Thrown if parameter is not declared, e.g. either set or get was called without first declaring.
class ParameterNotDeclaredException : public std::runtime_error
{
// Inherit constructors from runtime_error.
using std::runtime_error::runtime_error;
};

/// Thrown if parameter is immutable and therefore cannot be undeclared.
class ParameterImmutableException : public std::runtime_error
{
// Inherit constructors from runtime_error.
using std::runtime_error::runtime_error;
};

Expand Down
535 changes: 493 additions & 42 deletions rclcpp/include/rclcpp/node.hpp

Large diffs are not rendered by default.

133 changes: 90 additions & 43 deletions rclcpp/include/rclcpp/node_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,58 @@ Node::create_service(
group);
}

template<typename CallbackT>
void
Node::register_param_change_callback(CallbackT && callback)
template<typename ParameterT>
ParameterT
Node::declare_parameter(
const std::string & name,
const ParameterT & default_value,
rcl_interfaces::msg::ParameterDescriptor parameter_descriptor)
{
this->node_parameters_->register_param_change_callback(std::forward<CallbackT>(callback));
return this->declare_parameter(
name,
rclcpp::ParameterValue(default_value),
parameter_descriptor
).get<ParameterT>();
}

template<typename ParameterT>
std::vector<ParameterT>
Node::declare_parameters(
const std::string & namespace_,
const std::map<std::string, ParameterT> & parameters)
{
std::vector<ParameterT> result;
std::string normalized_namespace = namespace_.empty() ? "" : (namespace_ + ".");
std::transform(
parameters.begin(), parameters.end(), std::back_inserter(result),
[this, &normalized_namespace](auto element) {
return this->declare_parameter(normalized_namespace + element.first, element.second);
}
);
return result;
}

template<typename ParameterT>
std::vector<ParameterT>
Node::declare_parameters(
const std::string & namespace_,
const std::map<
std::string,
std::pair<ParameterT, rcl_interfaces::msg::ParameterDescriptor>
> & parameters)
{
std::vector<ParameterT> result;
std::string normalized_namespace = namespace_.empty() ? "" : (namespace_ + ".");
std::transform(
parameters.begin(), parameters.end(), std::back_inserter(result),
[this, &normalized_namespace](auto element) {
return this->declare_parameter(
normalized_namespace + element.first,
element.second.first,
element.second.second);
}
);
return result;
}

template<typename ParameterT>
Expand All @@ -234,33 +281,26 @@ Node::set_parameter_if_not_set(
const std::string & name,
const ParameterT & value)
{
std::string parameter_name_with_sub_namespace =
extend_name_with_sub_namespace(name, this->get_sub_namespace());

rclcpp::Parameter parameter;
if (!this->get_parameter(parameter_name_with_sub_namespace, parameter)) {
this->set_parameters({
rclcpp::Parameter(parameter_name_with_sub_namespace, value),
});
if (!this->has_parameter(name)) {
this->set_parameter(rclcpp::Parameter(name, value));
}
}

// this is a partially-specialized version of set_parameter_if_not_set above,
// where our concrete type for ParameterT is std::map, but the to-be-determined
// type is the value in the map.
template<typename MapValueT>
template<typename ParameterT>
void
Node::set_parameters_if_not_set(
const std::string & name,
const std::map<std::string, MapValueT> & values)
const std::map<std::string, ParameterT> & values)
{
std::vector<rclcpp::Parameter> params;

for (const auto & val : values) {
std::string param_name = name + "." + val.first;
rclcpp::Parameter parameter;
if (!this->get_parameter(param_name, parameter)) {
params.push_back(rclcpp::Parameter(param_name, val.second));
std::string parameter_name = name + "." + val.first;
if (!this->has_parameter(parameter_name)) {
params.push_back(rclcpp::Parameter(parameter_name, val.second));
}
}

Expand All @@ -269,56 +309,56 @@ Node::set_parameters_if_not_set(

template<typename ParameterT>
bool
Node::get_parameter(const std::string & name, ParameterT & value) const
Node::get_parameter(const std::string & name, ParameterT & parameter) const
{
std::string sub_name = extend_name_with_sub_namespace(name, this->get_sub_namespace());

rclcpp::Parameter parameter;
rclcpp::Parameter parameter_variant;

bool result = get_parameter(sub_name, parameter);
bool result = get_parameter(sub_name, parameter_variant);
if (result) {
value = parameter.get_value<ParameterT>();
parameter = parameter_variant.get_value<ParameterT>();
}

return result;
}

template<typename ParameterT>
bool
Node::get_parameter_or(
const std::string & name,
ParameterT & parameter,
const ParameterT & alternative_value) const
{
std::string sub_name = extend_name_with_sub_namespace(name, this->get_sub_namespace());

bool got_parameter = get_parameter(sub_name, parameter);
if (!got_parameter) {
parameter = alternative_value;
}
return got_parameter;
}

// this is a partially-specialized version of get_parameter above,
// where our concrete type for ParameterT is std::map, but the to-be-determined
// type is the value in the map.
template<typename MapValueT>
template<typename ParameterT>
bool
Node::get_parameters(
const std::string & name,
std::map<std::string, MapValueT> & values) const
const std::string & prefix,
std::map<std::string, ParameterT> & values) const
{
std::map<std::string, rclcpp::Parameter> params;
bool result = node_parameters_->get_parameters_by_prefix(name, params);
bool result = node_parameters_->get_parameters_by_prefix(prefix, params);
if (result) {
for (const auto & param : params) {
values[param.first] = param.second.get_value<MapValueT>();
values[param.first] = param.second.get_value<ParameterT>();
}
}

return result;
}

template<typename ParameterT>
bool
Node::get_parameter_or(
const std::string & name,
ParameterT & value,
const ParameterT & alternative_value) const
{
std::string sub_name = extend_name_with_sub_namespace(name, this->get_sub_namespace());

bool got_parameter = get_parameter(sub_name, value);
if (!got_parameter) {
value = alternative_value;
}
return got_parameter;
}

template<typename ParameterT>
void
Node::get_parameter_or_set(
Expand All @@ -337,6 +377,13 @@ Node::get_parameter_or_set(
}
}

template<typename CallbackT>
void
Node::register_param_change_callback(CallbackT && callback)
{
this->node_parameters_->register_param_change_callback(std::forward<CallbackT>(callback));
}

} // namespace rclcpp

#endif // RCLCPP__NODE_IMPL_HPP_
73 changes: 50 additions & 23 deletions rclcpp/include/rclcpp/node_interfaces/node_parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ namespace rclcpp
namespace node_interfaces
{

// Internal struct for holding useful info about parameters
struct ParameterInfo
{
/// Current value of the parameter.
rclcpp::ParameterValue value;

/// A description of the parameter
rcl_interfaces::msg::ParameterDescriptor descriptor;
};

/// Implementation of the NodeParameters part of the Node API.
class NodeParameters : public NodeParametersInterface
{
Expand All @@ -49,90 +59,107 @@ class NodeParameters : public NodeParametersInterface
RCLCPP_PUBLIC
NodeParameters(
const node_interfaces::NodeBaseInterface::SharedPtr node_base,
const node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
const node_interfaces::NodeTopicsInterface::SharedPtr node_topics,
const node_interfaces::NodeServicesInterface::SharedPtr node_services,
const node_interfaces::NodeClockInterface::SharedPtr node_clock,
const std::vector<Parameter> & initial_parameters,
bool use_intra_process,
bool start_parameter_services,
bool start_parameter_event_publisher,
const rmw_qos_profile_t & parameter_event_qos_profile);
const rmw_qos_profile_t & parameter_event_qos_profile,
bool allow_undeclared_parameters);

RCLCPP_PUBLIC
virtual
~NodeParameters();

RCLCPP_PUBLIC
virtual
const rclcpp::ParameterValue &
declare_parameter(
const std::string & name,
const rclcpp::ParameterValue & default_value,
rcl_interfaces::msg::ParameterDescriptor parameter_descriptor) override;

RCLCPP_PUBLIC
void
undeclare_parameter(const std::string & name) override;

RCLCPP_PUBLIC
bool
has_parameter(const std::string & name) const override;

RCLCPP_PUBLIC
std::vector<rcl_interfaces::msg::SetParametersResult>
set_parameters(
const std::vector<rclcpp::Parameter> & parameters);
const std::vector<rclcpp::Parameter> & parameters) override;

RCLCPP_PUBLIC
virtual
rcl_interfaces::msg::SetParametersResult
set_parameters_atomically(
const std::vector<rclcpp::Parameter> & parameters);
const std::vector<rclcpp::Parameter> & parameters) override;

RCLCPP_PUBLIC
virtual
std::vector<rclcpp::Parameter>
get_parameters(const std::vector<std::string> & names) const;
get_parameters(const std::vector<std::string> & names) const override;

RCLCPP_PUBLIC
virtual
rclcpp::Parameter
get_parameter(const std::string & name) const;
get_parameter(const std::string & name) const override;

RCLCPP_PUBLIC
virtual
bool
get_parameter(
const std::string & name,
rclcpp::Parameter & parameter) const;
rclcpp::Parameter & parameter) const override;

RCLCPP_PUBLIC
virtual
bool
get_parameters_by_prefix(
const std::string & prefix,
std::map<std::string, rclcpp::Parameter> & parameters) const;
std::map<std::string, rclcpp::Parameter> & parameters) const override;

RCLCPP_PUBLIC
virtual
std::vector<rcl_interfaces::msg::ParameterDescriptor>
describe_parameters(const std::vector<std::string> & names) const;
describe_parameters(const std::vector<std::string> & names) const override;

RCLCPP_PUBLIC
virtual
std::vector<uint8_t>
get_parameter_types(const std::vector<std::string> & names) const;
get_parameter_types(const std::vector<std::string> & names) const override;

RCLCPP_PUBLIC
virtual
rcl_interfaces::msg::ListParametersResult
list_parameters(const std::vector<std::string> & prefixes, uint64_t depth) const;
list_parameters(const std::vector<std::string> & prefixes, uint64_t depth) const override;

RCLCPP_PUBLIC
virtual
OnParametersSetCallbackType
set_on_parameters_set_callback(OnParametersSetCallbackType callback) override;

[[deprecated("use set_on_parameters_set_callback() instead")]]
RCLCPP_PUBLIC
void
register_param_change_callback(ParametersCallbackFunction callback);
register_param_change_callback(OnParametersSetCallbackType callback) override;

private:
RCLCPP_DISABLE_COPY(NodeParameters)

mutable std::mutex mutex_;

ParametersCallbackFunction parameters_callback_ = nullptr;
OnParametersSetCallbackType on_parameters_set_callback_ = nullptr;

std::map<std::string, ParameterInfo> parameters_;

std::map<std::string, rclcpp::ParameterValue> initial_parameter_values_;

std::map<std::string, rclcpp::Parameter> parameters_;
bool allow_undeclared_ = false;

Publisher<rcl_interfaces::msg::ParameterEvent>::SharedPtr events_publisher_;

std::shared_ptr<ParameterService> parameter_service_;

std::string combined_name_;

node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
node_interfaces::NodeClockInterface::SharedPtr node_clock_;
};

Expand Down
Loading