PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/firmware/src/shared/PID.hh

http://github.com/makerbot/G3Firmware
C++ Header | 99 lines | 31 code | 19 blank | 49 comment | 0 complexity | 94c042afe309eff3f7498ad5e6b83637 MD5 | raw file
 1/*
 2 * Copyright 2010 by Adam Mayer	 <adam@makerbot.com>
 3 *
 4 * This program is free software: you can redistribute it and/or modify
 5 * it under the terms of the GNU General Public License as published by
 6 * the Free Software Foundation, either version 3 of the License, or
 7 * (at your option) any later version.
 8 *
 9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>
16 *
17 * This simplified PID controller was written with reference to:
18 * * The Heater.h implementation (lookup credits)
19 * * Brett Beauregard's Arduino PID implementation
20 *  Created on: Feb 19, 2010
21 *      Author: phooky
22 */
23
24#ifndef PID_HH_
25#define PID_HH_
26
27#include <stdint.h>
28
29/// Number of delta samples to
30#define DELTA_SAMPLES 4
31
32/// The PID controller module implements a simple PID controller.
33/// \ingroup SoftwareLibraries
34class PID {
35private:
36    float p_gain; ///< proportional gain
37    float i_gain; ///< integral gain
38    float d_gain; ///< derivative gain
39
40    /// Data for approximating d (smoothing to handle discrete nature of sampling).
41    /// See PID.cc for a description of why we do this.
42    int16_t delta_history[DELTA_SAMPLES];
43    float delta_summation;      ///< ?
44    uint8_t delta_idx;          ///< Current index in the delta history buffer
45    int prev_error;             ///< Previous input for calculating next delta
46    int error_acc;              ///< Accumulated error, for calculating integral
47
48    int sp;                     ///< Process set point
49    int last_output;            ///< Last output of the PID controller
50
51public:
52    /// Initialize the PID module
53    PID();
54
55    /// Set the P term of the PID controller
56    /// \param[in] p_gain_in New proportional gain term
57    void setPGain(const float p_gain_in) { p_gain = p_gain_in; }
58
59    /// Set the I term of the PID controller
60    /// \param[in] i_gain_in New integration gain term
61    void setIGain(const float i_gain_in) { i_gain = i_gain_in; }
62
63    /// Set the D term of the PID controller
64    /// \param[in] d_gain_in New derivative gain term
65    void setDGain(const float d_gain_in) { d_gain = d_gain_in; }
66
67    /// Set the setpoint of the PID controller
68    /// \param[in] target New PID controller target
69    void setTarget(const int target);
70
71    /// Get the current PID target
72    /// \return Current setpoint
73    const int getTarget() const { return sp; }
74
75    /// Reset the PID to board-on values
76    void reset();
77
78    /// Reset only the PID control loop variables
79    void reset_state();
80
81    /// Calculate the next cycle of the PID loop.
82    /// \param[in] pv Process value (measured value from the sensor)
83    /// \return output value (used to control the output)
84    int calculate(int pv);
85
86    /// Get the current value of the error term
87    /// \return Error term
88    int getErrorTerm();
89
90    /// Get the current value of the delta term
91    /// \return Delta term
92    int getDeltaTerm();
93
94    /// Get the last process output value
95    /// \return Last process output value
96    int getLastOutput();
97};
98
99#endif /* PID_HH_ */