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会在调用时处理可用的内容,即使一段时间后还有更多工作要做,比如有一个事件或者请求,它仍回去处理。