mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[wpilib] Clamp input voltage in sim classes (#2955)
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <units/current.h>
|
||||
#include <units/time.h>
|
||||
|
||||
#include "frc/RobotController.h"
|
||||
#include "frc/StateSpaceUtil.h"
|
||||
#include "frc/system/LinearSystem.h"
|
||||
|
||||
@@ -82,7 +83,9 @@ class LinearSystemSim {
|
||||
*
|
||||
* @param u The system inputs.
|
||||
*/
|
||||
void SetInput(const Eigen::Matrix<double, Inputs, 1>& u) { m_u = u; }
|
||||
void SetInput(const Eigen::Matrix<double, Inputs, 1>& u) {
|
||||
m_u = ClampInput(u);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets the system inputs.
|
||||
@@ -90,7 +93,10 @@ class LinearSystemSim {
|
||||
* @param row The row in the input matrix to set.
|
||||
* @param value The value to set the row to.
|
||||
*/
|
||||
void SetInput(int row, double value) { m_u(row, 0) = value; }
|
||||
void SetInput(int row, double value) {
|
||||
m_u(row, 0) = value;
|
||||
ClampInput(m_u);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the system state.
|
||||
@@ -123,6 +129,19 @@ class LinearSystemSim {
|
||||
return m_plant.CalculateX(currentXhat, u, dt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clamp the input vector such that no element exceeds the given voltage. If
|
||||
* any does, the relative magnitudes of the input will be maintained.
|
||||
*
|
||||
* @param u The input vector.
|
||||
* @return The normalized input.
|
||||
*/
|
||||
Eigen::Matrix<double, Inputs, 1> ClampInput(
|
||||
Eigen::Matrix<double, Inputs, 1> u) {
|
||||
return frc::NormalizeInputVector<Inputs>(
|
||||
u, frc::RobotController::GetInputVoltage());
|
||||
}
|
||||
|
||||
LinearSystem<States, Inputs, Outputs> m_plant;
|
||||
|
||||
Eigen::Matrix<double, States, 1> m_x;
|
||||
|
||||
Reference in New Issue
Block a user