実際のところ
ROS2標準メッセージのPointを購読し、
その値を内部の変数"recent_value"に格納するようなケースを考えます
import rclpy from rclpy.node import Node from geometry_msgs.msg import Point # グローバル変数 global_value = None class PointSubscriber(Node): def __init__(self): super().__init__('point_subscriber') self.subscription = self.create_subscription( Point, 'point_topic', self.point_callback, 10) self.recent_value = (0.0, 0.0, 0.0) def point_callback(self, msg): self.recent_value = (msg.x , msg.y, msg.z) self.get_logger().info(f"Received point: x={msg.x}, y={msg.y}, z={msg.z}") def main(args=None): rclpy.init(args=args) node = PointSubscriber() # spin_once を使用 my_value = rclpy.spin_once(node, timeout_sec=1) # 試しに表示してみる print(my_value.x) node.destroy_node() rclpy.shutdown() if __name__ == '__main__': main()
まぁ、これくらいだと旨みがないですが。
実際には、保存したパラメータを別のコマンド用に食わせるとか色々と応用ができます。