MimicPlugin.cpp
Go to the documentation of this file.
1 #include "MimicPlugin.h"
2 
3 namespace gazebo {
4  namespace mimicplugin {
6  }
7 }
8 
9 void gazebo::mimicplugin::MimicPlugin::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
10 {
11  model = _parent;
12  world = _parent->GetWorld();
13 
14  std::cerr << "Loading mimic plugin" << std::endl;
15  if (_sdf->HasElement("mimic")) {
16  //std::cerr << "has mimic" << std::endl;
17  //std::cerr << _sdf->ToString("aa") << std::endl;
18  sdf::ElementPtr el = _sdf->GetElement("mimic");
19 
20  while (!!el) {
21  //std::cerr << "in while" << std::endl;
22  //std::cerr << el->ToString("bb") << std::endl;
23  std::string target_name = el->Get<std::string> ();
24  //std::cerr << "name: " << target_name << std::endl;
25  std::string source_name;
26  double offset = 0;
27  double multiplier = 1.0;
28 
29  sdf::ParamPtr p_jn = el->GetAttribute("joint");
30  if (!!p_jn) {
31  p_jn->Get(source_name);
32  } else if ( el->HasElement("joint") ) {
33  sdf::ElementPtr pt = el->GetElement("joint");
34  if (!!pt) {
35  source_name = pt->Get<std::string > ();
36  }
37  }
38 
39  if (source_name.size() > 0) {
40  //std::cerr << "mimic: " << source_name << std::endl;
41  sdf::ParamPtr p_off = el->GetAttribute("offset");
42  if (!!p_off) { p_off->Get(offset); }
43  else if ( el->HasElement("offset") ) {
44  sdf::ElementPtr pt = el->GetElement("offset");
45  if (!!pt) {
46  offset = pt->Get<double > ();
47  }
48  }
49  //
50  sdf::ParamPtr p_mlt = el->GetAttribute("multiplier");
51  if (!!p_mlt) { p_mlt->Get(multiplier); }
52  else if ( el->HasElement("multiplier") ) {
53  sdf::ElementPtr pt = el->GetElement("multiplier");
54  if (!!pt) {
55  multiplier = pt->Get<double > ();
56  }
57  }
58  // create mimic
60  {
61  sdf::ParamPtr attr = el->GetAttribute("P");
62  if (!!attr) { attr->Get(param.p); }
63  else if ( el->HasElement("P") ) {
64  sdf::ElementPtr pt = el->GetElement("P");
65  if (!!pt) {
66  param.p = pt->Get<double > ();
67  }
68  }
69  }
70  {
71  sdf::ParamPtr attr = el->GetAttribute("I");
72  if (!!attr) { attr->Get(param.i); }
73  else if ( el->HasElement("I") ) {
74  sdf::ElementPtr pt = el->GetElement("I");
75  if (!!pt) {
76  param.i = pt->Get<double > ();
77  }
78  }
79  }
80  {
81  sdf::ParamPtr attr = el->GetAttribute("D");
82  if (!!attr) { attr->Get(param.d); }
83  else if ( el->HasElement("D") ) {
84  sdf::ElementPtr pt = el->GetElement("D");
85  if (!!pt) {
86  param.d = pt->Get<double > ();
87  }
88  }
89  }
90  {
91  sdf::ParamPtr attr = el->GetAttribute("i_max");
92  if (!!attr) { attr->Get(param.i_max); }
93  else if ( el->HasElement("i_max") ) {
94  sdf::ElementPtr pt = el->GetElement("i_max");
95  if (!!pt) {
96  param.i_max = pt->Get<double > ();
97  }
98  }
99  }
100  {
101  sdf::ParamPtr attr = el->GetAttribute("i_min");
102  if (!!attr) { attr->Get(param.i_min); }
103  else if ( el->HasElement("i_min") ) {
104  sdf::ElementPtr pt = el->GetElement("i_min");
105  if (!!pt) {
106  param.i_min = pt->Get<double > ();
107  }
108  }
109  }
110  {
111  sdf::ParamPtr attr = el->GetAttribute("command_max");
112  if (!!attr) { attr->Get(param.cmd_max); }
113  else if ( el->HasElement("command_max") ) {
114  sdf::ElementPtr pt = el->GetElement("command_max");
115  if (!!pt) {
116  param.cmd_max = pt->Get<double > ();
117  }
118  }
119  }
120  {
121  sdf::ParamPtr attr = el->GetAttribute("command_min");
122  if (!!attr) { attr->Get(param.cmd_min); }
123  else if ( el->HasElement("command_min") ) {
124  sdf::ElementPtr pt = el->GetElement("command_min");
125  if (!!pt) {
126  param.cmd_min = pt->Get<double > ();
127  }
128  }
129  }
130  {
131  sdf::ParamPtr attr = el->GetAttribute("velocity");
132  if (!!attr) { attr->Get(param.velocity); }
133  else if ( el->HasElement("velocity") ) {
134  sdf::ElementPtr pt = el->GetElement("velocity");
135  if (!!pt) {
136  param.velocity = pt->Get<bool > ();
137  }
138  }
139  }
140  registerMimic(source_name, target_name, offset, multiplier, param);
141  } else {
142  // warn
143  }
144  el = el->GetNextElement("mimic");
145  }
146  }
147 #if GAZEBO_MAJOR_VERSION < 8
148  prev_tm = world->GetSimTime();
149 #else
150  prev_tm = world->SimTime();
151 #endif
152  updateConnection =
153  event::Events::ConnectWorldUpdateBegin(
154  boost::bind(&gazebo::mimicplugin::MimicPlugin::Update, this));
155 }
156 
157 void gazebo::mimicplugin::MimicPlugin::registerMimic(const std::string &src_joint,
158  const std::string &dst_joint,
159  double offset, double multiplier, const PidParams &_p)
160 {
161  std::cerr << "reg: " << src_joint << " -> " << dst_joint << std::endl;
162  std::cerr << "offset: " << offset << ", multiplier: " << multiplier << std::endl;
163 
164  gazebo::physics::JointPtr jp_source;
165  gazebo::physics::JointPtr jp_target;
166  jp_source = model->GetJoint(src_joint);
167  jp_target = model->GetJoint(dst_joint);
168  if (!jp_source || !jp_target) {
169  // warn
170  return;
171  }
172  MimicJointUpdater mimic(jp_source, jp_target, offset, multiplier, _p);
173  mimic_joint_list.push_back(mimic);
174 }
175 
177 {
178  //std::cerr << "up" << std::endl;
179 #if GAZEBO_MAJOR_VERSION < 8
180  gazebo::common::Time dt = world->GetSimTime() - prev_tm;
181 #else
182  gazebo::common::Time dt = world->SimTime() - prev_tm;
183 #endif
184  for(auto it : mimic_joint_list) {
185  it.update(dt);
186  }
187 }
GZ_REGISTER_MODEL_PLUGIN(MimicPlugin)
bool param(const std::string &param_name, T &param_val, const T &default_val)
Gazebo.
void registerMimic(const std::string &src_joint, const std::string &dst_joint, double offset, double multiplier, const PidParams &_p)
void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
Definition: MimicPlugin.cpp:9


seed_r7_gazebo
Author(s):
autogenerated on Sun Apr 18 2021 02:41:01