MPC for reference trajectory tracking

1,361 views
Skip to first unread message

gargso...@gmail.com

unread,
Nov 2, 2018, 10:42:34 AM11/2/18
to CasADi
Hi guys,

I'm new to casadi. I have been using Acado but since it does not support linear cost functions and due to other constraints, I have to shift. I have a reference trajectory stored in a vector. I want to minimize the error between my robot position and the reference position and implement an mpc loop for it in matlab. Can anyone direct me to an example of similar type, it would be a good start. 

Thanks,
Akash

Francis

unread,
Nov 2, 2018, 6:50:01 PM11/2/18
to CasADi
Hi,

Have a look at the example below.

gargso...@gmail.com

unread,
Nov 6, 2018, 5:35:31 AM11/6/18
to CasADi
Hey Francis,

Thanks for the link!
But I am not implementing mpc using simulink. I wonder how would the function calling occur if I eliminate simulink. Also I couldn't understand the way to implement the idea of mpc to loop over the time horizon with 1 step shifted prediction horizon.

Thanks,

Francis

unread,
Nov 6, 2018, 2:28:22 PM11/6/18
to CasADi
Hi Akash,

MPCTools might be of interest to you. It is an interface to CasADi, and it makes it very easy to define an MPC problem. The links to the tool are as follows.

Matlab interface:

Python interface:

Regarding your second question, I am not sure what did you mean by "loop over the time horizon with 1 step shifted prediction horizon".

Francis

gargso...@gmail.com

unread,
Nov 9, 2018, 6:23:15 AM11/9/18
to CasADi
Hi Francis,

Thanks for introducing rawlings-group/octave-mpctools. 
The library is amazing and nicely integrates casadi functionality. I found an exact reference to my sub problem in the examples section of the library.
I have one little question, in timevaryingmpc.m the mpc is solved for entire simulation horizon where as in onenorm.m they actually considers prediction horizon and solves the mpc in a loop (over simulation horizon). Do you think the former truly implements concept of mpc? 

Thanks a lot!

Francis

unread,
Nov 9, 2018, 8:38:07 AM11/9/18
to CasADi
Hi Akash,

I did not study the timevaryingmpc.m example very carefully. If I understand correctly, that example plots the solution within the entire horizon instead of extracting the first set of components from the solution sequences.

However, you can always solve that problem repeatedly in a time period larger than the horizon (Nt=250 in that example), and only extract the first set of components from the solution sequences, just as in other examples.

Francis

gargso...@gmail.com

unread,
Nov 9, 2018, 9:58:37 AM11/9/18
to CasADi
Exactly Francis, I am trying that thing!
for t = 1:Nsim
    controller.fixvar('x', 1, x(:,t));
    par = struct('xsp', [Xunit_2(t:t+Nt-1); Yunit_2(t:t+Nt-1)], 'Q', Q); -----------Line 1
    controller.solve();
    fprintf('Step %d: %s\n', t, controller.status);
    
    u(:,t) = controller.var.u(:,1);
    x(:,t+1) = controller.var.x(:,2);
    
    controller.saveguess();
end

Since I want to track a trajectory in x and y, x in code is a initialised as 
x = NaN(Nx, Nsim + 1);  where Nx = 2
So within the loop, I keep updating the reference (Xunit_2 and Yunit)2 matrices). But the output trajectory produced is different from reference trajctory.
Can you look into the loop thing?

Thanks a lot!!!! 

Francis

unread,
Nov 11, 2018, 1:38:58 PM11/11/18
to CasADi
Hi Akash,

Given that limited information is available, I am not sure what went wrong. But answering the following questions may be helpful.

1. Are the references correctly passed to the controller? When you change the references, will the result also change?
2. Are the parameters well tuned?

Francis

gargso...@gmail.com

unread,
Nov 12, 2018, 4:40:13 AM11/12/18
to CasADi
Hey Francis,

Great point!

I observed that references are not updated even though I explicitly update them in for loop. controller.par.xsp shows initial values only. I think that might be the major problem as of now. One way is to define controller in each iteration of the for loop but, that may not be the optimal solution (I tried that as well [below], in that case output tracks references in x direction well but in y direction the references are not tracked!!!). I have provided the code in attachment

Defining controller each time in for loop:
for t = 1:Nsim
    par = struct('xsp', [Xunit_2(t:t+Nt-1); Yunit_2(t:t+Nt-1)]);
    controller = mpc.nmpc('f', f, 'l', lcasadi, 'N', N, 'lb', lb, 'ub', ub, ...
                     'par', par);
    controller.solve();
    fprintf('Step %d: %s\n', t, controller.status);
    
    u(:,t) = controller.var.u(:,1);
    x(:,t+1) = controller.var.x(:,2);
    
    controller.saveguess();
end

BTW, thanks to great suggestions you have been offering!

Regards,
Akash
mainFile.m
ReferenceTraj.m

Francis

unread,
Nov 19, 2018, 5:42:49 PM11/19/18
to CasADi
Hi Akash,

The reason the reference did not get updated is that the par was not updated within the controller. Once the controller is built, the par is fixed unless you explicitly pass the par into the controller every time before the controller tries to find the solution. Therefore, the reference was not updated although the par itself was updated.

If you try rebuilding the controller below the par line within the for loop, the reference will be updated. However, the reference is not tracked well. This may be related to other parts of the code. Also, this is not an efficient way to update the reference at all. It is just given to show that the reference was not updated in the previous code. In the document, there is another way of updating the reference, i.e. by changing controller.par.xsp.

Best regards,
Francis
Reply all
Reply to author
Forward
0 new messages