EasySimpleMotorFeedForward
Simple Motor Feedforward
A motor feedforward model estimates the output needed to produce a requested velocity and acceleration.
Instead of calculating a correction from measurement error, it uses a model of static friction and motion. This can provide an initial output estimate for mechanisms such as flywheels and drivetrain wheels.
EasyStack's EasySimpleMotorFeedforward calculates this estimate using three gains: kS, kV, and kA.
It does not include gravity compensation.
Creating a Simple Motor Feedforward
To create an EasySimpleMotorFeedforward, provide the gains in the following order:
EasySimpleMotorFeedforward feedforward
new EasySimpleMotorFeedforward(kS, kV, kA);The parameters are:
| Parameter | Description |
|---|---|
kS | Static friction compensation, applied according to the sign of velocity. |
kV | Velocity gain. |
kA | Acceleration gain. |
For example:
EasySimpleMotorFeedforward feedforward =
new EasySimpleMotorFeedforward(
0.2,
0.1,
0.01
);These values are illustrative. Determine the gains for your mechanism, gearing, load, and measurement units.
Without Acceleration Compensation
A two-argument constructor is also available:
EasySimpleMotorFeedforward feedforward
new EasySimpleMotorFeedforward(kS, kV);This sets kA to 0.0.
For example:
EasySimpleMotorFeedforward feedforward =
new EasySimpleMotorFeedforward(0.2, 0.1);This is equivalent to:
EasySimpleMotorFeedforward feedforward
new EasySimpleMotorFeedforward(@.2, 0.1, 0.0);Units and Output
The units of the gains must match the velocity, acceleration, and intended output.
For a voltage-based model using angular velocity in radians per second:
| Gain | Units |
|---|---|
kS | Volts |
kV | Volts per radian per second |
kA | Volts per radian per second squared |
With these gains, calculate() returns volts.
You can also use other consistent measurement units:
| Velocity | Acceleration |
|---|---|
| Meters per second | Meters per second squared |
| Rotations per second | Rotations per second squared |
| Encoder ticks per second | Encoder ticks per second squared |
Changing measurement units requires corresponding changes to kV and kA.
The class does not convert units or apply the calculated output to a motor.
Calculating Feedforward
Use calculate() with the requested velocity and acceleration:
double voltage = feedforward.calculate(
targetVelocity,
targetAcceleration
);The parameters are:
| Parameter | Description |
|---|---|
velocity | Requested velocity in your chosen units. |
acceleration | Requested acceleration in the corresponding units per second. |
The calculation is:
output * Math.signum(velocity)
* velocity
* acceleration;For example:
double voltage feedforward.calculate(50.0, 20.0);Using the example gains:
output 0.2 + (0.1 * 50.0) + (0.01 * 20.0);The result is 5.4 volts.
For motion tracking, these inputs normally come from the desired motion reference. The method does not accept a measured velocity or calculate a tracking error separately.
Calculating Without Acceleration
Use the single-argument overload when acceleration compensation is not needed:
double voltage feedforward.calculate(targetVelocity);For example:
double voltage feedforward.calculate(50.0);This is equivalent to:
double voltage = feedforward.calculate(50.0, 0.0);The acceleration contribution is zero even if the object has a nonzero kA.
Using the example gains, the result is:
0.2 + (0.1 * 50.0) 5.2Static Friction Compensation
The static term follows the sign of the supplied velocity:
staticCompensation = kS * Math.signum(velocity);Its behavior is:
| Velocity | Static contribution |
|---|---|
| Positive | +kS |
| Negative | -kS |
| Zero | 0.0 |
For example:
feedforward.calculate(50.O);includes +kS, while:
feedforward.calculate(-50.0);includes -kS.
Starting from Zero Velocity
At exactly zero velocity, the static contribution is zero even if acceleration is nonzero:
double voltage = feedforward.calculate(0.0, 20.0);The result contains only:
kA * 20.0The implementation does not use acceleration to choose the direction of static friction compensation.
Velocity and Acceleration Direction
Positive and negative inputs represent opposite directions.
For example:
double voltage feedforward.calculate(-50 . 0, -20.0);Using the example gains:
output -0.2 5.0 0.2The result is -5.4 volts.
Acceleration can also oppose velocity:
double voltage feedforward.calculate(50.0, -20.0);This represents positive motion with negative acceleration. The acceleration term reduces the requested output.
The model calculates the combined voltage; it does not decide when the mechanism should accelerate or decelerate.
Zero Velocity and Acceleration
When both inputs are zero:
double voltage feedforward. .0,the result is zero.
This model does not provide a holding output against gravity. For a mechanism that needs gravity compensation, use an appropriate arm or elevator feedforward model.
Combining Feedforward with PID
Feedforward estimates the output needed for the desired motion. PID can correct the difference between the target velocity and measured velocity.
Add their outputs when they use the same output units:
double pidVoltage = controller.calculate(
measuredVelocity,
targetVelocity
);
double feedforwardVoltage = feedforward.calculate(
targetVelocity,
targetAcceleration
);
double requestedVoltage = pidVoltage + feedforwardVoltage;For a constant-speed reference:
double feedforwardVoltage
feedforward.calculate(targetVelocity);If feedforward returns volts, configure the PID gains to produce volts as well.
The feedforward class does not generate target velocity or acceleration. Your program must supply them.
Maximum Achievable Velocity
Use maxAchievableVelocity() to estimate the positive-velocity boundary for an available voltage and specified acceleration:
double maxVelocity = feedforward.maxAchievableVelocity(
maxVoltage,
acceleration
);The parameters are:
| Parameter | Description |
|---|---|
maxVoltage | Positive available voltage magnitude. |
acceleration | Specified acceleration in the model's units. |
For example:
double maxVelocity = feedforward.maxAchievableVelocity(
12.0,
0.0
);The calculation is:
maxVelocity =
(maxVoltage - kS - acceleration * kA) / kV;Using the example gains:
maxVelocity (12.0 0.2) / 0.1;The result is 118.0 velocity units.
This formula assumes positive velocity when applying static compensation. If it returns a negative value, that result is outside the assumed positive-velocity branch.
Minimum Achievable Velocity
Use minAchievableVelocity() for the negative-velocity branch:
double minVelocity = feedforward.minAchievableVelocity(
maxVoltage,
acceleration
);For example:
double minVelocity = feedforward.minAchievableVelocity(
12.0,
0.0
);The calculation is:
minVelocity =
(-maxVoltage + kS - acceleration * kA) / kV;Using the example gains, the result is -118.0 velocity units.
This formula assumes negative velocity.
Both velocity methods require a nonzero kV. Use a positive kV for the usual interpretation of maximum and minimum boundaries.
These values are model estimates. They do not enforce speed limits or guarantee that the hardware can achieve the predicted motion.
Maximum Achievable Acceleration
Use maxAchievableAcceleration():double maxAcceleration =
feedforward.maxAchievableAcceleration(
maxVoltage,
velocity
);For example:
double maxAcceleration =
feedforward.maxAchievableAcceleration(
12.0,
50.0
);The calculation is:
maxAcceleration =
(maxVoltage
- kS * Math.signum(velocity)
- kV * velocity)
/ kA;The result uses the acceleration units associated with your gains.
Static compensation follows the sign of the supplied velocity. At zero velocity, the static term is zero.
Minimum Achievable Acceleration
Use minAchievableAcceleration():double minAcceleration =
feedforward.minAchievableAcceleration(
maxVoltage,
velocity
);Internally, it evaluates the acceleration formula at negative maximum voltage:
return maxAchievableAcceleration(-maxVoltage, velocity);Both acceleration methods require a nonzero kA. Use a positive kA for the usual maximum and minimum interpretation.
The two-argument constructor sets kA to zero, so its acceleration-boundary calculations are not usable. Division by zero can produce infinity or NaN.
Reading and Changing the Gains
The gains are public and final:
double staticGain = feedforward.kS;
double velocityGain feedforward.kV;
double accelerationGain = feedfo rd.kA;They can be read directly but cannot be reassigned after construction.
To change the gains, create another instance:
feedforward =
new EasySimpleMotorFeedforward(
0.25,
0.12,
0.015
);The class does not validate the supplied values.
Limiting the Output
calculate() does not clamp its output to the available voltage.
Apply a limit before commanding the motor:
double requestedVoltage = feedforward.calculate(
targetVelocity,
targetAcceleration
);
double voltageLimit = 12.0;
double appliedVoltage = Math.max(
-voltageLimit,
Math.min(voltageLimit, requestedVoltage)
);For an FTC motor controlled through setPower(), convert the voltage request to normalized power using a valid measured supply voltage:
double power = appliedVoltage / batteryVoltage;
power = Math.max(-1.0, Math.min(1.0, power));
motor.setPower(power);This approximates a voltage command through motor power.
Complete Example
The following control-loop example combines the previously shown EasyPIDController with motor feedforward for velocity control.
It uses radians per second for velocity and volts for both controller outputs.
Create the controllers before entering the loop:
import dev.easyfamily.easystack.control.EasyPIDController;
import dev.easyfamily.easystack.control.wpiLibControllers.EasySimpleMotorFeedforward;
// Illustrative gains: tune for your mechanism.
// PID input: rad/s. PID output: volts.
EasyPIDController controller =
new EasyPIDController(0.1, 0.0, 0.0);
EasySimpleMotorFeedforward feedforward =
new EasySimpleMotorFeedforward(
0.2,
0.1,
0.01
);
controller.setTolerance(1.0);
double targetVelocity = 50.0;Inside the active control loop, obtain fresh values for measuredVelocityRadPerSec and batteryVoltage, then calculate:
double pidVoltage = controller.calculate(
measuredVelocityRadPerSec,
targetVelocity
);
// Constant-speed feedforward; acceleration is assumed zero.
double feedforwardVoltage =
feedforward.calculate(targetVelocity);
double requestedVoltage = pidVoltage + feedforwardVoltage;
double power = 0.0;
if (Double.isFinite(batteryVoltage) && batteryVoltage > 0.0) {
double voltageLimit = Math.min(12.0, batteryVoltage);
double appliedVoltage = Math.max(
-voltageLimit,
Math.min(voltageLimit, requestedVoltage)
); power = appliedVoltage / batteryVoltage;
motor.setPower(power);
addData("Target (rad/s) ", targetVelocity);
telemetry.addData("Velocity (rad/s) ", measuredVelocityRadPerSec);
addData("PID, pidVoltage);
telemetry.addData("Feedforward Voltage", feedforwardVoltage);
addData(controller.atTarget());
telemetry.addData("power", power);
update();Configure the FTC motor for direct power control when this loop supplies the velocity feedback control.
Convert encoder readings into the units used by your gains. FTC's no-argument DcMotorEx.getVelocity() returns ticks per second, so it cannot be substituted directly for a radian-per-second measurement without conversion.
For a moving velocity reference, include its requested acceleration:
double feedforwardVoltage = feedforward.calculate(
referenceVelocity,
referenceAcceleration
);When disabling control, stop applying the calculated output and command:
motor.setPower(0.0);
controller.reset();EasySimpleMotorFeedforward does not accumulate state and requires no reset.
Última atualização 2026-11-16