6 #include <dynamic-graph/factory.h>
8 #include <sot/core/debug.hh>
9 #include <sot/core/stop-watch.hh>
18 using namespace dynamicgraph::command;
21 #define PROFILE_PWM_DES_COMPUTATION \
22 "PositionController: desired pwm computation "
24 #define GAIN_SIGNALS m_KpSIN << m_KdSIN << m_KiSIN
25 #define REF_JOINT_SIGNALS m_qRefSIN << m_dqRefSIN
26 #define STATE_SIGNALS m_base6d_encodersSIN << m_jointsVelocitiesSIN
28 #define INPUT_SIGNALS STATE_SIGNALS << REF_JOINT_SIGNALS << GAIN_SIGNALS
30 #define OUTPUT_SIGNALS m_pwmDesSOUT << m_qErrorSOUT
35 typedef Eigen::Matrix<double, Eigen::Dynamic, 1>
VectorN;
36 typedef Eigen::Matrix<double, Eigen::Dynamic, 1>
VectorN6;
46 CONSTRUCT_SIGNAL_IN(base6d_encoders,
dynamicgraph::Vector),
47 CONSTRUCT_SIGNAL_IN(jointsVelocities,
dynamicgraph::Vector),
55 m_base6d_encodersSIN << m_qRefSIN),
56 m_robot_util(RefVoidRobotUtil()),
57 m_initSucceeded(false) {
61 addCommand(
"init", makeCommandVoid2(
63 docCommandVoid2(
"Initialize the entity.",
64 "Time period in seconds (double)",
65 "Reference to the robot (string)")));
66 addCommand(
"resetIntegral",
68 docCommandVoid0(
"Reset the integral.")));
72 if (dt <= 0.0)
return SEND_MSG(
"Timestep must be positive", MSG_TYPE_ERROR);
73 if (!m_base6d_encodersSIN.isPlugged())
74 return SEND_MSG(
"Init failed: signal base6d_encoders is not plugged",
76 if (!m_jointsVelocitiesSIN.isPlugged())
77 return SEND_MSG(
"Init failed: signal jointsVelocities is not plugged",
79 if (!m_qRefSIN.isPlugged())
80 return SEND_MSG(
"Init failed: signal qRef is not plugged", MSG_TYPE_ERROR);
81 if (!m_dqRefSIN.isPlugged())
82 return SEND_MSG(
"Init failed: signal dqRef is not plugged", MSG_TYPE_ERROR);
83 if (!m_KpSIN.isPlugged())
84 return SEND_MSG(
"Init failed: signal Kp is not plugged", MSG_TYPE_ERROR);
85 if (!m_KdSIN.isPlugged())
86 return SEND_MSG(
"Init failed: signal Kd is not plugged", MSG_TYPE_ERROR);
87 if (!m_KiSIN.isPlugged())
88 return SEND_MSG(
"Init failed: signal Ki is not plugged", MSG_TYPE_ERROR);
91 std::string localName(robotRef);
92 if (isNameInRobotUtil(localName))
95 SEND_MSG(
"You should have an entity controller manager initialized before",
120 if (!m_initSucceeded) {
121 SEND_WARNING_STREAM_MSG(
122 "Cannot compute signal pwmDes before initialization!");
128 const VectorN& Kp = m_KpSIN(iter);
129 const VectorN& Kd = m_KdSIN(iter);
130 const VectorN6& q = m_base6d_encodersSIN(iter);
131 const VectorN& dq = m_jointsVelocitiesSIN(iter);
132 const VectorN& qRef = m_qRefSIN(iter);
133 const VectorN& dqRef = m_dqRefSIN(iter);
135 assert(q.size() ==
static_cast<Eigen::VectorXd::Index
>(
136 m_robot_util->m_nbJoints + 6) &&
137 "Unexpected size of signal base6d_encoder");
139 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints) &&
140 "Unexpected size of signal dq");
141 assert(qRef.size() ==
142 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints) &&
143 "Unexpected size of signal qRef");
144 assert(dqRef.size() ==
145 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints) &&
146 "Unexpected size of signal dqRef");
148 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints) &&
149 "Unexpected size of signal Kd");
151 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints) &&
152 "Unexpected size of signal Kd");
154 m_pwmDes = Kp.cwiseProduct(qRef - q.tail(m_robot_util->m_nbJoints)) +
155 Kd.cwiseProduct(dqRef - dq);
158 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints))
159 s.resize(m_robot_util->m_nbJoints);
168 if (!m_initSucceeded) {
169 SEND_MSG(
"Cannot compute signal qError before initialization!",
170 MSG_TYPE_WARNING_STREAM);
174 const VectorN6& q = m_base6d_encodersSIN(iter);
175 const VectorN& qRef = m_qRefSIN(iter);
176 assert(q.size() ==
static_cast<Eigen::VectorXd::Index
>(
177 m_robot_util->m_nbJoints + 6) &&
178 "Unexpected size of signal base6d_encoder");
179 assert(qRef.size() ==
180 static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints) &&
181 "Unexpected size of signal qRef");
183 if (s.size() !=
static_cast<Eigen::VectorXd::Index
>(m_robot_util->m_nbJoints))
184 s.resize(m_robot_util->m_nbJoints);
185 s = qRef - q.tail(m_robot_util->m_nbJoints);
197 os <<
"PositionController " << getName();
199 getProfiler().report_all(3, os);
200 }
catch (ExceptionSignal e) {