private const Kp:Number = 0.033;
private const Ki:Number = 0.0165;
private const Kd:Number = 0.0165;
/**
* Returns a new control variable based on the setPoint and current processVariable
* using a PID Controller.
* @param setPoint - The target point for the process. (i.e. 80% milk 20% cereal)
* @param processVariable - The current input to control the process. (current spoonful's ratio)
* @returns The new input to the process. (next spoonful's ratio)
*/
var integral:Number = 0;
var lastError:Number = 0;
private function PIDControl(setPoint:Number, processVariable:Number):Number
{
var error:Number = setPoint - processVariable;
var manipulatedVariable:Number = 0; // Time Step is 1 frame.
integral += error;
var derivative:Number = error - lastError;
manipulatedVariable = Kp*error + Ki*integral + Kd*derivative;
lastError = error;
return manipulatedVariable;
}The reason I'm posting about this is because I'm using PID control in my Asteroids remake to guide seeking missiles to enemy targets. Try the demo below to see it in action.
The Process Variable in this application is the distance from the estimated nearest approach location (the empty green circles) to the predicted place where where the asteroid and missile will collide (the filled red circle). The Manipulated Variable is the change in heading (i.e. how much to turn left or right) of the missile. The regular method first finds the asteroid which requires the smallest deviation from its current path. It then turns the missile to this asteroid (n.b. both missiles have turn angle constraint).
![]() |
| Visualization of PID Control (from a single trial) |
The 'Smart' PID controlled method is much more effective compared to 'Regular' method and also makes for interesting trajectories!


http://abstrusegoose.com/307
ReplyDelete:P