-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPID.h
62 lines (52 loc) · 1.95 KB
/
PID.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* 2011 Eberhard Rensch, Pleasant Software
*
* Based on parts of the Makerbot firmware
* Copyright 2010 by Adam Mayer <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
* This simplified PID controller was written with reference to:
* * The Heater.h implementation (lookup credits)
* * Brett Beauregard's Arduino PID implementation
* Created on: Feb 19, 2010
* Author: phooky
*/
#ifndef PID_HH_
#define PID_HH_
#include <stdint.h>
#define DELTA_SAMPLES 4
/// This simplified PID controller makes several assumptions:
/// * The output range is limited to 0-255.
class PID {
private:
// Data for approximating d (smoothing to handle discrete nature of sampling).
// See PID.cc for a description of why we do this.
int16_t delta_history[DELTA_SAMPLES];
float delta_summation;
uint8_t delta_idx;
int prev_error; // previous input for calculating next delta
int error_acc; // accumulated error, for calculating integral
int sp; // set point
public:
PID() { reset(); }
void setTarget(const int target) { sp = target; }
const int getTarget() const { return sp; }
/// Reset the PID to board-on values
void reset();
/// PV is the process value; that is, the measured value
/// Returns the new value of the manipulated value; that is, the output
int calculate(int pv);
};
#endif /* PID_HH_ */