[wpimath] Refactor WheelVoltages inner class to a separate file (#4203)

This commit is contained in:
Tyler Veness
2022-05-01 11:01:20 -07:00
committed by GitHub
parent 87bf70fa8e
commit ce1a7d698a
8 changed files with 63 additions and 70 deletions

View File

@@ -21,20 +21,6 @@ public class DifferentialDriveAccelerationLimiter {
private final double m_maxLinearAccel;
private final double m_maxAngularAccel;
/** Motor voltages for a differential drive. */
@SuppressWarnings("MemberName")
public static class WheelVoltages {
public double left;
public double right;
private WheelVoltages() {}
public WheelVoltages(double left, double right) {
this.left = left;
this.right = right;
}
}
/**
* Constructs a DifferentialDriveAccelerationLimiter.
*
@@ -64,7 +50,7 @@ public class DifferentialDriveAccelerationLimiter {
* @return The constrained wheel voltages.
*/
@SuppressWarnings("LocalVariableName")
public WheelVoltages calculate(
public DifferentialDriveWheelVoltages calculate(
double leftVelocity, double rightVelocity, double leftVoltage, double rightVoltage) {
var u = new MatBuilder<>(Nat.N2(), Nat.N1()).fill(leftVoltage, rightVoltage);
@@ -99,6 +85,6 @@ public class DifferentialDriveAccelerationLimiter {
// u = B⁻¹(dx/dt - Ax)
u = m_system.getB().solve(dxdt.minus(m_system.getA().times(x)));
return new WheelVoltages(u.get(0, 0), u.get(1, 0));
return new DifferentialDriveWheelVoltages(u.get(0, 0), u.get(1, 0));
}
}

View File

@@ -0,0 +1,19 @@
// 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.
package edu.wpi.first.math.controller;
/** Motor voltages for a differential drive. */
@SuppressWarnings("MemberName")
public class DifferentialDriveWheelVoltages {
public double left;
public double right;
public DifferentialDriveWheelVoltages() {}
public DifferentialDriveWheelVoltages(double left, double right) {
this.left = left;
this.right = right;
}
}

View File

@@ -37,20 +37,6 @@ public class LTVDifferentialDriveController {
private Matrix<N5, N1> m_error = new Matrix<>(Nat.N5(), Nat.N1());
private Matrix<N5, N1> m_tolerance = new Matrix<>(Nat.N5(), Nat.N1());
/** Motor voltages for a differential drive. */
@SuppressWarnings("MemberName")
public static class WheelVoltages {
public double left;
public double right;
public WheelVoltages() {}
public WheelVoltages(double left, double right) {
this.left = left;
this.right = right;
}
}
/** States of the drivetrain system. */
enum State {
kX(0),
@@ -204,7 +190,7 @@ public class LTVDifferentialDriveController {
* @return Left and right output voltages of the LTV controller.
*/
@SuppressWarnings("LocalVariableName")
public WheelVoltages calculate(
public DifferentialDriveWheelVoltages calculate(
Pose2d currentPose,
double leftVelocity,
double rightVelocity,
@@ -245,7 +231,7 @@ public class LTVDifferentialDriveController {
var u = K.times(inRobotFrame).times(m_error);
return new WheelVoltages(u.get(0, 0), u.get(1, 0));
return new DifferentialDriveWheelVoltages(u.get(0, 0), u.get(1, 0));
}
/**
@@ -260,7 +246,7 @@ public class LTVDifferentialDriveController {
* @param desiredState The desired pose, linear velocity, and angular velocity from a trajectory.
* @return The left and right output voltages of the LTV controller.
*/
public WheelVoltages calculate(
public DifferentialDriveWheelVoltages calculate(
Pose2d currentPose,
double leftVelocity,
double rightVelocity,

View File

@@ -19,8 +19,7 @@ DifferentialDriveAccelerationLimiter::DifferentialDriveAccelerationLimiter(
m_maxLinearAccel{maxLinearAccel},
m_maxAngularAccel{maxAngularAccel} {}
DifferentialDriveAccelerationLimiter::WheelVoltages
DifferentialDriveAccelerationLimiter::Calculate(
DifferentialDriveWheelVoltages DifferentialDriveAccelerationLimiter::Calculate(
units::meters_per_second_t leftVelocity,
units::meters_per_second_t rightVelocity, units::volt_t leftVoltage,
units::volt_t rightVoltage) {

View File

@@ -94,8 +94,7 @@ void LTVDifferentialDriveController::SetTolerance(
leftVelocityTolerance.value(), rightVelocityTolerance.value()};
}
LTVDifferentialDriveController::WheelVoltages
LTVDifferentialDriveController::Calculate(
DifferentialDriveWheelVoltages LTVDifferentialDriveController::Calculate(
const Pose2d& currentPose, units::meters_per_second_t leftVelocity,
units::meters_per_second_t rightVelocity, const Pose2d& poseRef,
units::meters_per_second_t leftVelocityRef,
@@ -124,11 +123,11 @@ LTVDifferentialDriveController::Calculate(
Vectord<2> u = K * inRobotFrame * m_error;
return WheelVoltages{units::volt_t{u(0)}, units::volt_t{u(1)}};
return DifferentialDriveWheelVoltages{units::volt_t{u(0)},
units::volt_t{u(1)}};
}
LTVDifferentialDriveController::WheelVoltages
LTVDifferentialDriveController::Calculate(
DifferentialDriveWheelVoltages LTVDifferentialDriveController::Calculate(
const Pose2d& currentPose, units::meters_per_second_t leftVelocity,
units::meters_per_second_t rightVelocity,
const Trajectory::State& desiredState) {

View File

@@ -7,6 +7,7 @@
#include <wpi/SymbolExports.h>
#include "Eigen/Core"
#include "frc/controller/DifferentialDriveWheelVoltages.h"
#include "frc/system/LinearSystem.h"
#include "units/acceleration.h"
#include "units/angular_acceleration.h"
@@ -25,14 +26,6 @@ namespace frc {
*/
class WPILIB_DLLEXPORT DifferentialDriveAccelerationLimiter {
public:
/**
* Motor voltages for a differential drive.
*/
struct WheelVoltages {
units::volt_t left = 0_V;
units::volt_t right = 0_V;
};
/**
* Constructs a DifferentialDriveAccelerationLimiter.
*
@@ -55,10 +48,10 @@ class WPILIB_DLLEXPORT DifferentialDriveAccelerationLimiter {
* @param rightVoltage The unconstrained right motor voltage.
* @return The constrained wheel voltages.
*/
WheelVoltages Calculate(units::meters_per_second_t leftVelocity,
units::meters_per_second_t rightVelocity,
units::volt_t leftVoltage,
units::volt_t rightVoltage);
DifferentialDriveWheelVoltages Calculate(
units::meters_per_second_t leftVelocity,
units::meters_per_second_t rightVelocity, units::volt_t leftVoltage,
units::volt_t rightVoltage);
private:
LinearSystem<2, 2, 2> m_system;

View File

@@ -0,0 +1,19 @@
// 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.
#pragma once
#include "units/voltage.h"
namespace frc {
/**
* Motor voltages for a differential drive.
*/
struct DifferentialDriveWheelVoltages {
units::volt_t left = 0_V;
units::volt_t right = 0_V;
};
} // namespace frc

View File

@@ -9,6 +9,7 @@
#include <wpi/interpolating_map.h>
#include "frc/EigenCore.h"
#include "frc/controller/DifferentialDriveWheelVoltages.h"
#include "frc/geometry/Pose2d.h"
#include "frc/system/LinearSystem.h"
#include "frc/trajectory/Trajectory.h"
@@ -31,14 +32,6 @@ namespace frc {
*/
class WPILIB_DLLEXPORT LTVDifferentialDriveController {
public:
/**
* Motor voltages for a differential drive.
*/
struct WheelVoltages {
units::volt_t left = 0_V;
units::volt_t right = 0_V;
};
/**
* Constructs a linear time-varying differential drive controller.
*
@@ -95,12 +88,11 @@ class WPILIB_DLLEXPORT LTVDifferentialDriveController {
* @param leftVelocityRef The desired left velocity.
* @param rightVelocityRef The desired right velocity.
*/
WheelVoltages Calculate(const Pose2d& currentPose,
units::meters_per_second_t leftVelocity,
units::meters_per_second_t rightVelocity,
const Pose2d& poseRef,
units::meters_per_second_t leftVelocityRef,
units::meters_per_second_t rightVelocityRef);
DifferentialDriveWheelVoltages Calculate(
const Pose2d& currentPose, units::meters_per_second_t leftVelocity,
units::meters_per_second_t rightVelocity, const Pose2d& poseRef,
units::meters_per_second_t leftVelocityRef,
units::meters_per_second_t rightVelocityRef);
/**
* Returns the left and right output voltages of the LTV controller.
@@ -114,10 +106,10 @@ class WPILIB_DLLEXPORT LTVDifferentialDriveController {
* @param desiredState The desired pose, linear velocity, and angular velocity
* from a trajectory.
*/
WheelVoltages Calculate(const Pose2d& currentPose,
units::meters_per_second_t leftVelocity,
units::meters_per_second_t rightVelocity,
const Trajectory::State& desiredState);
DifferentialDriveWheelVoltages Calculate(
const Pose2d& currentPose, units::meters_per_second_t leftVelocity,
units::meters_per_second_t rightVelocity,
const Trajectory::State& desiredState);
private:
units::meter_t m_trackwidth;