48 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR =
double>
49 class DiscreteSystemLinearizerADCG :
public DiscreteLinearSystem<STATE_DIM, CONTROL_DIM, SCALAR>
52 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
54 typedef DiscreteLinearSystem<STATE_DIM, CONTROL_DIM, SCALAR> Base;
56 typedef CppAD::AD<CppAD::cg::CG<SCALAR>> ADCGScalar;
58 typedef DiscreteControlledSystem<STATE_DIM, CONTROL_DIM, ADCGScalar> system_t;
59 typedef DynamicsLinearizerADCG<STATE_DIM, CONTROL_DIM, ADCGScalar, int>
67 typedef typename Base::state_control_matrix_t state_control_matrix_t;
73 DiscreteSystemLinearizerADCG(std::shared_ptr<system_t> nonlinearSystem,
bool cacheJac =
true)
74 : Base(nonlinearSystem->getType()),
75 dFdx_(state_matrix_t::Zero()),
76 dFdu_(state_control_matrix_t::Zero()),
78 nonlinearSystem_(nonlinearSystem),
79 linearizer_(std::bind(&system_t::propagateControlledDynamics,
80 nonlinearSystem_.get(),
81 std::placeholders::_1,
82 std::placeholders::_2,
83 std::placeholders::_3,
84 std::placeholders::_4))
89 DiscreteSystemLinearizerADCG(
const DiscreteSystemLinearizerADCG& arg)
90 : Base(arg.nonlinearSystem_->getType()),
93 cacheJac_(arg.cacheJac_),
94 nonlinearSystem_(arg.nonlinearSystem_->clone()),
95 linearizer_(arg.linearizer_)
100 virtual ~DiscreteSystemLinearizerADCG() {}
102 DiscreteSystemLinearizerADCG<STATE_DIM, CONTROL_DIM, SCALAR>* clone()
const override 104 return new DiscreteSystemLinearizerADCG<STATE_DIM, CONTROL_DIM, SCALAR>(*this);
121 const state_matrix_t& getDerivativeState(
const state_vector_t& x,
const control_vector_t& u,
const int t = 0)
123 dFdx_ = linearizer_.getDerivativeState(x, u, t);
142 const state_control_matrix_t& getDerivativeControl(
const state_vector_t& x,
143 const control_vector_t& u,
146 dFdu_ = linearizer_.getDerivativeControl(x, u, t);
167 void getAandB(
const state_vector_t& x,
168 const control_vector_t& u,
169 const state_vector_t& x_next,
173 state_control_matrix_t& B)
override 175 dFdx_ = linearizer_.getDerivativeState(x, u, n);
176 dFdu_ = linearizer_.getDerivativeControl(x, u, n);
189 void compileJIT(
const std::string& libName =
"DiscreteSystemLinearizerADCG") { linearizer_.compileJIT(libName); }
204 void generateCode(
const std::string& systemName,
205 const std::string& outputDir = ct::core::CODEGEN_OUTPUT_DIR,
206 const std::string& templateDir = ct::core::CODEGEN_TEMPLATE_DIR,
207 const std::string& ns1 =
"core",
208 const std::string& ns2 =
"generated",
209 bool useReverse =
false,
210 bool ignoreZero =
true)
212 std::string codeJacA, codeJacB;
213 linearizer_.generateCode(codeJacA, codeJacB, useReverse, ignoreZero);
216 templateDir, outputDir, systemName, ns1, ns2, codeJacA, codeJacB,
"AUTOGENERATED_CODE_PLACEHOLDER");
220 const linearizer_t& getLinearizer()
const {
return linearizer_; }
234 void writeCodeFile(
const std::string& templateDir,
235 const std::string& outputDir,
236 const std::string& systemName,
237 const std::string& ns1,
238 const std::string& ns2,
239 const std::string& codeJacA,
240 const std::string& codeJacB,
241 const std::string& codePlaceholder)
243 std::cout <<
"Generating discrete linear system..." << std::endl;
245 size_t maxTempVarCountState, maxTempVarCountControl;
246 linearizer_.getMaxTempVarCount(maxTempVarCountState, maxTempVarCountControl);
248 std::string header = internal::CGHelpers::parseFile(templateDir +
"/DiscreteLinearSystem.tpl.h");
249 std::string sourceA = internal::CGHelpers::parseFile(templateDir +
"/DiscreteLinearSystem.tplA.cpp");
250 std::string sourceB = internal::CGHelpers::parseFile(templateDir +
"/DiscreteLinearSystem.tplB.cpp");
252 const std::string scalarName(linearizer_.getOutScalarType());
254 replaceSizesAndNames(header, systemName, scalarName, ns1, ns2);
255 replaceSizesAndNames(sourceA, systemName, scalarName, ns1, ns2);
256 replaceSizesAndNames(sourceB, systemName, scalarName, ns1, ns2);
258 internal::CGHelpers::replaceOnce(header,
"MAX_COUNT_STATE", std::to_string(maxTempVarCountState));
259 internal::CGHelpers::replaceOnce(header,
"MAX_COUNT_CONTROL", std::to_string(maxTempVarCountControl));
261 internal::CGHelpers::replaceOnce(sourceA, codePlaceholder +
"_JAC_A", codeJacA);
262 internal::CGHelpers::replaceOnce(sourceB, codePlaceholder +
"_JAC_B", codeJacB);
264 internal::CGHelpers::writeFile(outputDir +
"/" + systemName +
".h", header);
265 internal::CGHelpers::writeFile(outputDir +
"/" + systemName +
"_A.cpp", sourceA);
266 internal::CGHelpers::writeFile(outputDir +
"/" + systemName +
"_B.cpp", sourceB);
269 std::cout <<
"... Done! Successfully generated discrete linear system" << std::endl;
280 void replaceSizesAndNames(std::string& file,
281 const std::string& systemName,
282 const std::string& scalarName,
283 const std::string& ns1,
284 const std::string& ns2)
286 internal::CGHelpers::replaceAll(file,
"LINEAR_SYSTEM_NAME", systemName);
287 internal::CGHelpers::replaceAll(file,
"NS1", ns1);
288 internal::CGHelpers::replaceAll(file,
"NS2", ns2);
289 internal::CGHelpers::replaceAll(file,
"STATE_DIM", std::to_string(STATE_DIM));
290 internal::CGHelpers::replaceAll(file,
"CONTROL_DIM", std::to_string(CONTROL_DIM));
291 internal::CGHelpers::replaceAll(file,
"SCALAR", scalarName);
295 state_matrix_t dFdx_;
296 state_control_matrix_t dFdu_;
300 std::shared_ptr<system_t> nonlinearSystem_;
302 linearizer_t linearizer_;
Eigen::Matrix< double, nControls, 1 > control_vector_t
constexpr size_t n
Definition: MatrixInversionTest.cpp:14
Eigen::Matrix< double, nStates, nStates > state_matrix_t
Eigen::Matrix< double, nStates, 1 > state_vector_t