Skip to content

Commit d0dad84

Browse files
committed
Squashed 'src/example_behaviors/' content from commit d1451e3
git-subtree-dir: src/example_behaviors git-subtree-split: d1451e331ba27aee0af9bbaafaefc8bf37a48aca
0 parents  commit d0dad84

27 files changed

+1548
-0
lines changed

CMakeLists.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(example_behaviors CXX)
3+
4+
find_package(moveit_studio_common REQUIRED)
5+
find_package(example_interfaces REQUIRED)
6+
moveit_studio_package()
7+
8+
set(THIS_PACKAGE_INCLUDE_DEPENDS moveit_studio_behavior_interface pluginlib example_interfaces)
9+
foreach(package IN ITEMS ${THIS_PACKAGE_INCLUDE_DEPENDS})
10+
find_package(${package} REQUIRED)
11+
endforeach()
12+
13+
add_library(
14+
example_behaviors
15+
SHARED
16+
src/add_two_ints_service_client.cpp
17+
src/delayed_message.cpp
18+
src/get_string_from_topic.cpp
19+
src/fibonacci_action_client.cpp
20+
src/hello_world.cpp
21+
src/publish_color_rgba.cpp
22+
src/setup_mtc_pick_from_pose.cpp
23+
src/setup_mtc_place_from_pose.cpp
24+
src/setup_mtc_wave_hand.cpp
25+
src/register_behaviors.cpp)
26+
target_include_directories(
27+
example_behaviors
28+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
29+
$<INSTALL_INTERFACE:include>)
30+
ament_target_dependencies(example_behaviors
31+
${THIS_PACKAGE_INCLUDE_DEPENDS})
32+
33+
# Install Libraries
34+
install(
35+
TARGETS example_behaviors
36+
EXPORT example_behaviorsTargets
37+
ARCHIVE DESTINATION lib
38+
LIBRARY DESTINATION lib
39+
RUNTIME DESTINATION bin
40+
INCLUDES
41+
DESTINATION include)
42+
43+
install(DIRECTORY config DESTINATION share/${PROJECT_NAME})
44+
45+
if(BUILD_TESTING)
46+
moveit_pro_behavior_test(example_behaviors)
47+
endif()
48+
49+
# Export the behavior plugins defined in this package so they are available to
50+
# plugin loaders that load the behavior base class library from the
51+
# moveit_studio_behavior package.
52+
pluginlib_export_plugin_description_file(
53+
moveit_studio_behavior_interface example_behaviors_plugin_description.xml)
54+
55+
ament_export_targets(example_behaviorsTargets HAS_LIBRARY_TARGET)
56+
ament_export_dependencies(${THIS_PACKAGE_INCLUDE_DEPENDS})
57+
ament_package()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# example_behaviors

behavior_plugin.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
objectives:
2+
behavior_loader_plugins:
3+
example_behaviors:
4+
- "example_behaviors::ExampleBehaviorsBehaviorsLoader"

