Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
transformations.cpp
Go to the documentation of this file.
2
3#include <cmath>
4
5namespace common_lib::maths {
6
7double normalize_angle(double angle) {
8 while (angle <= -M_PI) {
9 angle += 2 * M_PI;
10 }
11 while (angle > M_PI) {
12 angle -= 2 * M_PI;
13 }
14 return angle;
15}
16
17double get_wheel_velocity_from_rpm(const double rpm, const double wheel_diameter) {
18 return rpm * wheel_diameter * M_PI / 60;
19}
20
21Eigen::Matrix2d get_rotation_matrix(const double angle) {
22 Eigen::Matrix2d rotation_matrix;
23 rotation_matrix << std::cos(angle), -std::sin(angle), std::sin(angle), std::cos(angle);
24 return rotation_matrix;
25}
26
27Eigen::Vector2d cartesian_to_cylindrical(const Eigen::Vector2d& cartesian) {
28 Eigen::Vector2d cylindrical;
29 cylindrical << cartesian.norm(), std::atan2(cartesian.y(), cartesian.x());
30 return cylindrical;
31}
32
33Eigen::VectorXd global_to_local_coordinates(const Eigen::Vector3d& local_reference_frame,
34 const Eigen::VectorXd& global_points) {
35 const double x = local_reference_frame(0);
36 const double y = local_reference_frame(1);
37 const double angle = local_reference_frame(2);
38 const Eigen::Matrix2d rotation_matrix = get_rotation_matrix(-angle);
39 Eigen::VectorXd local_points(global_points.size());
40 for (int i = 0; i < global_points.size(); i += 2) {
41 const double x_global = global_points(i);
42 const double y_global = global_points(i + 1);
43 const Eigen::Vector2d global_point(x_global - x, y_global - y);
44 const Eigen::Vector2d local_point = rotation_matrix * global_point;
45 local_points(i) = local_point(0);
46 local_points(i + 1) = local_point(1);
47 }
48 return local_points;
49}
50
51Eigen::VectorXd local_to_global_coordinates(const Eigen::Vector3d& local_reference_frame,
52 const Eigen::VectorXd& local_points) {
53 const double x = local_reference_frame(0);
54 const double y = local_reference_frame(1);
55 const double angle = local_reference_frame(2);
56 const Eigen::Matrix2d rotation_matrix = get_rotation_matrix(angle);
57 Eigen::VectorXd global_points_vector(local_points.size());
58 for (int i = 0; i < local_points.size(); i += 2) {
59 const double x_local = local_points(i);
60 const double y_local = local_points(i + 1);
61 const Eigen::Vector2d local_point(x_local, y_local);
62 const Eigen::Vector2d global_point = rotation_matrix * local_point;
63 global_points_vector(i) = global_point(0) + x;
64 global_points_vector(i + 1) = global_point(1) + y;
65 }
66 return global_points_vector;
67}
68
69} // namespace common_lib::maths
Eigen::Matrix2d get_rotation_matrix(const double angle)
Function to get the rotation matrix for a given angle.
double get_wheel_velocity_from_rpm(const double rpm, const double wheel_diameter)
Function to get the translational velocity of a wheel from rpm and wheel diameter.
Eigen::Vector2d cartesian_to_cylindrical(const Eigen::Vector2d &cartesian)
Function to convert cartesian coordinates to cylindrical coordinates.
Eigen::VectorXd global_to_local_coordinates(const Eigen::Vector3d &local_reference_frame, const Eigen::VectorXd &global_points)
Transform points from a global reference frame to a local reference frame.
double normalize_angle(double angle)
Function to keep angle in [-Pi, Pi[ radians.
Eigen::VectorXd local_to_global_coordinates(const Eigen::Vector3d &local_reference_frame, const Eigen::VectorXd &local_points)
Transform points from a local reference frame to a global reference frame.