Skip to content

Commit

Permalink
Resolve merge conflicts from dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/swervelib/SwerveModule.java
  • Loading branch information
fovea1959 committed Jan 25, 2024
2 parents 159d858 + 399fdc9 commit 0788a62
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 15 deletions.
30 changes: 29 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ <h1 class="d-inline display-6">Front Left Module</h1>
<label class="form-label" for="frontleftencoder_canbus-input">Absolute Encoder CAN
Bus</label>
</div>
<div class="form-check">
<input class="form-check-input" id="frontleft_absoluteencoderinverted-input" name="absoluteEncoderInverted"
type="checkbox">
<label class="form-check-label" for="frontleft_absoluteencoderinverted-input">
Absolute Encoder Inverted
</label>
</div>
</form>
</div>
</div>
Expand Down Expand Up @@ -580,6 +587,13 @@ <h1 class="d-inline display-6">Front Right Module</h1>
<label class="form-label" for="frontrightencoder_canbus-input">Absolute Encoder CAN
Bus</label>
</div>
<div class="form-check">
<input class="form-check-input" id="frontright_absoluteencoderinverted-input" name="absoluteEncoderInverted"
type="checkbox">
<label class="form-check-label" for="frontright_absoluteencoderinverted-input">
Absolute Encoder Inverted
</label>
</div>

</form>
</div>
Expand Down Expand Up @@ -782,6 +796,14 @@ <h1 class="d-inline display-6">Back Left Module</h1>
Bus</label>
</div>

<div class="form-check">
<input class="form-check-input" id="backleft_absoluteencoderinverted-input" name="absoluteEncoderInverted"
type="checkbox">
<label class="form-check-label" for="backleft_absoluteencoderinverted-input">
Absolute Encoder Inverted
</label>
</div>

</form>
</div>
</div>
Expand Down Expand Up @@ -985,7 +1007,13 @@ <h1 class="d-inline display-6">Back Right Module</h1>
<label class="form-label" for="backrightencoder_canbus-input">Absolute Encoder CAN
Bus</label>
</div>

