行为树(BT)笔记(八):插件

在前面的示例中,用户定义的节点包含在内并静态链接到C ++项目中。

我们使用了BehaviorTreeFactory注册manualy这些自定义TreeNodes。

或者,我们可以使用预编译的动态共享库(即插件)在运行时加载用户定义的TreeNode 。

要创建插件,我们必须将一个或多个TreeNode的注册封装到一个函数中,如下所示:

// This is a macro that defines a function with a single argument 
// (BehaviorTreeFactory& factory)

BT_REGISTER_NODES(factory)
{
    static GripperInterface gi; // we can't have more than instance

    factory.registerSimpleAction("SayHello", std::bind(SayHello) );
    factory.registerSimpleAction("OpenGripper",  
                                 std::bind( &GripperInterface::open, &gi));
    factory.registerSimpleAction("CloseGripper", 
                                 std::bind( &GripperInterface::close, &gi));
    factory.registerNodeType<ApproachObject>("ApproachObject");
    factory.registerNodeType<SaySomething>("SaySomething");
}
此函数必须放在.cpp文件中,而不是头文件中。

这里,我们假设BT_REGISTER_NODES和我们的自定义TreeNodes的定义都在文件dummy_nodes.cpp中定义。

使用cmake编译插件时,请将参数添加SHARED到 add_library

#your CMakeLists.txt 
add_library (dummy_nodes   SHARED  dummy_nodes.cpp  )

在Linux中,将创建文件libdummy_nodes.so

因此,第一个教程变得更加简单:

// This is a macro that defines a function with a single argument 
// (BehaviorTreeFactory& factory)

BT_REGISTER_NODES(factory)
{
    static GripperInterface gi; // we can't have more than instance

    factory.registerSimpleAction("SayHello", std::bind(SayHello) );
    factory.registerSimpleAction("OpenGripper",  
                                 std::bind( &GripperInterface::open, &gi));
    factory.registerSimpleAction("CloseGripper", 
                                 std::bind( &GripperInterface::close, &gi));
    factory.registerNodeType<ApproachObject>("ApproachObject");
    factory.registerNodeType<SaySomething>("SaySomething");
}

显示插件的清单

BehaviorTree.CPP提供了一个名为bt_plugin_manifest的命令行工具 。

它显示了注册到插件中的所有用户定义的TreeNode及其对应的NodeParameters。

$> ./bt_plugin_manifest ./libdummy_nodes.so 

---------------
ApproachObject [Action]
  NodeParameters: 0
---------------
CloseGripper [Action]
  NodeParameters: 0
---------------
OpenGripper [Action]
  NodeParameters: 0
---------------
SayHello [Action]
  NodeParameters: 0
---------------
SaySomething [Action]
  NodeParameters: 1:
    - [Key]: "message" / [Default]: "" 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值