5 #ifndef __pinocchio_utils_code_generator_base_hpp__ 6 #define __pinocchio_utils_code_generator_base_hpp__ 8 #include "pinocchio/codegen/cppadcg.hpp" 10 #include "pinocchio/multibody/model.hpp" 11 #include "pinocchio/multibody/data.hpp" 13 #ifdef PINOCCHIO_WITH_CPPADCG_SUPPORT 18 template<
typename _Scalar>
21 typedef _Scalar Scalar;
22 typedef CppAD::cg::CG<Scalar> CGScalar;
23 typedef CppAD::AD<CGScalar> ADScalar;
34 typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic,Options> MatrixXs;
35 typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1,Options> VectorXs;
36 typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic,Options|Eigen::RowMajor> RowMatrixXs;
37 typedef Eigen::Matrix<ADScalar,Eigen::Dynamic,1,Options> ADVectorXs;
38 typedef Eigen::Matrix<ADScalar,Eigen::Dynamic,Eigen::Dynamic,Options> ADMatrixXs;
46 typedef CppAD::ADFun<CGScalar> ADFun;
49 const Eigen::DenseIndex dim_input,
50 const Eigen::DenseIndex dim_output,
53 : ad_model(model.template cast<ADScalar>())
60 ad_X = ADVectorXs(dim_input);
61 ad_Y = ADVectorXs(dim_output);
63 y = VectorXs(ad_Y.size());
65 jac = RowMatrixXs(ad_Y.size(),ad_X.size());
76 cgen_ptr = std::unique_ptr<CppAD::cg::ModelCSourceGen<Scalar> >(
new CppAD::cg::ModelCSourceGen<Scalar>(ad_fun,
function_name));
79 libcgen_ptr = std::unique_ptr<CppAD::cg::ModelLibraryCSourceGen<Scalar> >(
new CppAD::cg::ModelLibraryCSourceGen<Scalar>(*cgen_ptr));
82 = std::unique_ptr<CppAD::cg::DynamicModelLibraryProcessor<Scalar> >(
new CppAD::cg::DynamicModelLibraryProcessor<Scalar>(*libcgen_ptr,
library_name));
85 CppAD::cg::ModelCSourceGen<Scalar> & codeGenerator()
90 CppAD::cg::GccCompiler<Scalar> compiler;
91 std::vector<std::string> compile_options = compiler.getCompileFlags();
92 compile_options[0] =
"-Ofast";
93 compiler.setCompileFlags(compile_options);
94 dynamicLibManager_ptr->createDynamicLibrary(compiler,
false);
99 const std::string filename = dynamicLibManager_ptr->getLibraryName() + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION;
100 std::ifstream file(filename.c_str());
104 void loadLib(
const bool generate_if_not_exist =
true)
106 if(not existLib() && generate_if_not_exist)
109 const auto it = dynamicLibManager_ptr->getOptions().find(
"dlOpenMode");
110 if (it == dynamicLibManager_ptr->getOptions().end())
112 dynamicLib_ptr.reset(
new CppAD::cg::LinuxDynamicLib<Scalar>(dynamicLibManager_ptr->getLibraryName() + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION));
116 int dlOpenMode = std::stoi(it->second);
117 dynamicLib_ptr.reset(
new CppAD::cg::LinuxDynamicLib<Scalar>(dynamicLibManager_ptr->getLibraryName() + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION, dlOpenMode));
120 generatedFun_ptr = dynamicLib_ptr->model(function_name.c_str());
123 template<
typename Vector>
124 void evalFunction(
const Eigen::MatrixBase<Vector> & x)
128 generatedFun_ptr->ForwardZero(PINOCCHIO_EIGEN_CONST_CAST(Vector,x),y);
131 template<
typename Vector>
132 void evalJacobian(
const Eigen::MatrixBase<Vector> & x)
136 CppAD::cg::ArrayView<const Scalar> x_(PINOCCHIO_EIGEN_CONST_CAST(Vector,x).data(),(
size_t)x.size());
137 CppAD::cg::ArrayView<Scalar> jac_(jac.data(),(size_t)jac.size());
138 generatedFun_ptr->Jacobian(x_,jac_);
162 ADVectorXs ad_X, ad_Y;
165 ADConfigVectorType ad_q, ad_q_plus;
166 ADTangentVectorType ad_dq, ad_v, ad_a;
171 std::unique_ptr<CppAD::cg::ModelCSourceGen<Scalar> > cgen_ptr;
172 std::unique_ptr<CppAD::cg::ModelLibraryCSourceGen<Scalar> > libcgen_ptr;
173 std::unique_ptr<CppAD::cg::DynamicModelLibraryProcessor<Scalar> > dynamicLibManager_ptr;
174 std::unique_ptr<CppAD::cg::DynamicLib<Scalar> > dynamicLib_ptr;
175 std::unique_ptr<CppAD::cg::GenericModel<Scalar> > generatedFun_ptr;
181 #endif // PINOCCHIO_WITH_CPPADCG_SUPPORT 183 #endif // ifndef __pinocchio_utils_code_generator_base_hpp__ bool build_forward
Options to generate or not the source code for the evaluation function.
Eigen::DenseIndex getOutputDimension() const
Dimension of the output vector.
const std::string function_name
Name of the function.
Eigen::DenseIndex getInputDimension() const
Dimension of the input vector.
VectorXs ConfigVectorType
Dense vectorized version of a joint configuration vector.
const std::string library_name
Name of the library.
bool build_jacobian
Options to build or not the Jacobian of he function.
virtual void buildMap()=0
build the mapping Y = f(X)
Main pinocchio namespace.
std::string name
Model name;.
VectorXs TangentVectorType
Dense vectorized version of a joint tangent vector (e.g. velocity, acceleration, etc). It also handles the notion of co-tangent vector (e.g. torque, etc).