Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
test_formats.py
Go to the documentation of this file.
1import unittest
2import numpy.testing as npt
3import numpy as np
4from custom_interfaces.msg import ConeArray, VehicleState, PathPointArray, Cone, PathPoint, Point2d
5from eufs_msgs.msg import ConeArrayWithCovariance, CarState, ConeWithCovariance
6from geometry_msgs.msg import TransformStamped, TwistWithCovarianceStamped
7from visualization_msgs.msg import MarkerArray, Marker
8from nav_msgs.msg import Odometry
9from evaluator.formats import (
10 format_vehicle_state_msg,
11 format_cone_array_msg,
12 format_marker_array_msg,
13 format_transform_stamped_msg,
14 format_twist_with_covariance_stamped_msg,
15 format_car_state_msg,
16 format_eufs_cone_array_with_covariance_msg,
17 format_nav_odometry_msg,
18 format_path_point_array_msg,
19 format_point2d_msg
20)
21
22
23class TestFormats(unittest.TestCase):
24 """
25 Test case for the format methods of the Evaluator class.
26 """
27
28 def setUp(self):
29 """
30 Set up the test environment by initializing expected value
31 """
32
33 self.expected_position = np.array(
34 [
35 7.0,
36 3.0,
37 42.0,
38 ]
39 ),
40
41 self.expected_velocities = np.array(
42 [
43 1.0,
44 1.0,
45 2.0
46 ]
47 ),
48
49 self.expected_cone_array = np.array(
50 [
51 np.array(
52 [
53 1,
54 2,
55 0,
56 73,
57 ]
58 )
59 ]
60
61 )
62
63 self.expected_stamped_message = np.array(
64 [
65 1,
66 2,
67 1.5708
68 ]
69 )
70
72 [
73 np.array(
74 [
75 6,
76 9,
77 3,
78 ]
79 )
80 ]
81 )
82
83
85 """
86 Test case to check the formatting of a VehicleState message
87 """
88
89 msg = VehicleState()
90 msg.position.x = 7.0
91 msg.position.y = 3.0
92 msg.theta = 42.0
93 msg.linear_velocity = 1.0
94 msg.angular_velocity = 2.0
95
96 format_position, format_velocities = format_vehicle_state_msg(msg)
97
98 npt.assert_array_almost_equal(format_position, self.expected_position[0], decimal=6)
99 npt.assert_array_almost_equal(format_velocities, self.expected_velocities[0], decimal=6)
100
102 """
103 Test case to check the formatting of a ConeArray message
104 """
105
106 cone_msg = Cone()
107 cone_msg.position.x = 1.0
108 cone_msg.position.y = 2.0
109 cone_msg.color = 'blue'
110 cone_msg.confidence = 73.0
111 msg = ConeArray()
112 msg.cone_array.append(cone_msg)
113
114 formated_position = format_cone_array_msg(msg)
115
116 npt.assert_array_almost_equal(formated_position, self.expected_cone_array, decimal=6)
117
119 """
120 Test case to check the formatting of a MarkerArray message
121 """
122
123 marker = Marker()
124 marker.pose.position.x = 1.0
125 marker.pose.position.y = 2.0
126 msg = MarkerArray()
127 msg.markers.append(marker)
128
129 formated_msg = format_marker_array_msg(msg)
130 formated_msg[0][3] = 73
131 formated_msg[0][2] = 0.0
132
133 npt.assert_array_almost_equal(formated_msg, self.expected_cone_array, decimal=6)
134
136 """
137 Test case to check the formatting of a TransformStamped message
138 """
139
140 msg = TransformStamped()
141 msg.transform.translation.x = 1.0
142 msg.transform.translation.y = 2.0
143 msg.transform.translation.z = 0.0
144 msg.transform.rotation.x = 0.0
145 msg.transform.rotation.y = 0.0
146 msg.transform.rotation.z = 0.7071
147 msg.transform.rotation.w = 0.7071
148
149 formated_msg = format_transform_stamped_msg(msg)
150
151 npt.assert_array_almost_equal(formated_msg, self.expected_stamped_message, decimal=4)
152
153
155 """
156 Test case to check the formatting of a TwistWithCovarianceStamped message
157 """
158
159 msg = TwistWithCovarianceStamped()
160 msg.twist.twist.linear.x = 1.0
161 msg.twist.twist.linear.y = 1.0
162 msg.twist.twist.angular.z = 2.0
163
164 formated_msg = format_twist_with_covariance_stamped_msg(msg)
165
166 npt.assert_array_almost_equal(formated_msg, self.expected_velocities[0], decimal=6)
167
169 """
170 Test case to check the formatting of a CarState message
171 """
172
173 msg = CarState()
174 msg.pose.pose.position.x = 1.0
175 msg.pose.pose.position.y = 2.0
176 msg.pose.pose.orientation.x = 0.0
177 msg.pose.pose.orientation.y = 0.0
178 msg.pose.pose.orientation.z = 0.7071
179 msg.pose.pose.orientation.w = 0.7071
180 msg.twist.twist.linear.x = 1.0
181 msg.twist.twist.linear.y = 1.0
182 msg.twist.twist.angular.z = 2.0
183
184 formated_msg1, formated_msg2 = format_car_state_msg(msg)
185
186 npt.assert_array_almost_equal(formated_msg1, self.expected_stamped_message, decimal=4)
187 npt.assert_array_almost_equal(formated_msg2, self.expected_velocities[0], decimal=4)
188
190 """
191 Test case to check the formatting of a ConeArrayWithCovariance message
192 """
193
194 msg = ConeArrayWithCovariance()
195 cone = ConeWithCovariance()
196 cone.point.x = 1.0
197 cone.point.y = 2.0
198 msg.blue_cones.append(cone)
199
200 formated_msg = format_eufs_cone_array_with_covariance_msg(msg)
201 formated_msg[0][3] = 73
202
203 npt.assert_array_almost_equal(formated_msg, self.expected_cone_array, decimal=4)
204
206 """
207 Test case to check the formatting of a Odometry message
208 """
209
210 msg = Odometry()
211 msg.pose.pose.position.x = 1.0
212 msg.pose.pose.position.y = 2.0
213 msg.pose.pose.orientation.x = 0.0
214 msg.pose.pose.orientation.y = 0.0
215 msg.pose.pose.orientation.z = 0.7071
216 msg.pose.pose.orientation.w = 0.7071
217 msg.twist.twist.linear.x = 1.0
218 msg.twist.twist.linear.y = 1.0
219 msg.twist.twist.angular.z = 2.0
220
221 formated_msg1, formated_msg2 = format_nav_odometry_msg(msg)
222
223 npt.assert_array_almost_equal(formated_msg1, self.expected_stamped_message, decimal=4)
224 npt.assert_array_almost_equal(formated_msg2, self.expected_velocities[0], decimal=4)
225
227 """
228 Test case to check the formatting of a PathPointArray message
229 """
230
231 msg = PathPointArray()
232 path_point = PathPoint()
233 path_point.x = 6.0
234 path_point.y = 9.0
235 path_point.v = 3.0
236 msg.pathpoint_array.append(path_point)
237
238 formated_msg = format_path_point_array_msg(msg)
239
240 npt.assert_array_almost_equal(formated_msg, self.expected_path_point_array, decimal=6)
241
243 """
244 Test case to check the formatting of a Point2d message
245 """
246
247 msg = Point2d()
248 msg.x = 2.0
249 msg.y = 1.0
250
251 formated_msg = format_point2d_msg(msg)
252
253 npt.assert_array_almost_equal(formated_msg, [2, 1], decimal=6)
test_format_eufs_cone_array_with_covariance(self)