Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b073746
feat(chirp): share the trust band and let the estimator pool several …
Sep 19, 2026
f367eef
feat(chirp): score every chirp run in a log, not just the longest
Sep 19, 2026
7e16369
fix(chirp): pick the run that moved the axis, not the longest one
Sep 19, 2026
5e53060
test: fix two assertions that passed a message where a tolerance goes
Sep 19, 2026
453ff57
refactor(chirp): split identification out of the plotting
Sep 19, 2026
e80ee3c
fix(chirp): bound the trust band by the plant coherence, not tracking…
Sep 19, 2026
dabdfb7
feat(chirp): build the controller family once as a basis for the scan
Sep 19, 2026
0d6fd35
feat(chirp): scan P and D for the widest loop the margins allow
Sep 19, 2026
4c382e8
fix(chirp): set the D dominance limit from measured tunes, not from a…
Sep 19, 2026
23deb6e
fix(chirp): scale I with P so cutting gain does not move the PI corner
Sep 19, 2026
479ef70
fix(parser): read the log section number from the csv name, not its l…
Sep 20, 2026
7dc0133
feat(chirp): carry the sweep, saturation and throttle scores through …
Sep 20, 2026
1b95987
feat(chirp): refuse logs that cannot be tuned from, with thresholds r…
Sep 20, 2026
47ea5ee
refactor(filter-sim): hoist the clipboard helpers so other tools can …
Sep 20, 2026
7b9f594
feat(chirp): build the CLI text, with no way to emit a filter setting
Sep 20, 2026
88cff9a
fix(chirp): walk whole gains so the answer stops depending on the gri…
Sep 20, 2026
e8a2599
feat(chirp): let the aggressiveness setting reach the cut it implies
Sep 20, 2026
1061244
fix(chirp): calibrate the swept-axis floor through the import path, n…
Sep 20, 2026
bc3459b
feat(ui): add the autotune window and its control panel button
Sep 20, 2026
554a0f7
docs: credit the Betaflight Configurator autotune as the behavioural …
Sep 20, 2026
86d77ac
feat(ui): show all three autotune settings at once, computed once and…
Sep 20, 2026
9d7e8c4
fix(ui): drop the accent colour from the table, print each flown gain…
Sep 20, 2026
6eb07bd
feat(ui): make autotune a full-screen tool with a step response per axis
Sep 20, 2026
cc0c319
fix(ui): band the table rows, colour each value by direction, one col…
Sep 20, 2026
4a18ece
fix(chirp): tune I against the step instead of scaling it blindly with P
Sep 20, 2026
bc743df
fix(ui): reconstruct the step on the full grid, name overlapping curv…
Sep 20, 2026
d5e0bd3
feat(ui): add an editable column that draws the closed loop from your…
Sep 20, 2026
0a47975
feat(chirp): judge whether the flown tune needs changing at all
Sep 20, 2026
ec2e070
feat(ui): copy the custom column too, and tint it like the rest
Sep 20, 2026
5f8a788
fix(chirp): put the overshoot limit inside the gap the logs actually …
Sep 20, 2026
862d608
fix(chirp): stop judging a tune by a step the firmware never runs
Sep 20, 2026
92c3bbc
fix(chirp): settle I inside the scan so the judged cell is the report…
Sep 20, 2026
9dab53d
fix(ui): a gate warning is not a refusal, plus verdict banner, colour…
Sep 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions PIDscope.m
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
posInfo.spectrogramButton = [cpL+cpM vPos-rs*row cpW-2*cpM rh]; row=row+1;
posInfo.TuningButton = [cpL+cpM vPos-rs*row cpW-2*cpM rh]; row=row+1;
posInfo.PIDsliderButton=[cpL+cpM vPos-rs*row cpW-2*cpM rh]; row=row+1;
posInfo.autotuneButton= [cpL+cpM vPos-rs*row cpW-2*cpM rh]; row=row+1;
posInfo.filterSimButton=[cpL+cpM vPos-rs*row cpW/2-cpM rh];
posInfo.testSignalButton=[cpL+cpW/2 vPos-rs*row cpW/2-cpM rh]; row=row+1;
posInfo.PIDErrorButton = [cpL+cpM vPos-rs*row cpW/2-cpM rh];
Expand Down Expand Up @@ -311,6 +312,15 @@
'end']);
set(guiHandles.PIDsliderButton, 'ForegroundColor', th.btnDash3);

guiHandles.autotuneButton = uicontrol(PSfig,'string','Autotune (chirp)','fontsize',fontsz,...
'TooltipString','Propose PID gains from a chirp log','units','normalized',...
'Position',[posInfo.autotuneButton],...
'callback',['if ~exist(''T'',''var''),warndlg(''Please select file(s)'');' ...
'else,tmpFcnt=get(guiHandles.FileNum,''Value'');tmpFcnt=tmpFcnt(1);' ...
'PSautotuneUI(T{tmpFcnt},SetupInfo{tmpFcnt},1000*A_lograte(tmpFcnt),tIND{tmpFcnt},fnameMaster{tmpFcnt});' ...
'clear tmpFcnt;end']);
set(guiHandles.autotuneButton, 'ForegroundColor', th.btnAutotune);

