Skip to content

Commit d893f51

Browse files
New component at_pidv2 based on at_pid and pid components.
Started with a copy of pid.c, and copied relevant code from at_pid.c and inserted it into relevant locations. This way at_pidv2 become compatible with the current pid component, and those that want to test autotuning can insert it in-place without modifying any pins and signals in their HAL files. The original at_pid code was commited by Peter G. Vavaroutsos, so insert his name as author and also copy the module name over. Manual page at_pidv2(9) is based on pid(9) with the comment about the auto-tuning code not being updated since 2011 added and the relevant parts coped over from at_pid(9). This module is tested and found to be giving not completely unusable P, I and D values for horizontal movements.
1 parent ec91c44 commit d893f51

4 files changed

Lines changed: 345 additions & 19 deletions

File tree

docs/man/man9/at_pidv2.9

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ pid \- proportional/integral/derivative controller
66
\fBloadrt pid [num_chan=\fInum\fB | names=\fIname1\fB[,\fIname2...\fB]] [\fBdebug=\fIdbg\fR]
77

88
.SH DESCRIPTION
9+
\fBNOTE:\fR The auto-tuning part of this component have seen
10+
no development since 2011.
11+
.P
912
\fBpid\fR is a classic Proportional/Integral/Derivative controller,
1013
used to control position or speed feedback loops for servo motors and
1114
other closed-loop applications.
@@ -161,6 +164,29 @@ output = bias + error * Pgain + errorI * Igain +
161164
limit output to +/- maxoutput
162165
.fi
163166

167+
.P
168+
This component has a built in auto tune mode. It works by setting up a limit
169+
cycle to characterize the process. From this, \fBPgain/Igain/Dgain\fR or
170+
\fBPgain/Igain/FF1\fR can be determined using Ziegler-Nichols. When using
171+
\fBFF1\fR, scaling must be set so that \fBoutput\fR is in user units per second.
172+
.P
173+
During auto tuning, the \fBcommand\fR input should not change. The limit
174+
cycle is setup around the commanded position. No initial tuning values are
175+
required to start auto tuning. Only \fBtune\-cycles\fR, \fBtune\-effort\fR
176+
and \fBtune\-mode\fR need be set before starting auto tuning. When auto tuning
177+
completes, the tuning parameters will be set. If running from LinuxCNC, the
178+
FERROR setting for the axis being tuned may need to be loosened up as it must
179+
be larger than the limit cycle amplitude in order to avoid a following error.
180+
.P
181+
To perform auto tuning, take the following steps. Move the axis to be tuned,
182+
to somewhere near the center of it's travel. Set \fBtune\-cycles\fR (the
183+
default value should be fine in most cases) and \fBtune\-mode\fR. Set
184+
\fBtune\-effort\fR to a small value. Set \fBenable\fR to true. Set
185+
\fBtune\-mode\fR to true. Set \fBtune\-start\fR to true. If no oscillation
186+
occurs, or the oscillation is too small, slowly increase \fBtune\-effort\fR.
187+
Auto tuning can be aborted at any time by setting \fBenable\fR or
188+
\fBtune\-mode\fR to false.
189+
164190
.SH NAMING
165191
The names for pins, parameters, and functions are prefixed as:
166192
\fBpid.N.\fR for N=0,1,...,num\-1 when using \fBnum_chan=num\fR
@@ -339,6 +365,40 @@ When true, the current PID output is saturated. That is,
339365
\fBpid.\fIN\fB.saturated\-count\fR s32 out
340366
When true, the output of PID was continually saturated for this many seconds
341367
(\fBsaturated\-s\fR) or periods (\fBsaturated\-count\fR).
368+
369+
.SS Additional auto tuning pins
370+
.TP
371+
\fBpid.\fIN\fB.tune\-mode\fR bit in
372+
When true, enables auto tune mode. When false, normal PID calculations are
373+
performed.
374+
.TP
375+
\fBpid.\fIN\fB.tune\-start\fR bit io
376+
When set to true, starts auto tuning. Cleared when the auto tuning completes.
377+
.TP
378+
\fBpid.\fIN\fB.tune\-type\fR u32 rw
379+
When set to 0, \fBPgain/Igain/Dgain\fR are calculated. When set to 1,
380+
\fBPgain/Igain/FF1\fR are calculated.
381+
.TP
382+
\fBpid.\fIN\fB.tune\-cycles\fR u32 rw
383+
Determines the number of cycles to run to characterize the process.
384+
\fBtune\-cycles\fR actually sets the number of half cycles. More cycles results
385+
in a more accurate characterization as the average of all cycles is used.
386+
.TP
387+
\fBpid.\fIN\fB.tune\-effort\fR float rw
388+
Determines the effort used in setting up the limit cycle in the process.
389+
\fBtune\-effort\fR should be set to a positive value less than \fBmaxoutput\fR.
390+
Start with something small and work up to a value that results in a good
391+
portion of the maximum motor current being used. The smaller the value, the
392+
smaller the amplitude of the limit cycle.
393+
.TP
394+
\fBpid.\fIN\fB.ultimate\-gain\fR float ro (only if debug=1)
395+
Determined from process characterization. \fBultimate\-gain\fR is the ratio of
396+
\fBtune\-effort\fR to the limit cycle amplitude multiplied by 4.0 divided by Pi.
397+
.TP
398+
\fBpid.\fIN\fB.ultimate\-period\fR float ro (only if debug=1)
399+
Determined from process characterization. \fBultimate\-period\fR is the period
400+
of the limit cycle.
401+
342402
.SH PARAMETERS
343403
.TP
344404
\fBpid.\fIN\fB.errorI\fR float ro (only if debug=1)
@@ -373,4 +433,4 @@ gains to fix it is a mistake; set the scaling correctly elsewhere
373433
instead.
374434

