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"
16 template<
typename _Scalar>
19 typedef _Scalar Scalar;
20 typedef CppAD::cg::CG<Scalar> CGScalar;
21 typedef CppAD::AD<CGScalar> ADScalar;
35 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Options> MatrixXs;
36 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Options> VectorXs;
37 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Options | Eigen::RowMajor>
39 typedef Eigen::Matrix<ADScalar, Eigen::Dynamic, 1, Options> ADVectorXs;
40 typedef Eigen::Matrix<ADScalar, Eigen::Dynamic, Eigen::Dynamic, Options> ADMatrixXs;
48 typedef CppAD::ADFun<CGScalar> ADFun;
52 const Eigen::DenseIndex dim_input,
53 const Eigen::DenseIndex dim_output,
56 : ad_model(model.template cast<ADScalar>())
63 ad_X = ADVectorXs(dim_input);
64 ad_Y = ADVectorXs(dim_output);
66 y = VectorXs(ad_Y.size());
68 jac = RowMatrixXs(ad_Y.size(), ad_X.size());
83 cgen_ptr = std::unique_ptr<CppAD::cg::ModelCSourceGen<Scalar>>(
84 new CppAD::cg::ModelCSourceGen<Scalar>(ad_fun,
function_name));
87 libcgen_ptr = std::unique_ptr<CppAD::cg::ModelLibraryCSourceGen<Scalar>>(
88 new CppAD::cg::ModelLibraryCSourceGen<Scalar>(*cgen_ptr));
90 dynamicLibManager_ptr = std::unique_ptr<CppAD::cg::DynamicModelLibraryProcessor<Scalar>>(
91 new CppAD::cg::DynamicModelLibraryProcessor<Scalar>(*libcgen_ptr,
library_name));
94 CppAD::cg::ModelCSourceGen<Scalar> & codeGenerator()
100 const std::string & gcc_path =
"/usr/bin/gcc",
const std::string & compile_options =
"-Ofast")
102 CppAD::cg::GccCompiler<Scalar> compiler(gcc_path);
103 std::vector<std::string> compile_flags = compiler.getCompileFlags();
104 compile_flags[0] = compile_options;
105 compiler.setCompileFlags(compile_flags);
106 dynamicLibManager_ptr->createDynamicLibrary(compiler,
false);
109 bool existLib()
const
111 const std::string filename = dynamicLibManager_ptr->getLibraryName()
112 + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION;
113 std::ifstream file(filename.c_str());
117 void compileAndLoadLib(
const std::string & gcc_path)
119 compileLib(gcc_path);
124 const bool generate_if_not_exist =
true,
125 const std::string & gcc_path =
"/usr/bin/gcc",
126 const std::string & compile_options =
"-Ofast")
128 if (!existLib() && generate_if_not_exist)
129 compileLib(gcc_path, compile_options);
131 const auto it = dynamicLibManager_ptr->getOptions().find(
"dlOpenMode");
132 if (it == dynamicLibManager_ptr->getOptions().end())
134 dynamicLib_ptr.reset(
new CppAD::cg::LinuxDynamicLib<Scalar>(
135 dynamicLibManager_ptr->getLibraryName()
136 + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION));
140 int dlOpenMode = std::stoi(it->second);
141 dynamicLib_ptr.reset(
new CppAD::cg::LinuxDynamicLib<Scalar>(
142 dynamicLibManager_ptr->getLibraryName()
143 + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION,
147 generatedFun_ptr = dynamicLib_ptr->model(
function_name.c_str());
150 template<
typename Vector>
151 void evalFunction(
const Eigen::MatrixBase<Vector> & x)
155 generatedFun_ptr->ForwardZero(PINOCCHIO_EIGEN_CONST_CAST(Vector, x), y);
158 template<
typename Vector>
159 void evalJacobian(
const Eigen::MatrixBase<Vector> & x)
163 CppAD::cg::ArrayView<const Scalar> x_(
164 PINOCCHIO_EIGEN_CONST_CAST(Vector, x).data(), (
size_t)
x.size());
165 CppAD::cg::ArrayView<Scalar> jac_(jac.data(), (
size_t)jac.size());
166 generatedFun_ptr->Jacobian(x_, jac_);
195 ADVectorXs ad_X, ad_Y;
198 ADConfigVectorType ad_q, ad_q_plus;
199 ADTangentVectorType ad_dq, ad_v, ad_a;
204 std::unique_ptr<CppAD::cg::ModelCSourceGen<Scalar>> cgen_ptr;
205 std::unique_ptr<CppAD::cg::ModelLibraryCSourceGen<Scalar>> libcgen_ptr;
206 std::unique_ptr<CppAD::cg::DynamicModelLibraryProcessor<Scalar>> dynamicLibManager_ptr;
207 std::unique_ptr<CppAD::cg::DynamicLib<Scalar>> dynamicLib_ptr;
208 std::unique_ptr<CppAD::cg::GenericModel<Scalar>> generatedFun_ptr;
Main pinocchio namespace.
Eigen::DenseIndex getOutputDimension() const
Dimension of the output vector.
Eigen::DenseIndex getInputDimension() const
Dimension of the input vector.
virtual void buildMap()=0
build the mapping Y = f(X)
bool build_jacobian
Options to build or not the Jacobian of he function.
const std::string function_name
Name of the function.
bool build_forward
Options to generate or not the source code for the evaluation function.
const std::string library_name
Name of the library.
VectorXs TangentVectorType
Dense vectorized version of a joint tangent vector (e.g. velocity, acceleration, etc)....
VectorXs ConfigVectorType
Dense vectorized version of a joint configuration vector.