iCub-main
observerThread.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 RobotCub Consortium, European Commission FP6 Project IST-004370
3  * Author: Marco Randazzo, Matteo Fumagalli
4  * email: marco.randazzo@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17 */
18 
19 #ifndef OBSERVER_THREAD
20 #define OBSERVER_THREAD
21 
22 #include <yarp/os/all.h>
23 #include <yarp/sig/all.h>
24 #include <yarp/dev/all.h>
25 #include <iCub/ctrl/math.h>
27 #include <iCub/iDyn/iDyn.h>
28 #include <iCub/iDyn/iDynBody.h>
30 
31 #include <iostream>
32 #include <iomanip>
33 #include <cstring>
34 #include <list>
35 
36 using namespace yarp::os;
37 using namespace yarp::sig;
38 using namespace yarp::math;
39 using namespace yarp::dev;
40 using namespace iCub::ctrl;
41 using namespace iCub::iDyn;
42 using namespace std;
43 
44 constexpr int8_t MAX_JN = 12;
45 constexpr int8_t MAX_FILTER_ORDER = 6;
46 constexpr float_t SKIN_EVENTS_TIMEOUT = 0.2F; // max time (in sec) a contact is kept without reading anything from the skin events port
47 
50 
51 // struct version
52 // {
53 // int head_version;
54 // int legs_version;
55 // };
56 
57 // filter
58 double lpf_ord1_3hz(double input, int j);
59 
61 {
62  public:
63  double timestamp;
64  Vector all_q_up, all_dq_up, all_d2q_up;
65  Vector all_q_low, all_dq_low, all_d2q_low;
66 
67  Vector ft_arm_left;
68  Vector ft_arm_right;
69  Vector ft_leg_left;
70  Vector ft_leg_right;
71  Vector ft_foot_left;
72  Vector ft_foot_right;
73 
74  Vector inertial_w0,inertial_dw0,inertial_d2p0;
75 
76  bool iCub_not_moving{};
77 
79  {
80  timestamp=0;
81  ft_arm_left=0;
82  ft_arm_right=0;
83  ft_leg_left=0;
84  ft_leg_right=0;
85  ft_foot_left=0;
86  ft_foot_right=0;
87  inertial_w0.resize(3); inertial_w0.zero();
88  inertial_dw0.resize(3); inertial_dw0.zero();
89  inertial_d2p0.resize(3); inertial_d2p0.zero();
90  all_q_up.resize(3+7+7); all_q_up.zero();
91  all_dq_up.resize(3+7+7); all_dq_up.zero();
92  all_d2q_up.resize(3+7+7); all_d2q_up.zero();
93  all_q_low.resize(3+6+6); all_q_low.zero();
94  all_dq_low.resize(3+6+6); all_dq_low.zero();
95  all_d2q_low.resize(3+6+6); all_d2q_low.zero();
96  ft_arm_left.resize(6); ft_arm_left.zero();
97  ft_arm_right.resize(6); ft_arm_right.zero();
98  ft_leg_left.resize(6); ft_leg_left.zero();
99  ft_leg_right.resize(6); ft_leg_right.zero();
100  ft_foot_left.resize(6); ft_foot_left.zero();
101  ft_foot_right.resize(6); ft_foot_right.zero();
102  }
103 
104  Vector get_q_head() {return all_q_up.subVector(0,2);}
105  Vector get_dq_head() {return all_dq_up.subVector(0,2);}
106  Vector get_d2q_head() {return all_d2q_up.subVector(0,2);}
107  Vector get_q_larm() {return all_q_up.subVector(3,9);}
108  Vector get_dq_larm() {return all_dq_up.subVector(3,9);}
109  Vector get_d2q_larm() {return all_d2q_up.subVector(3,9);}
110  Vector get_q_rarm() {return all_q_up.subVector(10,16);}
111  Vector get_dq_rarm() {return all_dq_up.subVector(10,16);}
112  Vector get_d2q_rarm() {return all_d2q_up.subVector(10,16);}
113  Vector get_q_torso() {return all_q_low.subVector(0,2);}
114  Vector get_dq_torso() {return all_dq_low.subVector(0,2);}
115  Vector get_d2q_torso() {return all_d2q_low.subVector(0,2);}
116  Vector get_q_lleg() {return all_q_low.subVector(3,8);}
117  Vector get_dq_lleg() {return all_dq_low.subVector(3,8);}
118  Vector get_d2q_lleg() {return all_d2q_low.subVector(3,8);}
119  Vector get_q_rleg() {return all_q_low.subVector(9,14);}
120  Vector get_dq_rleg() {return all_dq_low.subVector(9,14);}
121  Vector get_d2q_rleg() {return all_d2q_low.subVector(9,14);}
122 
123  bool checkIcubNotMoving();
124  void dump (FILE* f);
125 
126 };
127 
128 // class inverseDynamics: class for reading from Vrow and providing FT on an output port
129 class inverseDynamics: public PeriodicThread
130 {
131 public:
134  bool dummy_ft;
140 
141 private:
142  string robot_name;
143  string local_name;
144  bool autoconnect;
145  version_tag icub_type;
146 
147  PolyDriver *ddAL;
148  PolyDriver *ddAR;
149  PolyDriver *ddH;
150  IEncoders *iencs_arm_left;
151  IEncoders *iencs_arm_right;
152  IEncoders *iencs_head;
153  IInteractionMode *iint_arm_left;
154  IInteractionMode *iint_arm_right;
155  IInteractionMode *iint_head;
156  IControlMode *icmd_arm_left;
157  IControlMode *icmd_arm_right;
158  IControlMode *icmd_head;
159  ISixAxisForceTorqueSensors* m_left_arm_FT{nullptr};
160  ISixAxisForceTorqueSensors* m_right_arm_FT{nullptr};
161  ISixAxisForceTorqueSensors* m_left_leg_FT{nullptr};
162  ISixAxisForceTorqueSensors* m_right_leg_FT{nullptr};
163 
164 
165  PolyDriver *ddLL;
166  PolyDriver *ddLR;
167  PolyDriver *ddT;
168  IEncoders *iencs_leg_left;
169  IEncoders *iencs_leg_right;
170  IEncoders *iencs_torso;
171  IInteractionMode *iint_leg_left;
172  IInteractionMode *iint_leg_right;
173  IInteractionMode *iint_torso;
174  IControlMode *icmd_leg_left;
175  IControlMode *icmd_leg_right;
176  IControlMode *icmd_torso;
177 
178  string part;
179 
180  const long double zero_sens_tolerance;
181  double skinContactsTimestamp;
182 
183  //input ports
184  BufferedPort<Vector> *port_inertial_thread;
185  BufferedPort<iCub::skinDynLib::skinContactList> *port_skin_contacts;
186 
187  //output ports
188  BufferedPort<Bottle> *port_RATorques;
189  BufferedPort<Bottle> *port_RLTorques;
190  BufferedPort<Bottle> *port_RWTorques;
191  BufferedPort<Bottle> *port_LATorques;
192  BufferedPort<Bottle> *port_LLTorques;
193  BufferedPort<Bottle> *port_LWTorques;
194  BufferedPort<Bottle> *port_TOTorques;
195  BufferedPort<Bottle> *port_HDTorques;
196  BufferedPort<Vector> *port_external_wrench_RA;
197  BufferedPort<Vector> *port_external_wrench_LA;
198  BufferedPort<Vector> *port_external_wrench_RL;
199  BufferedPort<Vector> *port_external_wrench_LL;
200  BufferedPort<Vector> *port_external_wrench_RF;
201  BufferedPort<Vector> *port_external_wrench_LF;
202  BufferedPort<Vector> *port_external_cartesian_wrench_RA;
203  BufferedPort<Vector> *port_external_cartesian_wrench_LA;
204  BufferedPort<Vector> *port_external_cartesian_wrench_RL;
205  BufferedPort<Vector> *port_external_cartesian_wrench_LL;
206  BufferedPort<Vector> *port_external_cartesian_wrench_RF;
207  BufferedPort<Vector> *port_external_cartesian_wrench_LF;
208  BufferedPort<Vector> *port_sensor_wrench_RL;
209  BufferedPort<Vector> *port_sensor_wrench_LL;
210  BufferedPort<Vector> *port_model_wrench_RL;
211  BufferedPort<Vector> *port_model_wrench_LL;
212  BufferedPort<Vector> *port_external_wrench_TO;
213  BufferedPort<Vector> *port_com_all;
214  BufferedPort<Vector> *port_com_all_foot;
215  BufferedPort<Vector> *port_com_lb;
216  BufferedPort<Vector> *port_com_ub;
217  BufferedPort<Vector> *port_com_la;
218  BufferedPort<Vector> *port_com_ra;
219  BufferedPort<Vector> *port_com_ll;
220  BufferedPort<Vector> *port_com_rl;
221  BufferedPort<Vector> *port_com_hd;
222  BufferedPort<Vector> *port_com_to;
223  BufferedPort<Vector> *port_monitor;
224  BufferedPort<iCub::skinDynLib::skinContactList> *port_contacts;
225  BufferedPort<Vector> *port_dumpvel;
226  BufferedPort<Vector> *port_COM_vel;
227  BufferedPort<Matrix> *port_COM_Jacobian;
228  BufferedPort<Vector> *port_all_velocities;
229  BufferedPort<Vector> *port_all_positions;
230  BufferedPort<Matrix> *port_root_position_mat;
231  BufferedPort<Vector> *port_root_position_vec;
232 
233  // ports outputing the external dynamics seen at the F/T sensor
234  BufferedPort<Vector> *port_external_ft_arm_left;
235  BufferedPort<Vector> *port_external_ft_arm_right;
236  BufferedPort<Vector> *port_external_ft_leg_left;
237  BufferedPort<Vector> *port_external_ft_leg_right;
238  yarp::os::Stamp timestamp;
239 
240  bool first;
241  thread_status_enum thread_status;
242 
243  AWLinEstimator *InertialEst;
244  AWLinEstimator *linEstUp;
245  AWQuadEstimator *quadEstUp;
246  AWLinEstimator *linEstLow;
247  AWQuadEstimator *quadEstLow;
248 
249  int ctrlJnt;
250  int allJnt;
251  iCubWholeBody *icub;
252  iCubWholeBody *icub_sens;
253  iCubStatus current_status;
254 
255  size_t status_queue_size;
256  list<iCubStatus> previous_status;
257  list<iCubStatus> not_moving_status;
258 
259  Vector encoders_arm_left;
260  Vector encoders_arm_right;
261  Vector encoders_head;
262 
263  Vector encoders_leg_left;
264  Vector encoders_leg_right;
265  Vector encoders_torso;
266 
267  Vector Fend,Muend;
268  Vector F_LArm, F_RArm;
269  Vector F_iDyn_LArm, F_iDyn_RArm, Offset_LArm, Offset_RArm;
270  Vector F_ext_left_arm, F_ext_right_arm, F_ext_torso;
271  Vector F_ext_cartesian_left_arm, F_ext_cartesian_right_arm;
272  Vector F_ext_left_leg, F_ext_right_leg;
273  Vector F_ext_left_foot, F_ext_right_foot;
274  Vector F_sns_left_leg, F_sns_right_leg;
275  Vector F_mdl_left_leg, F_mdl_right_leg;
276  Vector F_ext_cartesian_left_leg, F_ext_cartesian_right_leg;
277  Vector F_ext_cartesian_left_foot, F_ext_cartesian_right_foot;
278  Vector F_LLeg, F_RLeg;
279  Vector F_LFoot, F_RFoot;
280  Vector F_iDyn_LLeg, F_iDyn_RLeg, Offset_LLeg, Offset_RLeg;
281  Vector F_iDyn_LFoot, F_iDyn_RFoot, Offset_LFoot, Offset_RFoot;
282  Matrix F_sens_up, F_sens_low, F_ext_up, F_ext_low;
283  Vector F_ext_sens_right_arm, F_ext_sens_left_arm; // external wrench seen at the F/T sensors
284  Vector F_ext_sens_right_leg, F_ext_sens_left_leg; // external wrench seen at the F/T sensors
285 
288 
289 
290  // icub model
291  int comp;
292  Matrix FM_sens_up,FM_sens_low;
293 
294  //COM Jacobian Matrix
295  Matrix com_jac;
296 
297  Vector evalVelUp(const Vector &x);
298  Vector evalVelLow(const Vector &x);
299  Vector eval_domega(const Vector &x);
300  Vector evalAccUp(const Vector &x);
301  Vector evalAccLow(const Vector &x);
302 
303  void init_upper();
304  void init_lower();
305  void setUpperMeasure(bool _init=false);
306  void setLowerMeasure(bool _init=false);
307 
308  void addSkinContacts();
309 
310 public:
311  inverseDynamics(int _rate, PolyDriver *_ddAL, PolyDriver *_ddAR,
312  PolyDriver *_ddH, PolyDriver *_ddLL, PolyDriver *_ddLR,
313  PolyDriver *_ddT, string _robot_name, string _local_name,
314  version_tag icub_type, bool _autoconnect=false,
315  ISixAxisForceTorqueSensors* m_left_arm_FT = nullptr,
316  ISixAxisForceTorqueSensors* m_right_arm_FT = nullptr,
317  ISixAxisForceTorqueSensors* m_left_leg_FT = nullptr,
318  ISixAxisForceTorqueSensors* m_right_leg_FT = nullptr);
319  bool threadInit() override;
320  void setStiffMode();
322  {
323  return thread_status;
324  }
325 
326  void run() override;
327  void threadRelease() override;
328  void closePort(Contactable *_port);
329  void writeTorque(Vector _values, int _address, BufferedPort<Bottle> *_port);
330  template <class T> void broadcastData(T& _values, BufferedPort<T> *_port);
331  void calibrateOffset(calib_enum calib_code=CALIB_ALL);
332  bool readAndUpdate(bool waitMeasure=false, bool _init=false);
333  bool getLowerEncodersSpeedAndAcceleration();
334  bool getUpperEncodersSpeedAndAcceleration();
335  void setZeroJntAngVelAcc();
336  void sendMonitorData();
337  void sendVelAccData();
338 
339 };
340 
341 #endif
342 
343 
Vector get_dq_torso()
Vector ft_leg_left
Vector ft_arm_left
Vector get_q_larm()
Vector ft_foot_right
Vector get_dq_larm()
Vector get_dq_rarm()
Vector ft_foot_left
Vector inertial_d2p0
Vector all_d2q_low
Vector get_d2q_rleg()
Vector ft_arm_right
Vector all_d2q_up
Vector get_q_torso()
Vector get_dq_rleg()
Vector get_q_lleg()
Vector get_d2q_rarm()
Vector get_q_rleg()
Vector get_d2q_head()
Vector get_q_rarm()
Vector get_d2q_lleg()
Vector get_d2q_larm()
Vector ft_leg_right
Vector get_dq_lleg()
double timestamp
Vector get_d2q_torso()
Vector get_q_head()
Vector get_dq_head()
Adaptive window linear fitting to estimate the first derivative.
Adaptive window quadratic fitting to estimate the second derivative.
A class for connecting UpperTorso and LowerTorso of the iCub, then getting the WholeBody in the dynam...
Definition: iDynBody.h:1472
Class representing a list of external contacts.
Class representing a list of external contacts acting on the iCub' skin.
thread_status_enum getThreadStatus()
@ _init
Definition: dc1394thread.h:15
thread_status_enum
Definition: gravityThread.h:41
double lpf_ord1_3hz(double input, int j)
constexpr int8_t MAX_FILTER_ORDER
@ STATUS_OK
@ STATUS_DISCONNECTED
constexpr int8_t MAX_JN
calib_enum
@ CALIB_LEGS
@ CALIB_ARMS
@ CALIB_FEET
@ CALIB_ALL
constexpr float_t SKIN_EVENTS_TIMEOUT