Skip to content

Commit

Permalink
Rewrite sim motors to support lingering setpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vichik123 committed Jan 23, 2024
1 parent 33ff5f8 commit a2c3f5c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
12 changes: 10 additions & 2 deletions src/main/java/lib/motors/SimMotor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.math.system.plant.LinearSystemId;
import edu.wpi.first.wpilibj.simulation.DCMotorSim;
import java.util.function.DoubleSupplier;

public class SimMotor {

Expand All @@ -16,7 +17,7 @@ public class SimMotor {
protected ProfiledPIDController profiledController = null;

protected double lastTimestampSeconds = 0;
protected double voltageRequest = 0;
protected MotorSetpoint voltageRequest = MotorSetpoint.simpleVoltage(0);

protected final double conversionFactor;

Expand Down Expand Up @@ -47,8 +48,15 @@ public void setProfiledController(ProfiledPIDController profiledController) {
}

public void update(double timestampSeconds) {
motorSim.setInputVoltage(voltageRequest.getAsDouble());
motorSim.update(timestampSeconds - lastTimestampSeconds);

lastTimestampSeconds = timestampSeconds;
}

protected interface MotorSetpoint extends DoubleSupplier {

static MotorSetpoint simpleVoltage(double voltage) {
return () -> voltage;
}
}
}
19 changes: 11 additions & 8 deletions src/main/java/lib/motors/SparkMaxSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,35 @@ public void set(double speed) {
}

public void setInputVoltage(double voltage) {
voltageRequest = voltage;
motorSim.setInputVoltage(voltageRequest);
voltageRequest = MotorSetpoint.simpleVoltage(voltage);
}

public void setReference(double value, CANSparkMax.ControlType ctrl) {
setReference(value, ctrl, 0);
}

private void setInputVoltage(MotorSetpoint voltage) {
voltageRequest = voltage;
}

public void setReference(double value, CANSparkMax.ControlType ctrl, double arbFeedforward) {
switch (ctrl) {
case kDutyCycle:
set(value);
break;
case kPosition:
setInputVoltage(controller.calculate(getPosition(), value) + arbFeedforward);
setInputVoltage(() -> controller.calculate(getPosition(), value) + arbFeedforward);
break;
case kSmartMotion:
setInputVoltage(
profiledController.calculate(getPosition(), value) + arbFeedforward);
() -> profiledController.calculate(getPosition(), value) + arbFeedforward);
break;
case kVelocity:
setInputVoltage(controller.calculate(getVelocity(), value) + arbFeedforward);
setInputVoltage(() -> controller.calculate(getVelocity(), value) + arbFeedforward);
break;
case kSmartVelocity:
setInputVoltage(
profiledController.calculate(getVelocity(), value) + arbFeedforward);
() -> profiledController.calculate(getVelocity(), value) + arbFeedforward);
break;
case kVoltage:
setInputVoltage(value);
Expand All @@ -68,11 +71,11 @@ public void setReference(double value, CANSparkMax.ControlType ctrl, double arbF
}

public double getBusVoltage() {
return voltageRequest;
return voltageRequest.getAsDouble();
}

public double getAppliedOutput() {
return voltageRequest / 12.0;
return voltageRequest.getAsDouble() / 12.0;
}

public double getVelocity() {
Expand Down
42 changes: 19 additions & 23 deletions src/main/java/lib/motors/TalonFXSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,53 +30,49 @@ public TalonFXSim(
super(DCMotor.getFalcon500(numMotors), jKgMetersSquared, gearing, conversionFactor);
}

@Override
public void update(double timestampSeconds) {
super.update(timestampSeconds);

acceleration.update(getVelocity(), timestampSeconds);
}

public void setControl(DutyCycleOut request) {
this.setControl(new VoltageOut(request.Output * 12));
setControl(new VoltageOut(request.Output * 12));
}

public void setControl(VoltageOut request) {
voltageRequest = request.Output;
motorSim.setInputVoltage(voltageRequest);
voltageRequest = MotorSetpoint.simpleVoltage(request.Output);
}

public void setControl(PositionDutyCycle request) {
voltageRequest =
controller.calculate(motorSim.getAngularPositionRotations(), request.Position);
this.setControl(new VoltageOut(voltageRequest + 12 * request.FeedForward));
setControl(new PositionVoltage(request.Position).withFeedForward(request.FeedForward * 12));
}

public void setControl(PositionVoltage request) {
voltageRequest =
controller.calculate(motorSim.getAngularPositionRotations(), request.Position);
this.setControl(new VoltageOut(voltageRequest + request.FeedForward));
() -> controller.calculate(getPosition(), request.Position) + request.FeedForward;
}

public void setControl(VelocityDutyCycle request) {
voltageRequest =
controller.calculate(
Units.rpmToRps(motorSim.getAngularVelocityRPM()), request.Velocity);
this.setControl(new VoltageOut(voltageRequest + 12 * request.FeedForward));
setControl(new VelocityVoltage(request.Velocity).withFeedForward(request.FeedForward * 12));
}

public void setControl(VelocityVoltage request) {
voltageRequest =
controller.calculate(
Units.rpmToRps(motorSim.getAngularVelocityRPM()), request.Velocity);
this.setControl(new VoltageOut(voltageRequest + request.FeedForward));
() -> controller.calculate(getVelocity(), request.Velocity) + request.FeedForward;
}

public void setControl(MotionMagicDutyCycle request) {
voltageRequest =
profiledController.calculate(
motorSim.getAngularPositionRotations(), request.Position);
this.setControl(new VoltageOut(voltageRequest + 12 * request.FeedForward));
setControl(
new MotionMagicVoltage(request.Position).withFeedForward(request.FeedForward * 12));
}

public void setControl(MotionMagicVoltage request) {
voltageRequest =
profiledController.calculate(
motorSim.getAngularPositionRotations(), request.Position);
this.setControl(new VoltageOut(voltageRequest + request.FeedForward));
() ->
profiledController.calculate(getPosition(), request.Position)
+ request.FeedForward;
}

public double getVelocity() {
Expand All @@ -96,6 +92,6 @@ public double getAppliedCurrent() {
}

public double getAppliedVoltage() {
return voltageRequest;
return voltageRequest.getAsDouble();
}
}

0 comments on commit a2c3f5c

Please sign in to comment.