A sparse MPC solver for walking motion generation.
ip_matrix_E.cpp
Go to the documentation of this file.
1 
9 /****************************************
10  * INCLUDES
11  ****************************************/
12 
13 #include "ip_matrix_E.h"
14 
15 
16 
17 /****************************************
18  * FUNCTIONS
19  ****************************************/
20 
21 namespace IP
22 {
30  void matrix_E::form_Ex (const problem_parameters& ppar, const double *x, double *result)
31  {
32  const double *control = &x[ppar.N*SMPC_NUM_STATE_VAR];
33  // a pointer to 6 current elements of result
34  double *res = result;
35 
36 
37  state_parameters stp = ppar.spar[0];
38 
39  // a pointer to 6 current state variables
40  const double *xc = x;
41 
42  // result = -R * x + B * u
43  res[0] = -(stp.cos * xc[0] - stp.sin * xc[3]) + stp.B[0] * control[0];
44  res[1] = -xc[1] + stp.B[1] * control[0];
45  res[2] = -xc[2] + stp.B[2] * control[0];
46  res[3] = -(stp.sin * xc[0] + stp.cos * xc[3]) + stp.B[0] * control[1];
47  res[4] = -xc[4] + stp.B[1] * control[1];
48  res[5] = -xc[5] + stp.B[2] * control[1];
49 
50 
51  for (int i = 1; i < ppar.N; i++)
52  {
53  stp = ppar.spar[i];
54 
55  const double cosA = ppar.spar[i-1].cos;
56  const double sinA = ppar.spar[i-1].sin;
57 
58  // next control variables
59  control = &control[SMPC_NUM_CONTROL_VAR];
60  res = &res[SMPC_NUM_STATE_VAR];
61 
62  // result = A*R*x - R * x + B * u
63  res[0] = cosA * xc[0] + stp.A3 * xc[1] + stp.A6 * xc[2] - sinA * xc[3] - (stp.cos * xc[6] - stp.sin * xc[9]) + stp.B[0] * control[0];
64  res[1] = xc[1] + stp.A3 * xc[2] - xc[7] + stp.B[1] * control[0];
65  res[2] = xc[2] - xc[8] + stp.B[2] * control[0];
66  res[3] = cosA * xc[3] + stp.A3 * xc[4] + stp.A6 * xc[5] + sinA * xc[0] - (stp.sin * xc[6] + stp.cos * xc[9]) + stp.B[0] * control[1];
67  res[4] = xc[4] + stp.A3 * xc[5] - xc[10] + stp.B[1] * control[1];
68  res[5] = xc[5] - xc[11] + stp.B[2] * control[1];
69 
70  // a pointer to 6 current state variables
71  xc = &x[i*SMPC_NUM_STATE_VAR];
72  }
73  }
74 
75 
83  void matrix_E::form_ETx (const problem_parameters& ppar, const double *x, double *result)
84  {
85  int i;
86  state_parameters stp;
87 
88  double *res = result;
89  double *control_res = &result[ppar.N*SMPC_NUM_STATE_VAR];
90  const double *xc;
91 
92 
93  for (i = 0; i < ppar.N-1; i++)
94  {
95  stp = ppar.spar[i];
96  const double A3 = ppar.spar[i+1].A3;
97  const double A6 = ppar.spar[i+1].A6;
98 
99 
100  // a pointer to 6 current elements of nu
101  xc = &x[i*SMPC_NUM_STATE_VAR];
102 
103 
104  // result = -R' * nu + R' * A' * x
105  res[0] = -(stp.cos * xc[0] + stp.sin * xc[3]) + stp.cos * xc[6] + stp.sin * xc[9];
106  res[1] = -xc[1] + A3 * xc[6] + xc[7];
107  res[2] = -xc[2] + A6 * xc[6] + A3 * xc[7] + xc[8];
108  res[3] = -(- stp.sin * xc[0] + stp.cos * xc[3]) - stp.sin * xc[6] + stp.cos * xc[9];
109  res[4] = -xc[4] + A3 * xc[9] + xc[10];
110  res[5] = -xc[5] + A6 * xc[9] + A3 * xc[10] + xc[11];
111 
112 
113  // result = B' * x
114  control_res[0] = stp.B[0] * xc[0] + stp.B[1] * xc[1] + stp.B[2] * xc[2];
115  control_res[1] = stp.B[0] * xc[3] + stp.B[1] * xc[4] + stp.B[2] * xc[5];
116 
117 
118  // a pointer to 6 current elements of result
119  res = &res[SMPC_NUM_STATE_VAR];
120  control_res = &control_res[SMPC_NUM_CONTROL_VAR];
121  }
122 
123 
124  // no multiplication by A on the last iteration
125  stp = ppar.spar[i];
126 
127  // a pointer to 6 current elements of result
128  // a pointer to 6 current elements of nu
129  xc = &x[i*SMPC_NUM_STATE_VAR];
130 
131  // result = -R' * nu
132  res[0] = -(stp.cos * xc[0] + stp.sin * xc[3]);
133  res[1] = -xc[1];
134  res[2] = -xc[2];
135  res[3] = -(- stp.sin * xc[0] + stp.cos * xc[3]);
136  res[4] = -xc[4];
137  res[5] = -xc[5];
138 
139  // result = B' * x
140  control_res[0] = stp.B[0] * xc[0] + stp.B[1] * xc[1] + stp.B[2] * xc[2];
141  control_res[1] = stp.B[0] * xc[3] + stp.B[1] * xc[4] + stp.B[2] * xc[5];
142  }
143 }
#define SMPC_NUM_STATE_VAR
Number of state variables.
Definition: smpc_solver.h:24
A set of problem parameters.
state_parameters * spar
#define SMPC_NUM_CONTROL_VAR
Number of control variables.
Definition: smpc_solver.h:26
void form_Ex(const problem_parameters &, const double *, double *)
Forms E*x.
Definition: ip_matrix_E.cpp:30
void form_ETx(const problem_parameters &, const double *, double *)
Forms E' * x.
Definition: ip_matrix_E.cpp:83