-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76626df
commit ec4ac06
Showing
5 changed files
with
161 additions
and
1 deletion.
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
65 changes: 65 additions & 0 deletions
65
src/main/java/com/stuypulse/robot/subsystems/turret/Turret.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,65 @@ | ||
package com.stuypulse.robot.subsystems.turret; | ||
|
||
import com.stuypulse.robot.Robot; | ||
import com.stuypulse.robot.constants.Settings; | ||
import com.stuypulse.stuylib.control.Controller; | ||
import com.stuypulse.stuylib.control.feedback.PIDController; | ||
import com.stuypulse.stuylib.network.SmartNumber; | ||
|
||
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
|
||
public abstract class Turret extends SubsystemBase { | ||
|
||
public static final Turret instance; | ||
|
||
static { | ||
if(Robot.isReal()) { | ||
instance = new TurretImpl(Settings.Turret.port); | ||
} | ||
else { | ||
instance = new TurretSim(); | ||
} | ||
} | ||
|
||
public static Turret getInstance() { | ||
return instance; | ||
} | ||
|
||
public Controller controller; | ||
public SmartNumber targetAngle = new SmartNumber("Target Angle", 0); | ||
|
||
public void stop() { | ||
setTurretVoltage(0); | ||
} | ||
|
||
public Turret() { | ||
controller = new PIDController(3, 0, 0); | ||
} | ||
|
||
public abstract double getTurretAngle(); | ||
public abstract void setTurretVoltage(double voltage); | ||
|
||
public void setTurretAngle(double angle) { | ||
targetAngle.set(angle); | ||
} | ||
|
||
@Override | ||
public final void periodic() { | ||
controller.update( | ||
targetAngle.get(), | ||
getTurretAngle() | ||
); | ||
|
||
double output = controller.getOutput(); | ||
setTurretVoltage(output); | ||
SmartDashboard.putNumber("Calculated Voltage", output); | ||
SmartDashboard.putNumber("Turret Angle", getTurretAngle()); | ||
|
||
periodic2(); | ||
} | ||
|
||
public abstract void periodic2(); | ||
|
||
} | ||
|
32 changes: 32 additions & 0 deletions
32
src/main/java/com/stuypulse/robot/subsystems/turret/TurretImpl.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,32 @@ | ||
package com.stuypulse.robot.subsystems.turret; | ||
|
||
import com.revrobotics.CANSparkMax; | ||
import com.revrobotics.CANSparkMaxLowLevel.MotorType; | ||
import com.revrobotics.RelativeEncoder; | ||
|
||
public class TurretImpl extends Turret { | ||
|
||
private CANSparkMax motor; | ||
private RelativeEncoder encoder; | ||
|
||
public TurretImpl(int port) { | ||
motor = new CANSparkMax(port, MotorType.kBrushless); | ||
encoder = motor.getEncoder(); | ||
} | ||
|
||
@Override | ||
public double getTurretAngle() { | ||
return encoder.getPosition(); | ||
} | ||
|
||
@Override | ||
public void setTurretVoltage(double voltage) { | ||
motor.setVoltage(voltage); | ||
} | ||
|
||
@Override | ||
public void periodic2() { | ||
|
||
} | ||
} | ||
|
54 changes: 54 additions & 0 deletions
54
src/main/java/com/stuypulse/robot/subsystems/turret/TurretSim.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,54 @@ | ||
package com.stuypulse.robot.subsystems.turret; | ||
|
||
|
||
import com.stuypulse.robot.constants.Settings; | ||
|
||
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.numbers.N1; | ||
import edu.wpi.first.math.numbers.N2; | ||
import edu.wpi.first.math.system.plant.LinearSystemId; | ||
import edu.wpi.first.wpilibj.simulation.LinearSystemSim; | ||
import edu.wpi.first.wpilibj.smartdashboard.Field2d; | ||
import edu.wpi.first.wpilibj.smartdashboard.FieldObject2d; | ||
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
|
||
public class TurretSim extends Turret { | ||
|
||
private LinearSystemSim<N2, N1, N1> turretSim; | ||
private final FieldObject2d turretPose2d; | ||
|
||
private Field2d field; | ||
|
||
public TurretSim() { | ||
turretSim = new LinearSystemSim<N2, N1, N1>( | ||
LinearSystemId.identifyPositionSystem(Settings.Turret.kV.get(), Settings.Turret.kA.get()) | ||
); | ||
|
||
field = new Field2d(); | ||
turretPose2d = field.getObject("Turret Pose 2d"); | ||
|
||
turretPose2d.setPose(new Pose2d(4, 4, Rotation2d.fromDegrees(0))); | ||
SmartDashboard.putData(field); | ||
} | ||
|
||
@Override | ||
public double getTurretAngle() { | ||
return turretSim.getOutput(0); | ||
} | ||
|
||
@Override | ||
public void setTurretVoltage(double voltage) { | ||
turretSim.setInput(MathUtil.clamp(voltage, -12, 12)); | ||
} | ||
|
||
@Override | ||
public void periodic2() { | ||
turretSim.update(Settings.DT); | ||
turretPose2d.setPose(new Pose2d( | ||
turretPose2d.getPose().getTranslation(), | ||
Rotation2d.fromDegrees(getTurretAngle()) | ||
)); | ||
} | ||
} |