mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpimath] Clean up VecBuilder and MatBuilder (#5906)
This commit is contained in:
@@ -5,7 +5,10 @@
|
||||
package edu.wpi.first.math.controller;
|
||||
|
||||
import edu.wpi.first.math.MatBuilder;
|
||||
import edu.wpi.first.math.Matrix;
|
||||
import edu.wpi.first.math.Nat;
|
||||
import edu.wpi.first.math.VecBuilder;
|
||||
import edu.wpi.first.math.numbers.N1;
|
||||
import edu.wpi.first.math.numbers.N2;
|
||||
import edu.wpi.first.math.system.LinearSystem;
|
||||
|
||||
@@ -80,10 +83,10 @@ public class DifferentialDriveAccelerationLimiter {
|
||||
*/
|
||||
public DifferentialDriveWheelVoltages calculate(
|
||||
double leftVelocity, double rightVelocity, double leftVoltage, double rightVoltage) {
|
||||
var u = new MatBuilder<>(Nat.N2(), Nat.N1()).fill(leftVoltage, rightVoltage);
|
||||
Matrix<N2, N1> u = VecBuilder.fill(leftVoltage, rightVoltage);
|
||||
|
||||
// Find unconstrained wheel accelerations
|
||||
var x = new MatBuilder<>(Nat.N2(), Nat.N1()).fill(leftVelocity, rightVelocity);
|
||||
var x = VecBuilder.fill(leftVelocity, rightVelocity);
|
||||
var dxdt = m_system.getA().times(x).plus(m_system.getB().times(u));
|
||||
|
||||
// Convert from wheel accelerations to linear and angular accelerations
|
||||
@@ -98,9 +101,7 @@ public class DifferentialDriveAccelerationLimiter {
|
||||
// [α] [-1/trackwidth 1/trackwidth][dxdt(1)]
|
||||
//
|
||||
// accels = M dxdt where M = [0.5, 0.5; -1/trackwidth, 1/trackwidth]
|
||||
var M =
|
||||
new MatBuilder<>(Nat.N2(), Nat.N2())
|
||||
.fill(0.5, 0.5, -1.0 / m_trackwidth, 1.0 / m_trackwidth);
|
||||
var M = MatBuilder.fill(Nat.N2(), Nat.N2(), 0.5, 0.5, -1.0 / m_trackwidth, 1.0 / m_trackwidth);
|
||||
var accels = M.times(dxdt);
|
||||
|
||||
// Constrain the linear and angular accelerations
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
package edu.wpi.first.math.controller;
|
||||
|
||||
import edu.wpi.first.math.Matrix;
|
||||
import edu.wpi.first.math.MatBuilder;
|
||||
import edu.wpi.first.math.Nat;
|
||||
import edu.wpi.first.math.system.plant.LinearSystemId;
|
||||
|
||||
@@ -93,8 +93,8 @@ public class ElevatorFeedforward {
|
||||
var plant = LinearSystemId.identifyVelocitySystem(this.kv, this.ka);
|
||||
var feedforward = new LinearPlantInversionFeedforward<>(plant, dtSeconds);
|
||||
|
||||
var r = Matrix.mat(Nat.N1(), Nat.N1()).fill(currentVelocity);
|
||||
var nextR = Matrix.mat(Nat.N1(), Nat.N1()).fill(nextVelocity);
|
||||
var r = MatBuilder.fill(Nat.N1(), Nat.N1(), currentVelocity);
|
||||
var nextR = MatBuilder.fill(Nat.N1(), Nat.N1(), nextVelocity);
|
||||
|
||||
return kg + ks * Math.signum(currentVelocity) + feedforward.calculate(r, nextR).get(0, 0);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import edu.wpi.first.math.MathUtil;
|
||||
import edu.wpi.first.math.Matrix;
|
||||
import edu.wpi.first.math.Nat;
|
||||
import edu.wpi.first.math.StateSpaceUtil;
|
||||
import edu.wpi.first.math.VecBuilder;
|
||||
import edu.wpi.first.math.Vector;
|
||||
import edu.wpi.first.math.geometry.Pose2d;
|
||||
import edu.wpi.first.math.numbers.N1;
|
||||
@@ -88,46 +89,48 @@ public class LTVDifferentialDriveController {
|
||||
// Control law derivation is in section 8.7 of
|
||||
// https://file.tavsys.net/control/controls-engineering-in-frc.pdf
|
||||
var A =
|
||||
new MatBuilder<>(Nat.N5(), Nat.N5())
|
||||
.fill(
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.5,
|
||||
0.5,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
-1.0 / m_trackwidth,
|
||||
1.0 / m_trackwidth,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
plant.getA(0, 0),
|
||||
plant.getA(0, 1),
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
plant.getA(1, 0),
|
||||
plant.getA(1, 1));
|
||||
MatBuilder.fill(
|
||||
Nat.N5(),
|
||||
Nat.N5(),
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.5,
|
||||
0.5,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
-1.0 / m_trackwidth,
|
||||
1.0 / m_trackwidth,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
plant.getA(0, 0),
|
||||
plant.getA(0, 1),
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
plant.getA(1, 0),
|
||||
plant.getA(1, 1));
|
||||
var B =
|
||||
new MatBuilder<>(Nat.N5(), Nat.N2())
|
||||
.fill(
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
plant.getB(0, 0),
|
||||
plant.getB(0, 1),
|
||||
plant.getB(1, 0),
|
||||
plant.getB(1, 1));
|
||||
MatBuilder.fill(
|
||||
Nat.N5(),
|
||||
Nat.N2(),
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
plant.getB(0, 0),
|
||||
plant.getB(0, 1),
|
||||
plant.getB(1, 0),
|
||||
plant.getB(1, 1));
|
||||
var Q = StateSpaceUtil.makeCostMatrix(qelems);
|
||||
var R = StateSpaceUtil.makeCostMatrix(relems);
|
||||
|
||||
@@ -136,11 +139,7 @@ public class LTVDifferentialDriveController {
|
||||
// Ax = -Bu
|
||||
// x = -A⁻¹Bu
|
||||
double maxV =
|
||||
plant
|
||||
.getA()
|
||||
.solve(plant.getB().times(new MatBuilder<>(Nat.N2(), Nat.N1()).fill(12.0, 12.0)))
|
||||
.times(-1.0)
|
||||
.get(0, 0);
|
||||
plant.getA().solve(plant.getB().times(VecBuilder.fill(12.0, 12.0))).times(-1.0).get(0, 0);
|
||||
|
||||
if (maxV <= 0.0) {
|
||||
throw new IllegalArgumentException(
|
||||
@@ -201,13 +200,12 @@ public class LTVDifferentialDriveController {
|
||||
public void setTolerance(
|
||||
Pose2d poseTolerance, double leftVelocityTolerance, double rightVelocityTolerance) {
|
||||
m_tolerance =
|
||||
new MatBuilder<>(Nat.N5(), Nat.N1())
|
||||
.fill(
|
||||
poseTolerance.getX(),
|
||||
poseTolerance.getY(),
|
||||
poseTolerance.getRotation().getRadians(),
|
||||
leftVelocityTolerance,
|
||||
rightVelocityTolerance);
|
||||
VecBuilder.fill(
|
||||
poseTolerance.getX(),
|
||||
poseTolerance.getY(),
|
||||
poseTolerance.getRotation().getRadians(),
|
||||
leftVelocityTolerance,
|
||||
rightVelocityTolerance);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -234,13 +232,12 @@ public class LTVDifferentialDriveController {
|
||||
// This implements the linear time-varying differential drive controller in
|
||||
// theorem 9.6.3 of https://tavsys.net/controls-in-frc.
|
||||
var x =
|
||||
new MatBuilder<>(Nat.N5(), Nat.N1())
|
||||
.fill(
|
||||
currentPose.getX(),
|
||||
currentPose.getY(),
|
||||
currentPose.getRotation().getRadians(),
|
||||
leftVelocity,
|
||||
rightVelocity);
|
||||
VecBuilder.fill(
|
||||
currentPose.getX(),
|
||||
currentPose.getY(),
|
||||
currentPose.getRotation().getRadians(),
|
||||
leftVelocity,
|
||||
rightVelocity);
|
||||
|
||||
var inRobotFrame = Matrix.eye(Nat.N5());
|
||||
inRobotFrame.set(0, 0, Math.cos(x.get(State.kHeading.value, 0)));
|
||||
@@ -249,13 +246,12 @@ public class LTVDifferentialDriveController {
|
||||
inRobotFrame.set(1, 1, Math.cos(x.get(State.kHeading.value, 0)));
|
||||
|
||||
var r =
|
||||
new MatBuilder<>(Nat.N5(), Nat.N1())
|
||||
.fill(
|
||||
poseRef.getX(),
|
||||
poseRef.getY(),
|
||||
poseRef.getRotation().getRadians(),
|
||||
leftVelocityRef,
|
||||
rightVelocityRef);
|
||||
VecBuilder.fill(
|
||||
poseRef.getX(),
|
||||
poseRef.getY(),
|
||||
poseRef.getRotation().getRadians(),
|
||||
leftVelocityRef,
|
||||
rightVelocityRef);
|
||||
m_error = r.minus(x);
|
||||
m_error.set(
|
||||
State.kHeading.value, 0, MathUtil.angleModulus(m_error.get(State.kHeading.value, 0)));
|
||||
|
||||
@@ -149,7 +149,7 @@ public class LTVUnicycleController {
|
||||
// A = [0 0 v] B = [0 0]
|
||||
// [0 0 0] [0 1]
|
||||
var A = new Matrix<>(Nat.N3(), Nat.N3());
|
||||
var B = new MatBuilder<>(Nat.N3(), Nat.N2()).fill(1.0, 0.0, 0.0, 0.0, 0.0, 1.0);
|
||||
var B = MatBuilder.fill(Nat.N3(), Nat.N2(), 1.0, 0.0, 0.0, 0.0, 0.0, 1.0);
|
||||
var Q = StateSpaceUtil.makeCostMatrix(qelems);
|
||||
var R = StateSpaceUtil.makeCostMatrix(relems);
|
||||
|
||||
@@ -226,8 +226,12 @@ public class LTVUnicycleController {
|
||||
|
||||
var K = m_table.get(linearVelocityRef);
|
||||
var e =
|
||||
new MatBuilder<>(Nat.N3(), Nat.N1())
|
||||
.fill(m_poseError.getX(), m_poseError.getY(), m_poseError.getRotation().getRadians());
|
||||
MatBuilder.fill(
|
||||
Nat.N3(),
|
||||
Nat.N1(),
|
||||
m_poseError.getX(),
|
||||
m_poseError.getY(),
|
||||
m_poseError.getRotation().getRadians());
|
||||
var u = K.times(e);
|
||||
|
||||
return new ChassisSpeeds(
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
package edu.wpi.first.math.controller;
|
||||
|
||||
import edu.wpi.first.math.Matrix;
|
||||
import edu.wpi.first.math.MatBuilder;
|
||||
import edu.wpi.first.math.Nat;
|
||||
import edu.wpi.first.math.system.plant.LinearSystemId;
|
||||
|
||||
@@ -62,8 +62,8 @@ public class SimpleMotorFeedforward {
|
||||
var plant = LinearSystemId.identifyVelocitySystem(this.kv, this.ka);
|
||||
var feedforward = new LinearPlantInversionFeedforward<>(plant, dtSeconds);
|
||||
|
||||
var r = Matrix.mat(Nat.N1(), Nat.N1()).fill(currentVelocity);
|
||||
var nextR = Matrix.mat(Nat.N1(), Nat.N1()).fill(nextVelocity);
|
||||
var r = MatBuilder.fill(Nat.N1(), Nat.N1(), currentVelocity);
|
||||
var nextR = MatBuilder.fill(Nat.N1(), Nat.N1(), nextVelocity);
|
||||
|
||||
return ks * Math.signum(currentVelocity) + feedforward.calculate(r, nextR).get(0, 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user