Skip to content

Fix: Corrected velocity measurement in CascadeController #36

Open
Nataso08 wants to merge 1 commit into
FTC-23511:masterfrom
PlanckTeam:master
Open

Fix: Corrected velocity measurement in CascadeController #36
Nataso08 wants to merge 1 commit into
FTC-23511:masterfrom
PlanckTeam:master

Conversation

@Nataso08

@Nataso08 Nataso08 commented Jun 5, 2026

Copy link
Copy Markdown

What fix

This PR fixes the velocity calculation in the CascadeController class. The previous implementation incorrectly calculated measured velocity as the derivative of position error rather than the derivative of measured position itself, resulting in inaccurate velocity feedback in the cascade controller loop.

Previous error (fixed)

The original code computed velocity as velMeasuredValue = (errorVal_p - prevErrorVal) / period;, which is mathematically incorrect since velocity should represent the rate of change of position, not error. This fix implements the correct calculation by tracking the previous measured position and computing velMeasuredValue = (measuredValue - prevMeasuredValue) / period;.

Fix implementation

The implementation introduces a prevMeasuredValue field to maintain position history, updates the timestamp only when the period is valid (greater than 1E-6) for improved efficiency, applies the corrected formula to the setSetPoints() method, and adds a new getMeasuredVel() getter for public access to the measured velocity. These changes improve accuracy of velocity feedback, ensure physical consistency with the mathematical definition of velocity, and enhance the overall stability of cascade control behavior.

Possible breaks

This fix could brake some previous tuning because of it change measured value of velocity controller (private Controller secondary) as above explained.

CLOSES #35

Copilot AI review requested due to automatic review settings June 5, 2026 18:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates CascadeController to compute measured velocity from successive measured values (instead of error deltas) and exposes the computed velocity via a new accessor.

Changes:

  • Track previous measured value (prevMeasuredValue) to calculate velMeasuredValue from position changes over time.
  • Adjust timestamp/period handling in calculateOutput.
  • Add getMeasuredVel() accessor.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 20 to +32
double currentTimeStamp = (double) System.nanoTime() / 1E9;
if (lastTimeStamp == 0) lastTimeStamp = currentTimeStamp;
period = currentTimeStamp - lastTimeStamp;
lastTimeStamp = currentTimeStamp;

if (measuredValue == pv) {
errorVal_p = setPoint - measuredValue;
} else {
errorVal_p = setPoint - pv;
if (measuredValue != pv) {
measuredValue = pv;
}
errorVal_p = setPoint - measuredValue;

if (Math.abs(period) > 1E-6) {
velMeasuredValue = (errorVal_p - prevErrorVal) / period;
} else {
velMeasuredValue = 0;
}
velMeasuredValue = (measuredValue - prevMeasuredValue) / period;
lastTimeStamp = currentTimeStamp;
}
Comment on lines 48 to 52
setPoint = psp;
velSetPoint = vsp;
errorVal_p = setPoint - measuredValue;
velMeasuredValue = (errorVal_p - prevErrorVal) / period;
velMeasuredValue = (measuredValue - prevMeasuredValue) / period;
errorVal_v = velSetPoint - velMeasuredValue;
private final Controller primary;
private final Controller secondary;
private double velMeasuredValue;
private double prevMeasuredValue;
@Override
protected double calculateOutput(double pv) {
prevErrorVal = errorVal_p;
prevMeasuredValue = measuredValue;
Comment on lines +55 to +57
public double getMeasuredVel() {
return velMeasuredValue;
}
@Nataso08 Nataso08 changed the title #35 *Fix*: Corrected velocity measurement in CascadeController [#35](https://github.com/FTC-23511/SolversLib/issues/35) *Fix*: Corrected velocity measurement in CascadeController Jun 5, 2026
@Nataso08 Nataso08 changed the title [#35](https://github.com/FTC-23511/SolversLib/issues/35) *Fix*: Corrected velocity measurement in CascadeController *Fix*: Corrected velocity measurement in CascadeController Jun 5, 2026
@Nataso08 Nataso08 changed the title *Fix*: Corrected velocity measurement in CascadeController *Fix* : Corrected velocity measurement in CascadeController Jun 5, 2026
@Nataso08 Nataso08 changed the title *Fix* : Corrected velocity measurement in CascadeController Fix: Corrected velocity measurement in CascadeController Jun 5, 2026
@Nataso08 Nataso08 closed this Jun 8, 2026
@Nataso08 Nataso08 reopened this Jun 8, 2026
@Nataso08

Nataso08 commented Jun 8, 2026

Copy link
Copy Markdown
Author

Also, because my bad at the first time, I saw in CascadeController class is never setted the setPoint to the position Controller, both in calculateOutput function or in other class methods.

This cause a wrong reference to the primary controller, witch want to go back to 0 position (in witch errorVal_p = 0).

An easy adjutment to this problem is the extension of primary.calculate(measuredValue); method adding the sp value in the for primary.calculate(measuredValue, setPoint);.

If possible, I'm able to add this change to my PR, in order to simplify your work. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong velocity measurement in CascadeController class

2 participants