-
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.
* WORK IN PROGRESS. basic arm setpoint control. TODO: switch to abs encoder, make open loop control. * changed rel encoder to absolute, and finished sim. TODO: tune PID values and add setpoints * added basic setpoints, tried to get sim to work. TODO: fix sim * Fix git ignore. * Fix compile Error broken import * Very hacky simulation support. * Fix arm simulation * Add pose3d for advantagescope * Renamed Closed loop command to open loop. Added bad static example for armcommand * Cleanup * updated commands' location, fixed formatting * removed unused import in robot container * Clamp Sim voltage * Simulation Fixes. * Update sim to use constant --------- Co-authored-by: m10653 <[email protected]>
- Loading branch information
1 parent
591706e
commit c509c15
Showing
7 changed files
with
297 additions
and
8 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
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
87 changes: 87 additions & 0 deletions
87
src/main/java/org/jmhsrobotics/frc2024/subsystems/arm/ArmSubsystem.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,87 @@ | ||
package org.jmhsrobotics.frc2024.subsystems.arm; | ||
|
||
import org.jmhsrobotics.frc2024.Constants; | ||
import org.jmhsrobotics.warcore.nt.NT4Util; | ||
|
||
import com.revrobotics.CANSparkBase.IdleMode; | ||
import com.revrobotics.CANSparkLowLevel.MotorType; | ||
import com.revrobotics.CANSparkMax; | ||
import com.revrobotics.SparkAbsoluteEncoder.Type; | ||
|
||
import edu.wpi.first.math.MathUtil; | ||
import edu.wpi.first.math.geometry.Pose3d; | ||
import edu.wpi.first.math.geometry.Rotation3d; | ||
import edu.wpi.first.math.system.plant.DCMotor; | ||
import edu.wpi.first.math.util.Units; | ||
import edu.wpi.first.wpilibj.RobotBase; | ||
import edu.wpi.first.wpilibj.simulation.SingleJointedArmSim; | ||
import edu.wpi.first.wpilibj.smartdashboard.Mechanism2d; | ||
import edu.wpi.first.wpilibj.smartdashboard.MechanismLigament2d; | ||
import edu.wpi.first.wpilibj.smartdashboard.MechanismRoot2d; | ||
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
|
||
public class ArmSubsystem extends SubsystemBase { | ||
|
||
private MechanismLigament2d m_arm; | ||
private CANSparkMax armPivot = new CANSparkMax(9, MotorType.kBrushless); | ||
private SimableAbsoluteEncoder pitchEncoder; | ||
private Mechanism2d mech; | ||
|
||
public ArmSubsystem() { | ||
pitchEncoder = new SimableAbsoluteEncoder(armPivot.getAbsoluteEncoder(Type.kDutyCycle)); | ||
armPivot.setSmartCurrentLimit(40); | ||
armPivot.setIdleMode(IdleMode.kBrake); | ||
init2d(); | ||
if (RobotBase.isSimulation()) { | ||
initSim(); | ||
} | ||
} | ||
|
||
public void setArmPivot(double amount) { | ||
armPivot.set(amount); | ||
// SmartDashboard.putNumber("ArmSubsystem/data/ArmPivotSpeed", amount); | ||
} | ||
|
||
public double getArmPitch() { | ||
return this.pitchEncoder.getPosition(); | ||
} | ||
|
||
public void init2d() { | ||
// TODO: finish sim | ||
mech = new Mechanism2d(3, 3); | ||
MechanismRoot2d root = mech.getRoot("base", 1.5, 1.5); | ||
m_arm = root.append(new MechanismLigament2d("arm", 2, 0)); | ||
// pitchencsim = new SparkAnalogSensor(pitchEncoder); | ||
|
||
} | ||
|
||
@Override | ||
public void periodic() { | ||
// TODO Auto-generated method stub | ||
super.periodic(); | ||
m_arm.setAngle(getArmPitch()); | ||
SmartDashboard.putData("ArmSubsystem/armSIM", mech); | ||
SmartDashboard.putNumber("ArmSubsystem/velocity", this.pitchEncoder.getVelocity()); | ||
SmartDashboard.putNumber("ArmSubsystem/encoder", pitchEncoder.getPosition()); | ||
NT4Util.putPose3d("ArmSubsystem/armpose3d", | ||
new Pose3d(0.2, 0, 0.283, new Rotation3d(0, Units.degreesToRadians(getArmPitch()), 0))); | ||
|
||
} | ||
SingleJointedArmSim armSim; | ||
public void initSim() { | ||
double armGearRatio = 183.33; | ||
double moi = 0.399649199419221; | ||
armSim = new SingleJointedArmSim(DCMotor.getNEO(2), armGearRatio, moi, Units.inchesToMeters(23), 0, | ||
Units.degreesToRadians(180), true, 0); | ||
// simEncoder = new RevEncoderSimWrapper(null, null); | ||
} | ||
@Override | ||
public void simulationPeriodic() { | ||
double armVolts = MathUtil.clamp(armPivot.get() * 12, -12, 12); | ||
armSim.setInputVoltage(armVolts); | ||
armSim.update(Constants.ksimDtSec); | ||
pitchEncoder.setPosition(Units.radiansToDegrees(armSim.getAngleRads())); | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/org/jmhsrobotics/frc2024/subsystems/arm/SimableAbsoluteEncoder.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,86 @@ | ||
package org.jmhsrobotics.frc2024.subsystems.arm; | ||
|
||
import com.revrobotics.AbsoluteEncoder; | ||
import com.revrobotics.REVLibError; | ||
|
||
import edu.wpi.first.wpilibj.RobotBase; | ||
|
||
public class SimableAbsoluteEncoder implements AbsoluteEncoder { | ||
|
||
private AbsoluteEncoder enc; | ||
private double position; | ||
|
||
SimableAbsoluteEncoder(AbsoluteEncoder enc) { | ||
this.enc = enc; | ||
} | ||
|
||
@Override | ||
public double getPosition() { | ||
if (RobotBase.isSimulation()) { | ||
return position; | ||
} else { | ||
return enc.getPosition(); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public double getVelocity() { | ||
return enc.getVelocity(); | ||
} | ||
|
||
@Override | ||
public REVLibError setPositionConversionFactor(double factor) { | ||
return enc.setPositionConversionFactor(factor); | ||
} | ||
|
||
@Override | ||
public double getPositionConversionFactor() { | ||
return enc.getPositionConversionFactor(); | ||
} | ||
|
||
@Override | ||
public REVLibError setVelocityConversionFactor(double factor) { | ||
return enc.setVelocityConversionFactor(factor); | ||
} | ||
|
||
@Override | ||
public double getVelocityConversionFactor() { | ||
return enc.getVelocityConversionFactor(); | ||
} | ||
|
||
@Override | ||
public REVLibError setInverted(boolean inverted) { | ||
return enc.setInverted(inverted); | ||
} | ||
|
||
@Override | ||
public boolean getInverted() { | ||
return enc.getInverted(); | ||
} | ||
|
||
@Override | ||
public REVLibError setAverageDepth(int depth) { | ||
return enc.setAverageDepth(depth); | ||
} | ||
|
||
@Override | ||
public int getAverageDepth() { | ||
return enc.getAverageDepth(); | ||
} | ||
|
||
@Override | ||
public REVLibError setZeroOffset(double offset) { | ||
return enc.setZeroOffset(offset); | ||
} | ||
|
||
@Override | ||
public double getZeroOffset() { | ||
return enc.getZeroOffset(); | ||
} | ||
|
||
public void setPosition(double position) { | ||
this.position = position; | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/org/jmhsrobotics/frc2024/subsystems/arm/commands/ArmCommand.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,60 @@ | ||
package org.jmhsrobotics.frc2024.subsystems.arm.commands; | ||
|
||
import edu.wpi.first.wpilibj2.command.Command; | ||
|
||
import org.jmhsrobotics.frc2024.subsystems.arm.ArmSubsystem; | ||
|
||
import edu.wpi.first.math.MathUtil; | ||
import edu.wpi.first.math.controller.ProfiledPIDController; | ||
import edu.wpi.first.math.trajectory.TrapezoidProfile.Constraints; | ||
import edu.wpi.first.math.trajectory.TrapezoidProfile.State; | ||
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; | ||
|
||
public class ArmCommand extends Command { | ||
|
||
private ArmSubsystem armSubsystem; | ||
private double angle; | ||
private static ProfiledPIDController armPID; | ||
private static int num = 0; | ||
public ArmCommand(double angle, ArmSubsystem armSubsystem) { | ||
this.armSubsystem = armSubsystem; | ||
this.angle = angle; | ||
// TODO: tune Values; | ||
armPID = new ProfiledPIDController(0.01, 0, 0, new Constraints(180, 180)); | ||
SmartDashboard.putData("ArmPID2", this.armPID); | ||
addRequirements(this.armSubsystem); | ||
|
||
} | ||
|
||
@Override | ||
public void initialize() { | ||
// TODO Auto-generated method stub | ||
|
||
armPID.reset(new State(0, 0)); | ||
armPID.setGoal(this.angle); | ||
armPID.setTolerance(1, 3); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
|
||
double PIDOut = this.armPID.calculate(this.armSubsystem.getArmPitch()); | ||
PIDOut = MathUtil.clamp(PIDOut, -.5, .5); | ||
armSubsystem.setArmPivot(PIDOut); | ||
SmartDashboard.putNumber("ArmCommand/data/goal", this.angle); | ||
SmartDashboard.putNumber("ArmCommand/data/setPoint", this.armPID.getSetpoint().position); | ||
SmartDashboard.putNumber("ArmCommand/data/PIDOut", PIDOut); | ||
SmartDashboard.putNumber("ArmCommand/data/Current", this.armSubsystem.getArmPitch()); | ||
} | ||
|
||
@Override | ||
public boolean isFinished() { | ||
return false; | ||
// return armPID.atGoal(); | ||
} | ||
|
||
@Override | ||
public void end(boolean interrupted) { | ||
armSubsystem.setArmPivot(0); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...main/java/org/jmhsrobotics/frc2024/subsystems/arm/commands/ArmOpenLoopControlCommand.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,29 @@ | ||
package org.jmhsrobotics.frc2024.subsystems.arm.commands; | ||
|
||
import org.jmhsrobotics.frc2024.controlBoard.ControlBoard; | ||
import org.jmhsrobotics.frc2024.subsystems.arm.ArmSubsystem; | ||
|
||
import edu.wpi.first.wpilibj2.command.Command; | ||
|
||
public class ArmOpenLoopControlCommand extends Command { | ||
|
||
ArmSubsystem armSubsystem; | ||
ControlBoard control; | ||
|
||
public ArmOpenLoopControlCommand(ArmSubsystem armSubsystem, ControlBoard control) { | ||
this.armSubsystem = armSubsystem; | ||
this.control = control; | ||
addRequirements(armSubsystem); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
this.armSubsystem.setArmPivot(0); | ||
|
||
} | ||
@Override | ||
public void execute() { | ||
armSubsystem.setArmPivot(control.pitchInput()); | ||
} | ||
|
||
} |