-
Notifications
You must be signed in to change notification settings - Fork 158
pid - Add PID controller and associated utility functions #1793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
99729ab
Add PID controller and utility functions
TheCandianVendingMachine 7dbff0a
fix wrong defaults
TheCandianVendingMachine 1052f1e
fix typo
TheCandianVendingMachine d257431
update docs
TheCandianVendingMachine 96e6754
update header
TheCandianVendingMachine f0301d0
update headers
TheCandianVendingMachine 37cbd87
fix trailing commas
TheCandianVendingMachine 88e4601
fix stringtable
TheCandianVendingMachine f6ee938
fix somethings not updated
TheCandianVendingMachine 8bb49b3
typo
TheCandianVendingMachine 31c7ddf
math variable -> better name
TheCandianVendingMachine beae46f
fix large numbers
TheCandianVendingMachine c7b497e
add defaults as macros
TheCandianVendingMachine 092da72
dont pollute history with same-x values
TheCandianVendingMachine 232f5ed
better derivative approximation
TheCandianVendingMachine 676852d
better representation of what we want
TheCandianVendingMachine 5c17794
Update addons/pid/fnc_setGains.sqf
TheCandianVendingMachine 470c096
fix bug on first integral sum
TheCandianVendingMachine b2a41a1
Merge branch 'pid' of github.com:TheCandianVendingMachine/CBA_A3 into…
TheCandianVendingMachine 541aa07
fix incorrect derivative calculation
TheCandianVendingMachine a44c4ae
switch to better integration method
TheCandianVendingMachine f73a35d
Add optional setting to function
TheCandianVendingMachine 31f1a59
remove trailing comma
TheCandianVendingMachine 3e8ad7a
Update addons/pid/fnc_update.sqf
TheCandianVendingMachine a4818b0
update API to be a bit more ergonomic
TheCandianVendingMachine c6a0632
store error in history, not logged values
TheCandianVendingMachine b799856
update API again, the error function isn't touched much in practice
TheCandianVendingMachine 35976eb
Update addons/pid/fnc_create.sqf
TheCandianVendingMachine 8422ec5
doc update
TheCandianVendingMachine 9434d17
Apply suggestions from code review
TheCandianVendingMachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| x\cba\addons\pid |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Extended_PreStart_EventHandlers { | ||
| class ADDON { | ||
| init = QUOTE(call COMPILE_SCRIPT(XEH_preStart)); | ||
| }; | ||
| }; | ||
|
|
||
| class Extended_PreInit_EventHandlers { | ||
| class ADDON { | ||
| init = QUOTE(call COMPILE_SCRIPT(XEH_preInit)); | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| PREP(create); | ||
| PREP(error_degree); | ||
| PREP(error_linear); | ||
| PREP(reset); | ||
| PREP(setGains); | ||
| PREP(setpoint); | ||
| PREP(update); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #include "script_component.hpp" | ||
|
|
||
| ADDON = false; | ||
|
|
||
| #include "XEH_PREP.hpp" | ||
|
|
||
| ADDON = true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #include "script_component.hpp" | ||
|
|
||
| #include "XEH_PREP.hpp" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include "script_component.hpp" | ||
|
|
||
| class CfgPatches { | ||
| class ADDON { | ||
| name = CSTRING(component); | ||
| units[] = {}; | ||
| weapons[] = {}; | ||
| requiredVersion = REQUIRED_VERSION; | ||
| requiredAddons[] = {"cba_common"}; | ||
| author = "$STR_CBA_Author"; | ||
| authors[] = {"tcvm"}; | ||
| url = "$STR_CBA_URL"; | ||
| VERSION_CONFIG; | ||
| }; | ||
| }; | ||
|
|
||
| #include "CfgEventHandlers.hpp" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #include "script_component.hpp" | ||
| /* ---------------------------------------------------------------------------- | ||
| Function: CBA_pid_fnc_create | ||
|
|
||
| Description: | ||
| Creates a PID controller. | ||
|
|
||
| Parameters: | ||
| _pGain - gain for the immediate error <NUMBER> | ||
| (Default: 0) | ||
| _iGain - gain for the persistent error over time <NUMBER> | ||
| (Default: 0) | ||
| _dGain - gain for the error rate over time <NUMBER> | ||
| (Default: 0) | ||
| _setpoint - initial setpoint for the controller <NUMBER> | ||
| (Default: 0) | ||
| _min - the minimum value the controller can return <NUMBER> | ||
| (Default: -1e30) | ||
| _max - the maximum value the controller can return <NUMBER> | ||
| (Default: 1e30) | ||
| _historyLength - how many past errors are stored and used to calculate the derivative/integral <NUMBER> | ||
| (Default: 10) | ||
| _errorFunction - the function which calculates the error which the controller operators <CODE> | ||
| (Default: <CBA_pid_fnc_error_linear>) | ||
|
|
||
| Returns: | ||
| _pid - a PID controller <LOCATION> | ||
|
|
||
| Examples: | ||
| (begin example) | ||
| private _pid = [1, 0.05, 0.2, 5, 0, 10, 15, CBA_pid_fnc_error_linear] call CBA_pid_fnc_create; | ||
| (end) | ||
|
|
||
| Author: | ||
| tcvm | ||
| ---------------------------------------------------------------------------- */ | ||
| SCRIPT(create); | ||
| params [ | ||
| ["_pGain", 0, [0]], | ||
| ["_iGain", 0, [0]], | ||
| ["_dGain", 0, [0]], | ||
| ["_setpoint", 0, [0]], | ||
| ["_min", -LARGE_NUMBER, [0]], | ||
| ["_max", LARGE_NUMBER, [0]], | ||
| ["_historyLength", DEFAULT_HISTORY_LENGTH, [0]], | ||
| ["_errorFunction", FUNC(error_linear), [{}]] | ||
| ]; | ||
|
|
||
| private _pid = call CBA_fnc_createNamespace; | ||
| _pid setVariable [QGVAR(gains), [_pGain, _iGain, _dGain]]; | ||
| _pid setVariable [QGVAR(bounds), [_min, _max]]; | ||
| _pid setVariable [QGVAR(history), []]; | ||
| _pid setVariable [QGVAR(historyLength), _historyLength]; | ||
| _pid setVariable [QGVAR(setpoint), _setpoint]; | ||
| _pid setVariable [QGVAR(errorFunction), _errorFunction]; | ||
|
|
||
| _pid |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #include "script_component.hpp" | ||
| /* ---------------------------------------------------------------------------- | ||
| Function: CBA_pid_fnc_error_degree | ||
|
|
||
| Description: | ||
| Error function that is adjusted for degree values. | ||
| This function will account for wraparound with values in the domain of [-180, 180] or [0, 360] | ||
|
|
||
| Parameters: | ||
| _observed - the observed value <NUMBER> | ||
| _setpoint - the setpoint <NUMBER> | ||
|
|
||
| Returns: | ||
| Wrapped error of `setpoint - observed` <NUMBER> | ||
|
|
||
| Examples: | ||
| (begin example) | ||
| private _error = [350, 5] call CBA_pid_fnc_error_degree; | ||
| // _error == 15 | ||
| (end) | ||
|
|
||
| Author: | ||
| tcvm | ||
| ---------------------------------------------------------------------------- */ | ||
| SCRIPT(error_degree); | ||
| params [ | ||
| ["_observed", 0, [0]], | ||
| ["_setpoint", 0, [0]] | ||
| ]; | ||
|
|
||
| private _absoluteError = _setpoint - _observed; | ||
| private _error = _absoluteError; | ||
| if (_absoluteError < -180) then { | ||
| _error = 360 + _absoluteError; | ||
| }; | ||
| if (_absoluteError > 180) then { | ||
| _error = _absoluteError - 360; | ||
| }; | ||
| _error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #include "script_component.hpp" | ||
| /* ---------------------------------------------------------------------------- | ||
| Function: CBA_pid_fnc_error_linear | ||
|
|
||
| Description: | ||
| Linear error function. | ||
|
|
||
| Parameters: | ||
| _observed - the observed value <NUMBER> | ||
| _setpoint - the setpoint <NUMBER> | ||
|
|
||
| Returns: | ||
| Error of `setpoint - observed` <NUMBER> | ||
|
|
||
| Examples: | ||
| (begin example) | ||
| private _error = [2, 5] call CBA_pid_fnc_error_linear; | ||
| // _error == 3 | ||
| (end) | ||
|
|
||
| Author: | ||
| tcvm | ||
| ---------------------------------------------------------------------------- */ | ||
| SCRIPT(error_linear); | ||
| params [ | ||
| ["_observed", 0, [0]], | ||
| ["_setpoint", 0, [0]] | ||
| ]; | ||
|
|
||
| _setpoint - _observed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #include "script_component.hpp" | ||
| /* ---------------------------------------------------------------------------- | ||
| Function: CBA_pid_fnc_reset | ||
|
|
||
| Description: | ||
| Reset a PID controller's history. | ||
|
|
||
| Parameters: | ||
| _pid - the controller <LOCATION> | ||
|
|
||
| Returns: | ||
| Nothing | ||
|
|
||
| Examples: | ||
| (begin example) | ||
| _pid call CBA_pid_fnc_reset; | ||
| (end) | ||
|
|
||
| Author: | ||
| tcvm | ||
| ---------------------------------------------------------------------------- */ | ||
| SCRIPT(reset); | ||
| params [ | ||
| ["_pid", locationNull, [locationNull]] | ||
| ]; | ||
|
|
||
| if (isNull _pid) exitWith {}; | ||
|
|
||
| _pid setVariable [QGVAR(history), []]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #include "script_component.hpp" | ||
| /* ---------------------------------------------------------------------------- | ||
| Function: CBA_pid_fnc_setGains | ||
|
|
||
| Description: | ||
| Set new gains for a PID controller. | ||
|
|
||
| Parameters: | ||
| _pid - a pid controller <LOCATION> | ||
| _pGain - the new proportional gain <NUMBER><NIL> (Default: `nil`) | ||
| OR | ||
| `nil`, to keep the current gain | ||
| _iGain - the new integral gain <NUMBER><NIL> (Default: `nil`) | ||
| OR | ||
| `nil`, to keep the current gain | ||
| _dGain - the new derivative gain <NUMBER><NIL> (Default: `nil`) | ||
| OR | ||
| `nil`, to keep the current gain | ||
| Returns: | ||
| Nothing | ||
|
|
||
| Examples: | ||
| (begin example) | ||
| [_pid, 5, nil, 0.2] call CBA_pid_fnc_setGains; | ||
| (end) | ||
|
|
||
| Author: | ||
| tcvm | ||
| ---------------------------------------------------------------------------- */ | ||
| SCRIPT(setGains); | ||
| params [ | ||
| ["_pid", locationNull, [locationNull]], | ||
| ["_pGain", nil, [0, nil]], | ||
| ["_iGain", nil, [0, nil]], | ||
| ["_dGain", nil, [0, nil]] | ||
| ]; | ||
|
|
||
| if (isNull _pid) exitWith {}; | ||
|
|
||
| (_pid getVariable [QGVAR(gains), [0, 0, 0]]) params ["_currentPGain", "_currentIGain", "_currentDGain"]; | ||
| if !(isNil "_pGain") then { | ||
| _currentPGain = _pGain; | ||
| }; | ||
| if !(isNil "_iGain") then { | ||
| _currentIGain = _iGain; | ||
| }; | ||
| if !(isNil "_dGain") then { | ||
| _currentDGain = _dGain; | ||
| }; | ||
| _pid setVariable [QGVAR(gains), [_currentPGain, _currentIGain, _currentDGain]]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #include "script_component.hpp" | ||
| /* ---------------------------------------------------------------------------- | ||
| Function: CBA_pid_fnc_setpoint | ||
|
|
||
| Description: | ||
| Set a new setpoint for a PID controller. | ||
|
|
||
| Parameters: | ||
| _pid - a pid controller <LOCATION> | ||
| _setpoint - the new target setpoint for the controller <NUMBER> | ||
| _reset - if we want to reset the PID for the new setpoint <BOOL> (Default: true) | ||
|
|
||
| Returns: | ||
| Nothing | ||
|
|
||
| Examples: | ||
| (begin example) | ||
| [_pid, 42] call CBA_pid_fnc_setpoint; | ||
| (end) | ||
|
|
||
| Author: | ||
| tcvm | ||
| ---------------------------------------------------------------------------- */ | ||
| SCRIPT(setpoint); | ||
| params [ | ||
| ["_pid", locationNull, [locationNull]], | ||
| ["_setpoint", 0, [0]], | ||
| ["_reset", true, [true]] | ||
| ]; | ||
|
|
||
| if (isNull _pid) exitWith {}; | ||
|
|
||
| _pid setVariable [QGVAR(setpoint), _setpoint]; | ||
| if (_reset) then { | ||
| _pid setVariable [QGVAR(history), []]; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.