Brick
Loading...
Searching...
No Matches
chassis_velocities.h
1#pragma once
2
3#include "core/math/geometry/pose2d.h"
4#include "core/math/geometry/rotation2d.h"
5#include "core/math/geometry/translation2d.h"
6#include "core/math/geometry/twist2d.h"
7#include "core/math/units.h"
8
9using namespace units::literals;
10
11/**
12 * @brief Represents robot chassis velocities.
13 */
14struct ChassisVelocities {
15 /** @brief Velocity along the x-axis. (Forward is positive) */
16 units::Velocity vx = 0_inps;
17
18 /** @brief Velocity along the y-axis. (Left is positive) */
19 units::Velocity vy = 0_inps;
20
21 /** @brief Angular velocity of the robot frame. (Counter-Clockwise is positive) */
22 units::AngularVelocity omega = 0_radps;
23
24 ChassisVelocities() = default;
25
26 ChassisVelocities(units::Velocity vx, units::Velocity vy, units::AngularVelocity omega)
27 : vx(vx), vy(vy), omega(omega) {}
28
29 /**
30 * @brief Converts this field-relative set of velocities into a robot-relative ChassisVelocities
31 * object.
32 * @param robotAngle The CCW-positive angle of the robot relative to the field.
33 * @return Robot-relative ChassisVelocities.
34 */
35 ChassisVelocities to_robot_relative(const Rotation2d& robotAngle) const {
36 Velocity2d rotated = Velocity2d(vx, vy).rotate_by(-robotAngle);
37 return ChassisVelocities(rotated.x(), rotated.y(), omega);
38 }
39
40 /**
41 * @brief Converts this robot-relative set of velocities into a field-relative ChassisVelocities
42 * object.
43 * @param robotAngle The CCW-positive angle of the robot relative to the field.
44 * @return Field-relative ChassisVelocities.
45 */
46 ChassisVelocities to_field_relative(const Rotation2d& robotAngle) const {
47 Velocity2d rotated = Velocity2d(vx, vy).rotate_by(robotAngle);
48 return ChassisVelocities(rotated.x(), rotated.y(), omega);
49 }
50
51 /**
52 * @brief Discretizes continuous-time chassis velocities.
53 *
54 * Compensates for translational skew when rotating a holonomic drivetrain. Converts
55 * the decoupled translation/rotation over a timestep into a constant-curvature
56 * trajectory representation.
57 *
58 * @param dt The duration of the timestep.
59 * @return Discretized ChassisVelocities.
60 */
61 ChassisVelocities discretize(units::Time dt) const {
62 // Construct the desired pose after a timestep, relative to the current pose
63 // with decoupled translation and rotation
64 Pose2d desired_pose(vx * dt, vy * dt, Rotation2d(omega * dt));
65
66 // Calculate the constant-curvature path (Twist2d) to get there
67 Transform2d transform = desired_pose - Pose2d{};
68 Twist2d twist = Pose2d{}.log(transform);
69
70 // Turn the deltas back into average velocities over the timestep
71 return ChassisVelocities(twist.dx() / dt, twist.dy() / dt, twist.dtheta() / dt);
72 }
73
74 // Operator overloads
75 ChassisVelocities operator+(const ChassisVelocities& other) const {
76 return ChassisVelocities(vx + other.vx, vy + other.vy, omega + other.omega);
77 }
78
79 ChassisVelocities operator-(const ChassisVelocities& other) const {
80 return ChassisVelocities(vx - other.vx, vy - other.vy, omega - other.omega);
81 }
82
83 ChassisVelocities operator-() const { return ChassisVelocities(-vx, -vy, -omega); }
84
85 ChassisVelocities operator*(double scalar) const {
86 return ChassisVelocities(vx * scalar, vy * scalar, omega * scalar);
87 }
88
89 ChassisVelocities operator/(double scalar) const {
90 return ChassisVelocities(vx / scalar, vy / scalar, omega / scalar);
91 }
92
93 bool operator==(const ChassisVelocities& other) const {
94 return vx == other.vx && vy == other.vy && omega == other.omega;
95 }
96
97 bool operator!=(const ChassisVelocities& other) const { return !(*this == other); }
98};
constexpr Q x() const
Gets the x component.
Definition translation2d.h:64
constexpr Q y() const
Gets the y component.
Definition translation2d.h:73
Definition pose2d.h:21
constexpr Twist2d log(const Transform2d &transform) const
Definition pose2d.h:253
Definition rotation2d.h:16
constexpr Rotation2d()=default
Default Constructor for Rotation2d.
Definition transform2d.h:17
Definition twist2d.h:20
constexpr units::Length dx() const
Gets the x displacement.
Definition twist2d.h:56
constexpr units::Length dy() const
Gets the y displacement.
Definition twist2d.h:65
constexpr units::Angle dtheta() const
Gets the Angle displacement.
Definition twist2d.h:74
Definition units.h:691
Represents robot chassis velocities.
Definition chassis_velocities.h:14
units::AngularVelocity omega
Angular velocity of the robot frame. (Counter-Clockwise is positive).
Definition chassis_velocities.h:22
ChassisVelocities to_field_relative(const Rotation2d &robotAngle) const
Converts this robot-relative set of velocities into a field-relative ChassisVelocities object.
Definition chassis_velocities.h:46
units::Velocity vy
Velocity along the y-axis. (Left is positive).
Definition chassis_velocities.h:19
ChassisVelocities to_robot_relative(const Rotation2d &robotAngle) const
Converts this field-relative set of velocities into a robot-relative ChassisVelocities object.
Definition chassis_velocities.h:35
units::Velocity vx
Velocity along the x-axis. (Forward is positive).
Definition chassis_velocities.h:16
ChassisVelocities discretize(units::Time dt) const
Discretizes continuous-time chassis velocities.
Definition chassis_velocities.h:61