Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. #ifndef PID_AUTOTUNE_H
  2. #define PID_AUTOTUNE_H
  3. #define AUTOTUNE_LIBRARY_VERSION 0.0.2
  4.  
  5. #include <inttypes.h>
  6. #include <stdbool.h>
  7. #include <avr/pgmspace.h>
  8.  
  9. // verbose debug option
  10. // requires open Serial port
  11. #undef AUTOTUNE_DEBUG
  12.  
  13. #undef USE_SIMULATION
  14.  
  15. // defining this option implements relay bias
  16. // this is useful to adjust the relay output values
  17. // during the auto tuning to recover symmetric
  18. // oscillations
  19. // this can compensate for load disturbance
  20. // and equivalent signals arising from nonlinear
  21. // or non-stationary processes
  22. // any improvement in the tunings seems quite modest
  23. // but sometimes unbalanced oscillations can be
  24. // persuaded to converge where they might not
  25. // otherwise have done so
  26. #undef AUTOTUNE_RELAY_BIAS
  27.  
  28. // average amplitude of successive peaks must differ by no more than this proportion
  29. // relative to half the difference between maximum and minimum of last 2 cycles
  30. #define AUTOTUNE_PEAK_AMPLITUDE_TOLERANCE 0.05
  31.  
  32. // ratio of up/down relay step duration should differ by no more than this tolerance
  33. // biasing the relay con give more accurate estimates of the tuning parameters but
  34. // setting the tolerance too low will prolong the autotune procedure unnecessarily
  35. // this parameter also sets the minimum bias in the relay as a proportion of its amplitude
  36. #define AUTOTUNE_STEP_ASYMMETRY_TOLERANCE 0.20
  37.  
  38. // auto tune terminates if waiting too long between peaks or relay steps
  39. // set larger value for processes with long delays or time constants
  40. #define AUTOTUNE_MAX_WAIT_MINUTES 5
  41.  
  42. // Ziegler-Nichols type auto tune rules
  43. // in tabular form
  44. struct Tuning
  45. {
  46.   uint8_t divisor[3];
  47.   /*
  48.   bool PI_controller()
  49.   {
  50.     return pgm_read_uint8_t_near(&_divisor[2]) == 0;
  51.   }
  52.  
  53.   float divisor(uint8_t index)  
  54.   {
  55.     return (float)pgm_read_uint8_t_near(&_divisor[index]) * 0.05;
  56.   }
  57.   */
  58. };
  59.  
  60.  
  61.  
  62.   // constants ***********************************************************************************
  63.  
  64.   // auto tune method
  65.   enum PID_AutoTune_Methods
  66.   {
  67.     ZIEGLER_NICHOLS_PI = 0,
  68.     ZIEGLER_NICHOLS_PID = 1,
  69.     TYREUS_LUYBEN_PI,
  70.     TYREUS_LUYBEN_PID,
  71.     CIANCONE_MARLIN_PI,
  72.     CIANCONE_MARLIN_PID,
  73.     AMIGOF_PI,
  74.     PESSEN_INTEGRAL_PID,
  75.     SOME_OVERSHOOT_PID,
  76.     NO_OVERSHOOT_PID
  77.   };
  78.  
  79.   // peak type
  80.   enum Peak
  81.   {
  82.     MINIMUM = -1,
  83.     NOT_A_PEAK = 0,
  84.     MAXIMUM = 1
  85.   };
  86.  
  87.   // auto tuner state
  88.   enum AutoTunerState
  89.   {
  90.     AUTOTUNER_OFF = 0,
  91.     STEADY_STATE_AT_BASELINE = 1,
  92.     STEADY_STATE_AFTER_STEP_UP = 2,
  93.     RELAY_STEP_UP = 4,
  94.     RELAY_STEP_DOWN = 8,
  95.     CONVERGED = 16,
  96.     FAILED = 128
  97.   };
  98.  
  99.   // tuning rule divisor
  100.   enum PID_Constant_Devisors
  101.   {
  102.     KP_DIVISOR = 0,
  103.     TI_DIVISOR = 1,
  104.     TD_DIVISOR = 2
  105.   };
  106.  
  107.  
  108.  
  109.  
  110.   // commonly used methods ***********************************************************************
  111.   void PID_ATune_Init(float* Input, float* Output);          // * Constructor.  links the Autotune to a given PID
  112.   bool PID_ATune_Runtime(void);                       // * Similar to the PID Compute function,
  113.                                         //   returns true when done, otherwise returns false
  114.   void PID_ATune_Cancel(void);                        // * Stops the AutoTune
  115.  
  116.   void PID_ATune_SetOutputStep(float);           // * how far above and below the starting value will
  117.                                         //   the output step?  
  118.   float PID_ATune_GetOutputStep(void);               //
  119.  
  120.   void PID_ATune_SetControlType(uint8_t);            // * Determines tuning algorithm
  121.   uint8_t PID_ATune_GetControlType(void);                // * Returns tuning algorithm
  122.  
  123.   void PID_ATune_SetLookbackSec(uint16_t);             // * how far back are we looking to identify peaks
  124.   int PID_ATune_GetLookbackSec(void);                 //
  125.  
  126.   void PID_ATune_SetNoiseBand(float);            // * the autotune will ignore signal chatter smaller
  127.                                         //   than this value
  128.   float PID_ATune_GetNoiseBand(void);                //   this should be accurately set
  129.  
  130.   float PID_ATune_GetKp(void);                       // * once autotune is complete, these functions contain the
  131.   float PID_ATune_GetKi(void);                       //   computed tuning parameters.  
  132.   float PID_ATune_GetKd(void);                       //
  133.  
  134. #endif
  135.