Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spark Max rapidly oscillates output when switching control types #41

Open
jwbonner opened this issue Nov 14, 2024 · 1 comment
Open
Labels
bug Something isn't working Pending The issue will be fixed in the next release REV Robotics

Comments

@jwbonner
Copy link
Member

jwbonner commented Nov 14, 2024

When switching from velocity control to duty cycle or voltage control, the applied output of the Spark Max oscillates rapidly between the correct output and the output from the previous control mode. Here's the code I've been using to test this:

private final SparkMax spark = new SparkMax(2, MotorType.kBrushless);
private final SparkClosedLoopController controller = spark.getClosedLoopController();

public Robot() {
  URCL.start();

  var config = new SparkMaxConfig();
  config.signals.appliedOutputPeriodMs(1);
  spark.configure(config, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);

  SmartDashboard.putBoolean("ClosedLoop", false);
}

@Override
public void robotPeriodic() {
  SmartDashboard.putBoolean("Enabled", isEnabled());
}

@Override
public void teleopPeriodic() {
  if (SmartDashboard.getBoolean("ClosedLoop", false)) {
    controller.setReference(0.0, ControlType.kVelocity);
  } else {
    controller.setReference(0.2, ControlType.kDutyCycle);
  }
}

Below is a screenshot from an URCL log, with the 1ms applied output period configured above. When enabling the first time in duty cycle mode, the applied output is correct and current draw is small. Velocity control also works as expected with no motion. When switching back to duty cycle mode, the applied output oscillates very rapidly. This causes the mechanism (a swerve drive wheel in this case) to rapidly oscillate, and the current draw is very high.

Image

When zooming in on the third enable, you can see that the Spark Max runs at the correct duty cycle only for very short periods of time (1ms as it appears in the log with 1ms sampling).

Image

This behavior is not perfectly consistent. Below is an example where I cycle between velocity and duty cycle control several times. Often, the oscillation is much less severe when switching back to duty cycle control (you can see occasional jumps and slightly higher current draw). However, I can reliably get the extreme oscillation by swapping back and forth many times (as in this example).

Image

Some other observations:

  • This occurs when switching from velocity control to either duty cycle or voltage control, but not switching from position control to either of the others.
  • It can be temporarily fixed by restarting robot code, but not by reconfiguring devices with the same code running.
  • When the code is started/restarted, enabling first in duty cycle/voltage control works properly but switching to velocity control as any point causes it to return (as shown in the screenshots).
  • After the extreme oscillation has occurred once, it seems much more likely to repeat when enabled subsequently.
  • If the example code is adjusted to include an arbitrary FF value, the oscillation is between the correct duty cycle output and the arbitrary FF value (the other PID gains are 0).

I've tested this on several Spark Max devices running 2025.0.0-prerelease.6, though not a Spark Flex. The REVLib version is 2025.0.0-beta-2. I've also attached WPILOG files with the URCL data from the two screenshots.

spark_issue.wpilog.zip
spark_issue_extended.wpilog.zip

@jwbonner jwbonner added the bug Something isn't working label Nov 14, 2024
@jfabellera
Copy link
Contributor

jfabellera commented Nov 19, 2024

Thank you for the extremely detailed report. This will be fixed in the next release of REVLib.

The issue is due to the fact that REVLib is not correctly stopping sending the previous setpoint command, so when you switch control types, it would be sending a velocity setpoint command along with a duty cycle setpoint command.

However, this only happens when switching to the duty cycle control type, which doesn't match up with your claim of it happening with voltage control type (and I haven't been able to reproduce this). I was able to reproduce this and this will also be fixed.

Interestingly enough, it seems that this bug has been present for the last 4 years.

@jfabellera jfabellera added the Pending The issue will be fixed in the next release label Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Pending The issue will be fixed in the next release REV Robotics
Projects
None yet
Development

No branches or pull requests

3 participants