An easy to implement solution (naive algorithm)
The idea is to first get A to match B's velocity. Once it's done, both objects will be stationary with respect to the other or, if you take A as the origin of your frame, B will be stationary in your frame and it becomes a problem of perform a rendez-vous with a stationary target (accelerate towards it, then decelerate before meeting it).
An more optimized solution
You'll need to use a state-space representation.
Write your system into equations using Newton's second law for both objects, then write using state-space representation and plug a suitable controller onto it.
First, let's model the system.
Variables with A (resp. B) subscript are relative to object A (resp. B). For state-space representation, I'm using the same notation as in the Wikipedia linked page.
Our goal is a rendez-vous, which means bringing A at B's position and with zero relative velocity.
Using \$X = x_B - x_A\$, our target is \$X = 0\$ and \$\dot{X} = 0\$.
Using Newton's second law, we can write
- \$T_A = m_A.\ddot{x}_A\$
- \$\ddot{X} = \ddot{x}_B - 1/m_A.T_A\$
The state vector is equal to the output vector
\$y = x = \begin{pmatrix} X \\ \dot{X} \end{pmatrix} \$
Then the system equation is
\$\dot{x} = \begin{pmatrix} \dot{X} \\ \ddot{X} \end{pmatrix} = \begin{bmatrix} 0 & 0 \\ 1 & 0 \end{bmatrix} x + \begin{pmatrix} 0 \\ -1/m_A \end{pmatrix} T_A + \begin{pmatrix} 0 \\ 1 \end{pmatrix} \ddot{x}_B \$
Oops, it's not linear, it's affine because of object B's acceleration. Depending on what is expected for \$\ddot{x}_B\$ you have several options:
- Mainly zero, B will only use its thrusters sporadically: you can ignore \$\ddot{x}_B\$ in the system equation.
- Its value is low compared to the other terms in the equation: you can also ignore it, but it might cause a steady-state error with a linear controller.
- Its value is not negligeable but can be considered constant with respect to time: you can add a 'virtual' constant input to make it look like it's linear. The system equation becomes:
\$\dot{x} = \begin{pmatrix} \dot{X} \\ \ddot{X} \end{pmatrix} = \begin{bmatrix} 0 & 0 \\ 1 & 0 \end{bmatrix} x + \begin{pmatrix} 0 & 0 \\ -1/m_A & \ddot{x}_B \end{pmatrix} \begin{pmatrix} T_A \\ 1 \end{pmatrix}\$
- Its value is not negligeable and time-dependant: expect a steady-state error, or a divergence.
In the first 3 cases, a linear controller such as a PID could do the trick. In the last case, you either have to use a higher-order controller, or use a modified PID to account for the non-linearity (in that case, the controller will not be linear).