device.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * Nicolas Mansard, Olivier Stasse, François Bleibel, Florent Lamiraux
4  *
5  * CNRS
6  *
7  */
8 
9 /* --------------------------------------------------------------------- */
10 /* --- INCLUDE --------------------------------------------------------- */
11 /* --------------------------------------------------------------------- */
12 
13 /* SOT */
14 #define ENABLE_RT_LOG
15 
16 #include "sot/core/device.hh"
17 
18 #include <iostream>
19 #include <sot/core/debug.hh>
20 #include <sot/core/macros.hh>
21 #include <sot/core/integrator.hh>
22 
23 using namespace std;
24 
26 #include <dynamic-graph/factory.h>
29 
30 #include <Eigen/Geometry>
31 #include <pinocchio/multibody/liegroup/special-euclidean.hpp>
33 using namespace dynamicgraph::sot;
34 using namespace dynamicgraph;
35 
36 const std::string Device::CLASS_NAME = "Device";
37 
38 // Return positive difference between input value and bounds if it saturates,
39 // 0 if it does not saturate
40 inline double saturateBounds(double &val, const double &lower,
41  const double &upper) {
42  double res = 0;
43  assert(lower <= upper);
44  if (val < lower) {
45  res = lower - val;
46  val = lower;
47  return res;
48  }
49  if (upper < val) {
50  res = val - upper;
51  val = upper;
52  return res;
53  }
54  return res;
55 }
56 
57 #define CHECK_BOUNDS(val, lower, upper, what, eps) \
58  for (int i = 0; i < val.size(); ++i) { \
59  double old = val(i); \
60  if (saturateBounds(val(i), lower(i), upper(i)) > eps) { \
61  std::ostringstream oss; \
62  oss << "Robot " what " bound violation at DoF " << i << ": requested " \
63  << old << " but set " << val(i) << '\n'; \
64  SEND_ERROR_STREAM_MSG(oss.str()); \
65  } \
66  }
67 
68 /* --------------------------------------------------------------------- */
69 /* --- CLASS ----------------------------------------------------------- */
70 /* --------------------------------------------------------------------- */
71 
72 Device::~Device() {
73  for (unsigned int i = 0; i < 4; ++i) {
74  delete forcesSOUT[i];
75  }
76 }
77 
78 Device::Device(const std::string &n)
79  : Entity(n),
80  state_(6),
81  sanityCheck_(true),
82  controlInputType_(POSITION_CONTROL),
83  controlSIN(NULL, "Device(" + n + ")::input(double)::control"),
84  attitudeSIN(NULL, "Device(" + n + ")::input(vector3)::attitudeIN"),
85  zmpSIN(NULL, "Device(" + n + ")::input(vector3)::zmp"),
86  stateSOUT("Device(" + n + ")::output(vector)::state")
87  //,attitudeSIN(NULL,"Device::input(matrixRot)::attitudeIN")
88  ,
89  velocitySOUT("Device(" + n + ")::output(vector)::velocity"),
90  attitudeSOUT("Device(" + n + ")::output(matrixRot)::attitude"),
91  motorcontrolSOUT("Device(" + n + ")::output(vector)::motorcontrol"),
92  previousControlSOUT("Device(" + n + ")::output(vector)::previousControl"),
93  ZMPPreviousControllerSOUT("Device(" + n +
94  ")::output(vector)::zmppreviouscontroller"),
95  robotState_("Device(" + n + ")::output(vector)::robotState"),
96  robotVelocity_("Device(" + n + ")::output(vector)::robotVelocity"),
97  pseudoTorqueSOUT("Device(" + n + ")::output(vector)::ptorque")
98 
99  ,
100  lastTimeControlWasRead_(0),
101  controlSize_(0),
102  forceZero6(6) {
103  forceZero6.fill(0);
104  /* --- SIGNALS --- */
105  for (int i = 0; i < 4; ++i) {
106  withForceSignals[i] = false;
107  }
108  forcesSOUT[0] =
109  new Signal<Vector, int>("Device(" + n + ")::output(vector6)::forceRLEG");
110  forcesSOUT[1] =
111  new Signal<Vector, int>("Device(" + n + ")::output(vector6)::forceLLEG");
112  forcesSOUT[2] =
113  new Signal<Vector, int>("Device(" + n + ")::output(vector6)::forceRARM");
114  forcesSOUT[3] =
115  new Signal<Vector, int>("Device(" + n + ")::output(vector6)::forceLARM");
116 
119  << attitudeSOUT << attitudeSIN << zmpSIN << *forcesSOUT[0]
120  << *forcesSOUT[1] << *forcesSOUT[2] << *forcesSOUT[3]
123  state_.fill(.0);
125 
126  velocity_.resize(state_.size());
127  velocity_.setZero();
129 
130  /* --- Commands --- */
131  {
132  std::string docstring;
133  /* Command setStateSize. */
134  docstring =
135  "\n"
136  " Set size of state vector\n"
137  "\n";
139  *this, &Device::setStateSize, docstring));
140  docstring =
141  "\n"
142  " Set state vector value\n"
143  "\n";
145  *this, &Device::setState, docstring));
146 
147  docstring =
148  "\n"
149  " Set velocity vector value\n"
150  "\n";
151  addCommand("setVelocity", new command::Setter<Device, Vector>(
152  *this, &Device::setVelocity, docstring));
153 
154  void (Device::*setRootPtr)(const Matrix &) = &Device::setRoot;
155  docstring = command::docCommandVoid1("Set the root position.",
156  "matrix homogeneous");
157  addCommand("setRoot",
158  command::makeCommandVoid1(*this, setRootPtr, docstring));
159 
160  /* Second Order Integration set. */
161  docstring =
162  "\n"
163  " Set the position calculous starting from \n"
164  " acceleration measure instead of velocity \n"
165  "\n";
166 
167  addCommand("setSecondOrderIntegration",
169  *this, &Device::setSecondOrderIntegration, docstring));
170 
171  /* Display information */
172  docstring =
173  "\n"
174  " Display information on device \n"
175  "\n";
177  docstring));
178 
179  /* SET of control input type. */
180  docstring =
181  "\n"
182  " Set the type of control input which can be \n"
183  " acceleration, velocity, or position\n"
184  "\n";
185 
186  addCommand("setControlInputType",
188  *this, &Device::setControlInputType, docstring));
189 
190  docstring =
191  "\n"
192  " Enable/Disable sanity checks\n"
193  "\n";
194  addCommand("setSanityCheck",
196  docstring));
197 
198  addCommand("setPositionBounds",
201  command::docCommandVoid2("Set robot position bounds",
202  "vector: lower bounds",
203  "vector: upper bounds")));
204 
205  addCommand("setVelocityBounds",
208  command::docCommandVoid2("Set robot velocity bounds",
209  "vector: lower bounds",
210  "vector: upper bounds")));
211 
212  addCommand("setTorqueBounds",
214  *this, &Device::setTorqueBounds,
215  command::docCommandVoid2("Set robot torque bounds",
216  "vector: lower bounds",
217  "vector: upper bounds")));
218 
219  addCommand("getTimeStep",
221  *this, &this->timestep_,
222  command::docDirectGetter("Time step", "double")));
223  }
224 }
225 
226 void Device::getControl(map<string, ControlValues> &controlOut,
227  const double &period) {
228  sotDEBUGIN(25);
229  std::vector<double> control;
230  lastTimeControlWasRead_ += (int)floor(period / Integrator::dt);
231 
233  // Specify the joint values for the controller.
234  control.resize(dgControl.size());
235 
237  CHECK_BOUNDS(dgControl, lowerPosition_, upperPosition_, "position", 1e-6);
238  }
239  for (unsigned int i = 0; i < dgControl.size(); ++i) control[i] = dgControl[i];
240  controlOut["control"].setValues(control);
241  sotDEBUGOUT(25);
242 }
243 
244 void Device::setStateSize(const unsigned int &size) {
245  state_.resize(size);
246  state_.fill(.0);
251 
253 
254  Vector zmp(3);
255  zmp.fill(.0);
257 }
258 
260 
261 int Device::getControlSize() const { return controlSize_; }
262 
263 void Device::setVelocitySize(const unsigned int &size) {
264  velocity_.resize(size);
265  velocity_.fill(.0);
267 }
268 
269 void Device::setState(const Vector &st) {}
270 
271 void Device::setVelocity(const Vector &vel) {
272  velocity_ = vel;
274 }
275 
276 void Device::setRoot(const Matrix &root) {
277  Eigen::Matrix4d _matrix4d(root);
278  MatrixHomogeneous _root(_matrix4d);
279  setRoot(_root);
280 }
281 
282 void Device::setRoot(const MatrixHomogeneous &worldMwaist) {
283  VectorRollPitchYaw r = (worldMwaist.linear().eulerAngles(2, 1, 0)).reverse();
284  Vector q = state_;
285  q = worldMwaist.translation(); // abusive ... but working.
286  for (unsigned int i = 0; i < 3; ++i) q(i + 3) = r(i);
287 }
288 
290 
292 
293 void Device::setControlInputType(const std::string &cit) {}
294 
295 void Device::setSanityCheck(const bool &enableCheck) {}
296 
297 void Device::setPositionBounds(const Vector &lower, const Vector &upper) {
298  std::ostringstream oss;
299  if (lower.size() != controlSize_) {
300  oss << "Lower bound size should be " << controlSize_ << ", got "
301  << lower.size();
302  throw std::invalid_argument(oss.str());
303  }
304  if (upper.size() != controlSize_) {
305  oss << "Upper bound size should be " << controlSize_ << ", got "
306  << upper.size();
307  throw std::invalid_argument(oss.str());
308  }
309  lowerPosition_ = lower;
310  upperPosition_ = upper;
311 }
312 
313 void Device::setVelocityBounds(const Vector &lower, const Vector &upper) {
314  std::ostringstream oss;
315  if (lower.size() != controlSize_) {
316  oss << "Lower bound size should be " << controlSize_ << ", got "
317  << lower.size();
318  throw std::invalid_argument(oss.str());
319  }
320  if (upper.size() != controlSize_) {
321  oss << "Upper bound size should be " << controlSize_ << ", got "
322  << upper.size();
323  throw std::invalid_argument(oss.str());
324  }
325  lowerVelocity_ = lower;
326  upperVelocity_ = upper;
327 }
328 
329 void Device::setTorqueBounds(const Vector &lower, const Vector &upper) {
330  // TODO I think the torque bounds size are controlSize_-6...
331  std::ostringstream oss;
332  if (lower.size() != controlSize_) {
333  oss << "Lower bound size should be " << controlSize_ << ", got "
334  << lower.size();
335  throw std::invalid_argument(oss.str());
336  }
337  if (upper.size() != controlSize_) {
338  oss << "Lower bound size should be " << controlSize_ << ", got "
339  << upper.size();
340  throw std::invalid_argument(oss.str());
341  }
342  lowerTorque_ = lower;
343  upperTorque_ = upper;
344 }
345 
346 /* --- DISPLAY ------------------------------------------------------------ */
347 
348 void Device::display(std::ostream &os) const {
349  os << name << ": " << state_ << endl
350  << "sanityCheck: " << sanityCheck_ << endl
351  << "controlInputType:" << controlInputType_ << endl;
352 }
353 
355  std::cout << name << ": " << state_ << endl
356  << "sanityCheck: " << sanityCheck_ << endl
357  << "controlInputType:" << controlInputType_ << endl;
358 }
Eigen::Transform< double, 3, Eigen::Affine > SOT_CORE_EXPORT MatrixHomogeneous
std::string docDirectGetter(const std::string &name, const std::string &type)
#define CHECK_BOUNDS(val, lower, upper, what, eps)
Definition: device.cpp:57
CommandVoid0< E > * makeCommandVoid0(E &entity, boost::function< void(void)> function, const std::string &docString)
Eigen::VectorXd Vector
Vec3f n
dynamicgraph::Signal< dynamicgraph::Vector, int > robotState_
Definition: device.hh:137
q
DirectGetter< E, T > * makeDirectGetter(E &entity, T *ptr, const std::string &docString)
dynamicgraph::Signal< dynamicgraph::Vector, int > pseudoTorqueSOUT
Definition: device.hh:144
int i
virtual void setRoot(const dynamicgraph::Matrix &root)
Definition: device.cpp:276
dynamicgraph::Vector state_
Definition: device.hh:60
virtual void display(std::ostream &os) const
Definition: device.cpp:348
void signalRegistration(const SignalArray< int > &signals)
dynamicgraph::Signal< dynamicgraph::Vector, int > * forcesSOUT[4]
The force torque sensors.
Definition: device.hh:141
#define sotDEBUGOUT(level)
Definition: debug.hh:212
void setControlSize(const int &size)
Definition: device.cpp:259
virtual void cmdDisplay()
Definition: device.cpp:354
void getControl(std::map< std::string, ControlValues > &anglesOut, const double &period)
Definition: device.cpp:226
void setPositionBounds(const Vector &lower, const Vector &upper)
Definition: device.cpp:297
dynamicgraph::Signal< dynamicgraph::Vector, int > ZMPPreviousControllerSOUT
The ZMP reference send by the previous controller.
Definition: device.hh:128
FCL_REAL r
dynamicgraph::Signal< dynamicgraph::Vector, int > robotVelocity_
Motor velocities.
Definition: device.hh:139
dynamicgraph::Vector forceZero6
Definition: device.hh:156
CommandVoid1< E, T > * makeCommandVoid1(E &entity, boost::function< void(const T &)> function, const std::string &docString)
dynamicgraph::SignalPtr< dynamicgraph::Vector, int > controlSIN
Definition: device.hh:115
virtual void setConstant(const T &t)
virtual void setControlInputType(const std::string &cit)
Definition: device.cpp:293
#define sotDEBUGIN(level)
Definition: debug.hh:211
std::string docCommandVoid1(const std::string &doc, const std::string &type)
void setTorqueBounds(const Vector &lower, const Vector &upper)
Definition: device.cpp:329
static const double dt
Definition: integrator.hh:115
void setSanityCheck(const bool &enableCheck)
Definition: device.cpp:295
Eigen::Vector3d SOT_CORE_EXPORT VectorRollPitchYaw
virtual void setVelocity(const dynamicgraph::Vector &vel)
Definition: device.cpp:271
virtual void setSecondOrderIntegration()
Definition: device.cpp:289
int getControlSize() const
Definition: device.cpp:261
virtual void setNoIntegration()
Definition: device.cpp:291
void setVelocityBounds(const Vector &lower, const Vector &upper)
Definition: device.cpp:313
dynamicgraph::Signal< dynamicgraph::Vector, int > motorcontrolSOUT
The current state of the robot from the command viewpoint.
Definition: device.hh:125
virtual void setStateSize(const unsigned int &size)
Definition: device.cpp:244
FCL_REAL size() const
dynamicgraph::SignalPtr< dynamicgraph::Vector, int > zmpSIN
Definition: device.hh:117
dynamicgraph::Signal< dynamicgraph::Vector, int > previousControlSOUT
Definition: device.hh:126
res
ControlInput controlInputType_
Definition: device.hh:64
dynamicgraph::Vector velocity_
Definition: device.hh:61
Eigen::MatrixXd Matrix
std::string docCommandVoid2(const std::string &doc, const std::string &type1, const std::string &type2)
void setVelocitySize(const unsigned int &size)
Definition: device.cpp:263
dynamicgraph::Signal< MatrixRotation, int > attitudeSOUT
Definition: device.hh:123
virtual void setState(const dynamicgraph::Vector &st)
Definition: device.cpp:269
void addCommand(const std::string &name, command::Command *command)
dynamicgraph::SignalPtr< dynamicgraph::Vector, int > attitudeSIN
Definition: device.hh:116
dynamicgraph::Signal< dynamicgraph::Vector, int > stateSOUT
Definition: device.hh:121
CommandVoid2< E, T1, T2 > * makeCommandVoid2(E &entity, boost::function< void(const T1 &, const T2 &)> function, const std::string &docString)
dynamicgraph::Signal< dynamicgraph::Vector, int > velocitySOUT
Definition: device.hh:122
double saturateBounds(double &val, const double &lower, const double &upper)
Definition: device.cpp:40


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Wed Jun 21 2023 02:51:26