-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from CyberCoyotes/starter-drive-code
starter drive code based off TAZ 1.
- Loading branch information
Showing
9 changed files
with
319 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"enableCppIntellisense": false, | ||
"currentLanguage": "java", | ||
"projectYear": "2024", | ||
"teamNumber": 3603 | ||
"enableCppIntellisense": false, | ||
"currentLanguage": "java", | ||
"projectYear": "2024", | ||
"teamNumber": 3603 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package frc.robot; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import com.ctre.phoenix6.Utils; | ||
import com.ctre.phoenix6.mechanisms.swerve.SwerveDrivetrain; | ||
import com.ctre.phoenix6.mechanisms.swerve.SwerveDrivetrainConstants; | ||
import com.ctre.phoenix6.mechanisms.swerve.SwerveModuleConstants; | ||
import com.ctre.phoenix6.mechanisms.swerve.SwerveRequest; | ||
|
||
import edu.wpi.first.wpilibj.Notifier; | ||
import edu.wpi.first.wpilibj.RobotController; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import edu.wpi.first.wpilibj2.command.Subsystem; | ||
|
||
/** | ||
* Class that extends the Phoenix SwerveDrivetrain class and implements subsystem | ||
* so it can be used in command-based projects easily. | ||
*/ | ||
public class CommandSwerveDrivetrain extends SwerveDrivetrain implements Subsystem { | ||
private static final double kSimLoopPeriod = 0.005; // 5 ms | ||
private Notifier m_simNotifier = null; | ||
private double m_lastSimTime; | ||
|
||
public CommandSwerveDrivetrain(SwerveDrivetrainConstants driveTrainConstants, double OdometryUpdateFrequency, SwerveModuleConstants... modules) { | ||
super(driveTrainConstants, OdometryUpdateFrequency, modules); | ||
if (Utils.isSimulation()) { | ||
startSimThread(); | ||
} | ||
} | ||
public CommandSwerveDrivetrain(SwerveDrivetrainConstants driveTrainConstants, SwerveModuleConstants... modules) { | ||
super(driveTrainConstants, modules); | ||
if (Utils.isSimulation()) { | ||
startSimThread(); | ||
} | ||
} | ||
|
||
public Command applyRequest(Supplier<SwerveRequest> requestSupplier) { | ||
return run(() -> this.setControl(requestSupplier.get())); | ||
} | ||
|
||
private void startSimThread() { | ||
m_lastSimTime = Utils.getCurrentTimeSeconds(); | ||
|
||
/* Run simulation at a faster rate so PID gains behave more reasonably */ | ||
m_simNotifier = new Notifier(() -> { | ||
final double currentTime = Utils.getCurrentTimeSeconds(); | ||
double deltaTime = currentTime - m_lastSimTime; | ||
m_lastSimTime = currentTime; | ||
|
||
/* use the measured time delta, get battery voltage from WPILib */ | ||
updateSimState(deltaTime, RobotController.getBatteryVoltage()); | ||
}); | ||
m_simNotifier.startPeriodic(kSimLoopPeriod); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package frc.robot; | ||
|
||
import com.ctre.phoenix6.Utils; | ||
import com.ctre.phoenix6.mechanisms.swerve.SwerveDrivetrain.SwerveDriveState; | ||
|
||
import edu.wpi.first.math.geometry.Pose2d; | ||
import edu.wpi.first.math.geometry.Translation2d; | ||
import edu.wpi.first.networktables.DoubleArrayPublisher; | ||
import edu.wpi.first.networktables.DoublePublisher; | ||
import edu.wpi.first.networktables.NetworkTable; | ||
import edu.wpi.first.networktables.NetworkTableInstance; | ||
import edu.wpi.first.networktables.StringPublisher; | ||
import edu.wpi.first.wpilibj.smartdashboard.Mechanism2d; | ||
import edu.wpi.first.wpilibj.smartdashboard.MechanismLigament2d; | ||
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
import edu.wpi.first.wpilibj.util.Color; | ||
import edu.wpi.first.wpilibj.util.Color8Bit; | ||
|
||
public class Telemetry { | ||
private final double MaxSpeed; | ||
|
||
/** | ||
* Construct a telemetry object, with the specified max speed of the robot | ||
* | ||
* @param maxSpeed Maximum speed in meters per second | ||
*/ | ||
public Telemetry(double maxSpeed) { | ||
MaxSpeed = maxSpeed; | ||
} | ||
|
||
/* What to publish over networktables for telemetry */ | ||
private final NetworkTableInstance inst = NetworkTableInstance.getDefault(); | ||
|
||
/* Robot pose for field positioning */ | ||
private final NetworkTable table = inst.getTable("Pose"); | ||
private final DoubleArrayPublisher fieldPub = table.getDoubleArrayTopic("robotPose").publish(); | ||
private final StringPublisher fieldTypePub = table.getStringTopic(".type").publish(); | ||
|
||
/* Robot speeds for general checking */ | ||
private final NetworkTable driveStats = inst.getTable("Drive"); | ||
private final DoublePublisher velocityX = driveStats.getDoubleTopic("Velocity X").publish(); | ||
private final DoublePublisher velocityY = driveStats.getDoubleTopic("Velocity Y").publish(); | ||
private final DoublePublisher speed = driveStats.getDoubleTopic("Speed").publish(); | ||
private final DoublePublisher odomPeriod = driveStats.getDoubleTopic("Odometry Period").publish(); | ||
|
||
/* Keep a reference of the last pose to calculate the speeds */ | ||
private Pose2d m_lastPose = new Pose2d(); | ||
private double lastTime = Utils.getCurrentTimeSeconds(); | ||
|
||
/* Mechanisms to represent the swerve module states */ | ||
private final Mechanism2d[] m_moduleMechanisms = new Mechanism2d[] { | ||
new Mechanism2d(1, 1), | ||
new Mechanism2d(1, 1), | ||
new Mechanism2d(1, 1), | ||
new Mechanism2d(1, 1), | ||
}; | ||
/* A direction and length changing ligament for speed representation */ | ||
private final MechanismLigament2d[] m_moduleSpeeds = new MechanismLigament2d[] { | ||
m_moduleMechanisms[0].getRoot("RootSpeed", 0.5, 0.5).append(new MechanismLigament2d("Speed", 0.5, 0)), | ||
m_moduleMechanisms[1].getRoot("RootSpeed", 0.5, 0.5).append(new MechanismLigament2d("Speed", 0.5, 0)), | ||
m_moduleMechanisms[2].getRoot("RootSpeed", 0.5, 0.5).append(new MechanismLigament2d("Speed", 0.5, 0)), | ||
m_moduleMechanisms[3].getRoot("RootSpeed", 0.5, 0.5).append(new MechanismLigament2d("Speed", 0.5, 0)), | ||
}; | ||
/* A direction changing and length constant ligament for module direction */ | ||
private final MechanismLigament2d[] m_moduleDirections = new MechanismLigament2d[] { | ||
m_moduleMechanisms[0].getRoot("RootDirection", 0.5, 0.5) | ||
.append(new MechanismLigament2d("Direction", 0.1, 0, 0, new Color8Bit(Color.kWhite))), | ||
m_moduleMechanisms[1].getRoot("RootDirection", 0.5, 0.5) | ||
.append(new MechanismLigament2d("Direction", 0.1, 0, 0, new Color8Bit(Color.kWhite))), | ||
m_moduleMechanisms[2].getRoot("RootDirection", 0.5, 0.5) | ||
.append(new MechanismLigament2d("Direction", 0.1, 0, 0, new Color8Bit(Color.kWhite))), | ||
m_moduleMechanisms[3].getRoot("RootDirection", 0.5, 0.5) | ||
.append(new MechanismLigament2d("Direction", 0.1, 0, 0, new Color8Bit(Color.kWhite))), | ||
}; | ||
|
||
/* Accept the swerve drive state and telemeterize it to smartdashboard */ | ||
public void telemeterize(SwerveDriveState state) { | ||
/* Telemeterize the pose */ | ||
Pose2d pose = state.Pose; | ||
fieldTypePub.set("Field2d"); | ||
fieldPub.set(new double[] { | ||
pose.getX(), | ||
pose.getY(), | ||
pose.getRotation().getDegrees() | ||
}); | ||
|
||
/* Telemeterize the robot's general speeds */ | ||
double currentTime = Utils.getCurrentTimeSeconds(); | ||
double diffTime = currentTime - lastTime; | ||
lastTime = currentTime; | ||
Translation2d distanceDiff = pose.minus(m_lastPose).getTranslation(); | ||
m_lastPose = pose; | ||
|
||
Translation2d velocities = distanceDiff.div(diffTime); | ||
|
||
speed.set(velocities.getNorm()); | ||
velocityX.set(velocities.getX()); | ||
velocityY.set(velocities.getY()); | ||
odomPeriod.set(state.OdometryPeriod); | ||
|
||
/* Telemeterize the module's states */ | ||
for (int i = 0; i < 4; ++i) { | ||
m_moduleSpeeds[i].setAngle(state.ModuleStates[i].angle); | ||
m_moduleDirections[i].setAngle(state.ModuleStates[i].angle); | ||
m_moduleSpeeds[i].setLength(state.ModuleStates[i].speedMetersPerSecond / (2 * MaxSpeed)); | ||
|
||
SmartDashboard.putData("Module " + i, m_moduleMechanisms[i]); | ||
} | ||
} | ||
} |
Oops, something went wrong.