sot-talos-balance  2.0.5
Collection of dynamic-graph entities aimed at implementing balance control on talos.
ft-wrist-calibration.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019
3  * LAAS-CNRS
4  * F. Bailly
5  * T. Flayols
6  * F. Risbourg
7  */
8 
9 #include <dynamic-graph/all-commands.h>
10 #include <dynamic-graph/factory.h>
11 
12 #include <sot/core/debug.hh>
13 #include <sot/core/stop-watch.hh>
16 
17 #define CALIB_ITER_TIME \
18  1000 // Iteration needed for sampling and averaging the FT sensors while
19  // calibrating
20 
21 namespace dynamicgraph {
22 namespace sot {
23 namespace talos_balance {
24 namespace dynamicgraph = ::dynamicgraph;
25 using namespace dynamicgraph;
26 using namespace dynamicgraph::command;
27 using namespace dg::sot::talos_balance;
28 
29 #define INPUT_SIGNALS m_rightWristForceInSIN << m_leftWristForceInSIN << m_qSIN
30 #define INNER_SIGNALS m_rightWeightSINNER << m_leftWeightSINNER
31 #define OUTPUT_SIGNALS m_rightWristForceOutSOUT << m_leftWristForceOutSOUT
32 
35 typedef FtWristCalibration EntityClassName;
36 
37 /* --- DG FACTORY ---------------------------------------------------- */
38 DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(FtWristCalibration, "FtWristCalibration");
39 
40 /* ------------------------------------------------------------------- */
41 /* --- CONSTRUCTION -------------------------------------------------- */
42 /* ------------------------------------------------------------------- */
44  : Entity(name),
45  CONSTRUCT_SIGNAL_IN(rightWristForceIn, dynamicgraph::Vector),
46  CONSTRUCT_SIGNAL_IN(leftWristForceIn, dynamicgraph::Vector),
47  CONSTRUCT_SIGNAL_IN(q, dynamicgraph::Vector),
48  CONSTRUCT_SIGNAL_INNER(rightWeight, dynamicgraph::Vector, INPUT_SIGNALS),
49  CONSTRUCT_SIGNAL_INNER(leftWeight, dynamicgraph::Vector, INPUT_SIGNALS),
50  CONSTRUCT_SIGNAL_OUT(rightWristForceOut, dynamicgraph::Vector,
52  CONSTRUCT_SIGNAL_OUT(leftWristForceOut, dynamicgraph::Vector,
54  m_robot_util(RefVoidRobotUtil()),
55  m_model(),
56  m_data(),
57  m_rightSensorId(),
58  m_leftSensorId(),
59  m_initSucceeded(false),
60  m_removeWeight(false) {
61  Entity::signalRegistration(INPUT_SIGNALS << INNER_SIGNALS << OUTPUT_SIGNALS);
62 
63  /* Commands. */
64  addCommand("init",
65  makeCommandVoid1(*this, &FtWristCalibration::init,
66  docCommandVoid1("Initialize the entity.",
67  "Robot reference (string)")));
68  addCommand(
69  "setRightHandConf",
70  makeCommandVoid2(*this, &FtWristCalibration::setRightHandConf,
71  docCommandVoid2("Set the data of the right hand",
72  "Vector of default forces in Newton",
73  "Vector of the weight lever arm")));
74  addCommand(
75  "setLeftHandConf",
76  makeCommandVoid2(*this, &FtWristCalibration::setLeftHandConf,
77  docCommandVoid2("Set the data of the left hand",
78  "Vector of default forces in Newton",
79  "Vector of the weight lever arm")));
80  addCommand("calibrateWristSensor",
81  makeCommandVoid0(*this, &FtWristCalibration::calibrateWristSensor,
82  docCommandVoid0("Calibrate the wrist sensors")));
83 
84  addCommand("setRemoveWeight",
85  makeCommandVoid1(
87  docCommandVoid1("set RemoveWeight", "desired removeWeight")));
88 }
89 
90 void FtWristCalibration::init(const std::string &robotRef) {
91  dgADD_OSTREAM_TO_RTLOG(std::cout);
92  std::string localName(robotRef);
93  if (!isNameInRobotUtil(localName)) {
94  m_robot_util = createRobotUtil(localName);
95  } else {
96  m_robot_util = getRobotUtil(localName);
97  }
98  m_right_FT_offset << 0, 0, 0, 0, 0, 0;
99  m_left_FT_offset << 0, 0, 0, 0, 0, 0;
100  m_right_FT_offset_calibration_sum << 0, 0, 0, 0, 0, 0;
101  m_left_FT_offset_calibration_sum << 0, 0, 0, 0, 0, 0;
102  m_right_weight_calibration_sum << 0, 0, 0, 0, 0, 0;
103  m_left_weight_calibration_sum << 0, 0, 0, 0, 0, 0;
104 
105  pinocchio::urdf::buildModel(m_robot_util->m_urdf_filename,
106  pinocchio::JointModelFreeFlyer(), m_model);
107  m_data = new pinocchio::Data(m_model);
108 
109  m_rightSensorId = m_model.getFrameId("wrist_right_ft_link");
110  m_leftSensorId = m_model.getFrameId("wrist_left_ft_link");
111 
112  m_initSucceeded = true;
113  SEND_MSG("Entity Initialized", MSG_TYPE_INFO);
114 }
115 
116 /* ------------------------------------------------------------------- */
117 /* --- SIGNALS ------------------------------------------------------- */
118 /* ------------------------------------------------------------------- */
119 
121  if (!m_initSucceeded) {
122  SEND_WARNING_STREAM_MSG(
123  "Cannot compute signal rightWeight before initialization!");
124  return s;
125  }
126  if (s.size() != 6) s.resize(6);
127 
128  const Vector &q = m_qSIN(iter);
129  assert(q.size() == m_model.nq && "Unexpected size of signal q");
130 
131  // Get sensorPlacement
132  pinocchio::framesForwardKinematics(m_model, *m_data, q);
133  const pinocchio::SE3 &sensorPlacement = m_data->oMf[m_rightSensorId];
134 
135  Eigen::Vector3d leverArm = sensorPlacement.rotation() * m_rightLeverArm;
136 
137  m_rightHandWeight[3] = leverArm(1) * m_rightHandWeight(2);
138  m_rightHandWeight[4] = -leverArm(0) * m_rightHandWeight(2);
139 
140  Eigen::Matrix<double, 6, 1> weight;
141 
142  weight.head<3>() =
143  sensorPlacement.rotation().transpose() * m_rightHandWeight.head<3>();
144  weight.tail<3>() =
145  sensorPlacement.rotation().transpose() * m_rightHandWeight.tail<3>();
146 
147  s = weight;
148 
149  return s;
150 }
151 
153  if (!m_initSucceeded) {
154  SEND_WARNING_STREAM_MSG(
155  "Cannot compute signal rightWeight before initialization!");
156  return s;
157  }
158  if (s.size() != 6) s.resize(6);
159 
160  const Vector &q = m_qSIN(iter);
161  assert(q.size() == m_model.nq && "Unexpected size of signal q");
162 
163  pinocchio::framesForwardKinematics(m_model, *m_data, q);
164  const pinocchio::SE3 &sensorPlacement = m_data->oMf[m_leftSensorId];
165 
166  Eigen::Vector3d leverArm = sensorPlacement.rotation() * m_leftLeverArm;
167 
168  m_leftHandWeight[3] = leverArm(1) * m_leftHandWeight(2);
169  m_leftHandWeight[4] = -leverArm(0) * m_leftHandWeight(2);
170 
171  Eigen::Matrix<double, 6, 1> weight;
172 
173  weight.head<3>() =
174  sensorPlacement.rotation().transpose() * m_leftHandWeight.head<3>();
175  weight.tail<3>() =
176  sensorPlacement.rotation().transpose() * m_leftHandWeight.tail<3>();
177 
178  s = weight;
179 
180  return s;
181 }
182 
184  if (!m_initSucceeded) {
185  SEND_WARNING_STREAM_MSG("Cannot compute signal sum before initialization!");
186  return s;
187  }
188  if (s.size() != 6) s.resize(6);
189  const Vector &rightWristForce = m_rightWristForceInSIN(iter);
190  assert(rightWristForce.size() == 6 &&
191  "Unexpected size of signal rightWristForceIn, should be 6.");
192  const Vector &rightWeight = m_rightWeightSINNER(iter);
193  assert(rightWeight.size() == 6 &&
194  "Unexpected size of signal rightWeight, should be 6.");
195 
196  // do offset calibration if needed
197  if (m_rightCalibrationIter > 0) {
198  m_right_FT_offset_calibration_sum += rightWristForce;
199  m_right_weight_calibration_sum += rightWeight;
200  m_rightCalibrationIter--;
201  } else if (m_rightCalibrationIter == 0) {
202  SEND_INFO_STREAM_MSG("Calibrating ft sensors...");
203  m_right_FT_offset = m_right_FT_offset_calibration_sum / CALIB_ITER_TIME;
204  m_right_FT_offset -= m_right_weight_calibration_sum / CALIB_ITER_TIME;
205  m_rightCalibrationIter--;
206  }
207 
208  s = rightWristForce - m_right_FT_offset;
209 
210  if (m_removeWeight) {
211  s -= rightWeight;
212  }
213  return s;
214 }
215 
217  if (!m_initSucceeded) {
218  SEND_WARNING_STREAM_MSG("Cannot compute signal sum before initialization!");
219  return s;
220  }
221  if (s.size() != 6) s.resize(6);
222  const Vector &leftWristForce = m_leftWristForceInSIN(iter);
223  assert(leftWristForce.size() == 6 &&
224  "Unexpected size of signal leftWristForceIn, should be 6.");
225  const Vector &leftWeight = m_leftWeightSINNER(iter);
226  assert(leftWeight.size() == 6 &&
227  "Unexpected size of signal leftWeight, should be 6.");
228 
229  // do offset calibration if needed
230  if (m_leftCalibrationIter > 0) {
231  m_left_FT_offset_calibration_sum += leftWristForce;
232  m_left_weight_calibration_sum += leftWeight;
233  m_leftCalibrationIter--;
234  } else if (m_leftCalibrationIter == 0) {
235  m_left_FT_offset = m_left_FT_offset_calibration_sum / CALIB_ITER_TIME;
236  m_left_FT_offset -= m_left_weight_calibration_sum / CALIB_ITER_TIME;
237  m_leftCalibrationIter--;
238  }
239  // remove offset and hand weight
240  s = leftWristForce - m_left_FT_offset;
241 
242  if (m_removeWeight) {
243  s -= leftWeight;
244  }
245  return s;
246 }
247 
248 /* --- COMMANDS ---------------------------------------------------------- */
249 
250 void FtWristCalibration::setRightHandConf(const double &rightW,
251  const Vector &rightLeverArm) {
252  if (!m_initSucceeded) {
253  SEND_WARNING_STREAM_MSG(
254  "Cannot set right hand weight before initialization!");
255  return;
256  }
257  m_rightHandWeight << 0, 0, rightW, 0, 0, 0;
259 }
260 
261 void FtWristCalibration::setLeftHandConf(const double &leftW,
262  const Vector &leftLeverArm) {
263  if (!m_initSucceeded) {
264  SEND_WARNING_STREAM_MSG(
265  "Cannot set left hand weight before initialization!");
266  return;
267  }
268  m_leftHandWeight << 0, 0, leftW, 0, 0, 0;
270 }
271 
273  SEND_WARNING_STREAM_MSG(
274  "Sampling FT sensor for offset calibration... Robot should be in the "
275  "air, with horizontal hand.");
278  m_right_FT_offset_calibration_sum << 0, 0, 0, 0, 0, 0;
279  m_left_FT_offset_calibration_sum << 0, 0, 0, 0, 0, 0;
280 }
281 
282 void FtWristCalibration::setRemoveWeight(const bool &removeWeight) {
283  m_removeWeight = removeWeight;
284 }
285 
286 /* --- PROTECTED MEMBER METHODS
287  * ---------------------------------------------------------- */
288 /* ------------------------------------------------------------------- */
289 /* --- ENTITY -------------------------------------------------------- */
290 /* ------------------------------------------------------------------- */
291 
292 void FtWristCalibration::display(std::ostream &os) const {
293  os << "FtWristCalibration " << getName();
294  try {
295  getProfiler().report_all(3, os);
296  } catch (ExceptionSignal e) {
297  }
298 }
299 
300 } // namespace talos_balance
301 } // namespace sot
302 } // namespace dynamicgraph
sot_talos_balance.test.appli_admittance_end_effector.q
list q
Definition: appli_admittance_end_effector.py:30
dynamicgraph::sot::talos_balance::FtWristCalibration::setRightHandConf
void setRightHandConf(const double &rightW, const Vector &rightLeverArm)
Commands for setting the hand weight.
Definition: ft-wrist-calibration.cpp:250
dynamicgraph::sot::talos_balance::FtWristCalibration::m_rightCalibrationIter
int m_rightCalibrationIter
Definition: ft-wrist-calibration.hh:116
dynamicgraph::sot::talos_balance::FtWristCalibration::m_right_FT_offset
Vector6d m_right_FT_offset
Offset or bias to be removed from Right FT sensor.
Definition: ft-wrist-calibration.hh:121
dynamicgraph::sot::talos_balance::FtWristCalibration::m_initSucceeded
bool m_initSucceeded
true if the entity has been successfully initialized
Definition: ft-wrist-calibration.hh:133
sot_talos_balance.test.appli_admittance_end_effector.sot
sot
Definition: appli_admittance_end_effector.py:117
dynamicgraph::sot::talos_balance::FtWristCalibration::m_right_weight_calibration_sum
Vector6d m_right_weight_calibration_sum
Variable used during average computation of the weight.
Definition: ft-wrist-calibration.hh:129
dynamicgraph::sot::talos_balance::FtWristCalibration::init
void init(const std::string &robotRef)
Initialize.
Definition: ft-wrist-calibration.cpp:90
INNER_SIGNALS
#define INNER_SIGNALS
Definition: ft-wrist-calibration.cpp:30
CALIB_ITER_TIME
#define CALIB_ITER_TIME
Definition: ft-wrist-calibration.cpp:17
dynamicgraph::sot::talos_balance::FtWristCalibration::setLeftHandConf
void setLeftHandConf(const double &leftW, const Vector &leftLeverArm)
Definition: ft-wrist-calibration.cpp:261
dynamicgraph
Definition: treeview.dox:24
dynamicgraph::sot::talos_balance::FtWristCalibration::m_removeWeight
bool m_removeWeight
If true, the weight of the end effector is removed from the force.
Definition: ft-wrist-calibration.hh:143
dynamicgraph::sot::talos_balance::FtWristCalibration::m_right_FT_offset_calibration_sum
Vector6d m_right_FT_offset_calibration_sum
Variable used during average computation of the offset.
Definition: ft-wrist-calibration.hh:125
INPUT_SIGNALS
#define INPUT_SIGNALS
Definition: ft-wrist-calibration.cpp:29
ft-wrist-calibration.hh
OUTPUT_SIGNALS
#define OUTPUT_SIGNALS
Definition: ft-wrist-calibration.cpp:31
dynamicgraph::sot::talos_balance::FtWristCalibration::m_rightLeverArm
Vector3d m_rightLeverArm
right hand lever arm
Definition: ft-wrist-calibration.hh:139
dynamicgraph::sot::talos_balance::FtWristCalibration::m_left_FT_offset
Vector6d m_left_FT_offset
Offset or bias to be removed from Left FT sensor.
Definition: ft-wrist-calibration.hh:123
dynamicgraph::sot::talos_balance::FtWristCalibration::m_robot_util
RobotUtilShrPtr m_robot_util
Robot Util instance to get the sensor frame.
Definition: ft-wrist-calibration.hh:105
dynamicgraph::sot::talos_balance::FtWristCalibration::FtWristCalibration
FtWristCalibration(const std::string &name)
Definition: ft-wrist-calibration.cpp:43
dynamicgraph::sot::talos_balance::DEFINE_SIGNAL_OUT_FUNCTION
DEFINE_SIGNAL_OUT_FUNCTION(dq, dynamicgraph::Vector)
Definition: admittance-controller-end-effector.cpp:210
statistics.hh
dynamicgraph::sot::talos_balance::FtWristCalibration::m_left_weight_calibration_sum
Vector6d m_left_weight_calibration_sum
Variable used during average computation of the weight.
Definition: ft-wrist-calibration.hh:131
dynamicgraph::sot::talos_balance::FtWristCalibration::setRemoveWeight
void setRemoveWeight(const bool &removeWeight)
Set to true if you want to remove the weight from the force.
Definition: ft-wrist-calibration.cpp:282
dynamicgraph::sot::talos_balance::math::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: fwd.hh:36
dynamicgraph::sot::talos_balance::FtWristCalibration::m_rightSensorId
pinocchio::FrameIndex m_rightSensorId
Id of the force sensor frame.
Definition: ft-wrist-calibration.hh:111
dynamicgraph::sot::talos_balance::FtWristCalibration::m_leftHandWeight
Vector6d m_leftHandWeight
weight of the left hand
Definition: ft-wrist-calibration.hh:137
dynamicgraph::sot::talos_balance::FtWristCalibration::display
virtual void display(std::ostream &os) const
Definition: ft-wrist-calibration.cpp:292
dynamicgraph::sot::talos_balance::FtWristCalibration::m_rightHandWeight
Vector6d m_rightHandWeight
weight of the right hand
Definition: ft-wrist-calibration.hh:135
dynamicgraph::sot::talos_balance::FtWristCalibration::m_leftLeverArm
Vector3d m_leftLeverArm
left hand lever arm
Definition: ft-wrist-calibration.hh:141
dynamicgraph::sot::talos_balance::FtWristCalibration::m_leftSensorId
pinocchio::FrameIndex m_leftSensorId
Id of the joint of the end-effector.
Definition: ft-wrist-calibration.hh:113
dynamicgraph::sot::talos_balance::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
dynamicgraph::sot::talos_balance::DEFINE_SIGNAL_INNER_FUNCTION
DEFINE_SIGNAL_INNER_FUNCTION(w_force, dynamicgraph::Vector)
Definition: admittance-controller-end-effector.cpp:145
dynamicgraph::sot::talos_balance::FtWristCalibration::m_leftCalibrationIter
int m_leftCalibrationIter
Definition: ft-wrist-calibration.hh:119
dynamicgraph::sot::talos_balance::FtWristCalibration::m_model
pinocchio::Model m_model
Pinocchio robot model.
Definition: ft-wrist-calibration.hh:107
sot_talos_balance.talos.ft_wrist_calibration_conf.rightLeverArm
list rightLeverArm
Definition: ft_wrist_calibration_conf.py:5
dynamicgraph::sot::talos_balance::EntityClassName
AdmittanceControllerEndEffector EntityClassName
Definition: admittance-controller-end-effector.cpp:46
sot_talos_balance.test.appli_dcm_zmp_control.name
name
Definition: appli_dcm_zmp_control.py:298
sot_talos_balance.talos.ft_wrist_calibration_conf.leftLeverArm
list leftLeverArm
Definition: ft_wrist_calibration_conf.py:4
dynamicgraph::sot::talos_balance::FtWristCalibration::calibrateWristSensor
void calibrateWristSensor()
Definition: ft-wrist-calibration.cpp:272
dynamicgraph::sot::talos_balance::FtWristCalibration::m_data
pinocchio::Data * m_data
Pinocchio robot data.
Definition: ft-wrist-calibration.hh:109
dynamicgraph::sot::talos_balance::FtWristCalibration::m_left_FT_offset_calibration_sum
Vector6d m_left_FT_offset_calibration_sum
Variable used during average computation of the offset.
Definition: ft-wrist-calibration.hh:127