Hi,
I'm using the code generation tool for an MPC and it works fine when I use dynamically changeable weights as mentioned here.
My model is the following:
DifferentialState x1;
DifferentialState x2;
DifferentialState theta;
DifferentialState v1;
DifferentialState v2;
DifferentialState omega;
Control a1;
Control a2;
Control atheta;
// Model equations
DifferentialEquation f;
f << dot(x1) == v1 cos(theta) - v2 sin(theta);
f << dot(x2) == v1 sin(theta) + v2 cos(theta);
f << dot(theta) == omega;
f << dot(v1) == a1;
f << dot(v2) == a2;
f << dot(omega) == atheta;
// Reference function and weighting matrices
Function h, hN;
h << x1 << x2 << theta << v1 << v2 << omega;
hN << x1 << x2 << theta << v1 << v2 << omega;
BMatrix Q = eye<bool>(h.getDim());
BMatrix QN = eye<bool>(hN.getDim());
Unfortunately when I use mpc.set(CGUSEVARIABLEWEIGHTINGMATRIX, YES); and when I set the weights dynamically like that
*which results in a nearly identity matrix for every prediction step, all the predicted controls are set to zero, all the "velocities" are set to the off diagonal elements of the WN Matrix (0.05) and the "positions" are set to the same value in the first step and change according to the "predicted" velocities afterwards.
These are the only changes to the working version in which the weights are set dynamically but are the same for all the prediction steps.
Does anybody know what I'm doing wrong or could anybody show me a working version with mpc.set(CGUSEVARIABLEWEIGHTINGMATRIX, YES) and C++ as I've only found the example with Matlab.
Thank you so much in advance!*
Last edit: Max B 2016-08-16
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
/* * This file was auto-generated using the ACADO Toolkit. * * While ACADO Toolkit is free software released under the terms of * the GNU Lesser General Public License (LGPL), the generated code * as such remains the property of the user who used ACADO Toolkit * to generate this code. In particular, user dependent data of the code * do not inherit the GNU LGPL license. On the other hand, parts of the * generated code that are a direct copy of source code from the * ACADO Toolkit or the software tools it is based on, remain, as derived * work, automatically covered by the LGPL license. * * ACADO Toolkit is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * *//*IMPORTANT: This file should serve as a starting point to develop the usercode for the OCP solver. The code below is for illustration purposes. Mostlikely you will not get good results if you execute this code without anymodification(s).Please read the examples in order to understand how to write user code howto run the OCP solver. You can find more info on the website:www.acadotoolkit.org*/#include"acado_common.h"#include"acado_auxiliary_functions.h"#include<stdio.h>/* Some convenient definitions. */#defineNXACADO_NX/* Number of differential state variables. */#defineNXAACADO_NXA/* Number of algebraic variables. */#defineNUACADO_NU/* Number of control inputs. */#defineNODACADO_NOD/* Number of online data values. */#defineNYACADO_NY/* Number of measurements/references on nodes 0..N - 1. */#defineNYNACADO_NYN/* Number of measurements/references on node N. */#defineNACADO_N/* Number of intervals in the horizon. */#defineNUM_STEPS10/* Number of real-time iterations. */#defineVERBOSE1/* Show iterations: 1, silent: 0. *//* Global variables used by the solver. */ACADOvariablesacadoVariables;ACADOworkspaceacadoWorkspace;/* A template for testing of the solver. */intmain(){/* Some temporary variables. */inti,j,k,iter;acado_timert;/* Initialize the solver. */acado_initializeSolver();/* Initialize the states and controls. */for(i=0;i<NX*(N+1);++i)acadoVariables.x[i]=0.0;for(i=0;i<NU*N;++i)acadoVariables.u[i]=0.0;/* Initialize the measurements/reference. */for(i=0;i<NY*N;++i)acadoVariables.y[i]=0.0;for(i=0;i<NYN;++i)acadoVariables.yN[i]=0.0;/* MPC: initialize the current state feedback. */#ifACADO_INITIAL_STATE_FIXEDfor(i=0;i<NX;++i)acadoVariables.x0[i]=0.1;#endif/* Initialize Weights */for(i=0;i<N*NY;i++){for(j=0;j<NY;j++){if(i%NY==j){acadoVariables.W[i * NY + j]=1;}}}for(i=0;i<NYN;i++){acadoVariables.WN[i + NYN * i]=1;}if(VERBOSE)acado_printHeader();/* Prepare first step */acado_preparationStep();/* Get the time before start of the loop. */acado_tic(&t);/* The "real-time iterations" loop. */for(iter=0;iter<NUM_STEPS;++iter){/* Perform the feedback step. */acado_feedbackStep();/* Apply the new control immediately to the process, first NU components. */if(VERBOSE)printf("\tReal-Time Iteration %d: KKT Tolerance = %.3e\n\n",iter,acado_getKKT());/* Optional: shift the initialization (look at acado_common.h). *//* acado_shiftStates(2, 0, 0); *//* acado_shiftControls( 0 ); *//* Prepare for the next step. */acado_preparationStep();}/* Read the elapsed time. */real_tte=acado_toc(&t);if(VERBOSE)printf("\n\nEnd of the RTI loop. \n\n\n");/* Eye-candy. */if(!VERBOSE)printf("\n\n Average time of one real-time iteration: %.3g microseconds\n\n",1e6*te/NUM_STEPS);acado_printDifferentialVariables();acado_printControlVariables();return0;}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
**NEVERMIND, found my error, can be deleted!
**
Hi,
I'm using the code generation tool for an MPC and it works fine when I use dynamically changeable weights as mentioned here.
My model is the following:
Unfortunately when I use
mpc.set(CGUSEVARIABLEWEIGHTINGMATRIX, YES);and when I set the weights dynamically like that*which results in a nearly identity matrix for every prediction step, all the predicted controls are set to zero, all the "velocities" are set to the off diagonal elements of the WN Matrix (0.05) and the "positions" are set to the same value in the first step and change according to the "predicted" velocities afterwards.
These are the only changes to the working version in which the weights are set dynamically but are the same for all the prediction steps.
Does anybody know what I'm doing wrong or could anybody show me a working version with
mpc.set(CGUSEVARIABLEWEIGHTINGMATRIX, YES)and C++ as I've only found the example with Matlab.Thank you so much in advance!*
Last edit: Max B 2016-08-16
I modified the getting started example in a similar way as my code and it seems to work fine. So probably I have to search my good on my own :(
Generating Code
test.c