Skip to content
This repository was archived by the owner on Dec 7, 2024. It is now read-only.
This repository was archived by the owner on Dec 7, 2024. It is now read-only.

RevPhysicsSim Position Control doesn't work #25

@jonathandao0

Description

@jonathandao0

Given this code example:

CANSparkMax m_motorA;
CANSparkMax m_motorB;

public void robotInit() {
    m_motorA =  new CANSparkMax(0, CANSparkMaxLowLevel.MotorType.kBrushless);
    m_motorB =  new CANSparkMax(0, CANSparkMaxLowLevel.MotorType.kBrushless);
    
    REVPhysicsSim.getInstance().addSparkMax(m_motorA, DCMotor.getNEO(1));
    REVPhysicsSim.getInstance().addSparkMax(m_motorB, DCMotor.getNEO(1));
}

doube positionSetpoint = 0;
public void robotPeriodic() {
    double velocitySetpoint = 5000;

    m_motorA.getPIDController().setReference(
          velocitySetpoint ,
          CANSparkMax.ControlType.kVelocity);

    m_motorB.getPIDController().setReference(
          positionSetpoint ,
          CANSparkMax.ControlType.kPosition);

    REVPhysicsSim.getInstance().run();
    positionSetpoint += 1;
}

When simulating this code in WPILib, m_motorA is correctly updated in simulation while m_motorB does not get updated. Digging into the SparkMaxSimProfile.class code, it does the following when the simulation runs:

    public void run() {
        double period = this.getPeriod();
        this._vel = this._spark.getEncoder().getVelocity();
        double position = this._spark.getEncoder().getPosition();
        double posFactor = this._spark.getEncoder().getPositionConversionFactor();
        this._spark.getEncoder().setPosition(position + this._vel * period / 60000.0D * posFactor);
    }

It doesn't look like setting a position setpoint is correctly updating the SparkMax in simulation. Will this be fixed in the 2023 RevLib? Another thing that would be helpful would be an API call in SparkMaxPIDController to get the calcuated PID output so that we can take the output and simulate the results manually.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions