Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
test.py
Go to the documentation of this file.
1import rclpy
2from rclpy.node import Node
3from visualization_msgs.msg import Marker
4from custom_interfaces.msg import Velocities
5import math
6
7class LocationPublisher(Node):
8 def __init__(self):
9 super().__init__('location_publisher')
10 self.publisher_ = self.create_publisher(Marker, 'location', 10)
11 self.timer = self.create_timer(1.0, self.publish_marker)
12 self.x = 0.0
13 self.y = 0.0
14 self.orientation = 0.0
15 self.last_time = 0
16 self.subscription = self.create_subscription(
17 Velocities,
18 '/state_estimation/velocities',
20 10
21 )
22
23 def velocities_callback(self, msg):
24 current_time = msg.header.stamp.sec + msg.header.stamp.nanosec * 1e-9
25 if self.last_time == 0:
26 self.last_time = current_time
27 return
28 dt = current_time - self.last_time
29 self.x += msg.velocity_x * dt * math.cos(self.orientation) - msg.velocity_y * dt * math.sin(self.orientation)
30 self.y += msg.velocity_x * dt * math.sin(self.orientation) + msg.velocity_y * dt * math.cos(self.orientation)
31 self.orientation += msg.angular_velocity * dt
32 self.last_time = current_time
33 self.publish_marker()
34
35 def publish_marker(self):
36 marker = Marker()
37 marker.header.frame_id = "map"
38 marker.header.stamp = self.get_clock().now().to_msg()
39 marker.ns = "location"
40 marker.id = 0
41 marker.type = Marker.CYLINDER
42 marker.action = Marker.ADD
43 marker.pose.position.x = self.x
44 marker.pose.position.y = self.y
45 marker.pose.position.z = 0.0
46 marker.pose.orientation.x = 0.0
47 marker.pose.orientation.y = 0.0
48 marker.pose.orientation.z = math.sin(self.orientation / 2.0)
49 marker.pose.orientation.w = math.cos(self.orientation / 2.0)
50 marker.scale.x = 1.0
51 marker.scale.y = 1.0
52 marker.scale.z = 1.0
53 marker.color.a = 1.0
54 marker.color.r = 0.0
55 marker.color.g = 1.0
56 marker.color.b = 0.0
57 self.publisher_.publish(marker)
58 self.get_logger().info(f'Current position: x={self.x:.4f}, y={self.y:.4f}, orientation={self.orientation:.4f}')
59
60def main(args=None):
61 rclpy.init(args=args)
62 node = LocationPublisher()
63 rclpy.spin(node)
64 node.destroy_node()
65 rclpy.shutdown()
66
67if __name__ == '__main__':
68 main()
velocities_callback(self, msg)
Definition test.py:23
Definition main.py:1