<div class="form-check">
<input class="form-check-input" id="backright_absoluteencoderinverted-input" name="absoluteEncoderInverted"
type="checkbox">
<label class="form-check-label" for="backright_absoluteencoderinverted-input">
Absolute Encoder Inverted
</label>
</div>
</form>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions simgui-ds.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"Keyboard 0 Settings": {
"window": {
"visible": true
}
},
"keyboardJoysticks": [
{
"axisConfig": [
Expand Down
3 changes: 2 additions & 1 deletion src/main/deploy/swerve/maxSwerve/modules/backleft.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
},
"inverted": {
"drive": false,
"angle": true
"angle": false
},
"absoluteEncoderInverted": true,
"absoluteEncoderOffset": 301.3301969,
"location": {
"front": -12,
Expand Down
3 changes: 2 additions & 1 deletion src/main/deploy/swerve/maxSwerve/modules/backright.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
},
"inverted": {
"drive": false,
"angle": true
"angle": false
},
"absoluteEncoderInverted": true,
"absoluteEncoderOffset": 7.6941204,
"location": {
"front": -12,
Expand Down
3 changes: 2 additions & 1 deletion src/main/deploy/swerve/maxSwerve/modules/frontleft.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
},
"inverted": {
"drive": false,
"angle": true
"angle": false
},
"absoluteEncoderInverted": true,
"absoluteEncoderOffset": 90,
"location": {
"front": 12,
Expand Down
3 changes: 2 additions & 1 deletion src/main/deploy/swerve/maxSwerve/modules/frontright.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
},
"inverted": {
"drive": false,
"angle": true
"angle": false
},
"absoluteEncoderInverted": true,
"absoluteEncoderOffset": -90,
"location": {
"front": 12,
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
package frc.robot;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.wpilibj.Filesystem;
import edu.wpi.first.wpilibj.RobotBase;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.button.CommandJoystick;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
Expand Down Expand Up @@ -100,6 +104,11 @@ private void configureBindings()

new JoystickButton(driverXbox, 1).onTrue((new InstantCommand(drivebase::zeroGyro)));
new JoystickButton(driverXbox, 3).onTrue(new InstantCommand(drivebase::addFakeVisionReading));
new JoystickButton(driverXbox,
2).whileTrue(
Commands.deferredProxy(() -> drivebase.driveToPose(
new Pose2d(new Translation2d(4, 4), Rotation2d.fromDegrees(0)))
));
// new JoystickButton(driverXbox, 3).whileTrue(new RepeatCommand(new InstantCommand(drivebase::lock, drivebase)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package frc.robot.subsystems.swervedrive;

import com.pathplanner.lib.auto.AutoBuilder;
import com.pathplanner.lib.path.PathConstraints;
import com.pathplanner.lib.path.PathPlannerPath;
import com.pathplanner.lib.util.HolonomicPathFollowerConfig;
import com.pathplanner.lib.util.PIDConstants;
Expand Down Expand Up @@ -147,6 +148,28 @@ public Command getAutonomousCommand(String pathName, boolean setOdomToStart)
return AutoBuilder.followPath(path);
}

/**
* Use PathPlanner Path finding to go to a point on the field.
*
* @param pose Target {@link Pose2d} to go to.
* @return PathFinding command
*/
public Command driveToPose(Pose2d pose)
{
// Create the constraints to use while pathfinding
PathConstraints constraints = new PathConstraints(
swerveDrive.getMaximumVelocity(), 4.0,
swerveDrive.getMaximumAngularVelocity(), Units.degreesToRadians(720));

// Since AutoBuilder is configured, we can use it to build pathfinding commands
return AutoBuilder.pathfindToPose(
pose,
constraints,
0.0, // Goal end velocity in meters/sec
0.0 // Rotation delay distance in meters. This is how far the robot should travel before attempting to rotate.
);
}

/**
* Command to drive the robot using translative values and heading as a setpoint.
*
Expand Down Expand Up @@ -177,7 +200,7 @@ public Command driveCommand(DoubleSupplier translationX, DoubleSupplier translat
*
* @param translationX Translation in the X direction.
* @param translationY Translation in the Y direction.
* @param rotation Rotation as a value between [-1, 1] converted to radians.
* @param rotation Rotation as a value between [-1, 1] converted to radians.
* @return Drive command.
*/
public Command simDriveCommand(DoubleSupplier translationX, DoubleSupplier translationY, DoubleSupplier rotation)
Expand Down Expand Up @@ -336,9 +359,8 @@ public void setMotorBrake(boolean brake)
}

/**
* Gets the current yaw angle of the robot, as reported by the swerve pose estimator in the underlying
* drivebase. Note, this is not the raw gyro reading, this may be corrected from calls to
* resetOdometry().
* Gets the current yaw angle of the robot, as reported by the swerve pose estimator in the underlying drivebase.
* Note, this is not the raw gyro reading, this may be corrected from calls to resetOdometry().
*
* @return The yaw angle
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/swervelib/SwerveDrive.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public SwerveDrive(
Rotation2d.fromDegrees(0))); // x,y,heading in radians; Vision measurement std dev, higher=less weight

zeroGyro();
setMaximumSpeed(maxSpeedMPS);

// Initialize Telemetry
if (SwerveDriveTelemetry.verbosity.ordinal() >= TelemetryVerbosity.LOW.ordinal())
Expand Down Expand Up @@ -820,6 +821,7 @@ public void setMaximumSpeed(double maximumSpeed, boolean updateModuleFeedforward
swerveDriveConfiguration.physicalCharacteristics.optimalVoltage = optimalVoltage;
for (SwerveModule module : swerveModules)
{
module.maxSpeed = maximumSpeed;
if (updateModuleFeedforward)
{
module.feedforward = SwerveMath.createDriveFeedforward(optimalVoltage,
Expand Down Expand Up @@ -1030,9 +1032,7 @@ public void addVisionMeasurement(Pose2d robotPose, double timestamp,

/**
* Add a vision measurement to the {@link SwerveDrivePoseEstimator} and update the {@link SwerveIMU} gyro reading with
* the given timestamp of the vision measurement. <br /> <b>IT IS HIGHLY RECOMMENDED TO UPDATE YOUR GYROSCOPE OFFSET
* AFTER USING THIS FUNCTION!</b> <br /> To update your gyroscope readings directly use
* {@link SwerveDrive#setGyroOffset(Rotation3d)}.
* the given timestamp of the vision measurement.
*
* @param robotPose Robot {@link Pose2d} as measured by vision.
* @param timestamp Timestamp the measurement was taken as time since startup, should be taken from
Expand Down
118 changes: 118 additions & 0 deletions src/main/java/swervelib/SwerveDriveTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package swervelib;

import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.math.kinematics.SwerveModuleState;
import edu.wpi.first.wpilibj.Timer;

/**
* Class to perform tests on the swerve drive.
*/
public class SwerveDriveTest
{

/**
* Set the modules to center to 0.
*
* @param swerveDrive Swerve Drive to control.
*/
public static void centerModules(SwerveDrive swerveDrive)
{
for (SwerveModule swerveModule : swerveDrive.getModules())
{
swerveModule.setDesiredState(new SwerveModuleState(0, Rotation2d.fromDegrees(0)), false, true);
}

// Update kinematics because we are not using setModuleStates
swerveDrive.kinematics.toSwerveModuleStates(new ChassisSpeeds());
}

/**
* Set the angle of the modules to a given {@link Rotation2d}
*
* @param swerveDrive {@link SwerveDrive} to use.
* @param moduleAngle {@link Rotation2d} to set every module to.
*/
public static void angleModules(SwerveDrive swerveDrive, Rotation2d moduleAngle)
{
for (SwerveModule swerveModule : swerveDrive.getModules())
{
swerveModule.setDesiredState(new SwerveModuleState(0, moduleAngle), false, true);
}
swerveDrive.kinematics.toSwerveModuleStates(new ChassisSpeeds());
}

/**
* Power the drive motors for the swerve drive to a set percentage.
*
* @param swerveDrive {@link SwerveDrive} to control.
* @param percentage Percentage of voltage to send to drive motors.
*/
public static void powerDriveMotors(SwerveDrive swerveDrive, double percentage)
{
for (SwerveModule swerveModule : swerveDrive.getModules())
{
swerveModule.getDriveMotor().set(percentage);
}
}

/**
* Power the angle motors for the swerve drive to a set percentage.
*
* @param swerveDrive {@link SwerveDrive} to control.
* @param percentage Percentage of voltage to send to angle motors.
*/
public static void powerAngleMotors(SwerveDrive swerveDrive, double percentage)
{
for (SwerveModule swerveModule : swerveDrive.getModules())
{
swerveModule.getAngleMotor().set(percentage);
}
}

/**
* Find the minimum amount of power required to move the swerve drive motors.
*
* @param swerveDrive {@link SwerveDrive} to control.
* @param minMovement Minimum amount of movement to drive motors.
* @param testDelaySeconds Time in seconds for the motor to move.
* @return minimum voltage required.
*/
public static double findDriveMotorKV(SwerveDrive swerveDrive, double minMovement, double testDelaySeconds)
{
double[] startingEncoders = new double[4];
double kV = 0;

SwerveDriveTest.powerDriveMotors(swerveDrive, 0);
SwerveModule[] modules = swerveDrive.getModules();
for (int i = 0; i < modules.length; i++)
{
startingEncoders[i] = Math.abs(modules[i].getDriveMotor().getPosition());
}

for (double kV_new = 0; kV_new < 0.1; kV_new += 0.0001)
{

SwerveDriveTest.powerDriveMotors(swerveDrive, kV);
boolean foundkV = false;
double startTimeSeconds = Timer.getFPGATimestamp();
while ((Timer.getFPGATimestamp() - startTimeSeconds) < testDelaySeconds && !foundkV)
{
for (int i = 0; i < modules.length; i++)
{
if ((modules[i].getDriveMotor().getPosition() - startingEncoders[i]) > minMovement)
{
foundkV = true;
break;
}
}
}
if (foundkV)
{
SwerveDriveTest.powerDriveMotors(swerveDrive, 0);
kV = kV_new;
}
}
return kV;
}
}
7 changes: 6 additions & 1 deletion src/main/java/swervelib/SwerveModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public SwerveModule(int moduleNumber, SwerveModuleConfiguration moduleConfigurat
{
absoluteEncoder.factoryDefault();
absoluteEncoder.configure(moduleConfiguration.absoluteEncoderInverted);
angleMotor.setPosition(getAbsolutePosition());
}

// Config angle motor/controller
Expand All @@ -122,6 +121,12 @@ public SwerveModule(int moduleNumber, SwerveModuleConfiguration moduleConfigurat
angleMotor.setInverted(moduleConfiguration.angleMotorInverted);
angleMotor.setMotorBrake(false);

// Set the position AFTER settings the conversion factor.
if (absoluteEncoder != null)
{
angleMotor.setPosition(getAbsolutePosition());
}

// Config drive motor/controller
driveMotor.configureIntegratedEncoder(moduleConfiguration.conversionFactors.drive);
driveMotor.configurePIDF(moduleConfiguration.velocityPIDF);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/swervelib/imu/Pigeon2Swerve.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ public Optional<Translation3d> getAccel()
{
// TODO: Switch to suppliers.
StatusSignal<Double> xAcc = imu.getAccelerationX();
StatusSignal<Double> yAcc = imu.getAccelerationX();
StatusSignal<Double> zAcc = imu.getAccelerationX();
StatusSignal<Double> yAcc = imu.getAccelerationY();
StatusSignal<Double> zAcc = imu.getAccelerationZ();

return Optional.of(new Translation3d(xAcc.refresh().getValue(),
yAcc.refresh().getValue(),
Expand Down

0 comments on commit 0788a62

Please sign in to comment.