00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005
00006 namespace PlatformController {
00007 public static class PidControl {
00008
00009 static double _Kp = 1, _Ki = 1, _Kd = 1;
00010
00011 public static double Kp {
00012 get { return PidControl._Kp; }
00013 }
00014
00015 public static double Ki {
00016 get { return PidControl._Ki; }
00017 }
00018
00019 public static double Kd {
00020 get { return PidControl._Kd; }
00021 }
00022 static double _pComponent = 0, _iComponent = 0, _dComponent = 0;
00023
00024 public static double PComponent {
00025 get { return PidControl._pComponent; }
00026 }
00027
00028 public static double IComponent {
00029 get { return PidControl._iComponent; }
00030 }
00031
00032 public static double DComponent {
00033 get { return PidControl._dComponent; }
00034 }
00035
00036 static double manipulatedVariable = 0;
00037
00038 public static void setPidCoefficients(double Kp, double Ki, double Kd) {
00039 _Kp = Kp;
00040 _Ki = Ki;
00041 _Kd = Kd;
00042 }
00043
00044 public static double getMvFromInput(double setpoint, List<int> processData, double timespan) {
00045 _pComponent = 0;
00046 _iComponent = 0;
00047 _dComponent = 0;
00048
00049 _pComponent = setpoint - ((double)processData.Sum() / timespan);
00050
00051 foreach (int d in processData)
00052 _iComponent += setpoint - ((double)d * ((double)processData.Count / timespan));
00053
00054 for (int i = 0; i < processData.Count; ++i ) {
00055 if ((double)processData[i] * ((double)processData.Count / timespan) > setpoint) {
00056 _dComponent = (double)(processData[processData.Count - 1] - processData[0]) / timespan;
00057 _dComponent = -1.0 * _dComponent;
00058 break;
00059 }
00060 }
00061 manipulatedVariable = _pComponent * _Kp + (_iComponent * _Ki) + (_dComponent * _Kd);
00062 return manipulatedVariable;
00063 }
00064
00065 }
00066 }