**Introduction**

This example uses the concepts described in the section 'HOW TO: Time-Varying MPC' and 'HOW TO: Non-quadratic Penalties'.

The goal is to design a controller to perform a spacecraft rendezvous operation, where a controlled chaser spacecraft is performing rendezvous with a passive target that is orbiting around Mars.

Using a time-varying prediction model allows to perform spacecraft maneouvers in elliptical orbits and allows the controller to be updated when the are changes in the system parameters or control objectives. This example is based on the models described in the following paper and the references therein

**Model**

The Yamanaka-Ankersen (Y-A) equations are used to describe the dynamics, where the six states \(x\) of the system represent the relative position and velocity of the chaser with respect to the target in the three dimensions. These equations apply in elliptical orbits, but are time-varying in terms of the true anomaly, \( v \), of the target, i.e. the model is given by

\[ x_{k+1}= A(v)x_k+ B(v)u_k \]

and the requirement is that the state at the end of the horizon is at the target. The plant input is modeled as an impulsive change in velocity, such that

\[ B(v)= A(v) \begin{bmatrix} 0 \\ I_3 \end{bmatrix}\]

The file 'YA_A_matrix.m' computes the time-varying matrices for the prediction horizon given the anomaly \(v \).

**Constraints**

The three impulsive control inputs can give a maximum change in velocity of 5 meters per second along each axis. In addition, the chaser spacecraft is required to remain within a cone of vision of 20 degrees from the target and must not go behind the target to facilitate the docking maneuver.

**Objective**

The goal of the controller is to balance the following objectives:

- the chaser should be always as close as possible to the target,
- use as little fuel as possible to get there.

The second objective is more important, hence it is weighed higher. We consider two types of cost functions: one where all the terms are weighed using standard quadratic penalties; and one where the inputs are penalised using the 1-norm, which better reflects the propellant consumption being directly proportional to delivered thrust and also attempts to minimise the use of the actuators. In order to implement the 1-norm cost we need to add slack variables and additional constraints as described in 'HOW TO: Non-quadratic Penalties'.

The following code shows how to generate an MPC controller for the spacecraft rendezvous problem with a time-varying model and a 1-norm penalty on the actuators.

% FORCES Pro multistage form

% assume variable ordering zi = [ui; xi+1, eui] for i=1...N-1

% Parameters: First Eq. RHS

parameter(1) = newParam('minusA_times_x0’,1,'eq.c’);

stages = MultistageProblem(N);

for i = 1:N

% dimension

stages(i).dims.n = nx+2*nu; % number of stage variables

stages(i).dims.r = nx; % number of equality constraints

stages(i).dims.l = nu; % number of lower bounds

stages(i).dims.u = nu; % number of upper bounds

stages(i).dims.p = 3+2*nu; % number of polytopic constraints

% cost

stages(i).cost.H = blkdiag(zeros(nu),Q,zeros(nu));

stages(i).cost.f = [zeros(nu,1); -Q*xs; ones(nu,1)];

% lower bounds

stages(i).ineq.b.lbidx = 1:nu; % lower bound acts on these indices

stages(i).ineq.b.lb = umin*ones(4,1); % lower bound for the input signal

% upper bounds

stages(i).ineq.b.ubidx = 1:nu; % upper bound acts on these indices

stages(i).ineq.b.ub = umax*ones(4,1); % upper bound for the input signal

% polytopic bounds

stages(i).ineq.p.A = [ zeros(3,nu), Hx, zeros(3,nu); ...

R, zeros(nu,nx), -eye(nu); ...

-R, zeros(nu,nx), -eye(nu)];

stages(i).ineq.p.b = [ hx; R*us; -R*us ];

% equality constraints

if( i < N )

params(end+1) = newParam(['C_',num2str(i)],i,'eq.C');

end

params(end+1) = newParam(['D_',num2str(i)],i,'eq.D');

if( i > 1 )

params(end+1) = newParam(['pre’,num2str(i+1),’_w’],i+1,'eq.c’);

end

end

**Spacecraft Rendezvous Manoeuvers with and without 1-Norm Cost**

The simulation describes a rendezvous maneover were the chaser starts 15km away from the target spacecraft and the goal is to approach the target to within 1000 meter distance, while respecting the constraints, to start the docking maneuver.

The target is modeled as being in a Keplerian orbit around Mars with an orbital radius of 3,600,000 meters. The controller sampling time is 200s but the target and chaser dynamics are simulated in intervals of 1s for illustration purposes.

The following plot illustrates the behaviour of the controlled spacecraft with standard quadratic cost.

The next plot shows the behaviour of the controller when the quadratic cost on the actuators is swapped with a 1-norm penalty. Notice the sparsity in the actuation commands - the thrusters are only engaged when necessary to keep the spacecraft within the cone of visibility of the target.