49 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR =
double>
50 class ADCodegenLinearizer :
public LinearSystem<STATE_DIM, CONTROL_DIM, SCALAR>
53 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
57 typedef CppAD::AD<CppAD::cg::CG<SCALAR>> ADCGScalar;
59 typedef ControlledSystem<STATE_DIM, CONTROL_DIM, ADCGScalar> system_t;
60 typedef DynamicsLinearizerADCG<STATE_DIM, CONTROL_DIM, ADCGScalar, ADCGScalar>
66 typedef typename Base::state_control_matrix_t state_control_matrix_t;
75 ADCodegenLinearizer(std::shared_ptr<system_t> nonlinearSystem,
bool cacheJac =
true)
76 : Base(nonlinearSystem->getType()),
77 dFdx_(state_matrix_t::Zero()),
78 dFdu_(state_control_matrix_t::Zero()),
80 nonlinearSystem_(nonlinearSystem),
81 linearizer_(std::bind(&system_t::computeControlledDynamics,
82 nonlinearSystem_.get(),
83 std::placeholders::_1,
84 std::placeholders::_2,
85 std::placeholders::_3,
86 std::placeholders::_4))
91 ADCodegenLinearizer(
const ADCodegenLinearizer<STATE_DIM, CONTROL_DIM>& arg)
92 : Base(arg.nonlinearSystem_->getType()),
95 cacheJac_(arg.cacheJac_),
96 nonlinearSystem_(arg.nonlinearSystem_->clone()),
97 linearizer_(arg.linearizer_)
102 ADCodegenLinearizer<STATE_DIM, CONTROL_DIM, SCALAR>* clone()
const override 104 return new ADCodegenLinearizer<STATE_DIM, CONTROL_DIM, SCALAR>(*this);
123 const state_matrix_t& getDerivativeState(
const state_vector_t& x,
124 const control_vector_t& u,
127 dFdx_ = linearizer_.getDerivativeState(x, u, t);
147 const state_control_matrix_t& getDerivativeControl(
const state_vector_t& x,
148 const control_vector_t& u,
151 dFdu_ = linearizer_.getDerivativeControl(x, u, t);
162 void compileJIT(
const std::string& libName =
"ADCodegenLinearizer") { linearizer_.compileJIT(libName); }
177 void generateCode(
const std::string& systemName,
178 const std::string& outputDir = ct::core::CODEGEN_OUTPUT_DIR,
179 const std::string& templateDir = ct::core::CODEGEN_TEMPLATE_DIR,
180 const std::string& ns1 =
"core",
181 const std::string& ns2 =
"generated",
182 bool useReverse =
false,
183 bool ignoreZero =
true)
185 std::string codeJacA, codeJacB;
186 linearizer_.generateCode(codeJacA, codeJacB, useReverse, ignoreZero);
189 templateDir, outputDir, systemName, ns1, ns2, codeJacA, codeJacB,
"AUTOGENERATED_CODE_PLACEHOLDER");
193 const linearizer_t& getLinearizer()
const {
return linearizer_; }
207 void writeCodeFile(
const std::string& templateDir,
208 const std::string& outputDir,
209 const std::string& systemName,
210 const std::string& ns1,
211 const std::string& ns2,
212 const std::string& codeJacA,
213 const std::string& codeJacB,
214 const std::string& codePlaceholder)
216 std::cout <<
"Generating linear system..." << std::endl;
218 size_t maxTempVarCountState, maxTempVarCountControl;
219 linearizer_.getMaxTempVarCount(maxTempVarCountState, maxTempVarCountControl);
221 std::string header = internal::CGHelpers::parseFile(templateDir +
"/LinearSystem.tpl.h");
222 std::string source = internal::CGHelpers::parseFile(templateDir +
"/LinearSystem.tpl.cpp");
224 const std::string scalarName(linearizer_.getOutScalarType());
226 replaceSizesAndNames(header, systemName, scalarName, ns1, ns2);
227 replaceSizesAndNames(source, systemName, scalarName, ns1, ns2);
229 internal::CGHelpers::replaceOnce(header,
"MAX_COUNT_STATE", std::to_string(maxTempVarCountState));
230 internal::CGHelpers::replaceOnce(header,
"MAX_COUNT_CONTROL", std::to_string(maxTempVarCountControl));
232 internal::CGHelpers::replaceOnce(source, codePlaceholder +
"_JAC_A", codeJacA);
233 internal::CGHelpers::replaceOnce(source, codePlaceholder +
"_JAC_B", codeJacB);
235 internal::CGHelpers::writeFile(outputDir +
"/" + systemName +
".h", header);
236 internal::CGHelpers::writeFile(outputDir +
"/" + systemName +
".cpp", source);
239 std::cout <<
"... Done! Successfully generated linear system" << std::endl;
250 void replaceSizesAndNames(std::string& file,
251 const std::string& systemName,
252 const std::string& scalarName,
253 const std::string& ns1,
254 const std::string& ns2)
256 internal::CGHelpers::replaceAll(file,
"LINEAR_SYSTEM_NAME", systemName);
257 internal::CGHelpers::replaceAll(file,
"NS1", ns1);
258 internal::CGHelpers::replaceAll(file,
"NS2", ns2);
259 internal::CGHelpers::replaceAll(file,
"STATE_DIM", std::to_string(STATE_DIM));
260 internal::CGHelpers::replaceAll(file,
"CONTROL_DIM", std::to_string(CONTROL_DIM));
261 internal::CGHelpers::replaceAll(file,
"SCALAR", scalarName);
264 state_matrix_t dFdx_;
265 state_control_matrix_t dFdu_;
269 std::shared_ptr<system_t> nonlinearSystem_;
271 linearizer_t linearizer_;
interface class for a general linear system or linearized system
Definition: LinearSystem.h:23
Eigen::Matrix< double, nControls, 1 > control_vector_t
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Eigen::Matrix< double, nStates, nStates > state_matrix_t
Eigen::Matrix< double, nStates, 1 > state_vector_t