-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/gtest' into parameter_handler
- Loading branch information
Showing
8 changed files
with
489 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
find_package(ament_cmake_gtest REQUIRED) | ||
|
||
find_package(rcl_interfaces REQUIRED) | ||
|
||
ament_add_gtest(${PROJECT_NAME}_test_param test_param.cpp) | ||
target_include_directories(${PROJECT_NAME}_test_param PUBLIC | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
$<INSTALL_INTERFACE:include> | ||
) | ||
ament_target_dependencies(${PROJECT_NAME}_test_param | ||
rclcpp | ||
rclcpp_components | ||
rcl_interfaces | ||
) | ||
|
||
# link the 'camera_component' via '--no-as-needed' to find all plugins automatically via the ClassLoader | ||
target_link_libraries(${PROJECT_NAME}_test_param "-Wl,--no-as-needed" camera_component) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
#include <class_loader/class_loader.hpp> | ||
#include <rclcpp/rclcpp.hpp> | ||
#include <rclcpp_components/node_factory.hpp> | ||
#include <rclcpp_components/node_factory_template.hpp> | ||
|
||
|
||
/** | ||
* @brief instantiate a component as node | ||
* | ||
* Search all linked libraries for the factory of the given component and | ||
* instantiate a node with optional options. | ||
* | ||
* Base on the `linktime_composition` example of the `composition` package: | ||
* https://github.com/ros2/demos/blob/rolling/composition/src/linktime_composition.cpp | ||
* | ||
* @throws class_loader::CreateClassException if component does not exist. | ||
* @throws rclcpp::exceptions::RCLInvalidArgument if `rclcpp::init()` has not been called before | ||
* | ||
* @param component name of the component | ||
* @param options node options | ||
* @return instance of the component as node | ||
*/ | ||
rclcpp_components::NodeInstanceWrapper | ||
instantiate_component(const std::string &component, | ||
const rclcpp::NodeOptions &options = rclcpp::NodeOptions {}) | ||
{ | ||
const std::string node_factory_class_name = "rclcpp_components::NodeFactoryTemplate<" + component + ">"; | ||
|
||
// load all classes from statically or dynamically linked libraries | ||
std::unique_ptr<class_loader::ClassLoader> loader = | ||
std::make_unique<class_loader::ClassLoader>(std::string {}); | ||
|
||
// instantiate the node factory for the component | ||
class_loader::ClassLoader::UniquePtr<rclcpp_components::NodeFactory> node_factory = | ||
loader->createUniqueInstance<rclcpp_components::NodeFactory>(node_factory_class_name); | ||
|
||
// instantiate the node | ||
return node_factory->create_node_instance(options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <rcl_interfaces/msg/log.hpp> | ||
#include <rclcpp/rclcpp.hpp> | ||
#include <regex> | ||
|
||
|
||
class LogClient : public rclcpp::Node | ||
{ | ||
public: | ||
LogClient(const rclcpp::NodeOptions &options, | ||
const std::string &camera_node_name) | ||
: Node("log_client", options), | ||
camera_node_name(camera_node_name) | ||
{ | ||
sub_log = this->create_subscription<rcl_interfaces::msg::Log>( | ||
"/rosout", rclcpp::RosoutQoS {}, | ||
std::bind(&LogClient::on_log, this, std::placeholders::_1)); | ||
} | ||
|
||
void | ||
reset() | ||
{ | ||
msgs.clear(); | ||
} | ||
|
||
bool | ||
regex_search(const std::string &pattern) | ||
{ | ||
std::regex re {pattern}; | ||
for (const std::string &msg : msgs) { | ||
if (std::regex_search(msg, re)) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private: | ||
rclcpp::Subscription<rcl_interfaces::msg::Log>::SharedPtr sub_log; | ||
const std::string camera_node_name; | ||
std::list<std::string> msgs; | ||
|
||
void | ||
on_log(const rcl_interfaces::msg::Log::SharedPtr msg) | ||
{ | ||
if (msg->name == camera_node_name) | ||
msgs.push_back(msg->msg); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <rclcpp/node.hpp> | ||
#include <rclcpp/parameter_client.hpp> | ||
|
||
|
||
class ParamClient | ||
{ | ||
public: | ||
ParamClient(const rclcpp::NodeOptions &options, | ||
const std::string &camera_node_name, | ||
rclcpp::Executor::SharedPtr executor) | ||
: node {rclcpp::Node::make_shared("param_client", options)}, | ||
client {executor, node, camera_node_name} | ||
{ | ||
// | ||
} | ||
|
||
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr | ||
get_node_base_interface() | ||
{ | ||
return node->get_node_base_interface(); | ||
} | ||
|
||
std::vector<std::string> | ||
list_parameters() | ||
{ | ||
return client.list_parameters({}, {}).names; | ||
} | ||
|
||
bool | ||
has_parameter(const std::string ¶meter_name) | ||
{ | ||
return client.has_parameter(parameter_name); | ||
} | ||
|
||
bool | ||
is_set_parameter(const std::string ¶meter_name) | ||
{ | ||
const std::vector<rclcpp::Parameter> parameters = | ||
client.get_parameters({parameter_name}); | ||
if (parameters.empty()) | ||
return false; | ||
return parameters.front().get_type() != rclcpp::PARAMETER_NOT_SET; | ||
} | ||
|
||
std::vector<rclcpp::Parameter> | ||
get_parameters(const std::vector<std::string> ¶meter_names) | ||
{ | ||
return client.get_parameters(parameter_names); | ||
} | ||
|
||
std::vector<rcl_interfaces::msg::SetParametersResult> | ||
set_parameters(const std::vector<rclcpp::Parameter> ¶meters) | ||
{ | ||
return client.set_parameters(parameters); | ||
} | ||
|
||
rcl_interfaces::msg::SetParametersResult | ||
set_parameters_atomically(const std::vector<rclcpp::Parameter> ¶meters) | ||
{ | ||
return client.set_parameters_atomically(parameters); | ||
} | ||
|
||
private: | ||
rclcpp::Node::SharedPtr node; | ||
rclcpp::SyncParametersClient client; | ||
}; |
Oops, something went wrong.