17 #define EIGEN_NO_MALLOC
18 #include <dynamic-graph/factory.h>
21 #include <Eigen/Dense>
22 #include <sot/core/debug.hh>
27 #define ODEBUG(x) std::cout << x << std::endl
31 #define ODEBUG3(x) std::cout << x << std::endl
33 #define DBGFILE "/tmp/debug-ddp_pyrene_actuator_solver.dat"
35 #define RESETDEBUG5() \
37 std::ofstream DebugFile; \
38 DebugFile.open(DBGFILE, std::ofstream::out); \
41 #define ODEBUG5FULL(x) \
43 std::ofstream DebugFile; \
44 DebugFile.open(DBGFILE, std::ofstream::app); \
45 DebugFile << __FILE__ << ":" << __FUNCTION__ << "(#" << __LINE__ \
46 << "):" << x << std::endl; \
51 std::ofstream DebugFile; \
52 DebugFile.open(DBGFILE, std::ofstream::app); \
53 DebugFile << x << std::endl; \
58 #define ODEBUG4FULL(x)
67 using namespace dynamicgraph::command;
68 using namespace Eigen;
70 #define ALL_INPUT_SIGNALS \
71 m_pos_desSIN << m_pos_joint_measureSIN << m_dx_joint_measureSIN \
74 #define ALL_OUTPUT_SIGNALS m_tauSOUT
82 "DdpPyreneActuatorSolver");
87 CONSTRUCT_SIGNAL_IN(pos_joint_measure,
dynamicgraph::Vector),
88 CONSTRUCT_SIGNAL_IN(dx_joint_measure,
dynamicgraph::Vector),
95 m_solver(m_model, m_cost, DISABLE_FULLDDP, DISABLE_QPBOX) {
106 docCommandVoid4(
"Initialize the DDP solver.",
"Control timestep [s].",
107 "Size of the preview window (in nb of samples)",
108 "Max. nb. of iterations",
"Stopping criteria")));
109 addCommand(
"setTorqueLimit",
111 docCommandVoid1(
"Set the Torque limit.",
112 "Limit of the motor torque.")));
116 docCommandVoid2(
"Set the angular limits of the joint.",
117 "Upper limit",
"Lower limit.")));
122 docCommandVoid2(
"Set the angular velocity limits of the joint.",
123 "Upper limit",
"Lower limit.")));
124 addCommand(
"setLoadParam",
126 docCommandVoid3(
"Setter of the Load parameters.",
127 "Mass of the load [g].",
128 "X coordinate of the Load",
129 "Y coordinate of the Load")));
130 addCommand(
"setLoadMass",
132 docCommandVoid1(
"Set the Load mass.",
133 "Mass of the load [g].")));
134 addCommand(
"removeLoad",
136 docCommandVoid0(
"Remove the Load.")));
141 docCommandVoid1(
"Set the Gain of the state cost matrix.",
142 "Matrix of Gains.")));
143 addCommand(
"setCostGainCommand",
146 docCommandVoid1(
"Set the Gain of the command cost matrix.",
147 "Matrix of Gains.")));
149 "setCostGainStateConstraint",
152 docCommandVoid1(
"Set the Gain of the constraints on the state.",
153 "Matrix of Gains.")));
154 addCommand(
"setCostGainTorqueConstraint",
157 docCommandVoid1(
"Set the Gain of the torque constraints.",
158 "Matrix of Gains.")));
165 if (!m_initSucceeded) {
166 SEND_WARNING_STREAM_MSG(
"Cannot compute signal tau before initialization!");
172 const dynamicgraph::Vector& pos_des = m_pos_desSIN(iter);
174 const dynamicgraph::Vector& pos_joint_measure = m_pos_joint_measureSIN(iter);
176 const dynamicgraph::Vector& dx_joint_measure = m_dx_joint_measureSIN(iter);
178 const dynamicgraph::Vector& tau_des = m_tau_desSIN(iter);
180 DDPSolver<double, 2, 1>::stateVec_t xinit, xDes;
182 xinit << pos_joint_measure(0), dx_joint_measure(0);
184 xDes << pos_des, 0.0;
187 m_solver.initSolver(xinit, xDes);
191 m_solver.solveTrajectory();
195 DDPSolver<double, 2, 1>::traj lastTraj;
196 lastTraj = m_solver.getLastSolvedTrajectory();
199 DDPSolver<double, 2, 1>::commandVecTab_t uList;
200 uList = lastTraj.uList;
203 double tau_ddp = uList[0](0, 0);
204 if (!isnan(tau_ddp)) {
216 const double& stopCriteria) {
217 if (!m_pos_desSIN.isPlugged())
218 return SEND_MSG(
"Init failed: signal pos_des is not plugged",
220 if (!m_pos_joint_measureSIN.isPlugged())
221 return SEND_MSG(
"Init failed: signal pos_joint_measure is not plugged",
223 if (!m_dx_joint_measureSIN.isPlugged())
224 return SEND_MSG(
"Init failed: signal dx_joint_measure is not plugged",
234 m_cost.setJointLimit(0.0, -2.35619449019);
235 m_cost.setJointVelLimit(30.0, -30.0);
245 const double& lowerLim) {
246 m_cost.setJointLimit(upperLim, lowerLim);
250 const double& lowerLim) {
251 m_cost.setJointVelLimit(upperLim, lowerLim);
255 const double& coordX,
256 const double& coordY) {
257 m_model.setLoadParam(mass, coordX, coordY);
267 const CostFunction<double, 2, 1>::stateMat_t Q_new =
268 Eigen::Map<const CostFunction<double, 2, 1>::stateMat_t,
269 Eigen::Unaligned>(Q.data(), 2, 1);
270 m_cost.setCostGainState(Q_new);
274 const dynamicgraph::Vector& W) {
275 const CostFunction<double, 2, 1>::stateMat_t W_new =
276 Eigen::Map<const CostFunction<double, 2, 1>::stateMat_t,
277 Eigen::Unaligned>(W.data(), 2, 1);
278 m_cost.setCostGainStateConstraint(W_new);
282 const dynamicgraph::Vector& R) {
283 const CostFunction<double, 2, 1>::commandMat_t R_new =
284 Eigen::Map<const CostFunction<double, 2, 1>::commandMat_t,
285 Eigen::Unaligned>(R.data(), 1);
286 m_cost.setCostGainCommand(R_new);
290 const dynamicgraph::Vector& P) {
291 const CostFunction<double, 2, 1>::commandMat_t P_new =
292 Eigen::Map<const CostFunction<double, 2, 1>::commandMat_t,
293 Eigen::Unaligned>(P.data(), 1);
294 m_cost.setCostGainTorqueConstraint(P_new);
299 <<
" stopCriteria: " <<
m_stopCrit << std::endl;