MCPcopy Create free account
hub / github.com/TinyMPC/TinyMPC / codegen_data_source

Function codegen_data_source

src/tinympc/codegen.cpp:170–382  ·  view source on GitHub ↗

Create src/tiny_data.cpp file

Source from the content-addressed store, hash-verified

168
169// Create src/tiny_data.cpp file
170int codegen_data_source(TinySolver* solver, const char* output_dir, int verbose) {
171 char data_cpp_fname[PATH_LENGTH];
172 FILE *data_cpp_f;
173
174 int nx = solver->work->nx;
175 int nu = solver->work->nu;
176 int N = solver->work->N;
177
178 sprintf(data_cpp_fname, "%s/src/tiny_data.cpp", output_dir);
179
180 // Open data source file
181 data_cpp_f = fopen(data_cpp_fname, "w+");
182 if (data_cpp_f == NULL)
183 ERROR_MSG(EXIT_FAILURE, "Failed to open file %s", data_cpp_fname);
184
185 // Preamble
186 time_t start_time;
187 time(&start_time);
188 fprintf(data_cpp_f, "/*\n");
189 fprintf(data_cpp_f, " * This file was autogenerated by TinyMPC on %s", ctime(&start_time));
190 fprintf(data_cpp_f, " */\n\n");
191
192 // Open extern C
193 fprintf(data_cpp_f, "#include \"tinympc/tiny_data.hpp\"\n\n");
194 fprintf(data_cpp_f, "#ifdef __cplusplus\n");
195 fprintf(data_cpp_f, "extern \"C\" {\n");
196 fprintf(data_cpp_f, "#endif\n\n");
197
198 // Solution
199 fprintf(data_cpp_f, "/* Solution */\n");
200 fprintf(data_cpp_f, "TinySolution solution = {\n");
201
202 fprintf(data_cpp_f, "\t%d,\t\t// iter\n", solver->solution->iter);
203 fprintf(data_cpp_f, "\t%d,\t\t// solved\n", solver->solution->solved);
204 fprintf(data_cpp_f, "\t(tinyMatrix(%d, %d) << ", nx, N);
205 print_matrix(data_cpp_f, MatrixXd::Zero(nx, N), nx * N);
206 fprintf(data_cpp_f, ").finished(),\t// x\n"); // x solution
207 fprintf(data_cpp_f, "\t(tinyMatrix(%d, %d) << ", nu, N-1);
208 print_matrix(data_cpp_f, MatrixXd::Zero(nu, N-1), nu * (N-1));
209 fprintf(data_cpp_f, ").finished(),\t// x\n"); // u solution
210
211 fprintf(data_cpp_f, "};\n\n");
212
213 // Cache
214 fprintf(data_cpp_f, "/* Matrices that must be recomputed with changes in time step, rho */\n");
215 fprintf(data_cpp_f, "TinyCache cache = {\n");
216
217 fprintf(data_cpp_f, "\t(tinytype)%.16f,\t// rho (step size/penalty)\n", solver->cache->rho);
218 fprintf(data_cpp_f, "\t(tinyMatrix(%d, %d) << ", nu, nx);
219 print_matrix(data_cpp_f, solver->cache->Kinf, nu * nx);
220 fprintf(data_cpp_f, ").finished(),\t// Kinf\n"); // Kinf
221 fprintf(data_cpp_f, "\t(tinyMatrix(%d, %d) << ", nx, nx);
222 print_matrix(data_cpp_f, solver->cache->Pinf, nx * nx);
223 fprintf(data_cpp_f, ").finished(),\t// Pinf\n"); // Pinf
224 fprintf(data_cpp_f, "\t(tinyMatrix(%d, %d) << ", nu, nu);
225 print_matrix(data_cpp_f, solver->cache->Quu_inv, nu * nu);
226 fprintf(data_cpp_f, ").finished(),\t// Quu_inv\n"); // Quu_inv
227 fprintf(data_cpp_f, "\t(tinyMatrix(%d, %d) << ", nx, nx);

Callers 1

tiny_codegenFunction · 0.85

Calls 1

print_matrixFunction · 0.85

Tested by

no test coverage detected