# Algorithm for centering an object that is changing speed

I'm trying to use a pan / tilt alt-azimuth telescope mount to track an object that is moving in the sky. The problem is that the object is changing speed constantly. I can easily track objects that are moving at a steady speed.

I'm using the OpenCV library to compute the object's distance from the center of the image. I should be able to tell the telescope mount to speed up or slow down to follow the object. The actual algorithm though seems surprisingly hard to write.

For instance if I tell the mount to speed up to follow the object, then the apparent speed between the two is much less, but still not zero. So the object is slowly drifting out of the view. So I have to recenter it which is totally different than adjusting the speed. It's like I need a re-centering algorithm and a speed adjusting algorithm running at the same time.

Does anyone have any ideas how to do this or where to ask? Neither the OpenCV or telescope forums seems the right place to ask this question.

Thanks

It seems that your algorithm that controls the speed of your telescope does not make use of any integral part, thus you've been observing drifts while tracking.

Let $$e_x$$ be the error in the image plane between the x-coordinate $$o_x$$ of the object and the center $$c_x$$: $$e_x=o_x-c_x.$$ Then, the azimuth velocity $$\dot{a}$$ of your telescope should be: $$\dot{a}=K_p \cdot e_x + K_I\int e_x dt,$$ being $$K_P$$ and $$K_I$$ the two gains you have to tune up to obtain a nice tracking. In particular, the integral gain $$K_I$$ is responsible for compensating the drift so as for delivering non-null velocity when $$e_x=0$$.

There exist many methods to tune the gains. In your case, my suggestion is to start with $$K_I=0$$ and play with $$K_P$$ up to the point when you'll get small $$e_x$$ at steady-state and a residual drift. Only then, slowly increase $$K_I$$ until the small difference in velocities is brought to zero.

Analogous reasoning applies to the elevation velocity.

Thanks for the replies. I figured out to make it work. I divided the image into different regions using horizontal lines. Then, I have a timer in C# that every x milliseconds figures out which region the centroid of the object is in. (The centroid is obtained by finding the centroid of the OpenCV tracker bounding box). Then each timer cycle it checks to see if the object went from one region to another. So I have a set of rules. If the object goes from region 0 to region 1, then the motor speeds up. If it goes from region 1 to 0, no speed change happens. If it goes from 0 to -1 then the motor speed is decreased. This acts like a funnel guiding the object towards the middle of the image. I have 10 horizontal regions and 8 vertical regions for each movement axis. There are opposite rules depending on if the motor is moving left or right. I also have a boolean value to keep track of which way the motor is spinning. For example xMovingLeft, xMovingRight, yMovingUp, yMovingDown. There is a different, opposite set of region rules if the motor is turning a different direction. The only downside to doing it like this is that the object is constantly moving left/right up/down towards the center of the image. But it's not that bad and often times the speed is exactly the same so the object is moving at exactly the same speed as the mount.

• Maybe you haven't any knowledge in control theory, but what you discovered yourself could be done in a more principled way by resorting to a PID controller, as I tried to outline in my answer assuming you were aware of it, instead. It's good anyway that you solved the challenge and came up with your own solution. However, the working principle of a PID is fairly simple yet effective, so that it's certainly worth learning it for supporting your future applications. There're plenty of resources to look up online for this. – Ugo Pattacini Mar 17 at 11:10

The description of the problem fits great to a technique which is called model predictive control. It can solve the problem of tracking an object with a telescope on a mathematical satisfying way. The first thing to do is to create a prediction model. The next step is to program a controller which is utilizing the estimation for the control the telescope itself. But let us go into the details.

The object at the sky is generating a trajectory during time at a gantt chart. On timestep 0 the object is at place #1 while on timestep 1 it is on place #2 and so on. A so called forward model is able to predict the future position of the object. The model takes as input the current position (x,y) and the desired future timestep (for example 100) and returns the position of the object. Creating such a forward model can be done with different techniques. A simple formula may be a starting point, but neural networks can do the same job.

The forward model itself is useless, because it doesn't answer how to move the telescope. This is done by the second software module called predictive control. This algorithm takes as input the current position of the telescope and the predicted position of the object and will move the telescope to the next position. Realizing the predictive control can be done also with many ways. In it's easiest case it's a behavior tree but some advanced mathematical equations can do the same job.

What is important to realize is, the separation between prediction and control. Both problems have to be treated independent from each other. The first question is what is the future position of the object in the sky, and the second problem is how to move the servo motor in reaction to this prediction.