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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Eigen::Vector3d chassisSpeedsVector;
|
|
|
|
|
chassisSpeedsVector << chassisSpeeds.vx.to<double>(),
|
|
|
|
|
chassisSpeeds.vy.to<double>(), chassisSpeeds.omega.to<double>();
|
|
|
|
|
|
|
|
|
|
Eigen::Matrix<double, 4, 1> wheelsMatrix =
|
|
|
|
|
m_inverseKinematics * chassisSpeedsVector;
|
|
|
|
|
|
|
|
|
|
MecanumDriveWheelSpeeds wheelSpeeds;
|
|
|
|
|
wheelSpeeds.frontLeft = units::meters_per_second_t{wheelsMatrix(0, 0)};
|
|
|
|
|
wheelSpeeds.frontRight = units::meters_per_second_t{wheelsMatrix(1, 0)};
|
|
|
|
|
wheelSpeeds.rearLeft = units::meters_per_second_t{wheelsMatrix(2, 0)};
|
|
|
|
|
wheelSpeeds.rearRight = units::meters_per_second_t{wheelsMatrix(3, 0)};
|
|
|
|
|
return wheelSpeeds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChassisSpeeds MecanumDriveKinematics::ToChassisSpeeds(
|
2020-04-14 01:32:25 -04:00
|
|
|
const MecanumDriveWheelSpeeds& wheelSpeeds) const {
|
2019-09-08 00:11:49 -04:00
|
|
|
Eigen::Matrix<double, 4, 1> wheelSpeedsMatrix;
|
|
|
|
|
// clang-format off
|
|
|
|
|
wheelSpeedsMatrix << wheelSpeeds.frontLeft.to<double>(), wheelSpeeds.frontRight.to<double>(),
|
|
|
|
|
wheelSpeeds.rearLeft.to<double>(), wheelSpeeds.rearRight.to<double>();
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
Eigen::Vector3d chassisSpeedsVector =
|
|
|
|
|
m_forwardKinematics.solve(wheelSpeedsMatrix);
|
|
|
|
|
|
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)}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MecanumDriveKinematics::SetInverseKinematics(Translation2d fl,
|
|
|
|
|
Translation2d fr,
|
|
|
|
|
Translation2d rl,
|
2020-04-14 01:32:25 -04:00
|
|
|
Translation2d rr) const {
|
2019-09-08 00:11:49 -04:00
|
|
|
// clang-format off
|
|
|
|
|
m_inverseKinematics << 1, -1, (-(fl.X() + fl.Y())).template to<double>(),
|
|
|
|
|
1, 1, (fr.X() - fr.Y()).template to<double>(),
|
|
|
|
|
1, 1, (rl.X() - rl.Y()).template to<double>(),
|
|
|
|
|
1, -1, (-(rr.X() + rr.Y())).template to<double>();
|
|
|
|
|
// clang-format on
|
|
|
|
|
m_inverseKinematics /= std::sqrt(2);
|
|
|
|
|
}
|