ScummVM API documentation
PidController.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22/*
23 * Copyright (C) 2006-2010 - Frictional Games
24 *
25 * This file is part of HPL1 Engine.
26 */
27
28#ifndef HPL_PID_CONTROLLER_H
29#define HPL_PID_CONTROLLER_H
30
31#include "hpl1/algorithms.h"
32#include "hpl1/engine/math/MathTypes.h"
33
34namespace hpl {
35
36template<class T>
38public:
39 float p, i, d;
41 // Constructors
44 Reset();
45 }
46 cPidController(float afP, float afI, float afD, int alErrorNum) {
47 p = afP;
48 i = afI;
49 d = afD;
50 SetErrorNum(alErrorNum);
51 Reset();
52 }
53
55 // Public
57
58 //------------------------------------
59 T Output(T aError, float afTimeStep) {
60 mvErrors[mlErrorNum] = aError;
61 mvTimeSteps[mlErrorNum] = afTimeStep;
62
63 integral = 0;
64 size_t lCount = mvErrors.size();
65 for (size_t error = 0; error < lCount; ++error) {
66 integral += mvErrors[error] * mvTimeSteps[error];
67 }
68
69 derivative = 0.0f;
70 if (mlLastNum >= 0) {
71 derivative = (mvErrors[mlErrorNum] - mvErrors[mlLastNum]) / afTimeStep;
72 }
73
74 mlLastNum = mlErrorNum;
75 mlErrorNum++;
76 if (mlErrorNum >= (int)mvErrors.size())
77 mlErrorNum = 0;
78
79 return mvErrors[mlLastNum] * p + integral * i + derivative * d;
80 }
81
82 //------------------------------------
83
84 void SetErrorNum(int alErrorNum) {
85 mvErrors.resize(alErrorNum);
86 Hpl1::resizeAndFill(mvTimeSteps, alErrorNum, 0.f);
87 }
88
89 //------------------------------------
90
91 void Reset() {
92 mlErrorNum = 0;
93 mlLastNum = -1;
94 Common::fill(mvTimeSteps.begin(), mvTimeSteps.end(), 0);
95
96 integral = 0;
97 derivative = 0;
98 }
99
100 //------------------------------------
101
102 T GetLastError() {
103 if (mlLastNum >= 0)
104 return mvErrors[mlLastNum];
105 return 0;
106 }
107
108 T GetLastDerivative() {
109 return derivative;
110 }
111
112 T GetLastIntegral() {
113 return integral;
114 }
115
116 //------------------------------------
117
118private:
119 Common::Array<T> mvErrors;
120 Common::Array<float> mvTimeSteps;
121
122 T integral, derivative;
123
124 int mlErrorNum;
125 int mlLastNum;
126};
127
128//---------------------------------
129
132
133} // namespace hpl
134
135#endif // HPL_PID_CONTROLLER_H
Definition: array.h:52
iterator end()
Definition: array.h:380
iterator begin()
Definition: array.h:375
Definition: PidController.h:37
signed char * fill(signed char *first, signed char *last, Value val)
Definition: algorithm.h:168
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1