sot-talos-balance  2.0.5
Collection of dynamic-graph entities aimed at implementing balance control on talos.
ft-calibration.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019
3  * LAAS-CNRS
4  * F. Bailly
5  * T. Flayols
6  */
7 
8 #include <dynamic-graph/all-commands.h>
9 #include <dynamic-graph/factory.h>
10 
11 #include <sot/core/debug.hh>
12 #include <sot/core/stop-watch.hh>
15 
16 #define CALIB_ITER_TIME \
17  1000 // Iteration needed for sampling and averaging the FT sensors while
18  // calibrating
19 
20 namespace dynamicgraph {
21 namespace sot {
22 namespace talos_balance {
23 namespace dynamicgraph = ::dynamicgraph;
24 using namespace dynamicgraph;
25 using namespace dynamicgraph::command;
26 using namespace dg::sot::talos_balance;
27 
28 #define INPUT_SIGNALS m_right_foot_force_inSIN << m_left_foot_force_inSIN
29 #define OUTPUT_SIGNALS m_right_foot_force_outSOUT << m_left_foot_force_outSOUT
30 
33 typedef FtCalibration EntityClassName;
34 
35 /* --- DG FACTORY ---------------------------------------------------- */
36 DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(FtCalibration, "FtCalibration");
37 
38 /* ------------------------------------------------------------------- */
39 /* --- CONSTRUCTION -------------------------------------------------- */
40 /* ------------------------------------------------------------------- */
42  : Entity(name),
43  CONSTRUCT_SIGNAL_IN(right_foot_force_in, dynamicgraph::Vector),
44  CONSTRUCT_SIGNAL_IN(left_foot_force_in, dynamicgraph::Vector),
45  CONSTRUCT_SIGNAL_OUT(right_foot_force_out, dynamicgraph::Vector,
46  m_right_foot_force_inSIN),
47  CONSTRUCT_SIGNAL_OUT(left_foot_force_out, dynamicgraph::Vector,
48  m_left_foot_force_inSIN),
49  m_robot_util(RefVoidRobotUtil()),
50  m_initSucceeded(false) {
51  Entity::signalRegistration(INPUT_SIGNALS << OUTPUT_SIGNALS);
52 
53  /* Commands. */
54  addCommand("init",
55  makeCommandVoid1(*this, &FtCalibration::init,
56  docCommandVoid1("Initialize the entity.",
57  "Robot reference (string)")));
58  addCommand("setRightFootWeight",
59  makeCommandVoid1(
61  docCommandVoid1(
62  "Set the weight of the right foot underneath the sensor",
63  "Vector of default forces in Newton")));
64  addCommand("setLeftFootWeight",
65  makeCommandVoid1(
67  docCommandVoid1(
68  "Set the weight of the left foot underneath the sensor",
69  "Vector of default forces in Newton")));
70  addCommand("calibrateFeetSensor",
71  makeCommandVoid0(*this, &FtCalibration::calibrateFeetSensor,
72  docCommandVoid0("Calibrate the feet senors")));
73 }
74 
75 void FtCalibration::init(const std::string &robotRef) {
76  dgADD_OSTREAM_TO_RTLOG(std::cout);
77  std::string localName(robotRef);
78  m_initSucceeded = true;
79  if (!isNameInRobotUtil(localName)) {
80  m_robot_util = createRobotUtil(localName);
81  } else {
82  m_robot_util = getRobotUtil(localName);
83  }
84  m_right_FT_offset << 0, 0, 0, 0, 0, 0;
85  m_left_FT_offset << 0, 0, 0, 0, 0, 0;
86  m_right_FT_offset_calibration_sum << 0, 0, 0, 0, 0, 0;
87  m_left_FT_offset_calibration_sum << 0, 0, 0, 0, 0, 0;
88  SEND_MSG("Entity Initialized", MSG_TYPE_INFO);
89 }
90 
91 /* ------------------------------------------------------------------- */
92 /* --- SIGNALS ------------------------------------------------------- */
93 /* ------------------------------------------------------------------- */
94 
96  if (!m_initSucceeded) {
97  SEND_WARNING_STREAM_MSG("Cannot compute signal sum before initialization!");
98  return s;
99  }
100  if (s.size() != 6) s.resize(6);
101 
102  const Vector &right_foot_force = m_right_foot_force_inSIN(iter);
103 
104  assert(right_foot_force.size() == 6 &&
105  "Unexpected size of signal right_foot_force_in, should be 6.");
106 
107  // do offset calibration if needed
108  if (m_right_calibration_iter > 0) {
109  m_right_FT_offset_calibration_sum += right_foot_force;
110  m_right_calibration_iter--;
111  } else if (m_right_calibration_iter == 0) {
112  SEND_INFO_STREAM_MSG("Calibrating ft sensors...");
113  m_right_FT_offset =
114  m_right_FT_offset_calibration_sum / CALIB_ITER_TIME; // todo copy
115  }
116 
117  // remove offset and foot weight
118  s = right_foot_force - m_left_foot_weight - m_right_FT_offset;
119 
120  return s;
121 }
122 
124  if (!m_initSucceeded) {
125  SEND_WARNING_STREAM_MSG("Cannot compute signal sum before initialization!");
126  return s;
127  }
128  if (s.size() != 6) s.resize(6);
129 
130  const Vector &left_foot_force = m_left_foot_force_inSIN(iter);
131 
132  assert(left_foot_force.size() == 6 &&
133  "Unexpected size of signal left_foot_force_in, should be 6.");
134 
135  // do offset calibration if needed
136  if (m_left_calibration_iter > 0) {
137  m_left_FT_offset_calibration_sum += left_foot_force;
138  m_left_calibration_iter--;
139  } else if (m_left_calibration_iter == 0) {
140  m_left_FT_offset = m_left_FT_offset_calibration_sum / CALIB_ITER_TIME;
141  }
142  // remove offset and foot weight
143  s = left_foot_force - m_left_foot_weight - m_left_FT_offset;
144 
145  return s;
146 }
147 /* --- COMMANDS ---------------------------------------------------------- */
148 
149 void FtCalibration::setRightFootWeight(const double &rightW) {
150  if (!m_initSucceeded) {
151  SEND_WARNING_STREAM_MSG(
152  "Cannot set right foot weight before initialization!");
153  return;
154  }
155  m_right_foot_weight << 0, 0, rightW, 0, 0, 0;
156 }
157 
158 void FtCalibration::setLeftFootWeight(const double &leftW) {
159  if (!m_initSucceeded) {
160  SEND_WARNING_STREAM_MSG(
161  "Cannot set left foot weight before initialization!");
162  return;
163  }
164  m_left_foot_weight << 0, 0, leftW, 0, 0, 0;
165 }
166 
168  SEND_WARNING_STREAM_MSG(
169  "Sampling FT sensor for offset calibration... Robot should be in the "
170  "air, with horizontal feet.");
173  m_right_FT_offset_calibration_sum << 0, 0, 0, 0, 0, 0;
174  m_left_FT_offset_calibration_sum << 0, 0, 0, 0, 0, 0;
175 }
176 
177 /* --- PROTECTED MEMBER METHODS
178  * ---------------------------------------------------------- */
179 /* ------------------------------------------------------------------- */
180 /* --- ENTITY -------------------------------------------------------- */
181 /* ------------------------------------------------------------------- */
182 
183 void FtCalibration::display(std::ostream &os) const {
184  os << "FtCalibration " << getName();
185  try {
186  getProfiler().report_all(3, os);
187  } catch (ExceptionSignal e) {
188  }
189 }
190 
191 } // namespace talos_balance
192 } // namespace sot
193 } // namespace dynamicgraph
dynamicgraph::sot::talos_balance::FtCalibration::m_left_FT_offset
Vector6d m_left_FT_offset
Offset or bias to be removed from Right FT sensor.
Definition: ft-calibration.hh:96
dynamicgraph::sot::talos_balance::FtCalibration::m_right_FT_offset
Vector6d m_right_FT_offset
Definition: ft-calibration.hh:94
CALIB_ITER_TIME
#define CALIB_ITER_TIME
Definition: ft-calibration.cpp:16
sot_talos_balance.test.appli_admittance_end_effector.sot
sot
Definition: appli_admittance_end_effector.py:117
dynamicgraph::sot::talos_balance::FtCalibration::m_left_calibration_iter
int m_left_calibration_iter
Definition: ft-calibration.hh:90
dynamicgraph
Definition: treeview.dox:24
dynamicgraph::sot::talos_balance::FtCalibration::FtCalibration
FtCalibration(const std::string &name)
Definition: ft-calibration.cpp:41
ft-calibration.hh
dynamicgraph::sot::talos_balance::DEFINE_SIGNAL_OUT_FUNCTION
DEFINE_SIGNAL_OUT_FUNCTION(dq, dynamicgraph::Vector)
Definition: admittance-controller-end-effector.cpp:210
statistics.hh
OUTPUT_SIGNALS
#define OUTPUT_SIGNALS
Definition: ft-calibration.cpp:29
dynamicgraph::sot::talos_balance::FtCalibration::m_right_FT_offset_calibration_sum
Vector6d m_right_FT_offset_calibration_sum
Offset or bias to be removed from Left FT sensor.
Definition: ft-calibration.hh:97
dynamicgraph::sot::talos_balance::FtCalibration::calibrateFeetSensor
void calibrateFeetSensor()
Definition: ft-calibration.cpp:167
dynamicgraph::sot::talos_balance::math::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: fwd.hh:36
dynamicgraph::sot::talos_balance::FtCalibration::m_robot_util
RobotUtilShrPtr m_robot_util
Definition: ft-calibration.hh:86
INPUT_SIGNALS
#define INPUT_SIGNALS
Definition: ft-calibration.cpp:28
dynamicgraph::sot::talos_balance::FtCalibration::m_initSucceeded
bool m_initSucceeded
Definition: ft-calibration.hh:102
dynamicgraph::sot::talos_balance::FtCalibration::setRightFootWeight
void setRightFootWeight(const double &rightW)
Commands for setting the feet weight.
Definition: ft-calibration.cpp:149
dynamicgraph::sot::talos_balance::FtCalibration::display
virtual void display(std::ostream &os) const
Definition: ft-calibration.cpp:183
dynamicgraph::sot::talos_balance::FtCalibration::m_right_calibration_iter
int m_right_calibration_iter
Definition: ft-calibration.hh:87
dynamicgraph::sot::talos_balance::FtCalibration::m_left_foot_weight
Vector6d m_left_foot_weight
Definition: ft-calibration.hh:106
dynamicgraph::sot::talos_balance::FtCalibration::init
void init(const std::string &robotRef)
Initialize.
Definition: ft-calibration.cpp:75
dynamicgraph::sot::talos_balance::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
dynamicgraph::sot::talos_balance::FtCalibration::setLeftFootWeight
void setLeftFootWeight(const double &leftW)
Definition: ft-calibration.cpp:158
dynamicgraph::sot::talos_balance::FtCalibration::m_right_foot_weight
Vector6d m_right_foot_weight
true if the entity has been successfully initialized
Definition: ft-calibration.hh:104
dynamicgraph::sot::talos_balance::EntityClassName
AdmittanceControllerEndEffector EntityClassName
Definition: admittance-controller-end-effector.cpp:46
dynamicgraph::sot::talos_balance::FtCalibration::m_left_FT_offset_calibration_sum
Vector6d m_left_FT_offset_calibration_sum
Definition: ft-calibration.hh:99
sot_talos_balance.test.appli_dcm_zmp_control.name
name
Definition: appli_dcm_zmp_control.py:298