はじめに
ROS2ログを使って、一定時間間隔でログを出力する方法。C++とPythonそれぞれ紹介する。
動作確認環境
- ROS2 Foxy
方法
C++、Pythonでそれぞれ一定時間間隔を指定する引数があるので設定する。
C++ (rclcpp)
ログレベルで、それぞれ関数が用意されている。
RCLCPP_{DEBUG,INFO,WARN,ERROR,FATAL}_THROTTLE
ストリームを使う場合
RCLCPP_{DEBUG,INFO,WARN,ERROR,FATAL}_STREAM_THROTTLE
例: 1秒おきにエラーログを出力
#include "rclcpp/rclcpp.hpp"
std::string test_err_str = "test error"
RCLCPP_ERROR_THROTTLE(
this->get_logger(),
*this->get_clock(),
1000,
"Some error message: %s",
test_err_str.c_str()
);
- 第1引数(logger):ROSロガー
- 第2引数(clock):ROSクロック
- 第3引数(duration):出力間隔時間(ミリ秒)
- 第4引数:メッセージ
ポイント
第2引数のROSクロックは実体で渡す必要があるので、this->get_clock()を使う場合は、*this->get_clock()として実体で渡す。
Python (rclpy)
追加の引数「throttle_duration_sec」で指定する。
例:1.0秒間隔でエラーログを表示
import rclpy
self.get_logger().error('Some error message', throttle_duration_sec=1.0)
- throttle_duration (float)
- 出力間隔時間(秒)
参考までに、rclpyのログの引数には以下が指定可能。
:Keyword Arguments:
* *throttle_duration_sec* (``float``) --
Duration of the throttle interval for the :py:class:Throttle: filter.
* *throttle_time_source_type* (``str``) --
Optional time source type for the :py:class:Throttle: filter (default of
``RCUTILS_STEADY_TIME``)
* *skip_first* (``bool``) --
If True, enable the :py:class:SkipFirst: filter.
* *once* (``bool``) --
If True, enable the :py:class:Once: filter.
まとめ
ROS2で一定時間間隔でログ出力する方法を紹介した。ログの量が多くなりすぎる場合に、重宝する。
参考