MAXMotion Position Control
MAXMotion Position Control is a second-degree closed loop controller, allowing for smooth and consistent motions from one position to another by limiting both the velocity and acceleration of the motor. These can be configured via the MAXMotion Parameters, setting a target acceleration and a "cruise" velocity. The motor will spin up, honoring the acceleration target, hold speed at the cruise velocity, and then slow down honoring the acceleration target to arrive at the setpoint. MAXMotion updates its motion profile every 10ms and the underlying PID controller every 1ms, which makes it extremely fast and responsive.
How it Works
MAXMotion generates a profile containing all the key transition points between the current position and the setpoint and uses that to calculate intermediate positions for the PID controller to follow.

Each point along the profile is a target for the PID controller at the point in time it corresponds to. If, at some point in time, the actual measured position is more than the configured Allowed Profile Error away from the profile, the profile will be regenerated from the current position and velocity. While the mechanism is within that margin, it will continue to track the same profile. This makes tuning easy and makes motions consistent and accurate.
Configuring MAXMotion
Feedforwards
The SPARK Feedforward system was designed with MAXMotion in mind, and MAXMotion can take advantage of all of its features.
The first step of setting up MAXMotion is to configure the PID feedforwards, as explained on the feedforward page. The kV and kA values from a calculator, converted to appropriate units, or from a tool like SysID are perfect starting points for tuning.
MAXMotion Constants
There are 3 primary constants to configure for MAXMotion:
Cruise Velocity: this is the speed you want the motion to hold through the middle of its path
Maximum Acceleration: this is the acceleration you want to use to speed up and slow down the motion
Allowed Profile Error: this is the amount of position deviation from the profile that is allowed before the profile is regenerated
If changes to quantities aren't showing the expected results, the Current Limits may be engaging. This will limit the acceleration of the system and can be remedied by increasing the Current Limit (within reason) or increasing the Gear Ratio.
What do the constants do?
Cruise Velocity
This is the top of the trapezoid, the velocity that is sustained through the center stage of the motion. Increasing it beyond what is achievable will result in a triangular "trapezoid" on you Velocity graph.
Maximum Acceleration
This is how quickly the mechanism accelerates. Increasing it too much will draw a lot of current, and may hit the current limits or stall the motors.
Allowed Profile Error
This is how "loose" the profile is, and how far your mechanism can get from the profile before the profile is regenerated. For tuning, it's helpful to set this to a large value so you can see the behavior without the profile resetting, but the end goal for your motion should be to minimize this margin.
kP
This is the position-tracking gain. This represents how much voltage is applied proportionally to the position error. Increasing it will make the mechanism move toward the position target more quickly, but increasing it too much will cause overshooting and stuttering.
kI
This is the integral gain, which is not often recommended for FRC use.
kD
This is the derivative gain, which helps track velocity within the position controller. For better velocity tracking, kV is a better choice.
kS
This is the static gain, which helps overcome a constant resistance like friction in a gearbox. It should be set to the maximum voltage in either direction that doesn't make the mechanism move at all, where any more causes motion. Increasing it will improve precision and make motions in different directions more consistent, but increasing it too much will cause jitter.
kV
This is the velocity-tracking gain. Increasing it will increase the voltage output proportionally to the velocity target, and will help track velocity more closely. Increasing it too much will cause overshooting on velocity or general instability.
kA
This is the acceleration-tracking gain. Increasing it will help track acceleration more closely, but increasing it too much will cause instability. It will make a noticeable difference in velocity tracking during acceleration and deceleration.
kG and kCos
These are gravity feedforwards, that help hold position against gravity and remove the gravity factor from the position and velocity tracking of the other constants. For more information on these gains, see Feed Forward Control
Tuning for MAXMotion Position Control
Ensure the mechanism is free to move and note any mechanical limits
Set up the Feedforwards for the mechanism. Note that these may not provide expected results until other values are setup
Set P to a very small number, relative to your position units. For the default units, kP = 0.01 is a good starting point. Keep in mind that kP will be multiplied by your position error and then become duty cycle percent output, so pick a "small" value relative to what your position error is expected to be
Set the Cruise Velocity and Max Acceleration to small numbers, relative to your velocity units and gear ratio. For a directly-driven mechanism with default units, 30 RPM and 10 RPM/s respectively are good starting points to see the effects of MAXMotion and clearly see the impacts of each parameter, but these are very slow and will need increased
Set the Allowed Profile Error to a high number relative to your units and the distance to your setpoint. For most motions at the default units, an Allowed Profile Error of 1 Rotation is enough to get started. If the results are confusing, especially if the acceleration targets appear to be too low, increase this. Increasing this value will let the motion continue for a longer period before regenerating the profile, which may uncover the root of a tuning issue
Set up a method to retrieve relevant info
If running a robot program, use NetworkTables to post this information
Several of these values can be fetched from the SparkClosedLoopController object
A dashboard like Glass or AdvantageScope can help graph and record these values
In the REV Hardware Client, use the Telemetry tab to enable this information
Good information to watch while tuning:
Position
Velocity
Applied Output
MAXMotion Position Setpoint
MAXMotion Velocity Setpoint
Setpoint
Current Draw
Note that fetching all these values may require modifying the Status Frame Periods for certain parameters as the CAN bus or the SPARK device reaches its limit. If the device stops responding and many Status Frames are timing out, power cycle all devices on the bus to clear the errors and try reducing traffic by increasing status frame periods
Graphing all the positions and all the velocities on 2 graphs will help this process
Tuning for your mechanism using Simulation is a safe, good way to start, but will probably still need further tweaking to make the actual mechanism's motion perfect
Be very careful, as this tuning process could cause your mechanism to move in unpredictable or unexpected ways if the constants are off, particularly if your units don't match up
Repeat the following process until the results are satisfactory
Run and record a motion to a known setpoint
If the motion is jittery or shaky, reduce kP. If your Allowed Profile Error is small, increase it to a large number while diagnosing issues
If the motion shoots to a high, uncontrollable velocity immediately, increase P to a larger number. With too small of a P value, the feedforwards are sensitive to tiny inaccuracies, but by increasing P it will increase this tolerance. If this persists, reduce your feedforward values, specifically kV
If the MAXMotion Position Setpoint jumps or spikes and resets more than a few times, increase the Allowed Profile Error. This won't contribute to fixing the issue, but will let you better observe the behavior and identify other factors.
If the motion is asymmetrical up vs down, recalculate kG and kS or try determining them experimentally
If the velocity lags behind the target velocity, increase kV
If the velocity overshoots the target velocity, decrease kV
If the position lags behind the target, increase kP slightly
If the mechanism overshoots the setpoint, reduce kV (or, if that doesn't fix it, kS)
If the motion is stable, smooth, and consistent, the the position and velocity targets are reached consistently, the mechanism doesn't overshoot the setpoint, and the MAXMotion Position Setpoint doesn't seem to "jump", reduce the Allowed Profile Error, increase the Cruise Velocity, or increase the Max Acceleration slightly
Repeat this process until you have the speed, smoothness, and accuracy that you want
What does "good tuning" look like?
A well-tuned MAXMotion controller will:
Track position, velocity, and acceleration closely and accurately
Respond quickly to a change in setpoint
Not stutter or reset
Move smoothly and in a controllable way
Move quickly
Not overshoot the setpoint
The acceleration will be set as high as is smooth without hitting the current limit
The velocity will be set as high as is achievable and smooth
The allowed profile error will be as small as possible
Using MAXMotion
After tuning your constants, calling MAXMotion is as simple as passing in the setpoint to the controller.
API Docs: setReference
API Reference: SetReference
Migrating from Smart Motion
As Smart Motion and MAXMotion Position Control use different underlying control methods, all PID constants will need to be re-tuned from scratch.
Smart Motion used a different method for smooth second-degree motion control, but MAXMotion can be applied anywhere Smart Motion was previously. Maximum velocity and acceleration constants may be transferable, but should be tested with caution. All other constants will need re-tuned from scratch, including all PIDs.
MAXMotion has several improvements over Smart Motion, and should offer better consistency, a better tuning experience, better position retention, and an all-around better user experience. It is highly recommended to migrate all systems using Smart Motion to MAXMotion.
Last updated
Was this helpful?