config/tree_nodes_model.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<root>
3+
<TreeNodesModel>
4+
<!-- Include additional SubTree metadata in this file. -->
5+
</TreeNodesModel>
6+
</root>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<library path="example_behaviors">
3+
<class
4+
type="example_behaviors::ExampleBehaviorsBehaviorsLoader"
5+
base_class_type="moveit_studio::behaviors::SharedResourcesNodeLoaderBase"
6+
/>
7+
</library>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
3+
#include <moveit_studio_behavior_interface/service_client_behavior_base.hpp>
4+
#include <example_interfaces/srv/add_two_ints.hpp>
5+
6+
using moveit_studio::behaviors::BehaviorContext;
7+
using moveit_studio::behaviors::ServiceClientBehaviorBase;
8+
using AddTwoInts = example_interfaces::srv::AddTwoInts;
9+
10+
namespace example_behaviors
11+
{
12+
class AddTwoIntsServiceClient final : public ServiceClientBehaviorBase<AddTwoInts>
13+
{
14+
public:
15+
AddTwoIntsServiceClient(const std::string& name, const BT::NodeConfiguration& config,
16+
const std::shared_ptr<BehaviorContext>& shared_resources);
17+
18+
/** @brief Implementation of the required providedPorts() function for the hello_world Behavior. */
19+
static BT::PortsList providedPorts();
20+
21+
/**
22+
* @brief Implementation of the metadata() function for displaying metadata, such as Behavior description and
23+
* subcategory, in the MoveIt Studio Developer Tool.
24+
* @return A BT::KeyValueVector containing the Behavior metadata.
25+
*/
26+
static BT::KeyValueVector metadata();
27+
28+
private:
29+
/** @brief User-provided function to get the name of the service when initializing the service client. */
30+
tl::expected<std::string, std::string> getServiceName() override;
31+
32+
/**
33+
* @brief User-provided function to create the service request.
34+
* @return Returns a service request message. If not successful, returns an error message. Note that the criteria for
35+
* success or failure is defined by the user's implementation of this function.
36+
*/
37+
tl::expected<AddTwoInts::Request, std::string> createRequest() override;
38+
39+
/** @brief Optional user-provided function to process the service response after the service has finished. */
40+
tl::expected<bool, std::string> processResponse(const AddTwoInts::Response& response) override;
41+
42+
/** @brief Classes derived from AsyncBehaviorBase must implement getFuture() so that it returns a shared_future class member */
43+
std::shared_future<tl::expected<bool, std::string>>& getFuture() override
44+
{
45+
return future_;
46+
}
47+
48+
/** @brief Classes derived from AsyncBehaviorBase must have this shared_future as a class member */
49+
std::shared_future<tl::expected<bool, std::string>> future_;
50+
};
51+
} // namespace example_behaviors
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#pragma once
2+
3+
#include <behaviortree_cpp/action_node.h>
4+
5+
// This header includes the SharedResourcesNode type
6+
#include <moveit_studio_behavior_interface/shared_resources_node.hpp>
7+
8+
#include <moveit_msgs/msg/move_it_error_codes.hpp>
9+
10+
#include <moveit_studio_behavior_interface/check_for_error.hpp>
11+
12+
namespace example_behaviors
13+
{
14+
/**
15+
* @brief DelayedMessage will use FailureLoggerROS to log a "Hello World" message after the duration specified on the input port
16+
*/
17+
class DelayedMessage : public moveit_studio::behaviors::SharedResourcesNode<BT::StatefulActionNode>
18+
{
19+
private:
20+
std::chrono::time_point<std::chrono::steady_clock> start_time_;
21+
double delay_duration_;
22+
23+
public:
24+
/**
25+
* @brief Constructor for the delayed_message behavior.
26+
* @param name The name of a particular instance of this Behavior. This will be set by the behavior tree factory when
27+
* this Behavior is created within a new behavior tree.
28+
* @param shared_resources A shared_ptr to a BehaviorContext that is shared among all SharedResourcesNode Behaviors in
29+
* the behavior tree. This BehaviorContext is owned by the Studio Agent's ObjectiveServerNode.
30+
* @param config This contains runtime configuration info for this Behavior, such as the mapping between the
31+
* Behavior's data ports on the behavior tree's blackboard. This will be set by the behavior tree factory when this
32+
* Behavior is created within a new behavior tree.
33+
* @details An important limitation is that the members of the base Behavior class are not instantiated until after
34+
* the initialize() function is called, so these classes should not be used within the constructor.
35+
*/
36+
DelayedMessage(const std::string& name, const BT::NodeConfiguration& config,
37+
const std::shared_ptr<moveit_studio::behaviors::BehaviorContext>& shared_resources);
38+
39+
/**
40+
* @brief Implementation of the required providedPorts() function for the delayed_message Behavior.
41+
* @details The BehaviorTree.CPP library requires that Behaviors must implement a static function named
42+
* providedPorts() which defines their input and output ports. If the Behavior does not use any ports, this function
43+
* must return an empty BT::PortsList. This function returns a list of ports with their names and port info, which is
44+
* used internally by the behavior tree.
45+
* @return If delayed_message does not expose any ports, this function returns an empty list.
46+
*/
47+
static BT::PortsList providedPorts();
48+
49+
/**
50+
* @brief Implementation of the metadata() function for displaying metadata, such as Behavior description and
51+
* subcategory, in the MoveIt Studio Developer Tool.
52+
* @return A BT::KeyValueVector containing the Behavior metadata.
53+
*/
54+
static BT::KeyValueVector metadata();
55+
56+
/**
57+
* @brief Implementation of onStart(). Runs when the Behavior is ticked for the first time.
58+
* @return Always returns BT::NodeStatus::RUNNING, since the success of Behavior's initialization is checked in @ref
59+
* onRunning().
60+
*/
61+
BT::NodeStatus onStart() override;
62+
63+
/**
64+
* @brief Implementation of onRunning(). Checks the status of the Behavior when it is ticked after it starts running.
65+
* @return BT::NodeStatus::RUNNING, BT::NodeStatus::SUCCESS, or BT::NodeStatus::FAILURE depending on the Behavior status.
66+
*/
67+
BT::NodeStatus onRunning() override;
68+
69+
void onHalted() override;
70+
};
71+
} // namespace example_behaviors
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <moveit_studio_behavior_interface/action_client_behavior_base.hpp>
4+
#include <example_interfaces/action/fibonacci.hpp>
5+
6+
using moveit_studio::behaviors::ActionClientBehaviorBase;
7+
using moveit_studio::behaviors::BehaviorContext;
8+
using Fibonacci = example_interfaces::action::Fibonacci;
9+
10+
namespace example_behaviors
11+
{
12+
class FibonacciActionClient final : public ActionClientBehaviorBase<Fibonacci>
13+
{
14+
public:
15+
FibonacciActionClient(const std::string& name, const BT::NodeConfiguration& config,
16+
const std::shared_ptr<BehaviorContext>& shared_resources);
17+
18+
/** @brief Implementation of the required providedPorts() function for the hello_world Behavior. */
19+
static BT::PortsList providedPorts();
20+
21+
/**
22+
* @brief Implementation of the metadata() function for displaying metadata, such as Behavior description and
23+
* subcategory, in the MoveIt Studio Developer Tool.
24+
* @return A BT::KeyValueVector containing the Behavior metadata.
25+
*/
26+
static BT::KeyValueVector metadata();
27+
28+
private:
29+
/** @brief User-provided function to get the name of the action when initializing the action client. */
30+
tl::expected<std::string, std::string> getActionName() override;
31+
32+
/** @brief User-provided function to create the action goal before sending the action goal request. */
33+
tl::expected<Fibonacci::Goal, std::string> createGoal() override;
34+
35+
/** @brief Optional user-provided function to process the action result after the action has finished. */
36+
tl::expected<bool, std::string> processResult(const std::shared_ptr<Fibonacci::Result> result) override;
37+
38+
/** @brief Optional user-provided function to process feedback sent by the action server. */
39+
void processFeedback(const std::shared_ptr<const Fibonacci::Feedback> feedback) override;
40+
41+
/** @brief Classes derived from AsyncBehaviorBase must implement getFuture() so that it returns a shared_future class member */
42+
std::shared_future<tl::expected<bool, std::string>>& getFuture() override
43+
{
44+
return future_;
45+
}
46+
47+
/** @brief Classes derived from AsyncBehaviorBase must have this shared_future as a class member */
48+
std::shared_future<tl::expected<bool, std::string>> future_;
49+
};
50+
} // namespace example_behaviors
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#include <moveit_studio_behavior_interface/get_message_from_topic.hpp>
4+
#include <std_msgs/msg/string.hpp>
5+
6+
using moveit_studio::behaviors::BehaviorContext;
7+
using moveit_studio::behaviors::GetMessageFromTopicBehaviorBase;
8+
9+
namespace example_behaviors
10+
{
11+
class GetStringFromTopic final : public GetMessageFromTopicBehaviorBase<std_msgs::msg::String>
12+
{
13+
public:
14+
GetStringFromTopic(const std::string& name, const BT::NodeConfiguration& config,
15+
const std::shared_ptr<BehaviorContext>& shared_resources);
16+
17+
/** @brief Implementation of the required providedPorts() function for the hello_world Behavior. */
18+
static BT::PortsList providedPorts();
19+
20+
/**
21+
* @brief Implementation of the metadata() function for displaying metadata, such as Behavior description and
22+
* subcategory, in the MoveIt Studio Developer Tool.
23+
* @return A BT::KeyValueVector containing the Behavior metadata.
24+
*/
25+
static BT::KeyValueVector metadata();
26+
27+
private:
28+
/** @brief Classes derived from AsyncBehaviorBase must implement getFuture() so that it returns a shared_future class member */
29+
std::shared_future<tl::expected<bool, std::string>>& getFuture() override
30+
{
31+
return future_;
32+
}
33+
34+
/** @brief Classes derived from AsyncBehaviorBase must have this shared_future as a class member */
35+
std::shared_future<tl::expected<bool, std::string>> future_;
36+
};
37+
} // namespace example_behaviors
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#pragma once
2+
3+
#include <behaviortree_cpp/action_node.h>
4+
5+
// This header includes the SharedResourcesNode type
6+
#include <moveit_studio_behavior_interface/shared_resources_node.hpp>
7+
8+
namespace example_behaviors
9+
{
10+
/**
11+
* @brief The HelloWorld Behavior uses FailureLoggerROS to log a "Hello World" message and will always return SUCCESS
12+
*/
13+
class HelloWorld : public moveit_studio::behaviors::SharedResourcesNode<BT::SyncActionNode>
14+
{
15+
public:
16+
/**
17+
* @brief Constructor for the hello_world behavior.
18+
* @param name The name of a particular instance of this Behavior. This will be set by the behavior tree factory when
19+
* this Behavior is created within a new behavior tree.
20+
* @param shared_resources A shared_ptr to a BehaviorContext that is shared among all SharedResourcesNode Behaviors in
21+
* the behavior tree. This BehaviorContext is owned by the Studio Agent's ObjectiveServerNode.
22+
* @param config This contains runtime configuration info for this Behavior, such as the mapping between the
23+
* Behavior's data ports on the behavior tree's blackboard. This will be set by the behavior tree factory when this
24+
* Behavior is created within a new behavior tree.
25+
* @details An important limitation is that the members of the base Behavior class are not instantiated until after
26+
* the initialize() function is called, so these classes should not be used within the constructor.
27+
*/
28+
HelloWorld(const std::string& name, const BT::NodeConfiguration& config,
29+
const std::shared_ptr<moveit_studio::behaviors::BehaviorContext>& shared_resources);
30+
31+
/**
32+
* @brief Implementation of the required providedPorts() function for the hello_world Behavior.
33+
* @details The BehaviorTree.CPP library requires that Behaviors must implement a static function named
34+
* providedPorts() which defines their input and output ports. If the Behavior does not use any ports, this function
35+
* must return an empty BT::PortsList. This function returns a list of ports with their names and port info, which is
36+
* used internally by the behavior tree.
37+
* @return hello_world does not expose any ports, so this function returns an empty list.
38+
*/
39+
static BT::PortsList providedPorts();
40+
41+
/**
42+
* @brief Implementation of the metadata() function for displaying metadata, such as Behavior description and
43+
* subcategory, in the MoveIt Studio Developer Tool.
44+
* @return A BT::KeyValueVector containing the Behavior metadata.
45+
*/
46+
static BT::KeyValueVector metadata();
47+
48+
/**
49+
* @brief Implementation of BT::SyncActionNode::tick() for HelloWorld.
50+
* @details This function is where the Behavior performs its work when the behavior tree is being run.
51+
* Since HelloWorld is derived from BT::SyncActionNode, it is very important that its tick() function always finishes
52+
* very quickly. If tick() blocks before returning, it will block execution of the entire behavior tree, which may
53+
* have undesirable consequences for other Behaviors that require a fast update rate to work correctly.
54+
* @return BT::NodeStatus::RUNNING, BT::NodeStatus::SUCCESS, or BT::NodeStatus::FAILURE depending on the result of the
55+
* work done in this function.
56+
*/
57+
BT::NodeStatus tick() override;
58+
};
59+
} // namespace example_behaviors

0 commit comments

Comments
 (0)