create_subscription的参数类型
时间: 2023-08-13 17:35:30 浏览: 38
`create_subscription`函数的参数类型如下:
```
template<typename MessageT, typename CallbackT>
rclcpp::Subscription<MessageT>::SharedPtr create_subscription(
const std::string & topic_name,
size_t qos_history_depth,
CallbackT && callback,
const rmw_qos_profile_t & qos_profile = rmw_qos_profile_default
);
```
其中,`MessageT`是消息类型,例如`std_msgs::msg::String`;`topic_name`是要订阅的主题名称,例如`"/my_topic"`;`qos_history_depth`是订阅器队列的深度,即用于存储未处理消息的缓存大小;`callback`是回调函数,用于处理接收到的消息,例如`void my_callback(const std_msgs::msg::String::SharedPtr msg)`;`qos_profile`是QoS配置,用于控制消息传输的质量。
相关问题
create_subscription
`create_subscription`是ROS2中一个用于创建订阅器的函数。它允许节点订阅一个或多个主题,并在主题发布新消息时接收这些消息。
`create_subscription`函数需要指定以下参数:
- Topic名称:指定要订阅的主题名称。
- 消息类型:指定订阅器要接收的消息类型。
- 回调函数:指定当订阅器接收到新消息时要调用的回调函数。
- QoS配置:指定订阅器的QoS配置,用于控制消息传输的质量。
例如,以下代码创建了一个名为`sub`的订阅器,用于订阅名为`/my_topic`的主题,并在接收到消息时调用名为`my_callback`的回调函数:
```
rclcpp::Subscription<MyMessage>::SharedPtr sub = node->create_subscription<MyMessage>(
"/my_topic",
10,
my_callback,
rmw_qos_profile_default
);
```
其中,`MyMessage`是自定义消息类型,`10`是订阅器的队列长度,`rmw_qos_profile_default`是默认的QoS配置。
nav2里行为树如何新建动作节点编写create_subscription() 函数来订阅某个节点的主题,并将其与行为节点连接起来
在nav2中新建动作节点时,需要在行为树的XML文件中添加动作节点的定义。动作节点的定义中包含了动作节点的名称、输入参数、输出参数、以及动作执行的具体逻辑。下面是一个示例动作节点的定义:
```xml
<BehaviorTree>
<root main_tree_to_execute="MainTree">
<BehaviorTree ID="MainTree">
<Sequence name="root">
<!-- other nodes -->
<Action name="subscribe_to_topic" param_topic_name="my_topic" param_message_type="std_msgs/String" output_value="subscription"/>
<!-- other nodes -->
</Sequence>
</BehaviorTree>
</root>
</BehaviorTree>
```
在上面的示例中,动作节点的名称为 `subscribe_to_topic`,它有两个输入参数 `param_topic_name` 和 `param_message_type`,以及一个输出参数 `output_value`。动作节点执行的逻辑是使用 `rclcpp::create_subscription()` 函数订阅名为 `my_topic` 的主题,并将订阅对象保存到输出参数 `subscription` 中。
下面是动作节点对应的C++代码实现:
```cpp
#include "rclcpp/rclcpp.hpp"
#include "behaviortree_cpp_v3/behavior_tree.h"
class SubscribeToTopic : public BT::SyncActionNode
{
public:
SubscribeToTopic(const std::string& name, const BT::NodeConfiguration& config)
: BT::SyncActionNode(name, config)
{
}
static BT::PortsList providedPorts()
{
return {
BT::InputPort<std::string>("topic_name", "Name of the topic to subscribe to"),
BT::InputPort<std::string>("message_type", "Type of the message to subscribe to"),
BT::OutputPort<rclcpp::SubscriptionBase::SharedPtr>("subscription", "Pointer to the subscription object")
};
}
BT::NodeStatus tick() override
{
std::string topic_name;
if (!getInput<std::string>("topic_name", topic_name)) {
throw BT::RuntimeError("missing required input [topic_name]");
}
std::string message_type;
if (!getInput<std::string>("message_type", message_type)) {
throw BT::RuntimeError("missing required input [message_type]");
}
auto node = rclcpp::Node::make_shared("subscribe_to_topic_node");
auto subscription = node->create_subscription<message_type>(
topic_name,
10,
[](const message_type::SharedPtr msg) {
// Do something with the received message
});
setOutput("subscription", subscription);
return BT::NodeStatus::SUCCESS;
}
};
```
在上面的代码中,`SubscribeToTopic` 类继承自 `BT::SyncActionNode`,表示它是一个同步动作节点。在 `providedPorts()` 函数中定义了节点的输入和输出端口,输入端口包括 `topic_name` 和 `message_type`,输出端口为 `subscription`。`tick()` 函数是节点的执行逻辑,其中通过 `getInput()` 函数获取输入参数的值,然后使用 `rclcpp::create_subscription()` 函数订阅主题,并将订阅对象保存到输出参数中。最后返回 `BT::NodeStatus::SUCCESS` 表示节点执行成功。
在行为树的XML文件中,可以使用 `<Action>` 标签来引用上面定义的 `SubscribeToTopic` 类型的动作节点,并通过 `param_topic_name` 和 `param_message_type` 属性来指定输入参数的值。例如:
```xml
<Action name="subscribe_to_topic" param_topic_name="my_topic" param_message_type="std_msgs/String" output_value="subscription"/>
```
这样就完成了动作节点的创建和使用。当行为树执行到这个节点时,它会调用 `SubscribeToTopic::tick()` 函数来执行订阅主题的操作,然后将订阅对象保存到 `output_value` 中。其他节点可以通过输入端口来获取这个订阅对象,然后使用它来访问订阅的主题。