6 #include <dynamic-graph/factory.h>
9 #include <sot/core/debug.hh>
18 #define ALL_INPUT_SIGNALS \
19 m_base6d_encodersSIN << m_accelerometerSIN << m_jointTorquesSIN \
22 #define ALL_OUTPUT_SIGNALS m_jointTorquesEstimatedSOUT
26 using namespace dynamicgraph::command;
27 using namespace Eigen;
36 "TorqueOffsetEstimator");
43 CONSTRUCT_SIGNAL_IN(base6d_encoders,
dynamicgraph::Vector),
44 CONSTRUCT_SIGNAL_IN(accelerometer,
dynamicgraph::Vector),
47 CONSTRUCT_SIGNAL_INNER(collectSensorData,
dynamicgraph::Vector,
49 CONSTRUCT_SIGNAL_OUT(jointTorquesEstimated,
dynamicgraph::Vector,
51 sensor_offset_status(PRECOMPUTATION),
58 "Initialize the estimator",
"urdfFilePath",
59 "Homogeneous transformation from chest frame to IMU frame",
60 "Maximum angular velocity allowed in either axis",
61 "Freeflyer joint name",
"chest joint name")));
62 addCommand(
"computeOffset",
65 docCommandVoid2(
"Compute the offset for sensor calibration",
66 "Number of iteration to average over.",
67 "Maximum allowed offset")));
72 docDirectGetter(
"Return the computed sensor offsets",
"vector")));
85 const Eigen::Matrix4d& m_torso_X_imu_,
86 const double& gyro_epsilon_,
87 const std::string& ffJointName,
88 const std::string& torsoJointName) {
91 std::string localName(robotRef);
92 if (isNameInRobotUtil(localName)) {
94 std::cerr <<
"m_robot_util:" <<
m_robot_util << std::endl;
97 "You should have an entity controller manager initialized before",
102 pinocchio::urdf::buildModel(
m_robot_util->m_urdf_filename,
103 pinocchio::JointModelFreeFlyer(),
m_model);
111 }
catch (
const std::exception& e) {
112 std::cout << e.what();
113 SEND_MSG(
"Init failed: Could load URDF :" +
m_robot_util->m_urdf_filename,
119 m_torso_X_imu.translation() = m_torso_X_imu_.block<3, 1>(0, 3);
124 const double& epsilon_) {
125 if (sensor_offset_status == PRECOMPUTATION) {
126 SEND_MSG(
"Starting offset computation with no. iterations:" + nIterations,
130 sensor_offset_status = INPROGRESS;
131 }
else if (sensor_offset_status == INPROGRESS) {
132 SEND_MSG(
"Collecting input signals. Please keep the graph running",
135 SEND_MSG(
"Recomputing offset with no. iterations:" + nIterations,
139 current_progress = 0;
144 sensor_offset_status = INPROGRESS;
150 if (sensor_offset_status == INPROGRESS) {
151 const Eigen::VectorXd& gyro = m_gyroscopeSIN(iter);
152 if (gyro.array().abs().maxCoeff() >= gyro_epsilon) {
153 SEND_MSG(
"Very High Angular Rotations.", MSG_TYPE_ERROR_STREAM);
157 int i = current_progress;
159 if (i < n_iterations) {
160 SEND_MSG(
"Collecting signals for iteration no:" + i,
161 MSG_TYPE_DEBUG_STREAM);
163 const Eigen::VectorXd& sot_enc = m_base6d_encodersSIN(iter);
164 const Eigen::VectorXd& IMU_acc = m_accelerometerSIN(iter);
165 const Eigen::VectorXd& tau = m_jointTorquesSIN(iter);
167 Eigen::VectorXd enc(m_model.nq);
169 enc.tail(m_model.nv - 6) = sot_enc.tail(m_model.nv - 6);
171 enc.head<6>().setZero();
176 pinocchio::forwardKinematics(m_model, *m_data, enc);
179 pinocchio::SE3 oMimu = m_data->oMi[torsoIndex] * m_torso_X_imu;
184 const dynamicgraph::Vector acc =
185 oMimu.rotation() * IMU_acc;
189 m_model.gravity.linear() = acc;
193 const Eigen::VectorXd& tau_rnea = pinocchio::rnea(
194 m_model, *m_data, enc, Eigen::VectorXd::Zero(m_model.nv),
195 Eigen::VectorXd::Zero(m_model.nv));
196 const Eigen::VectorXd current_offset =
197 tau - tau_rnea.tail(m_model.nv - 6);
198 if (current_offset.array().abs().maxCoeff() >= epsilon) {
199 SEND_MSG(
"Too high torque offset estimated for iteration" + i,
207 jointTorqueOffsets += current_offset;
209 }
else if (i == n_iterations) {
211 jointTorqueOffsets /= n_iterations;
212 sensor_offset_status = COMPUTED;
215 assert(
false &&
"Error in collectSensorData. ");
224 m_collectSensorDataSINNER(iter);
226 if (s.size() != m_model.nv - 6) s.resize(m_model.nv - 6);
228 if (sensor_offset_status == PRECOMPUTATION ||
229 sensor_offset_status == INPROGRESS) {
230 s = m_jointTorquesSIN(iter);
232 const dynamicgraph::Vector& inputTorques = m_jointTorquesSIN(iter);
233 s = inputTorques - jointTorqueOffsets;
239 os <<
"TorqueOffsetEstimator" << getName() <<
":\n";
241 getProfiler().report_all(3, os);
242 }
catch (ExceptionSignal e) {