Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MicroCART
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Distributed Autonomous Networked Control Lab
MicroCART
Commits
87b8e5c7
Commit
87b8e5c7
authored
8 years ago
by
dawehr
Browse files
Options
Downloads
Patches
Plain Diff
PID block now uses the derivative of input value rather than the error.
parent
5ae02e08
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
quad/src/graph_blocks/node_pid.c
+12
-5
12 additions, 5 deletions
quad/src/graph_blocks/node_pid.c
with
12 additions
and
5 deletions
quad/src/graph_blocks/node_pid.c
+
12
−
5
View file @
87b8e5c7
...
@@ -6,9 +6,10 @@ static double FLT_EPSILON = 0.0001;
...
@@ -6,9 +6,10 @@ static double FLT_EPSILON = 0.0001;
struct
pid_node_state
{
struct
pid_node_state
{
double
prev_
error
;
// Previous
error
double
prev_
val
;
// Previous
input value
double
acc_error
;
// Accumulated error
double
acc_error
;
// Accumulated error
double
last_filtered
;
// Last output from the filtered derivative
double
last_filtered
;
// Last output from the filtered derivative
int
just_reset
;
// Set to 1 after a call to reset
};
};
// The generic PID diagram. This function takes in pid inputs (CUR_POINT and SETOINT) and calculates the output "pid_correction"
// The generic PID diagram. This function takes in pid inputs (CUR_POINT and SETOINT) and calculates the output "pid_correction"
...
@@ -50,21 +51,27 @@ static void pid_computation(void *state, const double* params, const double *inp
...
@@ -50,21 +51,27 @@ static void pid_computation(void *state, const double* params, const double *inp
pid_state
->
acc_error
+=
error
;
pid_state
->
acc_error
+=
error
;
}
}
if
(
pid_state
->
just_reset
)
{
// On first call after a reset, set the previous value to
// the current value to prevent a spike in derivative
pid_state
->
prev_val
=
inputs
[
PID_CUR_POINT
];
pid_state
->
just_reset
=
0
;
}
// Compute each term's contribution
// Compute each term's contribution
P
=
params
[
PID_KP
]
*
error
;
P
=
params
[
PID_KP
]
*
error
;
I
=
params
[
PID_KI
]
*
pid_state
->
acc_error
*
inputs
[
PID_DT
];
I
=
params
[
PID_KI
]
*
pid_state
->
acc_error
*
inputs
[
PID_DT
];
// Low-pass filter on derivative
// Low-pass filter on derivative
double
change_in_
error
=
error
-
pid_state
->
prev_
error
;
double
change_in_
value
=
inputs
[
PID_CUR_POINT
]
-
pid_state
->
prev_
val
;
double
term1
=
params
[
PID_ALPHA
]
*
pid_state
->
last_filtered
;
double
term1
=
params
[
PID_ALPHA
]
*
pid_state
->
last_filtered
;
double
derivative
=
change_in_
error
/
inputs
[
PID_DT
];
double
derivative
=
change_in_
value
/
inputs
[
PID_DT
];
if
(
inputs
[
PID_DT
]
==
0
)
{
// Divide by zero check
if
(
inputs
[
PID_DT
]
==
0
)
{
// Divide by zero check
derivative
=
0
;
derivative
=
0
;
}
}
double
term2
=
params
[
PID_KD
]
*
(
1
.
0
f
-
params
[
PID_ALPHA
])
*
derivative
;
double
term2
=
params
[
PID_KD
]
*
(
1
.
0
f
-
params
[
PID_ALPHA
])
*
derivative
;
D
=
term1
+
term2
;
D
=
term1
+
term2
;
pid_state
->
last_filtered
=
D
;
// Store filtered value for next filter iteration
pid_state
->
last_filtered
=
D
;
// Store filtered value for next filter iteration
pid_state
->
prev_
error
=
error
;
// Store the current error into the state
pid_state
->
prev_
val
=
inputs
[
PID_CUR_POINT
]
;
// Store the current error into the state
outputs
[
PID_CORRECTION
]
=
P
+
I
+
D
;
// Store the computed correction
outputs
[
PID_CORRECTION
]
=
P
+
I
+
D
;
// Store the computed correction
}
}
...
@@ -74,8 +81,8 @@ static void pid_computation(void *state, const double* params, const double *inp
...
@@ -74,8 +81,8 @@ static void pid_computation(void *state, const double* params, const double *inp
static
void
reset_pid
(
void
*
state
)
{
static
void
reset_pid
(
void
*
state
)
{
struct
pid_node_state
*
pid_state
=
(
struct
pid_node_state
*
)
state
;
struct
pid_node_state
*
pid_state
=
(
struct
pid_node_state
*
)
state
;
pid_state
->
acc_error
=
0
;
pid_state
->
acc_error
=
0
;
pid_state
->
prev_error
=
0
;
pid_state
->
last_filtered
=
0
;
pid_state
->
last_filtered
=
0
;
pid_state
->
just_reset
=
1
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment