# Algorithm for orientation (pan/tilt) towards a moving point

### Main Question:

note: The code in this question is pseudo-code, I'm using Python, but my pseudo-code is a mix of Python and C++.

I have a camera on the top of my robot's "head" which has pan and tilt capabilities as follows:

move_head(int pan_degrees, int tilt_degrees);


For example, when I call move_head(30,-20) the robot's head will move 30 degrees to the right and 20 degrees down, from the current position.

Importantly: When a current movement command is underway, the next available command waits for it to complete before executing.

The camera is tracking a moving object (usually a person, but for this demo I'm just tracking a Red Ball for simplicity) and must tell the head which direction to move to keep the point as centered on the screen as possible. I get the object's current central point (centroid) as a pair of integers:

int x; //pan
int y; //tilt


I also have the center point as a pair of constant integers:

const int x_center;
const int y_center;


As you may have noticed, the function above does not have velocity controls in its parameter list and so the head moves only at pre-defined speeds (not slow, but I'm trying to remove jerky behavior). There is also a "full stop" function, but because of my desire to have no jerky behavior I'm hoping not to need to use it.

My approach so far has been to define thresholds across my input image, such as:

int xthreshold = 600; // pixels


When the point crosses a threshold, I would calculate the difference in position:

if(x > xthreshold):
int new_x = x - x_center; //new x
int new_y = y - y_center; //new y
move_head( new_x, new_y); //move to new position


However, this is an extremely primitive algorithm. I imagine I could use a statistical method, or the current velocity of the object at the time of measurement, to predict a slightly more accurate new point than simply using the current difference from the center.

My code base is large and complex and I'm not personally familiar with the many layers of classes involved in the definition of my move_head function. However, if anyone knows of an amazing algorithm that does what I'm talking about that uses additional parameters, I would really love to hear about it.

My guess is that a definition such as:

move_head(int pan_degrees, int pan_velocity, int tilt_degrees, int tilt_velocity);


or

move_head(int pan_velocity, int tilt_velocity);


or

move_head(int pan_acceleration, int tilt_acceleration);


may have a more intuitive implementation of an effective point tracking algorithm.

Additionally, would it be more useful to have my function update the desired destination mid-movement instead of blocking until movement is complete?

• Since you aren't familiar with your code base, could you link to where you got the code from? – NomadMaker Jun 23 '18 at 10:20