ROS2 spin_some, spin_once, and spin_until_future的不同地方

spinonce将在执行器中处理一个项目,但它可能是也可能不是您所期望的。有时你只想周期性地一次处理一个项目,而不在乎它是什么。可以与while(rclcpp::ok())配合使用。

其他时候,你想一次处理一个项目,直到某件事情完成,这就是spin_until_future_complete有用的地方,因为它基本上是做while(future.not_done())executor.spin_once()

源码最终会调用下面的函数

/// Spin (blocking) until the future is complete, it times out waiting, or rclcpp is interrupted.
/**
 * \param[in] executor The executor which will spin the node.
 * \param[in] node_ptr The node to spin.
 * \param[in] future The future to wait on. If `SUCCESS`, the future is safe to
 *   access after this function
 * \param[in] timeout Optional timeout parameter, which gets passed to
 *   Executor::spin_node_once.
 *   `-1` is block forever, `0` is non-blocking.
 *   If the time spent inside the blocking loop exceeds this timeout, return a `TIMEOUT` return code.
 * \return The return code, one of `SUCCESS`, `INTERRUPTED`, or `TIMEOUT`.
 */
template<typename ResponseT, typename TimeRepT = int64_t, typename TimeT = std::milli>
rclcpp::FutureReturnCode
spin_node_until_future_complete(
  rclcpp::Executor & executor,
  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
  const std::shared_future<ResponseT> & future,
  std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
{
  // TODO(wjwwood): does not work recursively; can't call spin_node_until_future_complete
  // inside a callback executed by an executor.
  executor.add_node(node_ptr);
  auto retcode = executor.spin_until_future_complete(future, timeout);
  executor.remove_node(node_ptr);
  return retcode;
}

注意spin_until_future_complete中有一个executor会将节点node_ptr加入到executor,如果一个node被多个executor添加,就会报错。

参考:(109条消息) ROS2(三) -一个报错引发的executor,node的思考_ErinLiu虎哥的铲屎员的博客-CSDN博客_ros2 executor

比如rclcpp::spin_until_future_complete(client_node_, cancel_future); 将会一直处理client_node,直到cancle_future任务完成。

spin_some略有不同。基本上,它执行零到多个项目,直到它“空闲”,基本上直到执行器无事可做。spin_Some会在调用时处理可用的内容,即使一段时间后还有更多工作要做,比如有一个事件或者请求,它仍回去处理。

参考:https://answers.ros.org/question/296480/functional-difference-between-ros2-spin_some-spin_once-and-spin_until_future/#:~:text=You%20could%20do%20this%20yourself%2C%20so%20spin_until_future_complete%20is,there%20is%20nothing%20for%20the%20executor%20to%20do.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值