I’m programming a quadrotor helicopter drone to chase a moving ground target automatically using image processing. The hardware I’m working with is pretty limited, so in order to make my automatic commands account for the drone’s velocity (it doesn’t know when it’s moving) I have to manually track my target’s relative position over time (which can be roughly translated into the drone’s movement).
So here’s what I have in that regard:
int lastX = Targets_Last_Position_Xcoord();
int lastY = Targets_Last_Position_Ycoord();
int nowX = Targets_Current_Position_Xcoord();
int nowY = Targets_Current_Position_Ycoord();
int speedModX = (float)(60 - (abs(lastX)-abs(nowX))) / 60.0f; // Image dimension is 120
int speedModY = (float)(73 - (abs(lastY)-abs(nowY))) / 73.0f; // Other dimension is 146
changePitch(((nowX - 60)/60.0f)*(1 + speedModX));
changeRoll(((nowY - 73)/73.0f)*(1 + speedModY));
The function “changePitch” and the others like it cause the next command sent to the drone to include a Pitch change of the specified percentage of the preset maximum tilt. So, I’m telling the drone to tilt an amount directly related to the target’s distance from the center of the screen; then I’m throwing in a multiplier based on where the target moved (the distance between the target’s last spot and its current spot).
This is my intention anyways; this code only seems to be helping a little bit, and other factors such as air flow and mechanical imbalance (it isn’t the most high-quality equipment out there) may be interfering, leaving me to simply see what I want to see.
Is the method I’m using to account for the drone’s velocity correct, and/or is there a better way to handle this?
EDIT: calculation of speedModX and speedModY was changed, previous form had unwanted results under some circumstances. New formula correctly creates a modifier based on the difference between the target’s previous distance from the origin and the target’s current distance from the origin.
If you can’t figure out a better way of modeling the system, you may want to look into tuning a proportional–integral–derivative (PID) controller for this. They’re a general solution used in automation for dealing with feedback loops.
Per the Wikipedia article: