Spark MAX Control Type kCurrent

I’ve been trying to configure a SparkMAX for closed-loop current control, but without having found any examples or documentation I’m struggling with tuning. REV’s site mentions that this is a feature, but doesn’t go into any detail about how it works. Further, the tuning I’ve tried has yielded unexpected results.

What I’ve Tried:
Using the robot’s drive (MAXSwerve in a rotation diamond)
kP, kI, kD = 0, kF = 0.5, setpoint = 2A. This did seem to cause rapid acceleration as I would expect, but it was fast enough that I disabled the robot without having noted any useful information. I then stopped trying to tune kF based on how motors work- there won’t be a constant volts/amp requested. This test was perfomed with the robot able to drive on the floor.

kI, kD, kF = 0, kP = 0.001, setpoint = 0.001, 0.01, 0.1, 1, 10A: no motion.
kI, kD, kF = 0, kP = 0.01, 0.1, 1: All of these had similar results. At low setpoints (0.001, 0.01), no motion. Sometimes, for mid-range setpoints (0.1-0.4), and mid-range kP (0.1), the motor would rotate slowly, at a seemingly constant velocity. The measured current reported by getOutputCurrent() seemed constant/oscillating around a constant, but above the setpoint. I assumed that was providing just enough force to balance static friction, so I tried increasing the setpoint. Result? Air-horn/falcon chirp noise and no motor rotation (i. e. motor is oscillating at audible frequencies). These tests were performed with the robot on the cart, modules free to move.

Does anyone know how to tune a SparkMAX current control loop, know where documentation/examples exist, or even just know what the current control mode does exactly?

Why I'm doing this

Choreo asks for the robot’s moment of inertia, and I want to measure that empirically. Moment of inertia is torque over angular acceleration, so I need to apply a constant torque to the robot, which can be done by applying constant drive force at the wheels. Constant wheel force = constant drive motor torque = constant drive motor current.

4 Likes

We did Current based control in the CTRE ecosystem last season for an arm. For Current based control loops the constants were much much larger than you use for voltage/position/velocity.

Again this is not for the REV system so I can’t tell you if that’s the problem or not, but worth a thought maybe.

Notes about manual tuning from this book are probably helpful

These steps apply to position PID controllers. Velocity PID controllers typically don’t need Kd.

  1. Set Kp, Ki, and Kd to zero.
  2. Increase Kp until the output starts to oscillate around the setpoint.
  3. Increase Kd as much as possible without introducing jittering in the system response.
    If the setpoint follows a trapezoid profile (see section 15.1), tuning becomes a lot easier. Plot the position setpoint, velocity setpoint, measured position, and measured velocity.The velocity setpoint can be obtained via numerical differentiation of the position setpoint.

Increase Kp until the position tracks well, then increase Kd until the velocity tracks well. If the controller settles at an output above or below the setpoint, one can increase Ki such that the controller reaches the setpoint in a reasonable amount of time.

However, a steady-state feedforward is strongly preferred over integral control (especially for velocity PID control).

Note: Adding an integral gain to the controller is an incorrect way to eliminate
2.7 Limitations 21 steady-state error. A better approach would be to tune it with an integrator added to the plant, but this requires a model. Since we are doing output-based rather than model-based control, our only option is to add an integrator to the controller. Beware that if Ki is too large, integral windup can occur. Following a large change in setpoint, the integral term can accumulate an error larger than the maximal control input. As a result, the system overshoots and continues to increase until this accumulated error is unwound.

3 Likes

Following… Excited to see what you find out!

(You didn’t want to try using the bifilar torsion pendulum method?)

2 Likes

Do you twist it and then measure the stable oscillation period? I was going for a characterization routine with just the robot because 1) I didn’t really know of a precise apparatus for measuring MOI and 2) it seemed faster, simpler, and more easily accessible to other teams, but if I don’t end up getting anywhere that’s certainly something to consider.

1 Like

Yes, exactly. Here’s a good article on it.

Developing a characterization routine sounds really neat-- and I agree, would be useful to a lot of teams.

2 Likes

I know I had wondered if it would be possible to measure the rotational energy, and I had thought maybe something weird with a pendulum— but I hadn’t put them together or really considered it.

1 Like

Some updates:
I gave up on using the integrated controller, and just made a PIDController to control % output based on measured current draw. I doing so, I was able to realize a fundamental flaw with my previous attempts:
A proportional controller is not suitable for controlling the current draw of a motor by commanding voltages.
This had been sort of bugging me, but I couldn’t quite place it. When using a proportional controller, the motor velocity will quickly become constant-- the current draw will be ~0, so the error will remain constant, and so the commanded motor speed will remain constant. Not useful.
Thinking about the physics briefly reveals that an integral controller is appropriate here: we are attempting to use motor voltage, proportional to velocity, to control motor current, proportional to acceleration. In order to keep acceleration constant, the commanded velocity must track the integral of said constant acceleration over time.
This also suggests an appropriate feedforward model to use in this system, which may prove useful.

For now, I was able to get semi-useful results and come up with a number for MOI to plug into Choreo- results of some initial testing are on our build thread (near end of post).

There isn’t significant value in tuning this process for GarbageBot, so I’ll be working on other things and revisiting this once our real robot is driving.

1 Like