Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
DriveFFController using bilinear regression
Browse files Browse the repository at this point in the history
  • Loading branch information
EtherexStudios committed Jul 15, 2024
1 parent 1e634f7 commit 9494b38
Show file tree
Hide file tree
Showing 36 changed files with 135 additions and 42 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file modified .gradle/8.5/checksums/checksums.lock
Binary file not shown.
Binary file modified .gradle/8.5/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/8.5/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/8.5/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/8.5/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/8.5/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/file-system.probe
Binary file not shown.
Binary file added bin/main/.DS_Store
Binary file not shown.
Binary file added bin/main/frc/.DS_Store
Binary file not shown.
Binary file added bin/main/frc/robot/.DS_Store
Binary file not shown.
Binary file modified bin/main/frc/robot/commands/shooter/ShooterWindup.class
Binary file not shown.
Binary file added bin/main/frc/robot/subsystems/.DS_Store
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified bin/main/frc/robot/subsystems/drivetrain/swerve/SwerveDrive.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/libs/2024-Crescendo-Offseason.jar
Binary file not shown.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
Binary file added src/main/java/.DS_Store
Binary file not shown.
Binary file added src/main/java/frc/.DS_Store
Binary file not shown.
Binary file added src/main/java/frc/robot/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package frc.robot.commands.shooter;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import frc.robot.subsystems.shooter.pivot.flywheel.Flywheel;

