Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / programming / pid.c
blob3d80a5346752e35d8c143ed1ea58fcdb313bb4d7
1 /*
2 * This file is part of INAV Project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/.
8 * Alternatively, the contents of this file may be used under the terms
9 * of the GNU General Public License Version 3, as described below:
11 * This file is free software: you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation, either version 3 of the License, or (at your
14 * option) any later version.
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
25 #include "platform.h"
27 #ifdef USE_PROGRAMMING_FRAMEWORK
29 #include "common/utils.h"
30 #include "config/config_reset.h"
31 #include "config/parameter_group.h"
32 #include "config/parameter_group_ids.h"
33 #include "navigation/navigation_private.h"
35 #include "programming/pid.h"
36 #include "programming/logic_condition.h"
38 EXTENDED_FASTRAM programmingPidState_t programmingPidState[MAX_PROGRAMMING_PID_COUNT];
39 static bool pidsInitiated = false;
41 PG_REGISTER_ARRAY_WITH_RESET_FN(programmingPid_t, MAX_PROGRAMMING_PID_COUNT, programmingPids, PG_PROGRAMMING_PID, 2);
43 void pgResetFn_programmingPids(programmingPid_t *instance)
45 for (int i = 0; i < MAX_PROGRAMMING_PID_COUNT; i++) {
46 RESET_CONFIG(programmingPid_t, &instance[i],
47 .enabled = 0,
48 .setpoint = {
49 .type = LOGIC_CONDITION_OPERAND_TYPE_VALUE,
50 .value = 0
52 .measurement = {
53 .type = LOGIC_CONDITION_OPERAND_TYPE_VALUE,
54 .value = 0
56 .gains = {
57 .P = 0,
58 .I = 0,
59 .D = 0,
60 .FF = 0,
66 void programmingPidUpdateTask(timeUs_t currentTimeUs)
68 static timeUs_t previousUpdateTimeUs;
69 const float dT = US2S(currentTimeUs - previousUpdateTimeUs);
71 if (!pidsInitiated) {
72 programmingPidInit();
73 pidsInitiated = true;
76 for (uint8_t i = 0; i < MAX_PROGRAMMING_PID_COUNT; i++) {
77 if (programmingPids(i)->enabled) {
78 const int setpoint = logicConditionGetOperandValue(programmingPids(i)->setpoint.type, programmingPids(i)->setpoint.value);
79 const int measurement = logicConditionGetOperandValue(programmingPids(i)->measurement.type, programmingPids(i)->measurement.value);
81 programmingPidState[i].output = navPidApply2(
82 &programmingPidState[i].controller,
83 setpoint,
84 measurement,
85 dT,
86 -1000,
87 1000,
88 PID_LIMIT_INTEGRATOR
94 previousUpdateTimeUs = currentTimeUs;
97 void programmingPidInit(void)
99 for (uint8_t i = 0; i < MAX_PROGRAMMING_PID_COUNT; i++) {
100 navPidInit(
101 &programmingPidState[i].controller,
102 programmingPids(i)->gains.P / 1000.0f,
103 programmingPids(i)->gains.I / 1000.0f,
104 programmingPids(i)->gains.D / 1000.0f,
105 programmingPids(i)->gains.FF / 1000.0f,
106 5.0f,
107 0.0f
112 int32_t programmingPidGetOutput(uint8_t i) {
113 return programmingPidState[constrain(i, 0, MAX_PROGRAMMING_PID_COUNT)].output;
116 void programmingPidReset(void)
118 for (uint8_t i = 0; i < MAX_PROGRAMMING_PID_COUNT; i++) {
119 navPidReset(&programmingPidState[i].controller);
123 #endif