-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/FRC5190/2024CompetitionSeason…
… into superstructure
- Loading branch information
Showing
10 changed files
with
657 additions
and
46 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
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,27 @@ | ||
# Hardware | ||
|
||
This document details all of the hardware (motors, motor controllers, sensors, etc.) on the robot along with relevant information for programming. | ||
|
||
## Motors and Motor Controllers | ||
<!-- Change for 2024 --> | ||
(Key: F = Front, B = Back, R = Right, L = Left) | ||
| ID | Mechanism | Motor | Motor Controller | PDH Port | | ||
| --- | ------------------- | ---------- | ---------------- | -------- | | ||
| 1 | Drivetrain FL Drive | NEO | SPARK MAX | ? | | ||
| 2 | Drivetrain FL Steer | NEO | SPARK MAX | ? | | ||
| 3 | Drivetrain FR Drive | NEO | SPARK MAX | ? | | ||
| 4 | Drivetrain FR Steer | NEO | SPARK MAX | ? | | ||
| 5 | Drivetrain BL Drive | NEO | SPARK MAX | ? | | ||
| 6 | Drivetrain BL Steer | NEO | SPARK MAX | ? | | ||
| 7 | Drivetrain BR Drive | NEO | SPARK MAX | ? | | ||
| 8 | Drivetrain BR Steer | NEO | SPARK MAX | ? | | ||
|
||
|
||
|
||
## Sensors and Limelight | ||
| ID | Sensor | Type | PDH Port | | ||
| --- | ----------- | ---------- | -------- | | ||
| 11 | FL CANCoder | Absolute | ? | | ||
| 12 | FR CANCoder | Absolute | ? | | ||
| 13 | BL CANCoder | Absolute | ? | | ||
| 14 | BR CANCoder | Absolute | ? | |
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
110 changes: 110 additions & 0 deletions
110
src/main/java/org/ghrobotics/frc2024/CANCoderSwerve.java
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 org.ghrobotics.frc2024; | ||
|
||
import com.ctre.phoenix6.StatusCode; | ||
import com.ctre.phoenix6.StatusSignal; | ||
import com.ctre.phoenix6.configs.CANcoderConfiguration; | ||
import com.ctre.phoenix6.configs.CANcoderConfigurator; | ||
import com.ctre.phoenix6.configs.MagnetSensorConfigs; | ||
import com.ctre.phoenix6.hardware.CANcoder; | ||
import com.ctre.phoenix6.signals.AbsoluteSensorRangeValue; | ||
import com.ctre.phoenix6.signals.SensorDirectionValue; | ||
|
||
/** | ||
* Swerve Absolute Encoder for CTRE CANCoders. | ||
*/ | ||
public class CANCoderSwerve { | ||
|
||
/** | ||
* CANCoder with WPILib sendable and support. | ||
*/ | ||
public CANcoder encoder; | ||
|
||
/** | ||
* Initialize the CANCoder on the standard CANBus. | ||
* | ||
* @param id CAN ID. | ||
*/ | ||
public CANCoderSwerve(int id) { | ||
// Empty string uses the default canbus for the system | ||
encoder = new CANcoder(id); | ||
} | ||
|
||
|
||
/** | ||
* Reset the encoder to factory defaults. | ||
*/ | ||
public void factoryDefault() { | ||
encoder.getConfigurator().apply(new CANcoderConfiguration()); | ||
} | ||
|
||
/** | ||
* Clear sticky faults on the encoder. | ||
*/ | ||
public void clearStickyFaults() { | ||
encoder.clearStickyFaults(); | ||
} | ||
|
||
/** | ||
* Configure the absolute encoder to read from [0, 360) per second. | ||
* | ||
* @param inverted Whether the encoder is inverted. | ||
*/ | ||
public void configure(boolean inverted) { | ||
CANcoderConfigurator cfg = encoder.getConfigurator(); | ||
MagnetSensorConfigs magnetSensorConfiguration = new MagnetSensorConfigs(); | ||
cfg.refresh(magnetSensorConfiguration); | ||
cfg.apply(magnetSensorConfiguration | ||
.withAbsoluteSensorRange(AbsoluteSensorRangeValue.Signed_PlusMinusHalf) | ||
.withSensorDirection(inverted ? SensorDirectionValue.Clockwise_Positive | ||
: SensorDirectionValue.CounterClockwise_Positive)); | ||
} | ||
|
||
/** | ||
* Get the absolute position of the encoder. Sets {@link SwerveAbsoluteEncoder#readingError} on erroneous readings. | ||
* | ||
* @return Absolute position in degrees from [0, 360). | ||
*/ | ||
|
||
public double getAbsolutePosition() { | ||
StatusSignal<Double> angle = encoder.getAbsolutePosition(); | ||
|
||
return angle.getValue() * 360; | ||
} | ||
|
||
/** | ||
* Get the instantiated absolute encoder Object. | ||
* | ||
* @return Absolute encoder object. | ||
*/ | ||
|
||
public Object getAbsoluteEncoder() { | ||
return encoder; | ||
} | ||
|
||
/** | ||
* Sets the Absolute Encoder Offset within the CANcoder's Memory. | ||
* | ||
* @param offset the offset the Absolute Encoder uses as the zero point in degrees. | ||
* @return if setting Absolute Encoder Offset was successful or not. | ||
*/ | ||
public boolean setAbsoluteEncoderOffset(double offset) { | ||
CANcoderConfigurator cfg = encoder.getConfigurator(); | ||
MagnetSensorConfigs magCfg = new MagnetSensorConfigs(); | ||
StatusCode error = cfg.refresh(magCfg); | ||
if (error != StatusCode.OK) | ||
{ | ||
return false; | ||
} | ||
error = cfg.apply(magCfg.withMagnetOffset(offset / 360)); | ||
return false; | ||
} | ||
|
||
/** | ||
* Get the velocity in degrees/sec. | ||
* | ||
* @return velocity in degrees/sec. | ||
*/ | ||
public double getVelocity() { | ||
return encoder.getVelocity().getValue() * 360; | ||
} | ||
} |
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,58 @@ | ||
package org.ghrobotics.frc2024; | ||
|
||
import org.ghrobotics.frc2024.subsystems.Drive; | ||
|
||
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.wpilibj.smartdashboard.SmartDashboard; | ||
|
||
public class RobotState { | ||
// Swerve Drive | ||
private final Drive drive_; | ||
|
||
// Position Estimator | ||
private final SwerveDrivePoseEstimator pose_estimator_; | ||
|
||
// Constructor | ||
public RobotState(Drive drive) { | ||
// Assign member variables | ||
drive_ = drive; | ||
|
||
// Initialize pose estimator | ||
pose_estimator_ = new SwerveDrivePoseEstimator( | ||
drive_.getKinematics(), drive.getAngle(), drive.getSwerveModulePositions(), | ||
new Pose2d()); | ||
} | ||
|
||
// Update | ||
public void update() { | ||
// Update pose estimator with new drive measurements | ||
pose_estimator_.update(drive_.getAngle(), drive_.getSwerveModulePositions()); | ||
SmartDashboard.putNumber("Robot Pose X", getPosition().getTranslation().getX()); | ||
SmartDashboard.putNumber("Robot Pose Y", getPosition().getTranslation().getY()); | ||
SmartDashboard.putNumber("Actual Robot Heading", drive_.getAngle().getDegrees()); | ||
SmartDashboard.putNumber("Estimated Robot Heading", getDegree()); | ||
} | ||
|
||
// Get Position | ||
public Pose2d getPosition() { | ||
return pose_estimator_.getEstimatedPosition(); | ||
} | ||
|
||
// Get Degree | ||
public double getDegree() { | ||
return pose_estimator_.getEstimatedPosition().getRotation().getDegrees(); | ||
} | ||
|
||
// Get Rotation2d | ||
public Rotation2d getRotation2d() { | ||
return pose_estimator_.getEstimatedPosition().getRotation(); | ||
} | ||
|
||
// Reset Position | ||
// Add this after testing | ||
public void reset(Pose2d pose) { | ||
pose_estimator_.resetPosition(drive_.getAngle(), drive_.getSwerveModulePositions(), pose); | ||
} | ||
} |
Oops, something went wrong.