Skip to content
Merged
Show file tree
Hide file tree
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 Nov 9, 2025
7dbff0a
fix wrong defaults
TheCandianVendingMachine Nov 9, 2025
1052f1e
fix typo
TheCandianVendingMachine Nov 9, 2025
d257431
update docs
TheCandianVendingMachine Nov 9, 2025
96e6754
update header
TheCandianVendingMachine Nov 9, 2025
f0301d0
update headers
TheCandianVendingMachine Nov 9, 2025
37cbd87
fix trailing commas
TheCandianVendingMachine Nov 9, 2025
88e4601
fix stringtable
TheCandianVendingMachine Nov 9, 2025
f6ee938
fix somethings not updated
TheCandianVendingMachine Nov 9, 2025
8bb49b3
typo
TheCandianVendingMachine Nov 9, 2025
31c7ddf
math variable -> better name
TheCandianVendingMachine Nov 9, 2025
beae46f
fix large numbers
TheCandianVendingMachine Nov 9, 2025
c7b497e
add defaults as macros
TheCandianVendingMachine Nov 9, 2025
092da72
dont pollute history with same-x values
TheCandianVendingMachine Nov 10, 2025
232f5ed
better derivative approximation
TheCandianVendingMachine Nov 10, 2025
676852d
better representation of what we want
TheCandianVendingMachine Nov 16, 2025
5c17794
Update addons/pid/fnc_setGains.sqf
TheCandianVendingMachine Dec 4, 2025
470c096
fix bug on first integral sum
TheCandianVendingMachine Dec 4, 2025
b2a41a1
Merge branch 'pid' of github.com:TheCandianVendingMachine/CBA_A3 into…
TheCandianVendingMachine Dec 4, 2025
541aa07
fix incorrect derivative calculation
TheCandianVendingMachine Dec 4, 2025
a44c4ae
switch to better integration method
TheCandianVendingMachine Dec 4, 2025
f73a35d
Add optional setting to function
TheCandianVendingMachine Dec 4, 2025
31f1a59
remove trailing comma
TheCandianVendingMachine Dec 4, 2025
3e8ad7a
Update addons/pid/fnc_update.sqf
TheCandianVendingMachine Dec 4, 2025
a4818b0
update API to be a bit more ergonomic
TheCandianVendingMachine Dec 5, 2025
c6a0632
store error in history, not logged values
TheCandianVendingMachine Dec 6, 2025
b799856
update API again, the error function isn't touched much in practice
TheCandianVendingMachine Dec 6, 2025
35976eb
Update addons/pid/fnc_create.sqf
TheCandianVendingMachine Dec 6, 2025
8422ec5
doc update
TheCandianVendingMachine Dec 6, 2025
9434d17
Apply suggestions from code review
TheCandianVendingMachine Dec 6, 2025
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
1 change: 1 addition & 0 deletions addons/pid/$PBOPREFIX$
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x\cba\addons\pid
11 changes: 11 additions & 0 deletions addons/pid/CfgEventHandlers.hpp
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));
};
};
7 changes: 7 additions & 0 deletions addons/pid/XEH_PREP.hpp
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);
7 changes: 7 additions & 0 deletions addons/pid/XEH_preInit.sqf
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;
3 changes: 3 additions & 0 deletions addons/pid/XEH_preStart.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "script_component.hpp"

#include "XEH_PREP.hpp"
17 changes: 17 additions & 0 deletions addons/pid/config.cpp
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"
57 changes: 57 additions & 0 deletions addons/pid/fnc_create.sqf
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
39 changes: 39 additions & 0 deletions addons/pid/fnc_error_degree.sqf
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
30 changes: 30 additions & 0 deletions addons/pid/fnc_error_linear.sqf
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
29 changes: 29 additions & 0 deletions addons/pid/fnc_reset.sqf
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), []];
50 changes: 50 additions & 0 deletions addons/pid/fnc_setGains.sqf
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]];
36 changes: 36 additions & 0 deletions addons/pid/fnc_setpoint.sqf
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), []];
};
Loading
Loading