Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
rosbag_node.py
Go to the documentation of this file.
1import rclpy
2from rclpy.node import Node
3from visualization_msgs.msg import Marker, MarkerArray
4
5
6class BagNode(Node):
7 """Class to load and publish ground truth data from two files as a single MarkerArray message.
8
9 Args:
10 node (rclpy.node.Node): The ROS2 node object.
11 """
12
13 def __init__(self, ground_truth_file_1, ground_truth_file_2):
14 super().__init__("bag_node")
15
17 ground_truth_file_1, "blue"
18 )
20 ground_truth_file_2, "yellow"
21 )
22
23 self.ground_truth_data_pub = self.create_publisher(
24 MarkerArray, "ground_truth_data", 10
25 )
26
27 self.timer = self.create_timer(1.0, self.publish_ground_truth)
28
29 def load_ground_truth_data(self, file_path, label):
30 """Load ground truth data from a file, skipping the header line and assigning each point a label.
31
32 Args:
33 file_path (str): The path to the file containing ground truth data.
34 label (str): The label to assign to the points.
35 Returns: ground_truth_data (list): A list of tuples containing the x, y coordinates and label of each point.
36 """
37 ground_truth_data = []
38 try:
39 with open(file_path, "r") as file:
40 next(file)
41 for line in file:
42 x, y = map(float, line.strip().split()[:2])
43 ground_truth_data.append((x, y, label))
44 self.get_logger().info(f"Loaded ground truth data for {label}.")
45 except Exception as e:
46 self.get_logger().error(
47 f"Failed to load ground truth data for {label}: {e}"
48 )
49 return ground_truth_data
50
52 """Publish the combined ground truth data as a single MarkerArray message.
53
54 Args: None
55 Returns: None
56 """
57 marker_array = MarkerArray()
58 id_counter = 0
59
60 for x, y, label in self.ground_truth_data_blue + self.ground_truth_data_yellow:
61 marker = Marker()
62 marker.header.frame_id = "map"
63 marker.type = Marker.CYLINDER
64 marker.action = Marker.ADD
65 marker.id = id_counter
66 marker.pose.position.x = x
67 marker.pose.position.y = y
68 marker.pose.position.z = 0.5
69 marker.scale.x = 0.2
70 marker.scale.y = 0.2
71 marker.scale.z = 0.3
72
73 if label == "blue":
74 marker.color.r = 0.0
75 marker.color.g = 0.0
76 marker.color.b = 1.0
77 else:
78 marker.color.r = 1.0
79 marker.color.g = 1.0
80 marker.color.b = 0.0
81
82 marker.color.a = 1.0
83 marker_array.markers.append(marker)
84 id_counter += 1
85
86 self.ground_truth_data_pub.publish(marker_array)
87 self.get_logger().info("Published combined ground truth data as MarkerArray.")
88
89
90def main(args=None):
91 rclpy.init(args=args)
92 # files with ground truth data. The first one for the blue cones and the second one for the yellow cones.
93 ground_truth_file_1 = "/home/ws/src/rosbag_groundtruth/ground_truth_data_1_dv5.txt"
94 ground_truth_file_2 = "/home/ws/src/rosbag_groundtruth/ground_truth_data_2_dv5.txt"
95 # format of the file
96 """x y yellow
97 4.878305912017822 -1.296193242073059
98 5.4650559425354 -1.289870023727417
99 6.302743911743164 -1.211436152458191
100 """
101 node = BagNode(ground_truth_file_1, ground_truth_file_2)
102 rclpy.spin(node)
103 node.destroy_node()
104 rclpy.shutdown()
105
106
107if __name__ == "__main__":
108 main()
load_ground_truth_data(self, file_path, label)
__init__(self, ground_truth_file_1, ground_truth_file_2)
Definition main.py:1