NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
motionplayer.h
1 //Motion module to play motions from trajectories
2 //Author: Sebastian Schüller <schuell1@cs.uni-bonn.de>, Hafez Farazi <farazi@ais.uni-bonn.de>
3 
4 #ifndef MOTION_PLAYER_H
5 #define MOTION_PLAYER_H
6 
7 #include <robotcontrol/model/robotmodel.h>
8 #include <robotcontrol/motionmodule.h>
9 #include <robotcontrol/FadeTorqueAction.h>
10 
11 #include "boost/filesystem/operations.hpp"
12 #include "boost/filesystem/path.hpp"
13 #include "boost/progress.hpp"
14 #include "boost/regex.hpp"
15 
16 #include <actionlib/client/simple_action_client.h>
17 
18 #include <motion_player/PlayMotion.h>
19 #include <motion_player/StreamMotion.h>
20 #include <motion_player/DumpMotions.h>
21 
22 #include <keyframe_player/KeyframePlayer.h>
23 #include <motion_file/motionfile.h>
24 #include <plot_msgs/plot_manager.h>
25 #include <ros/timer.h>
26 #include <Interpolator.hpp>
27 
28 #include <std_srvs/Empty.h>
29 
30 #include <motion_file/ruleapplier.h>
31 
32 namespace motionplayer
33 {
34 
35 class MappedMotion: public motionfile::Motion
36 {
37 public:
38  std::vector<int> motionToModel;
39  robotcontrol::RobotModel::State reqState;
40  robotcontrol::RobotModel::State transState;
41  robotcontrol::RobotModel::State resState;
42 
43  std::vector<kf_player::KeyframePlayer> player;
44 
45  MappedMotion(const MappedMotion& _in) :
46  motionfile::Motion(_in)
47  {
48  reqState = _in.reqState;
49  transState = _in.transState;
50  resState = _in.resState;
51  motionToModel = _in.motionToModel;
52  player = _in.player;
53  } // copy ctor
54 
55  MappedMotion()
56  {
57 
58  }
59 
60  MappedMotion& operator=(MappedMotion _in)
61  {
62  if (this != &_in)
63  {
64  motionfile::Motion::operator=(_in);
65  reqState = _in.reqState;
66  transState = _in.transState;
67  resState = _in.resState;
68  motionToModel = _in.motionToModel;
69  player = _in.player;
70  }
71  return *this;
72  }
73 
74  void resetPlayer();
75  void initPlayer();
76 };
77 
78 class MotionPlayer : public robotcontrol::MotionModule
79 {
80 public:
81  typedef boost::shared_ptr<config_server::Parameter<float> > FloatParamPtr;
82  typedef boost::shared_ptr<config_server::Parameter<bool> > BoolParamPtr;
83 
84  MotionPlayer();
85  virtual ~MotionPlayer() {}
86 
87  virtual bool init(robotcontrol::RobotModel* model);
88  virtual void step();
89  virtual bool isTriggered();
90 
91 private:
92  std::map<std::string, MappedMotion> m_motionNames;
93  std::map<std::string, std::vector<FloatParamPtr> > m_ruleParameters;
94  BoolParamPtr m_limit_inverse_space;
95  FloatParamPtr m_epsion;
96 
97  MappedMotion m_playingMotion;
98 
99  motionfile::RuleApplier m_ruleApplier;
100 
101  robotcontrol::RobotModel* m_model;
102  double m_dT;
103 
104  boost::shared_ptr<const urdf::Link> m_leftFootLink; // Left foot URDF link (used for setting support coefficients)
105  boost::shared_ptr<const urdf::Link> m_rightFootLink; // Right foot URDF link (used for setting support coefficients)
106 
107  ros::ServiceServer m_srv_play;
108  ros::ServiceServer m_srv_update;
109  ros::ServiceServer m_srv_reload;
110  ros::ServiceServer m_srv_dump_motions;
111 
112  std::vector<double> m_cmdPositions;
113  std::vector<double> m_cmdVelocities;
114  std::vector<double> m_cmdAccelerations;
115  std::vector<double> m_cmdEffort;
116 
117  //previous gain_select
118  std::vector<kf_player::gainSelectEnum> m_preGains;
119  //previous angles(roll,pitch,yaw)
120  Eigen::Vector3f m_preAngle;
121  std::vector<double> m_integralGains;
122 
123 
124  actionlib::SimpleActionClient<robotcontrol::FadeTorqueAction> m_torqueAct;
125 
126  // Predefined states
127  robotcontrol::RobotModel::State m_state_relaxed;
128  robotcontrol::RobotModel::State m_state_setting_pose;
129  robotcontrol::RobotModel::State m_state_init;
130  robotcontrol::RobotModel::State m_state_standing;
131  robotcontrol::RobotModel::State m_state_sitting;
132  robotcontrol::RobotModel::State m_state_falling;
133  robotcontrol::RobotModel::State m_state_lying_prone;
134  robotcontrol::RobotModel::State m_state_lying_supine;
135  static const std::string m_fallingStatePrefix;
136 
137  // Robot specification
138  std::string m_robot_name;
139  std::string m_robot_type;
140 
141  // Standard motion names
142  std::string m_init_pose_name;
143  std::string m_init_name;
144  std::string m_getup_prone_name;
145  std::string m_getup_supine_name;
146 
147  ros::Timer timer;
148  ros::Publisher statePublisher;
149 
150  bool handlePlay(motion_player::PlayMotionRequest& req, motion_player::PlayMotionResponse& res);
151  bool handleUpdateMotion(motion_player::StreamMotionRequest& req, motion_player::StreamMotionResponse& res);
152  bool handleReloadMotion(std_srvs::Empty::Request& req, std_srvs::Empty::Response& res);
153  bool handleDumpMotions(motion_player::DumpMotionsRequest& req, motion_player::DumpMotionsResponse& res); // Saves all currently loaded motions into temp folder
154 
155  bool tryToPlay(const std::string& motion, bool checkState = true);
156  void play(const std::string& motion);
157  void finished();
158 
159  bool m_isRelaxed;
160  bool m_isInitialized;
161  bool isPlaying;
162 
163  inline void startPlaying() { isPlaying = true; };
164  inline void stopPlaying() { isPlaying = false; };
165 
166  void writeCommands();
167 
168  void initMotions();
169  void initRuleParameters();
170 
171  void applyRule(motionplayer::MappedMotion &motion, int rule_id, const float delta);
172 
173  void publishState(const ros::TimerEvent& event);
174 
175  bool loadMotionFiles(const boost::filesystem::path& dir);
176  bool reloadMotionFiles(const boost::filesystem::path& dir);
177  int findIndex(std::string name);
178 
179  enum PMIds
180  {
181  PM_FRAME_INDEX=0,
182  PM_ROLL,
183  PM_PITCH,
184  PM_YAW,
185  PM_ROLLD,
186  PM_PITCHD,
187  PM_YAWD,
188  PM_DES_ROLL,
189  PM_DES_PITCH,
190  PM_DES_YAW,
191  PM_COUNT
192  };
193  plot_msgs::PlotManagerFS PM;
194 };
195 
196 }
197 
198 #endif