blob: e7a4ad18f95df2eb3ced7c24e1ad4d592bb42792 [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001/**
2 * Copyright 2017 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
James Feist22c257a2018-08-31 14:07:12 -070017#include "pidcontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070018
19#include "ec/pid.hpp"
20
Patrick Ventured8012182018-03-08 08:21:38 -080021#include <algorithm>
22#include <chrono>
James Feist572c43d2019-01-31 15:52:22 -080023#include <cmath>
Patrick Ventured8012182018-03-08 08:21:38 -080024#include <iostream>
25#include <map>
26#include <memory>
27#include <thread>
28#include <vector>
29
Patrick Venturea0764872020-08-08 07:48:43 -070030namespace pid_control
31{
32
James Feist22c257a2018-08-31 14:07:12 -070033void PIDController::process(void)
Patrick Ventured8012182018-03-08 08:21:38 -080034{
Patrick Venture5f59c0f2018-11-11 12:55:14 -080035 double input;
36 double setpt;
37 double output;
Patrick Ventured8012182018-03-08 08:21:38 -080038
39 // Get setpt value
Patrick Venture563a3562018-10-30 09:31:26 -070040 setpt = setptProc();
Patrick Ventured8012182018-03-08 08:21:38 -080041
42 // Get input value
Patrick Venture563a3562018-10-30 09:31:26 -070043 input = inputProc();
Patrick Ventured8012182018-03-08 08:21:38 -080044
James Feist572c43d2019-01-31 15:52:22 -080045 auto info = getPIDInfo();
46
47 // if no hysteresis, maintain previous behavior
48 if (info->positiveHysteresis == 0 && info->negativeHysteresis == 0)
49 {
50 // Calculate new output
51 output = ec::pid(info, input, setpt);
52
53 // this variable isn't actually used in this context, but we're setting
54 // it here incase somebody uses it later it's the correct value
55 lastInput = input;
56 }
57 else
58 {
59 // initialize if not set yet
60 if (std::isnan(lastInput))
61 {
62 lastInput = input;
63 }
64
65 // if reading is outside of hysteresis bounds, use it for reading,
66 // otherwise use last reading without updating it first
67 else if ((input - lastInput) > info->positiveHysteresis)
68 {
69 lastInput = input;
70 }
71 else if ((lastInput - input) > info->negativeHysteresis)
72 {
73 lastInput = input;
74 }
75
76 output = ec::pid(info, lastInput, setpt);
77 }
Patrick Ventured8012182018-03-08 08:21:38 -080078
79 // Output new value
Patrick Venture563a3562018-10-30 09:31:26 -070080 outputProc(output);
Patrick Ventured8012182018-03-08 08:21:38 -080081
82 return;
83}
Patrick Venturea0764872020-08-08 07:48:43 -070084
85} // namespace pid_control