2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
#include "frc/kinematics/MecanumDriveKinematics.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
MecanumDriveWheelSpeeds MecanumDriveKinematics::ToWheelSpeeds(
|
2020-04-14 01:32:25 -04:00
|
|
|
const ChassisSpeeds& chassisSpeeds,
|
|
|
|
|
const Translation2d& centerOfRotation) const {
|
2019-09-08 00:11:49 -04:00
|
|
|
// We have a new center of rotation. We need to compute the matrix again.
|
2019-09-08 14:20:26 -04:00
|
|
|
if (centerOfRotation != m_previousCoR) {
|
2019-09-08 00:11:49 -04:00
|
|
|
auto fl = m_frontLeftWheel - centerOfRotation;
|
|
|
|
|
auto fr = m_frontRightWheel - centerOfRotation;
|
|
|
|
|
auto rl = m_rearLeftWheel - centerOfRotation;
|
|
|
|
|
auto rr = m_rearRightWheel - centerOfRotation;
|
|
|
|
|
|
|
|
|
|
SetInverseKinematics(fl, fr, rl, rr);
|
|
|
|
|
|
|
|
|
|
m_previousCoR = centerOfRotation;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 08:58:12 -07:00
|
|
|
Eigen::Vector3d chassisSpeedsVector{chassisSpeeds.vx.value(),
|
|
|
|
|
chassisSpeeds.vy.value(),
|
|
|
|
|
chassisSpeeds.omega.value()};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2022-04-29 22:29:20 -07:00
|
|
|
Vectord<4> wheelsVector = m_inverseKinematics * chassisSpeedsVector;
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
MecanumDriveWheelSpeeds wheelSpeeds;
|
2021-08-19 00:23:48 -07:00
|
|
|
wheelSpeeds.frontLeft = units::meters_per_second_t{wheelsVector(0)};
|
|
|
|
|
wheelSpeeds.frontRight = units::meters_per_second_t{wheelsVector(1)};
|
|
|
|
|
wheelSpeeds.rearLeft = units::meters_per_second_t{wheelsVector(2)};
|
|
|
|
|
wheelSpeeds.rearRight = units::meters_per_second_t{wheelsVector(3)};
|
2019-09-08 00:11:49 -04:00
|
|
|
return wheelSpeeds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChassisSpeeds MecanumDriveKinematics::ToChassisSpeeds(
|
2020-04-14 01:32:25 -04:00
|
|
|
const MecanumDriveWheelSpeeds& wheelSpeeds) const {
|
2022-04-29 22:29:20 -07:00
|
|
|
Vectord<4> wheelSpeedsVector{
|
2021-10-25 08:58:12 -07:00
|
|
|
wheelSpeeds.frontLeft.value(), wheelSpeeds.frontRight.value(),
|
|
|
|
|
wheelSpeeds.rearLeft.value(), wheelSpeeds.rearRight.value()};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
Eigen::Vector3d chassisSpeedsVector =
|
2021-08-19 00:23:48 -07:00
|
|
|
m_forwardKinematics.solve(wheelSpeedsVector);
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2021-01-01 10:27:49 -08:00
|
|
|
return {units::meters_per_second_t{chassisSpeedsVector(0)}, // NOLINT
|
2019-09-08 00:11:49 -04:00
|
|
|
units::meters_per_second_t{chassisSpeedsVector(1)},
|
|
|
|
|
units::radians_per_second_t{chassisSpeedsVector(2)}};
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 15:28:59 -04:00
|
|
|
Twist2d MecanumDriveKinematics::ToTwist2d(
|
|
|
|
|
const MecanumDriveWheelPositions& wheelDeltas) const {
|
|
|
|
|
Vectord<4> wheelDeltasVector{
|
|
|
|
|
wheelDeltas.frontLeft.value(), wheelDeltas.frontRight.value(),
|
|
|
|
|
wheelDeltas.rearLeft.value(), wheelDeltas.rearRight.value()};
|
|
|
|
|
|
|
|
|
|
Eigen::Vector3d twistVector = m_forwardKinematics.solve(wheelDeltasVector);
|
|
|
|
|
|
|
|
|
|
return {units::meter_t{twistVector(0)}, // NOLINT
|
|
|
|
|
units::meter_t{twistVector(1)}, units::radian_t{twistVector(2)}};
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-08 00:11:49 -04:00
|
|
|
void MecanumDriveKinematics::SetInverseKinematics(Translation2d fl,
|
|
|
|
|
Translation2d fr,
|
|
|
|
|
Translation2d rl,
|
2020-04-14 01:32:25 -04:00
|
|
|
Translation2d rr) const {
|
2022-04-29 22:29:20 -07:00
|
|
|
m_inverseKinematics = Matrixd<4, 3>{{1, -1, (-(fl.X() + fl.Y())).value()},
|
|
|
|
|
{1, 1, (fr.X() - fr.Y()).value()},
|
|
|
|
|
{1, 1, (rl.X() - rl.Y()).value()},
|
|
|
|
|
{1, -1, (-(rr.X() + rr.Y())).value()}};
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|