NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cap_gait.h
1 // Capture step gait
2 // File: cap_gait.h
3 // Author: Philipp Allgeuer <pallgeuer@ais.uni-bonn.de>
4 
5 // Ensure header is only included once
6 #ifndef CAP_GAIT_H
7 #define CAP_GAIT_H
8 
9 // Includes - Local
10 #include <cap_gait/cap_gait_config.h>
11 #include <cap_gait/cap_com_filter.h>
12 
13 // Includes - Gait
14 #include <gait/gait_common.h>
15 #include <gait/gait_engine.h>
16 #include <gait/util/gait_joint_pose.h>
17 #include <gait/util/gait_inverse_pose.h>
18 #include <gait/util/gait_abstract_pose.h>
19 
20 // Includes - Misc
21 #include <plot_msgs/plot_manager.h>
22 #include <rc_utils/ew_integrator.h>
23 #include <rc_utils/mean_filter.h>
24 #include <rc_utils/wlbf_filter.h>
25 
26 // Includes - Library
27 #include <Eigen/Core>
28 
29 // Includes - Contrib [These are included last because they potentially contain Qt includes that break the Boost signals library... (e.g. used by TF)]
30 #include <cap_gait/contrib/RobotModel.h>
31 #include <cap_gait/contrib/RobotModelVis.h>
32 #include <cap_gait/contrib/LimpModel.h>
33 #include <cap_gait/contrib/Limp.h>
34 #include <cap_gait/contrib/ComFilter.h>
35 
41 namespace cap_gait
42 {
48  class CapGait : public gait::GaitEngine
49  {
50  public:
51  //
52  // Public functions
53  //
54 
55  // Constructor
56  CapGait();
57 
58  // Reset function
59  virtual void reset();
60 
61  // Step function
62  virtual void step();
63 
64  // Halt pose function
65  virtual void updateHaltPose();
66 
67  // Set odometry function
68  virtual void setOdometry(double posX, double posY, double rotZ);
69 
70  // Update odometry function
71  virtual void updateOdometry();
72 
73  // Handle joystick button function
74  virtual void handleJoystickButton(int button);
75 
76  private:
77  //
78  // Structs
79  //
80 
81  // Common motion data struct
82  struct CommonMotionData
83  {
84  // Constructor
85  CommonMotionData() // All data that is initialised here is just to avoid divisions by zero etc in case a bug causes an uninitialised value in this struct to be used by accident
86  : gcvX(0.0)
87  , gcvY(0.0)
88  , gcvZ(0.0)
89  , absGcvX(0.0)
90  , absGcvY(0.0)
91  , absGcvZ(0.0)
92  , gaitPhase(0.0)
93  , oppGaitPhase(M_PI)
94  , limbPhase(0.0)
95  , absPhase(0.0)
96  , doubleSupportPhase(0.1)
97  , swingStartPhase(0.0)
98  , swingStopPhase(M_PI)
99  , suppTransStartPhase(0.0)
100  , suppTransStopPhase(doubleSupportPhase)
101  , liftingPhaseLen(M_PI - doubleSupportPhase)
102  , suppPhaseLen(M_PI + doubleSupportPhase)
103  , nonSuppPhaseLen(M_2PI - suppPhaseLen)
104  , sinusoidPhaseLen(M_PI)
105  , linearPhaseLen(M_PI)
106  , swingAngle(0.0)
107  {}
108 
109  // Gait command vector variables
110  double gcvX;
111  double gcvY;
112  double gcvZ;
113  double absGcvX;
114  double absGcvY;
115  double absGcvZ;
116 
117  // Gait phase variables
118  double gaitPhase;
119  double oppGaitPhase;
120  double limbPhase;
121  double absPhase;
122 
123  // Phase marks
124  double doubleSupportPhase;
125  double swingStartPhase;
126  double swingStopPhase;
127  double suppTransStartPhase;
128  double suppTransStopPhase;
129 
130  // Extra swing variables
131  double liftingPhaseLen;
132  double suppPhaseLen;
133  double nonSuppPhaseLen;
134  double sinusoidPhaseLen;
135  double linearPhaseLen;
136 
137  // Swing angle
138  double swingAngle;
139  };
140 
141  //
142  // Private functions
143  //
144 
145  // Reset walking function (a lighter version of reset(), used for internal resets when starting and stopping walking)
146  void resetWalking(bool walking, const Eigen::Vector3d& gcvBias);
147 
148  // Input processing
149  void processInputs();
150  void updateRobot(const Eigen::Vector3d& gcvBias);
151 
152  // Helper functions
153  CommonMotionData calcCommonMotionData(bool isFirst) const;
154 
155  // Motion functions
156  void abstractLegMotion(gait::AbstractLegPose& leg);
157  void abstractArmMotion(gait::AbstractArmPose& arm);
158  void inverseLegMotion(gait::InverseLegPose& leg);
159 
160  // Coercion functions
161  void coerceAbstractPose(gait::AbstractPose& pose);
162  void coerceAbstractArmPose(gait::AbstractArmPose& arm);
163  void coerceAbstractLegPose(gait::AbstractLegPose& leg);
164 
165  // Output processing
166  void updateOutputs();
167 
168  // Blending functions
169  void resetBlending(double b = USE_HALT_POSE);
170  void setBlendTarget(double target, double phaseTime);
171  double blendFactor();
172 
173  // Motion stance functions
174  void resetMotionStance();
175 
176  //
177  // Gait variables
178  //
179 
180  // Constants
181  const std::string CONFIG_PARAM_PATH;
182  static const double USE_HALT_POSE = 1.0;
183  static const double USE_CALC_POSE = 0.0;
184 
185  // Gait configuration struct
186  CapConfig config;
187 
188  // Pose variables
189  gait::JointPose m_jointPose; // Joint representation of the pose to command in a step
190  gait::JointPose m_jointHaltPose; // Joint representation of the gait halt pose
191  gait::JointPose m_lastJointPose; // The last joint pose to have been commanded during walking
192  gait::InversePose m_inversePose; // Inverse representation of the pose to command in a step
193  gait::AbstractPose m_abstractPose; // Abstract representation of the pose to command in a step
194  gait::AbstractPose m_abstractHaltPose; // Abstract representation of the gait halt pose
195 
196  // Gait command vector variables
197  Eigen::Vector3d m_gcv; // Gait command velocity vector (slope-limited command velocities actually followed by the gait engine)
198  Eigen::Vector3d m_gcvInput; // Gait command velocity vector input (raw velocities commanded by the gait motion module)
199  rc_utils::GolayDerivative<Eigen::Vector3d, 1, 5, Eigen::aligned_allocator<Eigen::Vector3d> > m_gcvDeriv; // Derivative filter for the GCV to calculate the gait acceleration
200  rc_utils::MeanFilter m_gcvAccSmoothX;
201  rc_utils::MeanFilter m_gcvAccSmoothY;
202  rc_utils::MeanFilter m_gcvAccSmoothZ;
203  Eigen::Vector3d m_gcvAcc;
204 
205  // Gait flags
206  bool m_walk; // True if the gait engine should walk, false if it should not walk
207  bool m_walking; // True if the gait engine is currently producing a walking motion output
208  bool m_leftLegFirst; // True if the left leg should be the first to be lifted when starting walking
209 
210  // Step motion variables
211  double m_gaitPhase; // Current walking phase of the gait motion
212 
213  // Blending variables
214  bool m_blending;
215  double m_b_current;
216  double m_b_initial;
217  double m_b_target;
218  double m_blendPhase;
219  double m_blendEndPhase;
220 
221  // Motion stance variables
222  double m_motionLegAngleXFact; // Interpolation factor between feet narrow (= 0) and feet normal (= 1) legAngleX values
223 
224  //
225  // Basic feedback variables
226  //
227 
228  // Basic feedback filters
229  rc_utils::MeanFilter fusedXFeedFilter;
230  rc_utils::MeanFilter fusedYFeedFilter;
231  rc_utils::WLBFFilter dFusedXFeedFilter;
232  rc_utils::WLBFFilter dFusedYFeedFilter;
233  rc_utils::MeanFilter iFusedXFeedFilter;
234  rc_utils::MeanFilter iFusedYFeedFilter;
235  rc_utils::WLBFFilter gyroXFeedFilter;
236  rc_utils::WLBFFilter gyroYFeedFilter;
237 
238  // Integrators
239  rc_utils::EWIntegrator iFusedXFeedIntegrator;
240  rc_utils::EWIntegrator iFusedYFeedIntegrator;
241  config_server::Parameter<bool> m_resetIntegrators; // Rising edge triggered flag to reset any integrated or learned values in the gait that are not necessarily reset during start/stop of walking
242  config_server::Parameter<bool> m_saveIFeedToHaltPose; // Rising edge triggered flag to save the current integrated feedback values as offsets to the halt pose (only the ones in current use)
243  bool m_savedLegIFeed; // Flag that specifies within a cycle whether the integrated leg feedback has already been saved
244  bool m_savedArmIFeed; // Flag that specifies within a cycle whether the integrated arm feedback has already been saved
245  double iFusedXLastTime; // The last in.timestamp where iFusedX made a non-zero contribution to the CPG gait
246  double iFusedYLastTime; // The last in.timestamp where iFusedY made a non-zero contribution to the CPG gait
247  bool haveIFusedXFeed; // Boolean flag whether the iFusedX integrator had any effect on iFusedXFeed in the current cycle
248  bool haveIFusedYFeed; // Boolean flag whether the iFusedY integrator had any effect on iFusedYFeed in the current cycle
249  bool usedIFusedX; // Boolean flag whether the iFusedX integrator had any effect on the produced joint commands in the current cycle
250  bool usedIFusedY; // Boolean flag whether the iFusedY integrator had any effect on the produced joint commands in the current cycle
251  void resetIntegrators();
252  void resetSaveIntegrals();
253 
254  // Callbacks for updating the filter sizes
255  void resizeFusedFilters (int numPoints) { if(numPoints < 1) numPoints = 1; fusedXFeedFilter.resize(numPoints); fusedYFeedFilter.resize(numPoints); }
256  void resizeDFusedFilters(int numPoints) { if(numPoints < 1) numPoints = 1; dFusedXFeedFilter.resize(numPoints); dFusedYFeedFilter.resize(numPoints); }
257  void resizeIFusedFilters(int numPoints) { if(numPoints < 1) numPoints = 1; iFusedXFeedFilter.resize(numPoints); iFusedYFeedFilter.resize(numPoints); }
258  void resizeGyroFilters (int numPoints) { if(numPoints < 1) numPoints = 1; gyroXFeedFilter.resize(numPoints); gyroYFeedFilter.resize(numPoints); }
259 
260  // Basic feedback values
261  double fusedXFeed;
262  double fusedYFeed;
263  double dFusedXFeed;
264  double dFusedYFeed;
265  double iFusedXFeed;
266  double iFusedYFeed;
267  double gyroXFeed;
268  double gyroYFeed;
269 
270  //
271  // Capture step variables
272  //
273 
274  // Reset function
275  void resetCaptureSteps(bool resetRobotModel);
276 
277  // Capture step robot model
278  margait_contrib::RobotModel rxRobotModel;
280  config_server::Parameter<bool> m_showRxVis;
281  void callbackShowRxVis();
282 
283  // Linear inverted pendulum robot models
284  margait_contrib::LimpModel rxModel;
285  margait_contrib::LimpModel mxModel;
286  margait_contrib::LimpModel txModel;
287 
288  // Linear inverted pendulum model class for isolated calculations
289  margait_contrib::Limp limp;
290 
291  // CoM filter
292  ComFilter<5> m_comFilter;
293 
294  // Miscellaneous
295  config_server::Parameter<float> m_gcvZeroTime;
296  margait_contrib::Vec2f adaptation;
297  double lastSupportOrientation;
298  double oldGcvTargetY;
299  double virtualSlope;
300  double stepTimeCount;
301  double lastStepDuration;
302  int stepCounter;
303  int noCLStepsCounter;
304  int resetCounter;
305  int cycleNumber;
306 
307  //
308  // Plotting
309  //
310 
311  // Note: To add a variable for plotting you need to do the following...
312  // 1) Add an enum value below ==> PM_MODEL_MY_DATA,
313  // 2) Set the name of the plotted data in configurePlotManager() ==> m_PM.setName(PM_MODEL_MY_DATA, "model/myData");
314  // 3) Actually plot the required data in the function where you calculate it ==> m_PM.plotScalar(myData, PM_MODEL_MY_DATA);
315  // It is recommended to enclose the plotScaler() call in ==> if(m_PM.getEnabled()) { ... }
316 
317  // Plot manager
318  config_server::Parameter<bool> m_plotData;
319  plot_msgs::PlotManagerFS m_PM;
320  void configurePlotManager();
321  void callbackPlotData();
322  enum PMIDS
323  {
324  PM_GCV = 0,
325  PM_GCV_X = PM_GCV,
326  PM_GCV_Y,
327  PM_GCV_Z,
328  PM_GCV_ACC,
329  PM_GCV_ACC_X = PM_GCV_ACC,
330  PM_GCV_ACC_Y,
331  PM_GCV_ACC_Z,
332  PM_GAIT_PHASE,
333  PM_USED_IFUSEDX,
334  PM_USED_IFUSEDY,
335  PM_HALT_BLEND_FACTOR,
336  PM_LEG_EXTENSION_R,
337  PM_LEG_EXTENSION_L,
338  PM_LEG_SWING_ANGLE_R,
339  PM_LEG_SWING_ANGLE_L,
340  PM_LEG_SAG_SWING_R,
341  PM_LEG_SAG_SWING_L,
342  PM_LEG_LAT_SWING_R,
343  PM_LEG_LAT_SWING_L,
344  PM_LEG_ROT_SWING_R,
345  PM_LEG_ROT_SWING_L,
346  PM_LEG_LAT_HIP_SWING_R,
347  PM_LEG_LAT_HIP_SWING_L,
348  PM_LEG_SAG_LEAN_R,
349  PM_LEG_SAG_LEAN_L,
350  PM_LEG_LAT_LEAN_R,
351  PM_LEG_LAT_LEAN_L,
352  PM_LEG_FEED_HIPANGLEX_R,
353  PM_LEG_FEED_HIPANGLEX_L,
354  PM_LEG_FEED_HIPANGLEY_R,
355  PM_LEG_FEED_HIPANGLEY_L,
356  PM_LEG_FEED_FOOTANGLEX_R,
357  PM_LEG_FEED_FOOTANGLEX_L,
358  PM_LEG_FEED_FOOTANGLEY_R,
359  PM_LEG_FEED_FOOTANGLEY_L,
360  PM_LEG_FEED_FOOTANGLECX_R,
361  PM_LEG_FEED_FOOTANGLECX_L,
362  PM_LEG_FEED_FOOTANGLECY_R,
363  PM_LEG_FEED_FOOTANGLECY_L,
364  PM_LEG_ABS_LEGEXT_R,
365  PM_LEG_ABS_LEGEXT_L,
366  PM_LEG_ABS_LEGANGLEX_R,
367  PM_LEG_ABS_LEGANGLEX_L,
368  PM_LEG_ABS_LEGANGLEY_R,
369  PM_LEG_ABS_LEGANGLEY_L,
370  PM_LEG_ABS_LEGANGLEZ_R,
371  PM_LEG_ABS_LEGANGLEZ_L,
372  PM_LEG_ABS_FOOTANGLEX_R,
373  PM_LEG_ABS_FOOTANGLEX_L,
374  PM_LEG_ABS_FOOTANGLEY_R,
375  PM_LEG_ABS_FOOTANGLEY_L,
376  PM_LEG_FEED_COMSHIFTX_R,
377  PM_LEG_FEED_COMSHIFTX_L,
378  PM_LEG_FEED_COMSHIFTY_R,
379  PM_LEG_FEED_COMSHIFTY_L,
380  PM_LEG_VIRTUAL_SLOPE_R,
381  PM_LEG_VIRTUAL_SLOPE_L,
382  PM_LEG_VIRTUAL_COMP_R,
383  PM_LEG_VIRTUAL_COMP_L,
384  PM_ARM_SWING_ANGLE_R,
385  PM_ARM_SWING_ANGLE_L,
386  PM_ARM_SAG_SWING_R,
387  PM_ARM_SAG_SWING_L,
388  PM_ARM_FEED_ARMANGLEX_R,
389  PM_ARM_FEED_ARMANGLEX_L,
390  PM_ARM_FEED_ARMANGLEY_R,
391  PM_ARM_FEED_ARMANGLEY_L,
392  PM_ARM_ABS_ARMEXT_R,
393  PM_ARM_ABS_ARMEXT_L,
394  PM_ARM_ABS_ARMANGLEX_R,
395  PM_ARM_ABS_ARMANGLEX_L,
396  PM_ARM_ABS_ARMANGLEY_R,
397  PM_ARM_ABS_ARMANGLEY_L,
398  PM_RXRMODEL_SUPPVEC_X,
399  PM_RXRMODEL_SUPPVEC_Y,
400  PM_RXRMODEL_SUPPVEC_Z,
401  PM_RXRMODEL_STEPVEC_X,
402  PM_RXRMODEL_STEPVEC_Y,
403  PM_RXRMODEL_STEPVEC_Z,
404  PM_RXRMODEL_STEPVEC_FYAW,
405  PM_FUSED_X,
406  PM_FUSED_Y,
407  PM_COMFILTER_X,
408  PM_COMFILTER_Y,
409  PM_COMFILTER_VX,
410  PM_COMFILTER_VY,
411  PM_RXMODEL_X,
412  PM_RXMODEL_Y,
413  PM_RXMODEL_VX,
414  PM_RXMODEL_VY,
415  PM_RXMODEL_SUPPLEG,
416  PM_RXMODEL_TIMETOSTEP,
417  PM_MXMODEL_X,
418  PM_MXMODEL_Y,
419  PM_MXMODEL_VX,
420  PM_MXMODEL_VY,
421  PM_MXMODEL_SUPPLEG,
422  PM_MXMODEL_TIMETOSTEP,
423  PM_MXMODEL_ZMP_X,
424  PM_MXMODEL_ZMP_Y,
425  PM_TXMODEL_X,
426  PM_TXMODEL_Y,
427  PM_TXMODEL_VX,
428  PM_TXMODEL_VY,
429  PM_TXMODEL_SUPPLEG,
430  PM_TXMODEL_TIMETOSTEP,
431  PM_TXMODEL_STEPSIZEX,
432  PM_TXMODEL_STEPSIZEY,
433  PM_TXMODEL_STEPSIZEZ,
434  PM_ADAPTATION_X,
435  PM_ADAPTATION_Y,
436  PM_EXP_FUSED_X,
437  PM_EXP_FUSED_Y,
438  PM_DEV_FUSED_X,
439  PM_DEV_FUSED_Y,
440  PM_FEEDBACK_FUSED_X,
441  PM_FEEDBACK_FUSED_Y,
442  PM_FEEDBACK_DFUSED_X,
443  PM_FEEDBACK_DFUSED_Y,
444  PM_FEEDBACK_IFUSED_X,
445  PM_FEEDBACK_IFUSED_Y,
446  PM_FEEDBACK_GYRO_X,
447  PM_FEEDBACK_GYRO_Y,
448  PM_TIMING_FEED_WEIGHT,
449  PM_TIMING_FREQ_DELTA,
450  PM_GAIT_FREQUENCY,
451  PM_REM_GAIT_PHASE,
452  PM_TIMETOSTEP,
453  PM_STEPSIZE_X,
454  PM_STEPSIZE_Y,
455  PM_STEPSIZE_Z,
456  PM_GCVTARGET_X,
457  PM_GCVTARGET_Y,
458  PM_GCVTARGET_Z,
459  PM_LAST_STEP_DURATION,
460  PM_COUNT
461  };
462  };
463 }
464 
465 #endif
466 // EOF
Base implementation of a gait engine, made to work with the Gait motion module.
Definition: gait_engine.h:30
A capture step gait, implemented as a GaitEngine plugin for the Gait motion module.
Definition: cap_gait.h:48
virtual void setOdometry(double posX, double posY, double rotZ)
Set the CoM odometry to a particular 2D position and orientation.
Definition: cap_gait.cpp:238
virtual void step()
Main step function of the gait engine.
Definition: cap_gait.cpp:269
Data struct that encompasses the joint representation of a robot pose.
Definition: gait_joint_pose.h:242
virtual void updateHaltPose()
Update the halt pose desired by the gait engine.
Definition: cap_gait.cpp:979
Data struct that encompasses the abstract representation of an arm pose.
Definition: gait_abstract_pose.h:136
CapGait()
Default constructor.
Definition: cap_gait.cpp:33
Encapsulates a visualisation of the state of a RobotModel object.
Definition: RobotModelVis.h:29
virtual void handleJoystickButton(int button)
Notify the gait engine that a particular joystick button has been pressed and released by the user...
Definition: cap_gait.cpp:262
Configuration struct for the capture step gait.
Definition: cap_gait_config.h:21
Data struct that encompasses the inverse representation of a robot pose.
Definition: gait_inverse_pose.h:188
virtual void updateOdometry()
Force an update of the CoM odometry in terms of 3D position and orientation.
Definition: cap_gait.cpp:245
Data struct that encompasses the abstract representation of a robot pose.
Definition: gait_abstract_pose.h:214
Data struct that encompasses the abstract representation of a leg pose.
Definition: gait_abstract_pose.h:35
virtual void reset()
Reset the gait engine.
Definition: cap_gait.cpp:84
Data struct that encompasses the inverse representation of a leg pose.
Definition: gait_inverse_pose.h:36