2019-09-08 00:11:49 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-06-29 22:25:09 -07:00
|
|
|
/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */
|
2019-09-08 00:11:49 -04:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "frc/kinematics/MecanumDriveWheelSpeeds.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2020-06-29 22:25:09 -07:00
|
|
|
#include <units/math.h>
|
|
|
|
|
|
2019-09-08 00:11:49 -04:00
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
void MecanumDriveWheelSpeeds::Normalize(
|
|
|
|
|
units::meters_per_second_t attainableMaxSpeed) {
|
|
|
|
|
std::array<units::meters_per_second_t, 4> wheelSpeeds{frontLeft, frontRight,
|
|
|
|
|
rearLeft, rearRight};
|
|
|
|
|
units::meters_per_second_t realMaxSpeed = *std::max_element(
|
|
|
|
|
wheelSpeeds.begin(), wheelSpeeds.end(), [](const auto& a, const auto& b) {
|
|
|
|
|
return units::math::abs(a) < units::math::abs(b);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (realMaxSpeed > attainableMaxSpeed) {
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
|
wheelSpeeds[i] = wheelSpeeds[i] / realMaxSpeed * attainableMaxSpeed;
|
|
|
|
|
}
|
|
|
|
|
frontLeft = wheelSpeeds[0];
|
|
|
|
|
frontRight = wheelSpeeds[1];
|
|
|
|
|
rearLeft = wheelSpeeds[2];
|
|
|
|
|
rearRight = wheelSpeeds[3];
|
|
|
|
|
}
|
|
|
|
|
}
|