Skip to content

Allow to set server_name/service_name in RegisterRosAction/RegisterRosService #16

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 15 additions & 3 deletions include/behaviortree_ros/bt_action_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,22 @@ class RosActionNode : public BT::ActionNodeBase
template <class DerivedT> static
void RegisterRosAction(BT::BehaviorTreeFactory& factory,
const std::string& registration_ID,
ros::NodeHandle& node_handle)
ros::NodeHandle& node_handle,
const std::string& default_server_name = {} )
{
NodeBuilder builder = [&node_handle](const std::string& name, const NodeConfiguration& config) {
return std::make_unique<DerivedT>(node_handle, name, config );
NodeBuilder builder = [&node_handle, default_server_name](const std::string& name, const NodeConfiguration& config) {
auto server_name_port = config.input_ports.find("server_name");
if (server_name_port != config.input_ports.end() && !server_name_port->second.empty()) {
return std::make_unique<DerivedT>(node_handle, name, config );
}

if (!default_server_name.empty()) {
auto new_config = config;
new_config.input_ports["server_name"] = default_server_name;
return std::make_unique<DerivedT>(node_handle, name, new_config );
}

throw std::runtime_error("server_name not given as port or default in RegisterRosAction");
};

TreeNodeManifest manifest;
Expand Down
18 changes: 15 additions & 3 deletions include/behaviortree_ros/bt_service_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,22 @@ class RosServiceNode : public BT::SyncActionNode
template <class DerivedT> static
void RegisterRosService(BT::BehaviorTreeFactory& factory,
const std::string& registration_ID,
ros::NodeHandle& node_handle)
ros::NodeHandle& node_handle,
const std::string& default_service_name = {} )
{
NodeBuilder builder = [&node_handle](const std::string& name, const NodeConfiguration& config) {
return std::make_unique<DerivedT>(node_handle, name, config );
NodeBuilder builder = [&node_handle, default_service_name](const std::string& name, const NodeConfiguration& config) {
auto service_name_port = config.input_ports.find("service_name");
if (service_name_port != config.input_ports.end() && !service_name_port->second.empty()) {
return std::make_unique<DerivedT>(node_handle, name, config );
}

if (!default_service_name.empty()) {
auto new_config = config;
new_config.input_ports["service_name"] = default_service_name;
return std::make_unique<DerivedT>(node_handle, name, new_config );
}

throw std::runtime_error("service_name not given as port or default in RegisterRosService");
};

TreeNodeManifest manifest;
Expand Down
10 changes: 4 additions & 6 deletions test/test_bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,13 @@ RosActionNode<behaviortree_ros::FibonacciAction>(handle, name, conf) {}
<root >
<BehaviorTree>
<Sequence>
<AddTwoInts service_name = "add_two_ints"
first_int = "3" second_int = "4"
<AddTwoInts first_int = "3" second_int = "4"
sum = "{add_two_result}" />
<PrintValue message="{add_two_result}"/>

<RetryUntilSuccessful num_attempts="4">
<Timeout msec="300">
<Fibonacci server_name="fibonacci" order="5"
result="{fibonacci_result}" />
<Fibonacci order="5" result="{fibonacci_result}" />
</Timeout>
</RetryUntilSuccessful>
<PrintValue message="{fibonacci_result}"/>
Expand All @@ -186,8 +184,8 @@ int main(int argc, char **argv)
BehaviorTreeFactory factory;

factory.registerNodeType<PrintValue>("PrintValue");
RegisterRosService<AddTwoIntsAction>(factory, "AddTwoInts", nh);
RegisterRosAction<FibonacciServer>(factory, "Fibonacci", nh);
RegisterRosService<AddTwoIntsAction>(factory, "AddTwoInts", nh, "add_two_ints");
RegisterRosAction<FibonacciServer>(factory, "Fibonacci", nh, "fibonacci");

auto tree = factory.createTreeFromText(xml_text);

Expand Down