375435
.SH SEE ALSO
376-
\fBat_pid\fR(9)
436+
\fBpid\fR(9)

src/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,8 @@ obj-$(CONFIG_PID) += pid.o
894894
pid-objs := hal/components/pid.o $(MATHSTUB)
895895
obj-$(CONFIG_AT_PID) += at_pid.o
896896
at_pid-objs := hal/components/at_pid.o $(MATHSTUB)
897+
obj-$(CONFIG_AT_PIDV2) += at_pidv2.o
898+
at_pidv2-objs := hal/components/at_pidv2.o $(MATHSTUB)
897899
obj-$(CONFIG_PID) += threads.o
898900
threads-objs := hal/components/threads.o $(MATHSTUB)
899901
obj-$(CONFIG_SUPPLY) += supply.o
@@ -1226,6 +1228,7 @@ endif
12261228
../rtlib/pwmgen$(MODULE_EXT): $(addprefix objects/rt,$(pwmgen-objs))
12271229
../rtlib/siggen$(MODULE_EXT): $(addprefix objects/rt,$(siggen-objs))
12281230
../rtlib/at_pid$(MODULE_EXT): $(addprefix objects/rt,$(at_pid-objs))
1231+
../rtlib/at_pidv2$(MODULE_EXT): $(addprefix objects/rt,$(at_pidv2-objs))
12291232
../rtlib/pid$(MODULE_EXT): $(addprefix objects/rt,$(pid-objs))
12301233
../rtlib/threads$(MODULE_EXT): $(addprefix objects/rt,$(threads-objs))
12311234
../rtlib/supply$(MODULE_EXT): $(addprefix objects/rt,$(supply-objs))

src/Makefile.inc.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ CONFIG_FREQGEN=m
190190
CONFIG_PWMGEN=m
191191
CONFIG_SIGGEN=m
192192
CONFIG_AT_PID=m
193+
CONFIG_AT_PIDV2=m
193194
CONFIG_PID=m
194195
CONFIG_SUPPLY=m
195196
CONFIG_CLASSICLADDER_RT=m

0 commit comments

Comments
 (0)