Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 1 | /** |
| 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 Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 17 | #include "pidcontroller.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 18 | |
| 19 | #include "ec/pid.hpp" |
| 20 | |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <chrono> |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 23 | #include <cmath> |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 24 | #include <iostream> |
| 25 | #include <map> |
| 26 | #include <memory> |
| 27 | #include <thread> |
| 28 | #include <vector> |
| 29 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 30 | void PIDController::process(void) |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 31 | { |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 32 | double input; |
| 33 | double setpt; |
| 34 | double output; |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 35 | |
| 36 | // Get setpt value |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 37 | setpt = setptProc(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 38 | |
| 39 | // Get input value |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 40 | input = inputProc(); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 41 | |
James Feist | 572c43d | 2019-01-31 15:52:22 -0800 | [diff] [blame] | 42 | auto info = getPIDInfo(); |
| 43 | |
| 44 | // if no hysteresis, maintain previous behavior |
| 45 | if (info->positiveHysteresis == 0 && info->negativeHysteresis == 0) |
| 46 | { |
| 47 | // Calculate new output |
| 48 | output = ec::pid(info, input, setpt); |
| 49 | |
| 50 | // this variable isn't actually used in this context, but we're setting |
| 51 | // it here incase somebody uses it later it's the correct value |
| 52 | lastInput = input; |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | // initialize if not set yet |
| 57 | if (std::isnan(lastInput)) |
| 58 | { |
| 59 | lastInput = input; |
| 60 | } |
| 61 | |
| 62 | // if reading is outside of hysteresis bounds, use it for reading, |
| 63 | // otherwise use last reading without updating it first |
| 64 | else if ((input - lastInput) > info->positiveHysteresis) |
| 65 | { |
| 66 | lastInput = input; |
| 67 | } |
| 68 | else if ((lastInput - input) > info->negativeHysteresis) |
| 69 | { |
| 70 | lastInput = input; |
| 71 | } |
| 72 | |
| 73 | output = ec::pid(info, lastInput, setpt); |
| 74 | } |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 75 | |
| 76 | // Output new value |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 77 | outputProc(output); |
Patrick Venture | d801218 | 2018-03-08 08:21:38 -0800 | [diff] [blame] | 78 | |
| 79 | return; |
| 80 | } |