Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
track_loader.cpp
Go to the documentation of this file.
2
3Node get_child_node(Node parentNode, std::string tag) {
4 if (parentNode[tag]) {
5 Node childNode = parentNode[tag];
6 return childNode;
7 } else {
8 RCLCPP_ERROR(rclcpp::get_logger("slam"), ("Failed loading the map, no " + tag).c_str());
9 return Node();
10 }
11}
12
13void add_landmarks(Eigen::VectorXd& track, const Node& list) {
14 int N = list.size();
15 int track_size = track.size();
16 track.conservativeResize(track_size + N * 2);
17 for (const_iterator it = list.begin(); it != list.end(); ++it, track_size += 2) {
18 const Node& position = *it;
19 std::vector<double> vi = position["position"].as<std::vector<double>>();
20 track.segment(track_size, 2) << vi[0], vi[1];
21 }
22}
23
24void load_initial_state(std::string mapPath, Eigen::Vector3d& start_pose, Eigen::VectorXd& track) {
25 Node map = LoadFile(mapPath);
26 // loads the whole track node
27 Node track_node = get_child_node(map, "track");
28
29 // loads the cones
30 Node left = get_child_node(track_node, "left");
31 Node right = get_child_node(track_node, "right");
32 Node unknown = get_child_node(track_node, "unknown");
33 // TODO: Catch pose size != 3
34
35 std::vector<double> state_pose_vector = track_node["start"]["pose"].as<std::vector<double>>();
36 if (state_pose_vector.size() != 3) {
37 RCLCPP_ERROR(rclcpp::get_logger("slam"), "Start pose vector size is not 3!");
38 throw std::runtime_error("Start pose vector size is not 3!");
39 }
40 start_pose = Eigen::Vector3d(state_pose_vector.data());
41
42 add_landmarks(track, left);
43 add_landmarks(track, right);
44 add_landmarks(track, unknown);
45}
46
47void load_acceleration_track(Eigen::Vector3d& start_pose, Eigen::VectorXd& track) {
48 std::string package_prefix = ament_index_cpp::get_package_prefix("slam");
49 std::string mapPath = package_prefix + "/../../src/slam/maps/acceleration.yaml";
50 load_initial_state(mapPath, start_pose, track);
51 transform_track(track, start_pose);
52}
53
54void load_skidpad_track(Eigen::Vector3d& start_pose, Eigen::VectorXd& track) {
55 std::string package_prefix = ament_index_cpp::get_package_prefix("slam");
56 std::string mapPath = package_prefix + "/../../src/slam/maps/skidpad.yaml";
57 load_initial_state(mapPath, start_pose, track);
58 transform_track(track, start_pose);
59}
60
61void transform_track(Eigen::VectorXd& track, Eigen::Vector3d& start_pose) {
62 double theta = start_pose(2);
63 Eigen::Vector2d translation = start_pose.head<2>();
64
65 // Compute inverse rotation matrix (transpose of rotation matrix)
66 Eigen::Matrix2d rotation_matrix;
67 rotation_matrix << cos(theta), -sin(theta), sin(theta), cos(theta);
68 Eigen::Matrix2d inverse_rotation = rotation_matrix.transpose();
69
70 for (int i = 0; i < track.size(); i += 2) {
71 Eigen::Vector2d point(track[i], track[i + 1]);
72
73 // Translate to pose frame and rotate
74 point = inverse_rotation * (point - translation);
75
76 track[i] = point[0];
77 track[i + 1] = point[1];
78 }
79 start_pose = Eigen::Vector3d(0, 0, 0); // Reset start pose to origin after transformation
80}
void load_initial_state(std::string mapPath, Eigen::Vector3d &start_pose, Eigen::VectorXd &track)
loads the map from the specified path and fills the start pose and track.
void load_acceleration_track(Eigen::Vector3d &start_pose, Eigen::VectorXd &track)
loads the acceleration track from the default path.
void transform_track(Eigen::VectorXd &track, Eigen::Vector3d &start_pose)
Transforms the track as seen from the start pose.
void add_landmarks(Eigen::VectorXd &track, const Node &list)
adds the landmarks in the list to the track.
void load_skidpad_track(Eigen::Vector3d &start_pose, Eigen::VectorXd &track)
loads the skidpad track from the default path.
Node get_child_node(Node parentNode, std::string tag)
Get the child node from a parent node by tag.