19 #include <dynamic-graph/all-commands.h>
20 #include <dynamic-graph/factory.h>
24 #include <sot/core/debug.hh>
25 #include <sot/core/stop-watch.hh>
29 namespace talos_balance {
30 namespace dg = ::dynamicgraph;
32 using namespace dg::command;
34 #define PROFILE_HIPFLEXIBILITYCOMPENSATION_TAUFILT_COMPUTATION \
35 "HipFlexibilityCompensation: Torque filter computation "
36 #define PROFILE_HIPFLEXIBILITYCOMPENSATION_DELTAQ_COMPUTATION \
37 "HipFlexibilityCompensation: Angular correction computation "
38 #define PROFILE_HIPFLEXIBILITYCOMPENSATION_QCMD_COMPUTATION \
39 "HipFlexibilityCompensation: Corrected joint configuration computation "
41 #define JOINT_DES_SIGNALS m_q_desSIN
43 #define INPUT_SIGNALS \
44 m_phaseSIN << m_tauSIN << m_K_rSIN << m_K_lSIN //<< m_K_dSIN
46 #define OUTPUT_SIGNALS m_tau_filtSOUT << m_delta_qSOUT << m_q_cmdSOUT
54 "HipFlexibilityCompensation");
61 CONSTRUCT_SIGNAL_IN(phase, int),
64 CONSTRUCT_SIGNAL_IN(K_l, double),
65 CONSTRUCT_SIGNAL_IN(K_r, double),
71 m_initSucceeded(false),
72 m_torqueLowPassFilterFrequency(1),
73 m_delta_q_saturation(0.01),
74 m_rate_limiter(0.003) {
79 addCommand(
"init", makeCommandVoid2(
81 docCommandVoid2(
"Initialize the entity.",
82 "Robot time step",
"Robot name")));
85 "setTorqueLowPassFilterFrequency",
89 "Set the LowPassFilter frequency for the torque computation.",
90 "Value of the frequency")));
92 "setAngularSaturation",
96 "Set the saturation for the angular correction computation.",
97 "Value of the saturation")));
102 docCommandVoid1(
"Set the rate for the rate limiter of delta_q.",
103 "Value of the limiter")));
106 "getTorqueLowPassFilterFrequency",
110 "Get the current value of the torque LowPassFilter frequency.",
111 "frequency (double)")));
114 "getAngularSaturation",
117 docDirectGetter(
"Get the current value of the Angular Saturation.",
118 "saturation (double)")));
120 addCommand(
"getRateLimiter",
123 docDirectGetter(
"Get the current value of the rate limiter.",
131 if (!m_q_desSIN.isPlugged())
132 return SEND_MSG(
"Init failed: signal q_des is not plugged", MSG_TYPE_ERROR);
133 if (!m_tauSIN.isPlugged())
134 return SEND_MSG(
"Init failed: signal tau is not plugged", MSG_TYPE_ERROR);
135 if (!m_K_rSIN.isPlugged())
136 return SEND_MSG(
"Init failed: signal K_r is not plugged", MSG_TYPE_ERROR);
137 if (!m_K_lSIN.isPlugged())
138 return SEND_MSG(
"Init failed: signal K_l is not plugged", MSG_TYPE_ERROR);
141 std::string robotName_nonconst(
robotName);
143 if (!isNameInRobotUtil(robotName_nonconst)) {
144 SEND_MSG(
"You should have a robotUtil pointer initialized before",
148 std::cerr <<
"m_robot_util:" <<
m_robot_util << std::endl;
152 const Vector&
tau = m_tauSIN.accessCopy();
160 const double& frequency) {
165 const double& saturation) {
175 Vector& previous_signal) {
177 double alpha = exp(-
m_dt * 2 * M_PI * frequency);
178 Vector output = alpha * previous_signal + signal * (1 - alpha);
185 Vector rate = (signal - previous_signal) /
m_dt;
187 for (
unsigned int i = 0; i < signal.size(); i++) {
193 output[i] = signal[i];
202 if (!m_initSucceeded) {
203 SEND_WARNING_STREAM_MSG(
204 "Cannot compute signal tau_filt before initialization!");
212 if (s.size() !=
tau.size()) s.resize(
tau.size());
218 s = lowPassFilter(m_torqueLowPassFilterFrequency,
tau, m_previous_tau);
227 if (!m_initSucceeded) {
228 SEND_WARNING_STREAM_MSG(
229 "Cannot compute signal delta_q before initialization!");
235 const Vector&
tau = m_tau_filtSOUT(iter);
237 m_phaseSIN.isPlugged()
240 const double K_r = m_K_rSIN(iter);
241 const double K_l = m_K_lSIN(iter);
243 if (s.size() !=
tau.size()) s.resize(
tau.size());
254 if (s[1] > m_delta_q_saturation) {
255 s[1] = m_delta_q_saturation;
256 }
else if (s[1] < -m_delta_q_saturation) {
257 s[1] = -m_delta_q_saturation;
260 if (s[7] > m_delta_q_saturation) {
261 s[7] = m_delta_q_saturation;
262 }
else if (s[7] < -m_delta_q_saturation) {
263 s[7] = -m_delta_q_saturation;
271 if (!m_initSucceeded) {
272 SEND_WARNING_STREAM_MSG(
273 "Cannot compute signal q_cmd before initialization!");
279 const Vector& q_des = m_q_desSIN(iter);
280 const Vector& delta_q = m_delta_qSOUT(iter);
282 assert((q_des.size() == delta_q.size()) ||
283 (q_des.size() == delta_q.size() + 6));
285 if (s.size() != q_des.size()) s.resize(q_des.size());
287 if (m_limitedSignal.size() != delta_q.size())
288 m_limitedSignal.resize(delta_q.size());
289 rateLimiter(delta_q, m_previous_delta_q, m_limitedSignal);
290 m_previous_delta_q = m_limitedSignal;
296 s.tail(m_limitedSignal.size()) += m_limitedSignal;
308 os <<
"HipFlexibilityCompensation " << getName();
310 getProfiler().report_all(3, os);
311 }
catch (ExceptionSignal e) {