WPILibC++ 2023.4.3
ChassisSpeeds.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <wpi/SymbolExports.h>
8
11#include "units/velocity.h"
12
13namespace frc {
14/**
15 * Represents the speed of a robot chassis. Although this struct contains
16 * similar members compared to a Twist2d, they do NOT represent the same thing.
17 * Whereas a Twist2d represents a change in pose w.r.t to the robot frame of
18 * reference, this ChassisSpeeds struct represents a velocity w.r.t to the robot
19 * frame of reference.
20 *
21 * A strictly non-holonomic drivetrain, such as a differential drive, should
22 * never have a dy component because it can never move sideways. Holonomic
23 * drivetrains such as swerve and mecanum will often have all three components.
24 */
26 /**
27 * Represents forward velocity w.r.t the robot frame of reference. (Fwd is +)
28 */
29 units::meters_per_second_t vx = 0_mps;
30
31 /**
32 * Represents strafe velocity w.r.t the robot frame of reference. (Left is +)
33 */
34 units::meters_per_second_t vy = 0_mps;
35
36 /**
37 * Represents the angular velocity of the robot frame. (CCW is +)
38 */
39 units::radians_per_second_t omega = 0_rad_per_s;
40
41 /**
42 * Converts a user provided field-relative set of speeds into a robot-relative
43 * ChassisSpeeds object.
44 *
45 * @param vx The component of speed in the x direction relative to the field.
46 * Positive x is away from your alliance wall.
47 * @param vy The component of speed in the y direction relative to the field.
48 * Positive y is to your left when standing behind the alliance wall.
49 * @param omega The angular rate of the robot.
50 * @param robotAngle The angle of the robot as measured by a gyroscope. The
51 * robot's angle is considered to be zero when it is facing directly away from
52 * your alliance station wall. Remember that this should be CCW positive.
53 *
54 * @return ChassisSpeeds object representing the speeds in the robot's frame
55 * of reference.
56 */
58 units::meters_per_second_t vx, units::meters_per_second_t vy,
59 units::radians_per_second_t omega, const Rotation2d& robotAngle) {
60 return {vx * robotAngle.Cos() + vy * robotAngle.Sin(),
61 -vx * robotAngle.Sin() + vy * robotAngle.Cos(), omega};
62 }
63
64 /**
65 * Converts a user provided field-relative ChassisSpeeds object into a
66 * robot-relative ChassisSpeeds object.
67 *
68 * @param fieldRelativeSpeeds The ChassisSpeeds object representing the speeds
69 * in the field frame of reference. Positive x is away from your alliance
70 * wall. Positive y is to your left when standing behind the alliance wall.
71 * @param robotAngle The angle of the robot as measured by a gyroscope. The
72 * robot's angle is considered to be zero when it is facing directly away
73 * from your alliance station wall. Remember that this should be CCW
74 * positive.
75 * @return ChassisSpeeds object representing the speeds in the robot's frame
76 * of reference.
77 */
79 const ChassisSpeeds& fieldRelativeSpeeds, const Rotation2d& robotAngle) {
80 return FromFieldRelativeSpeeds(fieldRelativeSpeeds.vx,
81 fieldRelativeSpeeds.vy,
82 fieldRelativeSpeeds.omega, robotAngle);
83 }
84};
85} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
A rotation in a 2D coordinate frame represented by a point on the unit circle (cosine and sine).
Definition: Rotation2d.h:26
constexpr double Cos() const
Returns the cosine of the rotation.
Definition: Rotation2d.h:152
constexpr double Sin() const
Returns the sine of the rotation.
Definition: Rotation2d.h:159
Definition: AprilTagFieldLayout.h:22
Represents the speed of a robot chassis.
Definition: ChassisSpeeds.h:25
units::meters_per_second_t vx
Represents forward velocity w.r.t the robot frame of reference.
Definition: ChassisSpeeds.h:29
units::meters_per_second_t vy
Represents strafe velocity w.r.t the robot frame of reference.
Definition: ChassisSpeeds.h:34
static ChassisSpeeds FromFieldRelativeSpeeds(const ChassisSpeeds &fieldRelativeSpeeds, const Rotation2d &robotAngle)
Converts a user provided field-relative ChassisSpeeds object into a robot-relative ChassisSpeeds obje...
Definition: ChassisSpeeds.h:78
units::radians_per_second_t omega
Represents the angular velocity of the robot frame.
Definition: ChassisSpeeds.h:39
static ChassisSpeeds FromFieldRelativeSpeeds(units::meters_per_second_t vx, units::meters_per_second_t vy, units::radians_per_second_t omega, const Rotation2d &robotAngle)
Converts a user provided field-relative set of speeds into a robot-relative ChassisSpeeds object.
Definition: ChassisSpeeds.h:57