Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cherriae committed Jan 25, 2024
2 parents 1c7f324 + 86e253b commit b01c659
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.kinematics.SwerveDriveKinematics;
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine;

/**
* The Constants class provides a convenient place for teams to hold robot-wide numerical or boolean
Expand Down Expand Up @@ -76,7 +77,10 @@ public static class Physical {
}

public static class FeedForward {
public static final double ELEVATOR_KG = 0.0; // TODO: Find Kg constant.
public static final double ELEVATOR_KG = 0.0; // TODO: Find Kg constants

public static final double MODULE_DRIVE_KS = 0.0;
public static final double MODULE_DRIVE_KV = 0.0;
}

public static class PID {
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/frc/robot/subsystems/SwerveDriveSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.kinematics.SwerveModulePosition;
import edu.wpi.first.math.kinematics.SwerveModuleState;
import edu.wpi.first.units.Measure;
import edu.wpi.first.units.Units;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.StructArrayPublisher;
import edu.wpi.first.wpilibj.DriverStation;
Expand All @@ -22,12 +24,14 @@
import edu.wpi.first.wpilibj.smartdashboard.Field2d;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine;
import frc.robot.Constants;
import frc.robot.utils.BNO055;
import frc.robot.utils.SwerveModule;
import frc.robot.utils.UtilFuncs;

import java.util.Optional;

import javax.swing.text.html.Option;

/**
* @author Peter Gutkovich
Expand Down Expand Up @@ -101,6 +105,20 @@ public class SwerveDriveSubsystem extends SubsystemBase {

StructArrayPublisher<SwerveModuleState> publisher = NetworkTableInstance.getDefault().getStructArrayTopic("MyStates", SwerveModuleState.struct).publish();

// private final SysIdRoutine _sysID = new SysIdRoutine(
// new SysIdRoutine.Config(),
// new SysIdRoutine.Mechanism(
// (volts) -> {
// driveTest(UtilFuncs.FromVolts(volts.in(Units.Volts)));
// },
// (log) -> {
// new Measure()
// // log.motor("Front Left").linearVelocity(_frontLeft.getDriveVelocity());
// },
// this
// )
// );

// Pose Estimator -> Has built in odometry and uses supplied vision measurements
private final SwerveDrivePoseEstimator _estimator = new SwerveDrivePoseEstimator(
Constants.Physical.SWERVE_KINEMATICS,
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/frc/robot/utils/SwerveModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.ctre.phoenix6.hardware.TalonFX;
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.SwerveModulePosition;
import edu.wpi.first.math.kinematics.SwerveModuleState;
Expand All @@ -24,6 +25,11 @@ public class SwerveModule {
private final PIDController _driveController;
private final PIDController _rotationController;

private final SimpleMotorFeedforward _driveFeedforward = new SimpleMotorFeedforward(
Constants.FeedForward.MODULE_DRIVE_KS,
Constants.FeedForward.MODULE_DRIVE_KV
);

private final CANcoder _encoder;

private final String _name;
Expand Down Expand Up @@ -153,7 +159,8 @@ public void setState(SwerveModuleState state) {
0.150
);

double drive_feedforward = (speed / Constants.Speeds.SWERVE_DRIVE_MAX_SPEED);
// double drive_feedforward = (speed / Constants.Speeds.SWERVE_DRIVE_MAX_SPEED);
double drive_feedforward = UtilFuncs.FromVolts(_driveFeedforward.calculate(speed));
double drive_pid = _driveController.calculate(getDriveVelocity(), speed);

rotate(rotation_pid);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/frc/robot/utils/UtilFuncs.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package frc.robot.utils;

import edu.wpi.first.wpilibj.RobotController;

/** Any utility functions are here. */
public final class UtilFuncs {
/**
Expand All @@ -19,4 +21,14 @@ public static double ApplyDeadband(double val, double deadband) {

return 0;
}

/**
* Control a motor controller with voltage by converting a voltage output into percent output with a scale factor.
*
* @param volts The voltage output.
* @return The percent output to set the controller to.
*/
public static double FromVolts(double volts) {
return volts / 12.0;
}
}

0 comments on commit b01c659

Please sign in to comment.