Skip to content

Commit 69ef777

Browse files
committed
Make elevationSim work with inverted motor to be consistent with REAL robot motor config
1 parent 6d1a615 commit 69ef777

2 files changed

Lines changed: 26 additions & 25 deletions

File tree

src/main/java/frc/robot/subsystems/elevator/Elevator.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,19 @@ public class Elevator extends SubsystemBase {
3232
* @param io The hardware implementation for the elevator, either sim or real.
3333
*/
3434
public Elevator(ElevatorIO io) {
35+
var slot0Config = new Slot0Configs() // Motor PID and gain values.
36+
.withKP(ElevatorConstants.ELEVATOR_kP)
37+
.withKI(ElevatorConstants.ELEVATOR_kI)
38+
.withKD(ElevatorConstants.ELEVATOR_kD)
39+
.withKS(ElevatorConstants.ELEVATOR_kS)
40+
.withKV(ElevatorConstants.ELEVATOR_kV)
41+
.withKA(ElevatorConstants.ELEVATOR_kA)
42+
.withKG(ElevatorConstants.ELEVATOR_kG);
3543
var motorConfig = new TalonFXConfiguration()
36-
.withSlot0(
37-
new Slot0Configs() // Motor PID and gain values.
38-
.withKP(ElevatorConstants.ELEVATOR_kP)
39-
.withKI(ElevatorConstants.ELEVATOR_kI)
40-
.withKD(ElevatorConstants.ELEVATOR_kD)
41-
.withKS(ElevatorConstants.ELEVATOR_kS)
42-
.withKV(ElevatorConstants.ELEVATOR_kV)
43-
.withKA(ElevatorConstants.ELEVATOR_kA)
44-
.withKG(ElevatorConstants.ELEVATOR_kG))
45-
.withMotorOutput(
46-
new MotorOutputConfigs()
47-
.withInverted(InvertedValue.Clockwise_Positive) // Invert motor rotation.
48-
);
44+
.withSlot0(slot0Config)
45+
// .withMotorOutput(new MotorOutputConfigs().withInverted(InvertedValue.CounterClockwise_Positive));
46+
// Invert motor rotation.
47+
.withMotorOutput(new MotorOutputConfigs().withInverted(InvertedValue.Clockwise_Positive));
4948
var motor = new TalonFX(Constants.ELEVATOR_ID);
5049
motor.setNeutralMode(NeutralModeValue.Brake);
5150
motor.getConfigurator().apply(motorConfig);

src/main/java/frc/robot/subsystems/elevator/ElevatorIOSim.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ public class ElevatorIOSim implements ElevatorIO {
2424
ElevatorConstants.ELEVATOR_MIN_HEIGHT.in(Meter),
2525
ElevatorConstants.ELEVATOR_MAX_HEIGHT.in(Meter),
2626
true,
27-
0.00);
27+
ElevatorConstants.ELEVATOR_MIN_HEIGHT.in(Meter));
2828

29-
// TODO(vdikov): Looks like IOReal and IOSim could share the motor instance.
3029
private TalonFX motor;
3130
private TalonFXSimState motorSim;
3231

3332
// Used for actually moving the motor to a given position with PID applied to a voltage input.
34-
private final PositionVoltage positionControl = new PositionVoltage(0);
33+
private final PositionVoltage positionControl = new PositionVoltage(Rotations.of(0));
3534

3635
public ElevatorIOSim() {}
3736

@@ -55,25 +54,28 @@ public void updateState(ElevatorIOInputs inputs) {
5554

5655
private void updateSim() {
5756
motorSim.setSupplyVoltage(Volts.of(12));
57+
double motorInverted = -1.0; // -1 for inverted, 1 for forward motor.
5858

5959
// Apply the voltage to the sim elevator that we apply to the sim motor.
60-
elevatorSim.setInputVoltage(motorSim.getMotorVoltage());
60+
// Negating the sim motor value since it is set to use negative value when pushing
61+
// the cartrage UP.
62+
elevatorSim.setInputVoltage(motorInverted * motorSim.getMotorVoltage());
63+
elevatorSim.update(0.02); // Same update cycle as an actual robot, 20 ms.
6164

6265
// Logs to "Real Outputs" NT
6366
Logger.recordOutput("Simulated Elevator/motorSim/Voltage", motorSim.getMotorVoltage());
67+
Logger.recordOutput("Simulated Elevator/elevatorSim/position (meters)", elevatorSim.getPositionMeters());
6468
Logger.recordOutput("Simulated Elevator/elevatorSim/hitsUpperLimit", elevatorSim.hasHitUpperLimit());
6569
Logger.recordOutput("Simulated Elevator/elevatorSim/hitsLowerLimit", elevatorSim.hasHitLowerLimit());
6670

67-
elevatorSim.update(0.02); // Same update cycle as an actual robot, 20 ms.
68-
69-
motorSim.setRawRotorPosition(getMotorRotations(elevatorSim.getPositionMeters()));
71+
motorSim.setRawRotorPosition(motorInverted * getMotorRotations(elevatorSim.getPositionMeters()));
7072

7173
// angular velocity = linear velocity / radius, taken also from 5414
72-
motorSim.setRotorVelocity(
73-
((elevatorSim.getVelocityMetersPerSecond() / ElevatorConstants.ELEVATOR_SPOOL_RADIUS.in(Meters))
74-
// radians/sec to rotations/sec
75-
/ (2.0 * Math.PI))
76-
* ElevatorConstants.MOTOR_TO_ELEVATOR_GEARING);
74+
motorSim.setRotorVelocity(motorInverted
75+
* ((elevatorSim.getVelocityMetersPerSecond() / ElevatorConstants.ELEVATOR_SPOOL_RADIUS.in(Meters))
76+
// radians/sec to rotations/sec
77+
/ (2.0 * Math.PI))
78+
* ElevatorConstants.MOTOR_TO_ELEVATOR_GEARING);
7779
}
7880

7981
@Override

0 commit comments

Comments
 (0)