Skip to content

Commit

Permalink
output saturation
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlopez committed Aug 10, 2023
1 parent 36e1a5a commit f6aa33f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
7 changes: 4 additions & 3 deletions lib/Espfc/src/Control/Pid.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Pid
pScale(1.f), iScale(1.f), dScale(1.f), fScale(1.f),
pTerm(0.f), iTerm(0.f), dTerm(0.f), fTerm(0.f),
prevMeasure(0.f), prevError(0.f), prevSetpoint(0.f),
outputStaurated(false)
outputSaturated(false)
{}

void begin()
Expand All @@ -49,9 +49,10 @@ class Pid
pTerm = ptermFilter.update(pTerm);

// I-term
// TODO: https://github.com/betaflight/betaflight/blob/master/src/main/flight/pid.c#L667
if(Ki > 0.f && iScale > 0.f)
{
if(!outputStaurated)
if(!outputSaturated)
{
iTerm += Ki * error * dt * iScale;
iTerm = Math::clamp(iTerm, -iLimit, iLimit);
Expand Down Expand Up @@ -127,7 +128,7 @@ class Pid
float prevError;
float prevSetpoint;

bool outputStaurated;
bool outputSaturated;
};

}
Expand Down
5 changes: 0 additions & 5 deletions lib/Espfc/src/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,6 @@ class Controller
}

private:
float power3(float x)
{
return x * x * x;
}

Model& _model;
Rates _rates;
Filter _speedFilter;
Expand Down
7 changes: 4 additions & 3 deletions lib/Espfc/src/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,13 @@ class Model
return state.armingDisabledFlags & flag;
}

bool setOputputSaturated(bool val)
void setOutputSaturated(bool val)
{
state.outputSaturated = val;
for(size_t i = 0; i < 3; i++)
{
state.innerPid[i].outputStaurated = val;
state.outerPid[i].outputStaurated = val;
state.innerPid[i].outputSaturated = val;
state.outerPid[i].outputSaturated = val;
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/Espfc/src/ModelState.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ struct ModelState
float output[OUTPUT_CHANNELS];
int16_t outputUs[OUTPUT_CHANNELS];
int16_t outputDisarmed[OUTPUT_CHANNELS];
bool outputSaturated;

// other state
Kalman kalman[AXES];
Expand Down
9 changes: 5 additions & 4 deletions lib/Espfc/src/Output/Mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,14 @@ class Mixer
entry++;
}

bool saturated = false;
for(size_t i = 0; i < mixer.count; i++)
{
outputs[i] = limitOutput(outputs[i], _model.config.output.channel[i], _model.config.output.motorLimit);
const OutputChannelConfig& occ = _model.config.output.channel[i];
if(!occ.servo && outputs[i] >= 0.98f) saturated = true;
outputs[i] = limitOutput(outputs[i], occ, _model.config.output.motorLimit);
}
_model.setOutputSaturated(saturated);
}

float limitThrust(float thrust, ThrottleLimitType type, int8_t limit)
Expand Down Expand Up @@ -227,7 +231,6 @@ class Mixer
Stats::Measure mixerMeasure(_model.state.stats, COUNTER_MIXER_WRITE);

bool stop = _stop();
bool saturated = false;
for(size_t i = 0; i < OUTPUT_CHANNELS; i++)
{
const OutputChannelConfig& och = _model.config.output.channel[i];
Expand All @@ -244,13 +247,11 @@ class Mixer
}
else
{
if(out[i] >= 0.98f) saturated = true;
float v = Math::clamp(out[i], -1.f, 1.f);
_model.state.outputUs[i] = lrintf(Math::map(v, -1.f, 1.f, _model.state.minThrottle, _model.state.maxThrottle));
}
}
}
_model.setOputputSaturated(saturated);
applyOutput();
}

Expand Down

0 comments on commit f6aa33f

Please sign in to comment.