Advertisement
Guest User

PID Controller in C#

a guest
Jul 22nd, 2011
5,661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.89 KB | None | 0 0
  1. /*
  2.  
  3.   PID Controller
  4.   - supports bumpless transfer
  5.   - reset windup avoidance
  6.   - tuning change bump mitigation
  7.  
  8.  
  9. Copyright (C) 2011 by Greg Buehler
  10.  
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17.  
  18. The above copyright notice and this permission notice shall be included in
  19. all copies or substantial portions of the Software.
  20.  
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. THE SOFTWARE.
  28. */
  29.  
  30. using System;
  31.  
  32. namespace AbstergoPID
  33. {
  34.     public class PID
  35.     {
  36.         public enum OperationMode
  37.         {
  38.             Auto,
  39.             Manual
  40.         }
  41.  
  42.         public enum MovementDirection
  43.         {
  44.             Direct,
  45.             Reverse
  46.         }
  47.  
  48.         private long lastTime;
  49.         private float iTerm;
  50.         private float lastInput;
  51.  
  52.         public float Input { get; set; }
  53.         public float Output { get; set; }
  54.         public float Setpoint { get; set; }
  55.        
  56.         public float Kp { get; private set; }
  57.         public float Ki { get; private set; }
  58.         public float Kd { get; private set; }
  59.  
  60.         public int SampleTime { get; set; }
  61.         public float OutputMinimum { get; set; }
  62.         public float OutputMaximum { get; set; }
  63.  
  64.         public OperationMode Mode { get; set; }
  65.         public MovementDirection Direction { get; set; }
  66.  
  67.         public void Compute()
  68.         {
  69.             // early exit if in manual control
  70.             if (Mode == OperationMode.Manual) return;
  71.  
  72.             // early exit if called before sample interval
  73.             long now = DateTime.Now.Ticks;
  74.             long timeDelta = now - lastTime;
  75.             if (timeDelta < SampleTime) return;
  76.  
  77.             // computer working error
  78.             float error = Setpoint - Input;
  79.             iTerm += (Ki * error);
  80.  
  81.             // clamp intergral in output ranges
  82.             if (iTerm > OutputMaximum) iTerm = OutputMaximum;
  83.             if (iTerm < OutputMinimum) iTerm = OutputMinimum;
  84.  
  85.             // calculate input error factor
  86.             float iError = Input - lastInput;
  87.  
  88.             // calculate PID Output
  89.             Output = Kp * error + iTerm - Kd * iError;
  90.             if (Output > OutputMaximum) Output = OutputMaximum;
  91.             if (Output < OutputMinimum) Output = OutputMinimum;
  92.  
  93.             // replace interval variables for next iteration
  94.             lastInput = Input;
  95.             lastTime = now;
  96.         }
  97.  
  98.         public void SetTunings (float Kp, float Ki, float Kd)
  99.         {
  100.             // error check for settings less than 0
  101.             if (Kp < 0 || Ki < 0 || Kd < 0) return;
  102.  
  103.             // scale sample interval and apply to tuning
  104.             float SampleRate = (float)SampleTime / 1000;
  105.             this.Kp = Kp;
  106.             this.Ki = Ki * SampleRate;
  107.             this.Kd = Kd / SampleRate;
  108.  
  109.             // if direction is reversed, invert tuning
  110.             if (Direction == MovementDirection.Reverse)
  111.             {
  112.                 Kp = 0 - Kp;
  113.                 Ki = 0 - Ki;
  114.                 Kd = 0 - Kd;
  115.             }
  116.         }
  117.  
  118.         public void SetSampleTime(int NewSampleTime)
  119.         {
  120.             // new sample time must be valid
  121.             if (NewSampleTime > 0)
  122.             {
  123.                 // adjust scaling of intergral and derivative for new time
  124.                 float ratio = (float)NewSampleTime / (float)SampleTime;
  125.                 Ki *= ratio;
  126.                 Kd /= ratio;
  127.  
  128.                 this.SampleTime = NewSampleTime;
  129.             }
  130.         }
  131.  
  132.         public void SetOutputLimits(float Min, float Max)
  133.         {
  134.             // min must be less than max
  135.             if (Min > Max) return;
  136.             this.OutputMinimum = Min;
  137.             this.OutputMaximum = Max;
  138.  
  139.             // immediatly clamp the output value and intergral term
  140.             if (Output > OutputMaximum) Output = OutputMaximum;
  141.             if (Output < OutputMinimum) Output = OutputMinimum;
  142.  
  143.             if (iTerm > OutputMaximum) iTerm = OutputMaximum;
  144.             if (iTerm < OutputMinimum) iTerm = OutputMinimum;
  145.         }
  146.  
  147.         void SetMode(OperationMode Mode)
  148.         {
  149.             // if moving from manual to auto, reinitialize for bumpless transfer
  150.             bool movingToAuto = (Mode == OperationMode.Auto && this.Mode != Mode);
  151.             if (movingToAuto)
  152.             {
  153.                 Initialize();
  154.             }
  155.            
  156.             this.Mode = Mode;
  157.         }
  158.  
  159.         void SetDirection(MovementDirection Direction)
  160.         {
  161.             this.Direction = Direction;
  162.         }
  163.  
  164.         void Initialize()
  165.         {
  166.             lastInput = Input;
  167.             iTerm = Output;
  168.             if (iTerm > OutputMaximum) iTerm = OutputMaximum;
  169.             if (iTerm < OutputMinimum) iTerm = OutputMinimum;
  170.         }
  171.  
  172.         public PID(float Input, float Output, float Setpoint, float Kp, float Ki, float Kd, MovementDirection Direction)
  173.         {
  174.             this.Input = Input;
  175.             this.Output = Output;
  176.             this.Setpoint = Setpoint;
  177.             this.Kp = Kp;
  178.             this.Ki = Ki;
  179.             this.Kd = Kd;
  180.             this.Direction = Direction;
  181.  
  182.             Initialize();
  183.         }
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement