- #1
btb4198
- 572
- 10
I have a laser and I am adjusting it's position using a motor, and I am reading the laser light intensity at the top and bottom using a sensor.
then I divide the top from the bottom and the goal is to get around 0.97%. This is called delta, if delta is too high, you move the motor lower and if delta is too lower we move it up.
now I coded a PID, the program I have my command is in motor steps(position) but my feed back is in light intensity percent.
so how do I set up my PID ? can I even use a PID this way ?
here is my code:
then I divide the top from the bottom and the goal is to get around 0.97%. This is called delta, if delta is too high, you move the motor lower and if delta is too lower we move it up.
now I coded a PID, the program I have my command is in motor steps(position) but my feed back is in light intensity percent.
so how do I set up my PID ? can I even use a PID this way ?
here is my code:
Code:
GetValue()
{
PID_controller PID = new PID_controller(2, 1, 0);
****
stepsize = (int)PID.UpdatePID(Convert.ToDouble(stepsize), delta);
stepsize = changeStepSize( stepsize);
***
}
public PID_controller(int Proportional, int Integral, int differential)
{
this.Proportional = Proportional;
this.Integral = Integral;
this.differential = differential;
Istate = 0D;
error_counter = 0;
lastposition = 0;
} double getPTerm(double command , double position)
{
double error = command - position;
Istate += error;
error_counter++;
return error;
}
double getITerm()
{
double Integralvalue = Istate / error_counter;
return Integralvalue;
}
double getDTerm(double position)
{
lastposition = position;
double differential = lastposition - position;
return differential;
}
public double UpdatePID( double command, double position)
{
double error = getPTerm(command, position);
double perror = getITerm();
double derror = getDTerm(position);
command = (Proportional * error) + (Integral * perror) + (differential * derror);
return command;
}