-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinK_zv.c
313 lines (251 loc) · 7.73 KB
/
linK_zv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#define S_FUNCTION_NAME linK_zv
/*
* linK_zv.c
*
* run linearized system
* for descent direction (or neighboring extremal)
* with 'optimal' feedback Ko
* (determined by P, etc.)
* for
*
* dynamics.c system
*
* with
*
* cost.c incremental cost
*
*
* John Hauser
* Mar 04
* ...
* Dec 11
*
* states
* z(NS) linearized state
* Dg(xi) . zeta lin approx to cost
* D^2g(xi) . (zeta, zeta) quad approx to cost
*
* inputs
* xi(NS+NI) alf(NS) mu(NI) - trajectory for linearization
* Ko(NS*NI) optimal feedback
* vo(NI) optimal feedforward input
* q(NS) costate traj (for 2nd order approx)
* wt(NW) exogenous system input w(t)
* wlt(NWL) exogenous cost input w_c(t) (e.g., x_des, u_des)
*
* outputs
* v(NI) linearized control
*
* parameters
* none
*/
/* prontoTK spec:
dynamics(x,u,wt,ders, dx,y, fxu_x_,fxu_u_, q,q_fxu_x_x_,q_fxu_x_u_,q_fxu_u_u_);
ders: dx(1),y(2), A(4),B(8), Q(16), S(32), R(64)
cost(x,u,wlt,ders, lxu, lxu_x_,lxu_u_, lxu_x_x_,lxu_x_u_,lxu_u_u_);
ders: lxu(1), a(2),b(4), Q(8), S(16), R(32)
*/
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include <math.h>
// system description: dynamics with derivatives
#include "sys_sizes.h"
#include "dynamics.c"
// cost description, including derivatives
#include "cost.c"
// "sys_sizes.h" defines
// NS - number of system states
// NI - number of system inputs
// NO - number of system outputs
// NW - number of exogenous inputs for dynamics
// NWL - number of exogenous inputs for cost
#define NS2 (NS*NS)
#define NN12 ((NS*(NS+1))/2)
#define N_STATES (NS+1+1)
#define N_INPUTS (NS+NI + NI*NS + NI + NS + NW + NWL)
#define N_OUTPUTS (NI)
#define N_PARAMS (0)
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, N_PARAMS); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual parameters */
return;
}
ssSetNumContStates(S, N_STATES);
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, N_INPUTS);
ssSetInputPortDirectFeedThrough(S, 0, 1);
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, N_OUTPUTS);
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
ssSetOptions(S, 0);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
/* set initial conditions with set options */
#if defined(MDL_INITIALIZE_CONDITIONS)
#undef MDL_INITIALIZE_CONDITIONS
#endif
/*
* most matrices are stored COLUMNwise
* THE exception is the feedback gain matrix K
* which is stored ROWwise
*
* in each case, we set up macros to allow matlab-like indexing
* [ (1..n) vs (0..n-1) ]
*/
static void opt_v(double *v, double *z, double *u)
{
#define Ko(i,j) Ko_[ ((i)-1)*NS + ((j)-1) ] // rowwise
// pointers to 'input structure'
double *alf = u, *mu = alf+NS,
*Ko_ = mu+NI, *vo = Ko_+(NI*NS), *q = vo+NI,
*wt = q+NS, *wlt = wt+NW;
int i, j, k;
/* v = -Ko * z + vo */
for (i=1; i<=NI; i++) {
v[i-1] = vo[i-1];
for (j=1; j<=NS; j++) {
v[i-1] -= Ko(i,j)*z[j-1];
}
}
#undef Ko
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T *y = ssGetOutputPortRealSignal(S,0);
real_T *x = ssGetContStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
// double *param0 = mxGetPr(ssGetSFcnParam(S,0));
double u[N_INPUTS];
int i;
double *v=y, *z=x;
/* copy inputs to u[] to ensure contiguity ... and easy access*/
for (i=0; i<N_INPUTS; i++)
u[i] = *uPtrs[i];
/* output the control u and feedback Kux */
opt_v(v, z, u);
}
/* no model update */
#if defined(MDL_UPDATE)
#undef MDL_UPDATE
#endif
#define MDL_DERIVATIVES
static void mdlDerivatives(SimStruct *S)
{
real_T *dx = ssGetdX(S);
real_T *x = ssGetContStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
// double *params = mxGetPr(ssGetSFcnParam(S,0));
double u[N_INPUTS];
int i, j, k;
// pointers to 'input structure'
double *alf = u, *mu = alf+NS,
*Ko_ = mu+NI, *vo = Ko_+(NI*NS), *q = vo+NI,
*wt = q+NS, *wlt = wt+NW;
double *z = x;
double A_[NS2], B_[NS*NI], v[NI];
double a[NS], b[NI];
double Dl_zv, QRzv2;
double lxu_x_x_[NS*NS], lxu_x_u_[NS*NI], lxu_u_u_[NI*NI];
double q_fxu_x_x_[NS*NS], q_fxu_x_u_[NS*NI], q_fxu_u_u_[NI*NI];
/* index A & P with 1:NS (NOT 0:NS-1!) */
#define A(i,j) A_[((i)-1) + NS*((j)-1)]
#define B(i,j) B_[ ((i)-1) + ((j)-1)*(NS) ] // columnwise
#define lxu_x_x(i,j) lxu_x_x_[((i)-1) + NS*((j)-1)]
#define lxu_x_u(i,j) lxu_x_u_[((i)-1) + NS*((j)-1)]
#define lxu_u_u(i,j) lxu_u_u_[((i)-1) + NI*((j)-1)]
#define q_fxu_x_x(i,j) q_fxu_x_x_[((i)-1) + NS*((j)-1)]
#define q_fxu_x_u(i,j) q_fxu_x_u_[((i)-1) + NS*((j)-1)]
#define q_fxu_u_u(i,j) q_fxu_u_u_[((i)-1) + NI*((j)-1)]
/* copy inputs to u[] to ensure contiguity ... and easy access*/
for (i=0; i<N_INPUTS; i++)
u[i] = *uPtrs[i];
/* prontoTK spec:
dynamics(x,u,wt,ders, dx,y, fxu_x_,fxu_u_, q,q_fxu_x_x_,q_fxu_x_u_,q_fxu_u_u_);
ders: dx(1),y(2), A(4),B(8), Q(16), S(32), R(64)
cost(x,u,wlt,ders, lxu, lxu_x_,lxu_u_, lxu_x_x_,lxu_x_u_,lxu_u_u_);
ders: lxu(1), a(2),b(4), Q(8), S(16), R(32)
*/
// get derivs of vector field and cost about (alf(.),mu(.))
// need A B a b Q S R
// a(2), b(4), Q(8), S(16), R(32)
cost( alf,mu,wlt,
2+4+8+16+32,NULL, a, b, lxu_x_x_, lxu_x_u_, lxu_u_u_);
// A(4), B(8), Q(16), S(32), R(64)
dynamics(alf,mu,wt,
4+8+16+32+64,NULL,NULL, A_,B_, q, q_fxu_x_x_,q_fxu_x_u_,q_fxu_u_u_);
/* zdot = A z + B v */
opt_v(v, z, u);
for (i=1; i<= NS; i++) {
dx[i-1] = 0.0;
for (j=1; j<=NS; j++) {
dx[i-1] += A(i,j)*z[j-1];
}
}
for (i=1; i<= NS; i++) {
for (j=1; j<=NI; j++) {
dx[i-1] += B(i,j)*v[j-1];
}
}
/* Dg(xi) . zeta */
Dl_zv = 0.0;
for (i=1; i<=NS; i++) {
Dl_zv += a[i-1]*z[i-1];
}
for (i=1; i<=NI; i++) {
Dl_zv += b[i-1]*v[i-1];
}
dx[NS] = Dl_zv;
/* D^2g(xi) . (zeta, zeta) */
QRzv2 = 0.0;
for (i=1; i<= NS; i++){
for (j=1; j<= NS; j++){
QRzv2 += z[i-1]*(lxu_x_x(i,j)+q_fxu_x_x(i,j))*z[j-1];
}
}
for (i=1;i<=NS;i++) {
for (j=1;j<=NI;j++) {
QRzv2 += 2.0*z[i-1]*(lxu_x_u(i,j)+q_fxu_x_u(i,j))*v[j-1];
}
}
for (i=1; i<=NI; i++) {
for (j=1; j<=NI; j++){
QRzv2 += v[i-1]*(lxu_u_u(i,j)+q_fxu_u_u(i,j))*v[j-1];
}
}
dx[NS+1] = QRzv2;
#undef A
#undef B
#undef q_fxu_x_x
#undef q_fxu_x_u
#undef q_fxu_u_u
#undef lxu_x_x
#undef lxu_x_u
#undef lxu_u_u
}
/*
* mdlTerminate - called when the simulation is terminated.
*
* In this function, you should perform any actions that are necessary
* at the termination of a simulation. For example, if memory was allocated
* in mdlInitializeConditions, this is the place to free it.
*/
static void mdlTerminate(SimStruct *S)
{
}
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif