The PID function is used in mainly
control applications. PIDCalc performs one iteration of the PID
algorithm.
While the PID function works, main is just a dummy program showing
a typical usage.
=====================================================================================================*/
typedef struct PID {
int SetPoint; // ???? Desired Value
int Proportion; // ???? Proportional Const
int Integral; // ???? Integral Const
int Derivative; // ???? Derivative Const
int LastError; // Error[-1]
int PrevError; // Error[-2]
int SumError; // Sums of Errors
/*====================================================================================================
Main Program
=====================================================================================================*/
/*#define N 12
int sensor (void) // Dummy Sensor Function
{
char count,i,j;
int ad_data[N];
int sum=0;
int advalue=0;
int temp=0;
for (count = 0; count<=N; count++)
{
ad_data[count]=AI(1);
}
for (j=0;j<N-1;j++)
{
for (i=0;i<N-1;i++)
{
if (ad_data[i]>ad_data[i+1])
{
temp=ad_data[i];
ad_data[i]=ad_data[i+1];
ad_data[i+1]=temp;
}
}
}
for (count=0;count<N;count++)
{
sum=ad_data[count];
}
advalue=sum/(N-2);
return advalue;
int speed=0;
void actuator(int rDelta) // Dummy Actuator Function
{
speed=rDelta;
if(flag==1)
{
if (speed>100)
{
speed=100;
}
else if(speed<-100)
{
speed=-100;
}
}
else if(flag==2)
{
speed=rDelta/3;
if (speed>50)
{
speed=50;
}
else if(speed<-50)
{
speed=-50;
}
else if (speed<10 && speed>0)//電機死區
{
speed=8;
}
else if (speed>-10 && speed<0) //電機死區
{
speed=-8;
}
}
Run(speed,speed);
}
#define N 12
int sensor1(void)//均值濾波。最簡單的一種
{
int buffer[N];
int count=0;
int sum=0;
int ad=0;
for (count=0;count<N;count++)
{
buffer[count]=AI(1);
wait(0.001);
}
for (count=0;count<N;count++)
{
sum+=buffer[count];
}
ad=sum/N;
return ad;
}
void main(void)
{
PID setPID; // PID Control Structure
int OutPut; // PID Response (Output)
int InPut; // PID Feedback (Input)