WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
MecanumDrive.hpp
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 <functional>
8#include <string>
9
12#include "wpi/units/angle.hpp"
15
16namespace wpi {
17
18class MotorController;
19
20/**
21 * A class for driving Mecanum drive platforms.
22 *
23 * Mecanum drives are rectangular with one wheel on each corner. Each wheel has
24 * rollers toed in 45 degrees toward the front or back. When looking at the
25 * wheels from the top, the roller axles should form an X across the robot.
26 *
27 * Drive base diagram:
28 * <pre>
29 * \\_______/
30 * \\ | | /
31 * | |
32 * /_|___|_\\
33 * / \\
34 * </pre>
35 *
36 * Each Drive() function provides different inverse kinematic relations for a
37 * Mecanum drive robot.
38 *
39 * This library uses the NWU axes convention (North-West-Up as external
40 * reference in the world frame). The positive X axis points ahead, the positive
41 * Y axis points to the left, and the positive Z axis points up. Rotations
42 * follow the right-hand rule, so counterclockwise rotation around the Z axis is
43 * positive.
44 *
45 * Inputs smaller then 0.02 will be set to 0, and larger values will be scaled
46 * so that the full range is still used. This deadband value can be changed
47 * with SetDeadband().
48 *
49 * MotorSafety is enabled by default. The DriveCartesian or DrivePolar
50 * methods should be called periodically to avoid Motor Safety timeouts.
51 */
54 public wpi::util::SendableHelper<MecanumDrive> {
55 public:
56 /**
57 * Wheel velocities for a mecanum drive.
58 *
59 * Uses normalized voltage [-1.0..1.0].
60 */
62 /// Front-left wheel velocity.
63 double frontLeft = 0.0;
64 /// Front-right wheel velocity.
65 double frontRight = 0.0;
66 /// Rear-left wheel velocity.
67 double rearLeft = 0.0;
68 /// Rear-right wheel velocity.
69 double rearRight = 0.0;
70 };
71
72 /**
73 * Construct a MecanumDrive.
74 *
75 * If a motor needs to be inverted, do so before passing it in.
76 *
77 * @param frontLeftMotor Front-left motor.
78 * @param rearLeftMotor Rear-left motor.
79 * @param frontRightMotor Front-right motor.
80 * @param rearRightMotor Rear-right motor.
81 */
82 MecanumDrive(MotorController& frontLeftMotor, MotorController& rearLeftMotor,
83 MotorController& frontRightMotor,
84 MotorController& rearRightMotor);
85
86 /**
87 * Construct a MecanumDrive.
88 *
89 * If a motor needs to be inverted, do so before passing it in.
90 *
91 * @param frontLeftMotor Front-left motor setter.
92 * @param rearLeftMotor Rear-left motor setter.
93 * @param frontRightMotor Front-right motor setter.
94 * @param rearRightMotor Rear-right motor setter.
95 */
96 MecanumDrive(std::function<void(double)> frontLeftMotor,
97 std::function<void(double)> rearLeftMotor,
98 std::function<void(double)> frontRightMotor,
99 std::function<void(double)> rearRightMotor);
100
101 ~MecanumDrive() override = default;
102
105
106 /**
107 * Drive method for Mecanum platform.
108 *
109 * Angles are measured counterclockwise from the positive X axis. The robot's
110 * velocity is independent from its angle or rotation rate.
111 *
112 * @param xVelocity The robot's velocity along the X axis [-1.0..1.0]. Forward
113 * is positive.
114 * @param yVelocity The robot's velocity along the Y axis [-1.0..1.0]. Left is
115 * positive.
116 * @param zRotation The robot's rotation rate around the Z axis [-1.0..1.0].
117 * Counterclockwise is positive.
118 * @param gyroAngle The gyro heading around the Z axis. Use this to implement
119 * field-oriented controls.
120 */
121 void DriveCartesian(double xVelocity, double yVelocity, double zRotation,
122 wpi::math::Rotation2d gyroAngle = 0_rad);
123
124 /**
125 * Drive method for Mecanum platform.
126 *
127 * Angles are measured counterclockwise from the positive X axis. The robot's
128 * velocity is independent from its angle or rotation rate.
129 *
130 * @param magnitude The robot's velocity at a given angle [-1.0..1.0]. Forward
131 * is positive.
132 * @param angle The angle around the Z axis at which the robot drives.
133 * @param zRotation The robot's rotation rate around the Z axis [-1.0..1.0].
134 * Counterclockwise is positive.
135 */
136 void DrivePolar(double magnitude, wpi::math::Rotation2d angle,
137 double zRotation);
138
139 /**
140 * Cartesian inverse kinematics for Mecanum platform.
141 *
142 * Angles are measured counterclockwise from the positive X axis. The robot's
143 * velocity is independent from its angle or rotation rate.
144 *
145 * @param xVelocity The robot's velocity along the X axis [-1.0..1.0]. Forward
146 * is positive.
147 * @param yVelocity The robot's velocity along the Y axis [-1.0..1.0]. Left is
148 * positive.
149 * @param zRotation The robot's rotation rate around the Z axis [-1.0..1.0].
150 * Counterclockwise is positive.
151 * @param gyroAngle The gyro heading around the Z axis. Use this to implement
152 * field-oriented controls.
153 * @return Wheel velocities [-1.0..1.0].
154 */
156 double xVelocity, double yVelocity, double zRotation,
157 wpi::math::Rotation2d gyroAngle = 0_rad);
158
159 void StopMotor() override;
160 std::string GetDescription() const override;
161
163
164 private:
165 std::function<void(double)> m_frontLeftMotor;
166 std::function<void(double)> m_rearLeftMotor;
167 std::function<void(double)> m_frontRightMotor;
168 std::function<void(double)> m_rearRightMotor;
169
170 // Used for Sendable property getters
171 double m_frontLeftOutput = 0.0;
172 double m_rearLeftOutput = 0.0;
173 double m_frontRightOutput = 0.0;
174 double m_rearRightOutput = 0.0;
175
176 bool reported = false;
177};
178
179} // namespace wpi
void DrivePolar(double magnitude, wpi::math::Rotation2d angle, double zRotation)
Drive method for Mecanum platform.
std::string GetDescription() const override
Returns a description to print when an error occurs.
void StopMotor() override
Called to stop the motor when the timeout expires.
~MecanumDrive() override=default
MecanumDrive(MotorController &frontLeftMotor, MotorController &rearLeftMotor, MotorController &frontRightMotor, MotorController &rearRightMotor)
Construct a MecanumDrive.
MecanumDrive(MecanumDrive &&)=default
static WheelVelocities DriveCartesianIK(double xVelocity, double yVelocity, double zRotation, wpi::math::Rotation2d gyroAngle=0_rad)
Cartesian inverse kinematics for Mecanum platform.
void DriveCartesian(double xVelocity, double yVelocity, double zRotation, wpi::math::Rotation2d gyroAngle=0_rad)
Drive method for Mecanum platform.
void InitSendable(wpi::util::SendableBuilder &builder) override
Initializes this Sendable object.
MecanumDrive & operator=(MecanumDrive &&)=default
MecanumDrive(std::function< void(double)> frontLeftMotor, std::function< void(double)> rearLeftMotor, std::function< void(double)> frontRightMotor, std::function< void(double)> rearRightMotor)
Construct a MecanumDrive.
Interface for motor controlling devices.
Definition MotorController.hpp:14
A rotation in a 2D coordinate frame represented by a point on the unit circle (cosine and sine).
Definition Rotation2d.hpp:29
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.hpp:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.hpp:21
Interface for Sendable objects.
Definition Sendable.hpp:16
Definition CvSource.hpp:15
Wheel velocities for a mecanum drive.
Definition MecanumDrive.hpp:61
double frontRight
Front-right wheel velocity.
Definition MecanumDrive.hpp:65
double rearLeft
Rear-left wheel velocity.
Definition MecanumDrive.hpp:67
double frontLeft
Front-left wheel velocity.
Definition MecanumDrive.hpp:63
double rearRight
Rear-right wheel velocity.
Definition MecanumDrive.hpp:69