NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gait.h
1 // Generic gait motion module
2 // File: gait.h
3 // Author: Philipp Allgeuer <pallgeuer@ais.uni-bonn.de>
4 
5 // Ensure header is only included once
6 #ifndef GAIT_H
7 #define GAIT_H
8 
9 // Includes
10 #include <robotcontrol/motionmodule.h>
11 #include <robotcontrol/model/robotmodel.h>
12 #include <rc_utils/math_spline.h>
13 #include <config_server/parameter.h>
14 #include <plot_msgs/plot_manager.h>
15 #include <pluginlib/class_loader.h>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/make_shared.hpp>
18 
19 // Includes - Local
20 #include <gait/gait_common.h>
21 #include <gait/gait_command.h>
22 #include <gait/gait_engine.h>
23 
24 // Includes - Messages
25 #include <tf/transform_broadcaster.h>
26 #include <gait_msgs/GaitCommand.h>
27 #include <gait_msgs/GaitOdom.h>
28 #include <gait_msgs/SetOdom.h>
29 #include <sensor_msgs/Joy.h>
30 #include <diagnostic_msgs/DiagnosticArray.h>
31 #include <std_srvs/Empty.h>
32 
33 // Includes - ROS
34 #include <ros/subscriber.h>
35 
36 // Includes - C++ Standard Library
37 #include <string>
38 
44 namespace gait
45 {
54  class Gait : public robotcontrol::MotionModule
55  {
56  public:
57  // Constructor
58  Gait();
59  virtual ~Gait();
60 
61  // Initialisation function
62  virtual bool init(robotcontrol::RobotModel* model);
63 
64  // Trigger function
65  virtual bool isTriggered();
66 
67  // Step function
68  virtual void step();
69 
70  // Publish transforms function
71  virtual void publishTransforms();
72 
73  protected:
74  // Get function for gait name
75  const std::string& gaitName() const { return m_gaitName; }
76 
77  private:
78  // Constants
79  const std::string CONFIG_PARAM_PATH;
80 
81  // Configuration parameters
82  boost::shared_ptr<config_server::Parameter<bool> > m_enableGait; // Flag whether to enable this gait motion module. The config parameter name is specific to the gait engine name.
83  config_server::Parameter<bool> m_enableJoystick; // Flag whether to globally enable the use of the joystick to control the gait command vector. When enabled, the first button on the joystick toggles the joystick mode on and off.
84  config_server::Parameter<bool> m_plotData; // Flag whether to plot gait data to the plotter visualisation.
85  config_server::Parameter<bool> m_publishOdometry; // Flag whether to publish the gait odometry.
86  config_server::Parameter<bool> m_publishTransforms; // Flag whether to publish the ego_floor and gait odometry TF transforms.
87  config_server::Parameter<float> m_gcvNormP; // The p parameter with which to calculate the gait command vector norm (i.e. using the p-norm).
88  config_server::Parameter<float> m_gcvNormMax; // The maximum allowed gait command vector norm, beyond which normalisation to this maximum value is performed.
89  config_server::Parameter<float> m_gcvZeroTime; // The minimum time after which the robot has just started walking where a zero gcv is enforced for stability reasons.
90  config_server::Parameter<float> m_minWalkingTime; // The minimum time after which a robot is allowed to be told to stop walking again after it just started walking
91 
92  // Robot model
93  robotcontrol::RobotModel* m_model; // Pointer to the RobotModel object to work with
94  int m_jointMap[NUM_JOINTS]; // Maps a JointID enum to its corresponding joint index in RobotModel
95  boost::shared_ptr<const urdf::Link> m_trunkLink; // Trunk URDF link (used for setting support coefficients)
96  boost::shared_ptr<const urdf::Link> m_leftFootLink; // Left foot URDF link (used for setting support coefficients)
97  boost::shared_ptr<const urdf::Link> m_rightFootLink; // Right foot URDF link (used for setting support coefficients)
98 
99  // Step execution timing
100  ros::Time m_now;
101  ros::Time m_lastNow;
102  double m_dT;
103 
104  // Robot states
105  robotcontrol::RobotModel::State m_state_standing;
106  robotcontrol::RobotModel::State m_state_walking;
107 
108  // Gait name (valid after init() has been called)
109  std::string m_gaitName;
110 
111  // Finalise a step of the gait motion module
112  void finaliseStep();
113 
114  // Transforms
115  void updateTransforms();
116 
117  // Gait engine
118  boost::shared_ptr<GaitEngine> m_engine;
119  pluginlib::ClassLoader<GaitEngine> m_enginePluginLoader;
120  bool loadGaitEngine();
121  void updateHaltPose();
122  void updateGaitEngineInputs(GaitEngineInput& in);
123  void processGaitEngineOutputs(const GaitEngineOutput& out);
124  void writeJointCommands(const GaitEngineOutput& out);
125  void setSupportCoefficients(double leftLegCoeff, double rightLegCoeff);
126  void plotGaitEngineInputs(const GaitEngineInput& in);
127  void plotGaitEngineOutputs(const GaitEngineOutput& out);
128 
129  // Gait command
130  GaitCommand m_gaitCmd;
131  GaitCommand m_gaitCmdToUse;
132  ros::Subscriber m_sub_gaitCommand;
133  void handleGaitCommand(const gait_msgs::GaitCommandConstPtr& cmd);
134  void plotRawGaitCommand();
135 
136  // Motions
137  void setPendingMotion(MotionID ID, MotionStance stance = STANCE_DEFAULT, bool adjustLeft = true, bool adjustRight = true);
138  void clearPendingMotion();
139  MotionID m_motionPending; // The ID of a pending motion, if there is one, otherwise MID_NONE
140  MotionID m_oldMotionPending; // The value of m_motionPending in the last cycle, for algorithmic purposes
141  MotionStance m_motionStance; // The required stance of the robot before executing the motion
142  bool m_motionAdjustLeftFoot; // Flag whether the left foot should be adjusted to attain the required foot separation (how the required foot separation for various motions is defined is up to the gait engine)
143  bool m_motionAdjustRightFoot; // Flag whether the right foot should be adjusted to attain the required foot separation (how the required foot separation for various motions is defined is up to the gait engine)
144 
145  // Halt pose
146  rc_utils::TrapVelSpline m_jointSpline[NUM_JOINTS];
147  rc_utils::LinearSpline m_jointEffortSpline[NUM_JOINTS];
148  ros::Time m_reachStartTime;
149  ros::Time m_lastHaltTime;
150  double m_reachDuration;
151  bool m_reachedHalt;
152  bool m_updatedHalt;
153  void startReachHaltPose();
154  void continueReachHaltPose();
155  void stopReachHaltPose();
156 
157  // Joystick data
158  static const int GAIT_BUTTONS = 4; // Four fixed buttons are required to start/stop the gait and kick
159  static const int MISC_BUTTONS = 8; // Eight additional buttons are available for miscellaneous use
160  static const int JOY_BUTTONS = GAIT_BUTTONS + MISC_BUTTONS;
161  bool m_joystickEnabled;
162  bool m_joystickConnected;
163  bool m_joystickGaitCmdLock;
164  bool m_joystickButtonPressed[JOY_BUTTONS];
165  bool m_joystickButtonTriggered[MISC_BUTTONS];
166  ros::Subscriber m_sub_joystickData;
167  ros::Subscriber m_sub_joystickStatus;
168  void handleJoystickButtons();
169  void handleJoystickData(const sensor_msgs::JoyConstPtr& joy);
170  void handleJoystickStatus(const diagnostic_msgs::DiagnosticArrayConstPtr& array);
171  void setJoystickGaitCmdLock(bool lock);
172  void callbackEnableJoystick();
173 
174  // TF transforms
175  tf::TransformBroadcaster m_tf_broadcaster;
176  std::vector<tf::StampedTransform> m_tf_transforms;
177  tf::StampedTransform* m_tf_ego_floor;
178  tf::StampedTransform* m_tf_odom;
179  gait_msgs::GaitOdom m_gait_odom;
180  ros::Publisher m_pub_odom;
181  void configureTransforms();
182 
183  // Gait odometry
184  ros::ServiceServer m_srv_resetOdom;
185  ros::ServiceServer m_srv_setOdom;
186  bool handleResetOdometry(std_srvs::EmptyRequest& req, std_srvs::EmptyResponse& res);
187  bool handleSetOdometry(gait_msgs::SetOdomRequest &req, gait_msgs::SetOdomResponse &res);
188 
189  // Joint functions
190  bool constructJointMap();
191 
192  // Gait state
193  enum GaitState
194  {
195  GS_INACTIVE,
196  GS_REACHING_HALT_POSE,
197  GS_STARTING_WALKING,
198  GS_WALKING,
199  GS_STOPPING_WALKING
200  };
201  GaitState m_gaitState;
202  void resetGait();
203  void plotGaitStateEvent();
204 
205  // Plot manager
206  plot_msgs::PlotManagerFS m_PM;
207  void configurePlotManager();
208  void callbackPlotData();
209  enum PMIDS
210  {
211  PM_NOMINAL_DT = 0,
212  PM_TRUE_DT,
213  PM_GAITCMD_LIN_VEL_X,
214  PM_GAITCMD_LIN_VEL_Y,
215  PM_GAITCMD_ANG_VEL_Z,
216  PM_GAITCMD_WALK,
217  PM_JOINTCMD_FIRST,
218  PM_JOINTCMD_LAST = PM_JOINTCMD_FIRST + NUM_JOINTS - 1,
219  PM_JOINTEFFORT_FIRST,
220  PM_JOINTEFFORT_LAST = PM_JOINTEFFORT_FIRST + NUM_JOINTS - 1,
221  PM_USE_RAW_JOINT_CMDS,
222  PM_WALKING,
223  PM_LEFT_SUPPORT_COEFF,
224  PM_RIGHT_SUPPORT_COEFF,
225  PM_GAITCMDRAW_LIN_VEL_X,
226  PM_GAITCMDRAW_LIN_VEL_Y,
227  PM_GAITCMDRAW_ANG_VEL_Z,
228  PM_GAITCMDRAW_WALK,
229  PM_GAIT_STATE,
230  PM_GAIT_ODOM_X,
231  PM_GAIT_ODOM_Y,
232  PM_GAIT_ODOM_Z,
233  PM_GAIT_ODOM_ID,
234  PM_COUNT
235  };
236  };
237 }
238 
239 #endif /* GAIT_H */
240 // EOF
Generic gait motion module that can execute a given GaitEngine.
Definition: gait.h:54
MotionID
Enumeration of motion IDs that can be used in the GaitCommand::motion field to trigger motions throug...
Definition: gait_common.h:85
Gait command data structure.
Definition: gait_command.h:20
Data struct for passing gait engine output data from the gait engine to the generic gait motion modul...
Definition: gait_interface.h:68
MotionStance
Enumeration of motion stances that can be commanded to a gait engine.
Definition: gait_common.h:107
const std::string & gaitName() const
Retrieve the name of the gait (e.g. if the parameter string is cpg_gait::CPGGait then the gait name i...
Definition: gait.h:75
Data struct for passing gait engine input data from the generic gait motion module to the gait engine...
Definition: gait_interface.h:22
virtual ~Gait()
Destructor.
Definition: gait.cpp:87
Gait()
Default constructor.
Definition: gait.cpp:37