guiHandles.filterSimButton = uicontrol(PSfig,'string','Filter Simulator','fontsize',fontsz,...
'TooltipString','Simulate BF filter chain (theoretical response)','units','normalized',...
'Position',[posInfo.filterSimButton],...
Expand Down Expand Up @@ -445,15 +455,16 @@ try set(guiHandles.linewidth, 'Value', defaults.Values(find(strcmp(defaults.Para
cpItems{end+1} = struct('h', guiHandles.spectrogramButton, 'type','full', 'row',7, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.TuningButton, 'type','full', 'row',8, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.PIDsliderButton, 'type','full', 'row',9, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.filterSimButton, 'type','left', 'row',10, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.testSignalButton, 'type','right', 'row',10, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.PIDErrorButton, 'type','left', 'row',11, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.FlightStatsButton, 'type','right', 'row',11, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.period2Hz, 'type','left', 'row',12, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.DispInfoButton, 'type','right', 'row',12, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.saveFig, 'type','left', 'row',13, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.saveSettings, 'type','right', 'row',13, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.PIDtuningService, 'type','full', 'row',14, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.autotuneButton, 'type','full', 'row',10, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.filterSimButton, 'type','left', 'row',11, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.testSignalButton, 'type','right', 'row',11, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.PIDErrorButton, 'type','left', 'row',12, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.FlightStatsButton, 'type','right', 'row',12, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.period2Hz, 'type','left', 'row',13, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.DispInfoButton, 'type','right', 'row',13, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.saveFig, 'type','left', 'row',14, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.saveSettings, 'type','right', 'row',14, 'col',0, 'nrows',0);
cpItems{end+1} = struct('h', guiHandles.PIDtuningService, 'type','full', 'row',15, 'col',0, 'nrows',0);
nrows = max(cellfun(@(x) x.row, cpItems));
cpItems = [{struct('h', controlpanel, 'type','panel', 'row',0, 'col',0, 'nrows',nrows)}, cpItems];
PSregisterResize(PSfig, cpPx, cpItems, 'rows');
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ The chirp analysis in PIDscope builds on work by **Michael Peter ([pichim](https
- `src/core/PSstepFromFRD.m` - from `calculate_step_response_from_frd.m`
- `src/core/PSrotFiltFilt.m` - from `apply_rotfiltfilt.m`

The chirp logs used to validate the autotune come from the same repository and
are flown by the same author.

The autotune follows the behaviour of the [Betaflight Configurator](https://github.com/betaflight/betaflight-configurator)
autotune (GPL-3.0) - the three aggressiveness levels and their phase margin
targets, and the hard limit on the sensitivity peak. The algorithm here was
written from that description and no code was ported. It differs where it
matters: Betaflight approximates the open loop as `T/(1-T)`, which assumes unity
feedback, while PIDscope measures the plant separately through `axisSum` and so
works with the exact `L = P*(A+D)`. That is what lets it move D as well as P.

Arch Linux packaging is maintained by [FPVogel](https://github.com/FPVogel).

---
Expand Down
31 changes: 31 additions & 0 deletions src/core/PSautotuneBasis.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function b = PSautotuneBasis(gains, fp, Fs, freq)
%% PSautotuneBasis - the whole controller family as four fixed responses
% gains - only axis and tpa are read; the four gains are set here
% fp - filter params from PSparseFilterParams, or [] for no dterm filtering
% Fs - PID loop rate (Hz)
% freq - frequency vector (Hz)
%
% Every gain enters PSbuildController as a plain scalar multiplier, so two
% calls span the family a scan walks over:
% A = p*b.Ap + i*b.Ai, D = d*b.D1, F = f*b.F1
% A thousand-cell scan then costs two controller builds instead of a thousand.
%
% b.freq drops DC, where the integrator is Inf and a candidate with I = 0 would
% ask for 0*Inf. b.keep maps b.freq back onto the grid that came in.

freq = freq(:);
b.keep = freq > 0;
b.freq = freq(b.keep);

g = struct('axis', 0, 'tpa', []);
if isfield(gains, 'axis') && ~isempty(gains.axis), g.axis = gains.axis; end
if isfield(gains, 'tpa'), g.tpa = gains.tpa; end

g.P = 1; g.I = 0; g.D = 1; g.F = 1;
[Ap, b.D1, b.F1] = PSbuildController(g, fp, Fs, b.freq);
b.Ap = Ap;

g.P = 0; g.I = 1; g.D = 0; g.F = 0;
b.Ai = PSbuildController(g, fp, Fs, b.freq);

end
128 changes: 128 additions & 0 deletions src/core/PSautotuneGates.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
function [ok, msgs, info] = PSautotuneGates(id, opt)
%% PSautotuneGates - whether a measurement is fit to tune from
% id - from PSidentifyChirp
% opt - threshold overrides; every threshold is echoed in info
%
% ok - false when the axis must be refused
% msgs - what to tell the pilot; non-empty with ok true means a warning
% info - the thresholds that were applied, plus what the log actually scored
%
% These judge the measurement. The scan's own constraints judge the loop, and
% the two must not be confused: a log can be perfectly measured and still have
% no admissible tune, which is a different answer from "this log cannot be
% read".
%
% The numbers come from the pichim corpus - 18 axes across 6 logs from 3
% airframes - except where marked structural, meaning the log cannot mean
% anything else.

if nargin < 2 || isempty(opt), opt = struct(); end

o = struct('gyroVarMin', 500, ... % swept axes score 3523..6188, coupling reaches 137
'sweepFrac', 0.95, ... % structural: a short sweep has no top of band
'satFracMax', 0.01, ... % structural: saturation makes the plant a fiction
'nSegMin', 8, ... % the corpus runs 70 on every axis
'cohMin', 0.78, ... % the corpus sits at 0.999 median
'fTrustMin', 60, ... % the corpus runs 106..164 Hz
'FsPidRange', [500 16000]);
fn = fieldnames(o);
for k = 1:numel(fn)
if isfield(opt, fn{k}) && ~isempty(opt.(fn{k})), o.(fn{k}) = opt.(fn{k}); end
end

info = o;
info.dClampHi = 1.25;
info.gyroVar = getf(id, 'gyroVar');
info.satFrac = getf(id, 'satFrac');
info.thrStd = getf(id, 'thrStd'); % carried, not gated: no failing log measured yet
info.nSeg = getf(id, 'nSeg');
info.fTrust = getf(id, 'fTrust');
info.cohMedian = NaN;

msgs = {};
ok = false;

if ~isfield(id, 'ok') || ~id.ok
msgs{end+1} = id.msg;
return
end

if isempty(id.G_plant)
msgs{end+1} = 'The PID terms are not in this log, so the plant cannot be measured';
return
end

if ~(id.gains.P > 0 && id.gains.I > 0)
msgs{end+1} = 'P and I must both be non-zero for a gain to be scaled';
return
end

if ~(id.FsPid >= o.FsPidRange(1) && id.FsPid <= o.FsPidRange(2))
msgs{end+1} = sprintf('Loop rate reads %g Hz, which is outside %g..%g', ...
id.FsPid, o.FsPidRange(1), o.FsPidRange(2));
return
end

if ~(info.gyroVar > o.gyroVarMin)
msgs{end+1} = sprintf(['This axis was not swept in the run that was picked ' ...
'(gyro variance %.0f, needs %.0f)'], info.gyroVar, o.gyroVarMin);
return
end

if ~(id.durSec >= o.sweepFrac * id.chirpTime)
msgs{end+1} = sprintf('The sweep stopped after %.1f s of %.1f s, so the top of the band is missing', ...
id.durSec, id.chirpTime);
return
end

if ~(info.satFrac < o.satFracMax)
msgs{end+1} = sprintf(['The PID sum saturated for %.1f%% of the run, so the loop ' ...
'was not linear and the plant measured through it is not real'], ...
100*info.satFrac);
return
end

if ~(info.nSeg >= o.nSegMin)
msgs{end+1} = sprintf('Only %d segments to average, needs %d', info.nSeg, o.nSegMin);
return
end

if ~(info.fTrust >= o.fTrustMin)
msgs{end+1} = sprintf('The trusted band ends at %.0f Hz, needs %.0f', ...
info.fTrust, o.fTrustMin);
return
end

% the plant is G_track/G_uw, so judge it on the worse of the two. The upper edge
% follows fTrust, otherwise this and the trust band gate measure the same thing
% twice and on a 107 Hz log this one becomes unreachable.
hi = min(150, info.fTrust);
band = id.freq > 2 & id.freq <= hi;
if any(band)
C = id.C_track;
if ~isempty(id.C_uw), C = min(id.C_track, id.C_uw); end
info.cohMedian = median(C(band));
end
if ~(info.cohMedian >= o.cohMin)
msgs{end+1} = sprintf('Plant coherence is %.2f over 2..%.0f Hz, needs %.2f', ...
info.cohMedian, hi, o.cohMin);
return
end

ok = true;

% dynamic D is a warning: the log records the floor of a D that moved, and the
% controller model only covers the static value
if isfield(id.gains, 'dMax') && ~isempty(id.gains.dMax) && id.gains.dMax > id.gains.D
info.dClampHi = 1.0;
msgs{end+1} = sprintf(['Dynamic D was active (d_max %g above D %g), so D is held ' ...
'at or below what was flown'], id.gains.dMax, id.gains.D);
end

end


function v = getf(s, f)
v = NaN;
if isfield(s, f) && ~isempty(s.(f)), v = s.(f); end
end
Loading