public class ShooterWindup extends Command {
Expand Down
Binary file added src/main/java/frc/robot/subsystems/.DS_Store
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package frc.robot.subsystems.drivetrain.swerve;

import java.util.ArrayList;

public class DriveFFController {
ArrayList<double[]> points = new ArrayList<double[]>();
public DriveFFController() {
// drive meters per second, radians per second, mps error
points.add(new double[] {0, 0, 0});
points.add(new double[] {1, 0, 0});
points.add(new double[] {2, 0, 0});
points.add(new double[] {0, Math.PI, 0});
points.add(new double[] {0, 2 * Math.PI, 0});
points.add(new double[] {2, 2 * Math.PI, 0.45});
points.add(new double[] {1, 2 * Math.PI, 0.212});
points.add(new double[] {2, Math.PI, 0.344});
points.add(new double[] {1, Math.PI, 0.25});
}

public double calculate(double imps, double irps) {
double mps = Math.abs(imps);
double rps = Math.abs(irps);
for (int i = 0; i < points.size(); i++) {
if (points.get(i)[0] == mps && points.get(i)[1] == rps) {
return points.get(i)[2];
}
}

double[] inputPoint = new double[] {mps, rps, 0};

double[][] p = new double[4][3];
for (int i = 0; i < 4; i++) {
p[i] = new double[] {Integer.MAX_VALUE, Integer.MAX_VALUE, -1};
}

ArrayList<double[]> temp = points;
for (int n = 0; n < 4; n++) {
int index = 0;
for(int i = 0; i < temp.size(); i++) {
if (distance(temp.get(i), inputPoint) < distance(p[n], inputPoint)) {
boolean good = true;
for (int m = 0; m < n; m++) {
if (distance(p[m], temp.get(i)) > Math.sqrt(2) + 0.1) {
good = false;
}
}
if (good) {
p[n] = temp.get(i);
index = i;
}
}
}
temp.remove(index);
}

double[] tr, tl, br, bl;
tr = new double[] {-Integer.MAX_VALUE, -Integer.MAX_VALUE, 0};
tl = new double[] {Integer.MAX_VALUE, -Integer.MAX_VALUE, 0};
br = new double[] {-Integer.MAX_VALUE, Integer.MAX_VALUE, 0};
bl = new double[] {Integer.MAX_VALUE, Integer.MAX_VALUE, 0};

boolean trT = false;
boolean tlT = false;
boolean brT = false;
boolean blT = false;


for (int i = 0; i < 4; i++) {
if (p[i][0] >= inputPoint[0] && p[i][1] >= inputPoint[1] && !trT) {
tr = p[i];
trT = true;
}
else if (p[i][0] <= inputPoint[0] && p[i][1] >= inputPoint[1] && !tlT) {
tl = p[i];
tlT = true;
}
else if (p[i][0] >= inputPoint[0] && p[i][1] <= inputPoint[1] && !brT) {
br = p[i];
brT = true;
}
else if (p[i][0] <= inputPoint[0] && p[i][1] <= inputPoint[1] && !blT) {
bl = p[i];
blT = true;
}
}

double r1 = (tr[0] - inputPoint[0])/(tr[0] - tl[0]) * tl[2] + (inputPoint[0] - tl[0])/(tr[0] - tl[0]) * tr[2];
double r2 = (br[0] - inputPoint[0])/(br[0] - bl[0]) * bl[2] + (inputPoint[0] - bl[0])/(br[0] - bl[0]) * br[2];
double pf = (inputPoint[1] - br[1])/(tr[1] - br[1]) * r1 + (tr[1] - inputPoint[1])/(tr[1] - bl[1]) * r2;

if (Double.isNaN(r1)) {
return r2;
}
if (Double.isNaN(r2)) {
return r1;
}

if (mps >= 0 && rps >= 0) {
return pf;
}

if (mps >= 0 && rps <= 0) {
return -pf;
}

if (mps <= 0 && rps >= 0) {
return -pf;
}

return pf;

}

private double distance(double[] a, double[] b) {
return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow((a[1] - b[1]) / Math.PI, 2));
}

public static void main(String[] args) {
DriveFFController driveFFController = new DriveFFController();

System.out.println(driveFFController.calculate(1, 1));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

import org.littletonrobotics.junction.Logger;

import edu.wpi.first.math.InterpolatingMatrixTreeMap;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.math.estimator.SwerveDrivePoseEstimator;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Rotation3d;
import edu.wpi.first.math.interpolation.InterpolatingDoubleTreeMap;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
import edu.wpi.first.math.kinematics.SwerveModulePosition;
Expand Down Expand Up @@ -57,10 +59,10 @@ public class SwerveDrive extends SubsystemBase{
private ChassisSpeeds measuredRobotRelativeSpeeds = new ChassisSpeeds(0,0,0);
private ChassisSpeeds measuredFeildRelativeSpeeds = new ChassisSpeeds(0,0,0);

private PIDController m_angleFeedbackController = new PIDController(0.00, 0.0, 0.000);
private SimpleMotorFeedforward m_angleFeedForwardController = new SimpleMotorFeedforward(0, 1);
private PIDController m_angleFeedbackController;
private DriveFFController driveFFController;

private PIDController m_translationalFeedbackController = new PIDController(0.00, 0.0, 0.000);
private PIDController m_translationalFeedbackController;

private SwerveModuleState[] measuredModuleStates = {
new SwerveModuleState(),
Expand All @@ -81,6 +83,8 @@ public SwerveDrive() {
new ModuleIOSim()
};
gyroIO = new GyroIO() {};


break;
default:
modules = new ModuleIO[] {
Expand Down Expand Up @@ -182,16 +186,18 @@ public void periodic() {
m_poseEstimator.getEstimatedPosition().getRotation().getRadians()
});

desiredRobotRelativeSpeeds = ChassisSpeeds.fromFieldRelativeSpeeds(desiredFeildRelativeSpeeds, yaw);
Logger.recordOutput("SwerveDrive/desiredRobotRelativeSpeeds", desiredRobotRelativeSpeeds);
Logger.recordOutput("SwerveDrive/measuredRobotRelativeSpeeds", measuredRobotRelativeSpeeds);
Logger.recordOutput("SwerveDrive/desiredFeildRelativeSpeeds", desiredFeildRelativeSpeeds);
Logger.recordOutput("SwerveDrive/measuredFeildRelativeSpeeds", measuredFeildRelativeSpeeds);


ChassisSpeeds correctedSpeeds = desiredRobotRelativeSpeeds;
correctedSpeeds.omegaRadiansPerSecond =
m_angleFeedForwardController.calculate(desiredRobotRelativeSpeeds.omegaRadiansPerSecond, Math.signum(desiredRobotRelativeSpeeds.omegaRadiansPerSecond - measuredRobotRelativeSpeeds.omegaRadiansPerSecond)) +
m_angleFeedbackController.calculate(measuredRobotRelativeSpeeds.omegaRadiansPerSecond, desiredRobotRelativeSpeeds.omegaRadiansPerSecond);
// correctedSpeeds.omegaRadiansPerSecond =
// m_angleFeedForwardController.calculate(desiredRobotRelativeSpeeds.omegaRadiansPerSecond, Math.signum(desiredRobotRelativeSpeeds.omegaRadiansPerSecond - measuredRobotRelativeSpeeds.omegaRadiansPerSecond)) +
// m_angleFeedbackController.calculate(measuredRobotRelativeSpeeds.omegaRadiansPerSecond, desiredRobotRelativeSpeeds.omegaRadiansPerSecond);


Logger.recordOutput("SwerveDrive/correctedSpeeds", correctedSpeeds);

Expand All @@ -206,7 +212,6 @@ public void periodic() {

public void driveFieldRelative(ChassisSpeeds speeds) {
desiredFeildRelativeSpeeds = speeds;
desiredRobotRelativeSpeeds = ChassisSpeeds.fromFieldRelativeSpeeds(speeds, yaw);
}

public void driveRobotRelatve(ChassisSpeeds speeds) {
Expand Down

0 comments on commit 9494b38

Please sign in to comment.