motion_model.h
Go to the documentation of this file.
1 #ifndef _H_PLAY_MOTION_BUILDER_MOTION_MODEL
2 #define _H_PLAY_MOTION_BUILDER_MOTION_MODEL
3 
4 #include <ros/ros.h>
5 #include <sensor_msgs/JointState.h>
6 #include <algorithm>
7 #include <unordered_map>
8 
9 #include <yaml-cpp/yaml.h>
10 
11 namespace pal
12 {
14 struct PrintPoint
15 {
16  const static std::string TIME_KEY;
17  const static std::string POSITIONS_KEY;
18 
20  std::vector<double> positions_;
21 };
22 struct PrintMeta
23 {
24  const static std::string META_KEY;
25  const static std::string NAME_KEY;
26  const static std::string USAGE_KEY;
27  const static std::string DESC_KEY;
28 
29  bool print_;
30  std::string name_;
31  std::string usage_;
32  std::string description_;
33 };
35 {
36  const static std::string JOINTS_KEY;
37  const static std::string POINTS_KEY;
38 
39  std::vector<std::string> joints_;
40  std::vector<PrintPoint> points_;
42 };
43 
50 struct JointGroup
51 {
52  std::string group_name_;
53  std::vector<std::string> joints_;
54 
56  {
57  }
58 
59  JointGroup(const std::string& name) : group_name_(name)
60  {
61  }
62 };
66 struct JointModel
67 {
68  double lower_limit_;
69  double upper_limit_;
70 
72  {
73  }
74 
75  JointModel(double lower_limit, double upper_limit)
76  : lower_limit_(lower_limit), upper_limit_(upper_limit)
77  {
78  }
79 
80  bool inLimits(double position)
81  {
82  return lower_limit_ <= position && upper_limit_ >= position;
83  }
84 };
89 {
90  std::string joint_name_;
91  double position_;
92 
93  JointPosition(const std::string& joint_name, double position)
94  : joint_name_(joint_name), position_(position)
95  {
96  }
97 };
101 class KeyFrame
102 {
103 public:
104  KeyFrame(float time_increment);
105  KeyFrame(const KeyFrame& k);
106 
107  void addPosition(const std::string& name, double position);
108  double getJointPosition(const std::string& joint) const;
109  void cleanUnused(const std::map<std::string, bool>& used_joints);
110  PrintPoint print(double basetime, double downshift, const std::vector<std::string>& names) const;
111  PrintMotion print(const std::vector<std::string>& names) const;
112 
113  float getTime() const
114  {
115  return time_increment_;
116  }
117  void setTime(float time_increment)
118  {
119  time_increment_ = time_increment;
120  }
121  const std::vector<JointPosition>& getJoints() const
122  {
123  return joints_;
124  }
125  std::vector<JointPosition>& getJoints()
126  {
127  return joints_;
128  }
129 
130 private:
131  std::vector<JointPosition> joints_;
133 };
138 class Motion
139 {
140 public:
141  const static float DEFAULT_TIME;
142 
143  Motion(const std::string& robot_description, const std::string& robot_description_semantic,
144  const std::vector<std::string>& extra_joints);
145  Motion(XmlRpc::XmlRpcValue& param, const std::string& robot_description,
146  const std::string& robot_description_semantic,
147  const std::vector<std::string>& extra_joints);
148 
149  void setMotionGroups(const std::string& robot_description,
150  const std::string& robot_description_semantic);
151  void setParamName();
152  void addKeyFrame(const sensor_msgs::JointStateConstPtr& msg, float time_increment = DEFAULT_TIME);
153  void updateKeyFrame(const sensor_msgs::JointStateConstPtr& msg, int frame);
154  void addJointModel(const std::string& joint_name, const JointModel& joint_model);
155  void changeTime(int frame, float time_increment);
156  double changeJoint(int frame, const std::string& joint_name, double position);
157  void removeKeyFrame(int frame);
158  void copyFrame(int frame, int new_frame_pos = -1);
159  void loadFrame(int frame) const;
160  void loadYAML(double downshift) const;
161  void extendFrames(const sensor_msgs::JointStateConstPtr& msg);
162  PrintMotion print(double downshift = 1.0) const;
163  PrintMotion print(const std::string& name, const std::string& usage,
164  const std::string& description, double downshift = 1.0) const;
165 
166  void removeAllKeyFrames();
167  void addJointToGroup(const std::string& group, const std::string& joint);
168  bool addGroupToGroup(const std::string& group, const std::string& subgroup);
169 
170  const std::string& getParamName() const
171  {
172  return tmp_name_;
173  }
175  {
176  return !joint_groups_.empty();
177  }
178  const KeyFrame& getLastKeyFrame() const
179  {
180  return getKeyFrame(keyframes_.size() - 1);
181  }
182  const KeyFrame& getKeyFrame(int i) const
183  {
184  return keyframes_[i];
185  }
186  std::vector<std::string> getJoints() const
187  {
188  std::vector<std::string> joints;
189 
190  // If using a joint group, list it
191  if (group_used_ != "")
192  {
193  joints.insert(joints.end(), joint_groups_.at(group_used_).begin(),
194  joint_groups_.at(group_used_).end());
195  }
196 
197  // If using ungrouped joints, add them
198  for (const auto& extra_joint : extra_joints_)
199  {
200  if (extra_joint.second)
201  joints.push_back(extra_joint.first);
202  }
203 
204  return joints;
205  }
206  std::size_t size() const
207  {
208  return keyframes_.size();
209  }
210  const std::vector<KeyFrame> getKeyframes() const
211  {
212  return keyframes_;
213  }
214  const std::string& getCurrentGroup()
215  {
216  return group_used_;
217  }
218  bool setCurrentGroup(const std::string& group)
219  {
220  if (joint_groups_.find(group) != joint_groups_.end())
221  {
222  group_used_ = group;
223  return true;
224  }
225  else
226  {
227  return false;
228  }
229  }
230  bool isJointUsed(const std::string& joint_name) const
231  {
232  if (extra_joints_.find(joint_name) != extra_joints_.end())
233  return extra_joints_.at(joint_name);
234  else if (group_used_ != "")
235  return std::find(joint_groups_.at(group_used_).begin(),
236  joint_groups_.at(group_used_).end(),
237  joint_name) != joint_groups_.at(group_used_).end();
238  else
239  return false;
240  }
241  bool setExtraJointUsedState(const std::string& name, bool used)
242  {
243  if (extra_joints_.find(name) != extra_joints_.end())
244  {
245  extra_joints_[name] = used;
246  return true;
247  }
248  else
249  {
250  return false;
251  }
252  }
253  std::vector<std::string> getJointGroups()
254  {
255  std::vector<std::string> joint_groups;
256  for (const auto& pair : joint_groups_)
257  {
258  joint_groups.push_back(pair.first);
259  }
260 
261  return joint_groups;
262  }
263  std::vector<std::string> getExtraJoints()
264  {
265  std::vector<std::string> extra_joints;
266  for (const auto& pair : extra_joints_)
267  {
268  extra_joints.push_back(pair.first);
269  }
270 
271  return extra_joints;
272  }
273  std::vector<std::string> getAvailableJoints()
274  {
275  std::vector<std::string> joints;
276  for (const auto& pair : joint_groups_)
277  {
278  for (const auto& joint : pair.second)
279  {
280  if (std::find(joints.begin(), joints.end(), joint) == joints.end())
281  {
282  joints.push_back(joint);
283  }
284  }
285  }
286 
287  // Add extra joints
288  for (const auto& pair : extra_joints_)
289  {
290  joints.push_back(pair.first);
291  }
292 
293  return joints;
294  }
295 
296 private:
297  std::string tmp_name_;
298  std::vector<KeyFrame> keyframes_;
299  std::unordered_map<std::string, std::vector<std::string> > joint_groups_;
300  std::unordered_map<std::string, bool> extra_joints_;
301  std::string group_used_;
302  std::unordered_map<std::string, JointModel> joint_models_;
303 };
304 
306 void loadParams(const YAML::Emitter& param, const std::string& filename);
307 std::string cleanName(const std::string& name);
308 std::string rosifyName(const std::string& name);
309 double toDouble(XmlRpc::XmlRpcValue& value);
310 } // namespace pal
311 
313 namespace YAML
314 {
315 Emitter& operator<<(YAML::Emitter& out, const pal::PrintMotion& m);
316 Emitter& operator<<(YAML::Emitter& out, const pal::PrintPoint& k);
317 Emitter& operator<<(YAML::Emitter& out, const pal::PrintMeta& m);
318 } // namespace YAML
319 
320 #endif /* _H_PLAY_MOTION_BUILDER_MOTION_MODEL */
float time_increment_
Definition: motion_model.h:132
std::vector< std::string > joints_
Definition: motion_model.h:53
static const std::string POINTS_KEY
Definition: motion_model.h:37
static const std::string TIME_KEY
Definition: motion_model.h:16
std::vector< std::string > joints_
Definition: motion_model.h:39
std::string rosifyName(const std::string &name)
Emitter & operator<<(YAML::Emitter &out, const pal::PrintMeta &m)
std::string tmp_name_
Definition: motion_model.h:297
static const std::string DESC_KEY
Definition: motion_model.h:27
JointModel(double lower_limit, double upper_limit)
Definition: motion_model.h:75
std::vector< KeyFrame > keyframes_
Definition: motion_model.h:298
std::vector< std::string > getAvailableJoints()
Definition: motion_model.h:273
double toDouble(XmlRpc::XmlRpcValue &value)
std::vector< std::string > getJoints() const
Definition: motion_model.h:186
std::vector< JointPosition > & getJoints()
Definition: motion_model.h:125
std::unordered_map< std::string, std::vector< std::string > > joint_groups_
Definition: motion_model.h:299
std::string usage_
Definition: motion_model.h:31
JointGroup(const std::string &name)
Definition: motion_model.h:59
double lower_limit_
Definition: motion_model.h:68
ROSCONSOLE_DECL void print(FilterBase *filter, void *logger, Level level, const char *file, int line, const char *function, const char *fmt,...) ROSCONSOLE_PRINTF_ATTRIBUTE(7
bool setCurrentGroup(const std::string &group)
Definition: motion_model.h:218
bool isJointUsed(const std::string &joint_name) const
Definition: motion_model.h:230
PrintMeta meta_
Definition: motion_model.h:41
std::string cleanName(const std::string &name)
static const std::string POSITIONS_KEY
Definition: motion_model.h:17
std::string joint_name_
Definition: motion_model.h:90
float time_from_start_
Definition: motion_model.h:19
const KeyFrame & getLastKeyFrame() const
Definition: motion_model.h:178
static const std::string META_KEY
Definition: motion_model.h:24
std::vector< JointPosition > joints_
Definition: motion_model.h:131
std::vector< double > positions_
Definition: motion_model.h:20
const std::string & getParamName() const
Definition: motion_model.h:170
static const std::string NAME_KEY
Definition: motion_model.h:25
std::unordered_map< std::string, JointModel > joint_models_
Definition: motion_model.h:302
bool setExtraJointUsedState(const std::string &name, bool used)
Definition: motion_model.h:241
const std::string & getCurrentGroup()
Definition: motion_model.h:214
std::unordered_map< std::string, bool > extra_joints_
Definition: motion_model.h:300
const std::vector< JointPosition > & getJoints() const
Definition: motion_model.h:121
std::string group_name_
Definition: motion_model.h:52
const std::vector< KeyFrame > getKeyframes() const
Definition: motion_model.h:210
static const float DEFAULT_TIME
Definition: motion_model.h:141
std::vector< PrintPoint > points_
Definition: motion_model.h:40
float getTime() const
Definition: motion_model.h:113
bool inLimits(double position)
Definition: motion_model.h:80
void loadParams(const YAML::Emitter &param, const std::string &filename)
std::size_t size() const
Definition: motion_model.h:206
const KeyFrame & getKeyFrame(int i) const
Definition: motion_model.h:182
std::vector< std::string > getExtraJoints()
Definition: motion_model.h:263
std::vector< std::string > getJointGroups()
Definition: motion_model.h:253
static const std::string USAGE_KEY
Definition: motion_model.h:26
bool jointsLoaded()
Definition: motion_model.h:174
std::string name_
Definition: motion_model.h:30
double upper_limit_
Definition: motion_model.h:69
void setTime(float time_increment)
Definition: motion_model.h:117
JointPosition(const std::string &joint_name, double position)
Definition: motion_model.h:93
std::string description_
Definition: motion_model.h:32
static const std::string JOINTS_KEY
Definition: motion_model.h:36
std::string group_used_
Definition: motion_model.h:301


play_motion_builder
Author(s):
autogenerated on Mon Feb 28 2022 23:13:39