/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. #ifndef PID_HH_
  24. #define PID_HH_
  25. #include <stdint.h>
  26. /// Number of delta samples to
  27. #define DELTA_SAMPLES 4
  28. /// The PID controller module implements a simple PID controller.
  29. /// \ingroup SoftwareLibraries
  30. class PID {
  31. private:
  32. float p_gain; ///< proportional gain
  33. float i_gain; ///< integral gain
  34. float d_gain; ///< derivative gain
  35. /// Data for approximating d (smoothing to handle discrete nature of sampling).
  36. /// See PID.cc for a description of why we do this.
  37. int16_t delta_history[DELTA_SAMPLES];
  38. float delta_summation; ///< ?
  39. uint8_t delta_idx; ///< Current index in the delta history buffer
  40. int prev_error; ///< Previous input for calculating next delta
  41. int error_acc; ///< Accumulated error, for calculating integral
  42. int sp; ///< Process set point
  43. int last_output; ///< Last output of the PID controller
  44. public:
  45. /// Initialize the PID module
  46. PID();
  47. /// Set the P term of the PID controller
  48. /// \param[in] p_gain_in New proportional gain term
  49. void setPGain(const float p_gain_in) { p_gain = p_gain_in; }
  50. /// Set the I term of the PID controller
  51. /// \param[in] i_gain_in New integration gain term
  52. void setIGain(const float i_gain_in) { i_gain = i_gain_in; }
  53. /// Set the D term of the PID controller
  54. /// \param[in] d_gain_in New derivative gain term
  55. void setDGain(const float d_gain_in) { d_gain = d_gain_in; }
  56. /// Set the setpoint of the PID controller
  57. /// \param[in] target New PID controller target
  58. void setTarget(const int target);
  59. /// Get the current PID target
  60. /// \return Current setpoint
  61. const int getTarget() const { return sp; }
  62. /// Reset the PID to board-on values
  63. void reset();
  64. /// Reset only the PID control loop variables
  65. void reset_state();
  66. /// Calculate the next cycle of the PID loop.
  67. /// \param[in] pv Process value (measured value from the sensor)
  68. /// \return output value (used to control the output)
  69. int calculate(int pv);
  70. /// Get the current value of the error term
  71. /// \return Error term
  72. int getErrorTerm();
  73. /// Get the current value of the delta term
  74. /// \return Delta term
  75. int getDeltaTerm();
  76. /// Get the last process output value
  77. /// \return Last process output value
  78. int getLastOutput();
  79. };
  80. #endif /* PID_HH_ */