How to End Spark MAX PID Control

Our team is going to use the Spark MAX PID functionality this year, and we were thinking about using PID to move our mechanism to certain setpoints, but still have the operator be able to control the mechanism once it reaches its setpoint. For instance, we would have the code:

CANSparkMax motor = new CANSparkMax(1, MotorType.kBrushless);
CANPIDController motorPID = motor.getPIDController();
motorPID .setReference(10, ControlType.kPosition);

// The following code block would run once the mechanism is detected to be within the setpoint
     double value = // get operator value

Would the above work in successfully cancelling out the PID and giving control back to the operator? Or would we have to do something else involving the motorPID object?

Another quick question that might be related: What does ControlType.kDutyCycle do?

There are a few ways you can do this.

The way I recommend is to move the setpoint with the joystick. This way, the mechanism always stays in PID mode and is always holding position. When you press a button to move the target position, the new target position is set to the button’s position value. When you move the joystick, the previous setpoint is modified. You could take the joystick and multiply it by a constant, and then add that to the setpoint. Make sure you limit the setpoint to min/max values that will prevent collisions.

The other way is to change between duty cycle or voltage control and position control. If you do this, the PID will not hold the mechanism position when you move it with the joystick, and the mechanism may move due to gravity, collisions, etc.

Normally I setup manual overrides so they modify the setpoint, unless the feedback sensor has failed, and then they will be pure manual overrides. With the Spark MAX + NEO, the motor will not work without the encoder, so there’s no point in a pure manual override.

ControlType.kDutyCycle is essentially a direct percent command to the motor.

1 Like