Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

BehaviorTreeをつかう その1:環境構築

実際のところ

パッケージの導入

$ sudo apt update
$ sudo apt install ros-$ROS_DISTRO-behaviortree-cpp-v3

ROS2パッケージの作成

作業ディレクトリは"|/dev_ws"とし、サブディレクトリsrcもあるとします。

$ cd ~/dev_ws/src
$ ros2 pkg create --build-type ament_cmake your_behavior_tree_package

ソース類の用意

公式を読んでもサッパリ

$ cd ~/dev_ws/src/your_behavior_tree_package
$ tree
.
| -- CMakeLists.txt
|  include
|    | -- your_behavior_tree_package
| package.xml
| src

3 directories, 2 files

必要なディレクトリとファイルを用意

$ mkdir config
$ mkdir launch
$ touch src/simple_action_node.cpp
$ touch config/my_tree.xml
$ touch launch/behavior_tree.launch.py

"src/simple_action_node.cpp"。
treeファイルは"/home/user/dev_ws/src/your_behavior_tree/config/my_tree.xml";にあるとします。

#include <behaviortree_cpp_v3/behavior_tree.h>
#include <behaviortree_cpp_v3/bt_factory.h>
#include <iostream>
#include <string>


using namespace BT;

// Simple function that return a NodeStatus
BT::NodeStatus CheckBattery()
{
    std::cout << "[ Battery: OK ]" << std::endl;
    return BT::NodeStatus::SUCCESS;
}


int main()
{
    BehaviorTreeFactory factory;
    
    factory.registerSimpleCondition("CheckBattery", [&](TreeNode&) { return CheckBattery(); });
 
    //indicate the path to the file containing the tree
    std::string xml_file = "/home/tsukarm/dev_ws/src/your_behavior_tree_package/config/my_tree.xml";

    //auto tree = factory.createTreeFromFile(xml_file);
    auto tree = factory.createTreeFromFile( "/home/tsukarm/dev_ws/src/your_behavior_tree_package/config/my_tree.xml");
    
    tree.tickRootWhileRunning();

    return 0;
}

" launch/behavior_tree.launch.py"

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='your_behavior_tree_package',
            executable='simple_action_node',
            name='simple_action_node'
        )
    ])

"config/my_tree.xml"

<root main_tree_to_execute = "MainTree" >
    <BehaviorTree ID="MainTree">
        <Sequence name="root_sequence">
            <CheckBattery   name="check_battery"/>
            <!-- 追加はここに -->
        </Sequence>
    </BehaviorTree>
</root>

"CMakeLists.txt"を以下の様に

cmake_minimum_required(VERSION 3.8)
project(your_behavior_tree_package)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

##追加ここから
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(behaviortree_cpp_v3 REQUIRED)

add_executable(simple_action_node src/simple_action_node.cpp)
ament_target_dependencies(simple_action_node rclcpp behaviortree_cpp_v3)

install(TARGETS
  simple_action_node
  DESTINATION lib/${PROJECT_NAME}
)
##ここまで

ament_package()

ビルド

$ cd ~/dev_ws
$ colcon build
$ source ~/dev_ws/install/setup.bash