Hi,
I am workign in a project, righ now I am here.
With the camera I work out the position of the object (15 times per second) and with that value I move the servo trying to get the center point of the camera. I am doing this with this code:
void loop()
{
if(camera.color_detection(RED)) // True if an object is found, then the position is update in camera.pos_x/y
{
// Define the limits which we are going to consider as a valid centered positions "The frame is 160x120"
int upLimit = 65;
int downLimit = 55;
int rightLimit = 85;
int leftLimit = 75;
// Real the position of the Servos (degrees)
int lowerPos = camera.lowerServo.read();
int upperPos = camera.upperServo.read();
// If the position of the object is outside our limits we calculate our new position of the servos (Degrees)
// Based on the distance from the object to the center
if(camera.object.pos_x > rightLimit)
lowerPos -= (camera.object.pos_x - rightLimit)/gear;
else if(camera.object.pos_x < leftLimit)
lowerPos += (leftLimit - camera.object.pos_x)/gear;
// Same for upper servo
if(camera.object.pos_y > upLimit)
upperPos += (camera.object.pos_y - upLimit)/gear;
else if(camera.object.pos_y < downLimit)
upperPos -= (downLimit - camera.object.pos_y)/gear;
// Update the position of the Servos
camera.lowerServo.write(lowerPos);
camera.upperServo.write(upperPos);
// Serial prints for Debug
Serial.print("Object position: ");
Serial.print(camera.object.pos_x);
Serial.print(", ");
Serial.println(camera.object.pos_y);
Serial.print("Distance: ");
Serial.println(camera.object.distance);
Serial.print("Movement: ");
Serial.println(lowerPos);
}
}
the function camera.color_detection(RED) updates camera.object.pos_x/y with the position of the object.
The camera is working in QQVGA resolution (160,120) "Desired postition 80,60"
And gear is a constant I am using in order to change the speed of the response.
Now I would like to move to a PID controller or whatever what could improve the response. A have read several examples of PID but I don't know how to implement the code in my case. How to translate the diference from the desired point to degrees.
Thanks in advance.