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 (size_type 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 (std::size_t 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 (size_type i = 0; i < 4; ++i) {
106  withForceSignals[i] = false;
107  }
109  "Device(" + n + ")::output(vector6)::forceRLEG");
111  "Device(" + n + ")::output(vector6)::forceLLEG");
113  "Device(" + n + ")::output(vector6)::forceRARM");
115  "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;
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 (size_type i = 0; i < dgControl.size(); ++i) control[i] = dgControl[i];
240  controlOut["control"].setValues(control);
241  sotDEBUGOUT(25);
242 }
243 
245  state_.resize(size);
246  state_.fill(.0);
251 
253 
254  Vector zmp(3);
255  zmp.fill(.0);
257 }
258 
260 
262 
264  velocity_.resize(size);
265  velocity_.fill(.0);
267 }
268 
269 void Device::setState(const Vector &) {}
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 (std::size_t i = 0; i < 3; ++i) q(i + 3) = r(i);
287 }
288 
290 
292 
293 void Device::setControlInputType(const std::string &) {}
294 
295 void Device::setSanityCheck(const bool &) {}
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 }
dynamicgraph::Signal< Vector, sigtime_t >
dynamicgraph::sot::Device::setRoot
virtual void setRoot(const dynamicgraph::Matrix &root)
Definition: device.cpp:276
dynamicgraph::sot::MatrixHomogeneous
Eigen::Transform< double, 3, Eigen::Affine > SOT_CORE_EXPORT MatrixHomogeneous
Definition: matrix-geometry.hh:75
dynamicgraph::sot::Device::controlSIN
dynamicgraph::SignalPtr< dynamicgraph::Vector, sigtime_t > controlSIN
Definition: device.hh:115
dynamicgraph::sot::Device
Definition: device.hh:47
dynamicgraph
dynamicgraph::sot::Device::cmdDisplay
virtual void cmdDisplay()
Definition: device.cpp:354
dynamicgraph::sot::Device::forceZero6
dynamicgraph::Vector forceZero6
Definition: device.hh:157
dynamicgraph::sot::Device::state_
dynamicgraph::Vector state_
Definition: device.hh:60
i
int i
dynamicgraph::sot::Device::setPositionBounds
void setPositionBounds(const Vector &lower, const Vector &upper)
Definition: device.cpp:297
dynamicgraph::Entity
dynamicgraph::sot::Device::setControlSize
void setControlSize(const size_type &size)
Definition: device.cpp:259
dynamicgraph::sot::Device::getControl
void getControl(std::map< std::string, ControlValues > &anglesOut, const double &period)
Definition: device.cpp:226
dynamicgraph::sot::Device::setControlInputType
virtual void setControlInputType(const std::string &cit)
Definition: device.cpp:293
dynamicgraph::sot::Device::display
virtual void display(std::ostream &os) const
Definition: device.cpp:348
dynamicgraph::Entity::name
std::string name
dynamicgraph::Matrix
Eigen::MatrixXd Matrix
dynamicgraph::sot::Device::forcesSOUT
dynamicgraph::Signal< dynamicgraph::Vector, sigtime_t > * forcesSOUT[4]
The force torque sensors.
Definition: device.hh:142
dynamicgraph::sot::Device::upperVelocity_
Vector upperVelocity_
Definition: device.hh:71
dynamicgraph::sot::Device::controlSize_
size_type controlSize_
Definition: device.hh:155
dynamicgraph::sot::Device::setSecondOrderIntegration
virtual void setSecondOrderIntegration()
Definition: device.cpp:289
dynamicgraph::sot::Device::setSanityCheck
void setSanityCheck(const bool &enableCheck)
Definition: device.cpp:295
r
FCL_REAL r
dynamicgraph::sot::Device::upperTorque_
Vector upperTorque_
Definition: device.hh:72
dynamicgraph::sot::Device::setTorqueBounds
void setTorqueBounds(const Vector &lower, const Vector &upper)
Definition: device.cpp:329
dynamicgraph::command::makeCommandVoid0
CommandVoid0< E > * makeCommandVoid0(E &entity, boost::function< void(E *)> function, const std::string &docString)
debug.hh
dynamicgraph::command::makeDirectGetter
DirectGetter< E, T > * makeDirectGetter(E &entity, T *ptr, const std::string &docString)
res
res
dynamicgraph::sot::Device::lastTimeControlWasRead_
sigtime_t lastTimeControlWasRead_
Definition: device.hh:154
dynamicgraph::sot::Device::setVelocity
virtual void setVelocity(const dynamicgraph::Vector &vel)
Definition: device.cpp:271
device.hh
dynamicgraph::sot::VectorRollPitchYaw
Eigen::Vector3d SOT_CORE_EXPORT VectorRollPitchYaw
Definition: matrix-geometry.hh:80
sotDEBUGOUT
#define sotDEBUGOUT(level)
Definition: debug.hh:215
dynamicgraph::sot::Device::zmpSIN
dynamicgraph::SignalPtr< dynamicgraph::Vector, sigtime_t > zmpSIN
Definition: device.hh:117
dynamicgraph::command::docCommandVoid1
std::string docCommandVoid1(const std::string &doc, const std::string &type)
dynamicgraph::sigtime_t
int64_t sigtime_t
dynamicgraph::sot::Device::lowerTorque_
Vector lowerTorque_
Definition: device.hh:75
dynamicgraph::sot::Device::setVelocityBounds
void setVelocityBounds(const Vector &lower, const Vector &upper)
Definition: device.cpp:313
saturateBounds
double saturateBounds(double &val, const double &lower, const double &upper)
Definition: device.cpp:40
dynamicgraph::sot::Device::lowerPosition_
Vector lowerPosition_
Definition: device.hh:73
dynamicgraph::sot::Device::pseudoTorqueSOUT
dynamicgraph::Signal< dynamicgraph::Vector, sigtime_t > pseudoTorqueSOUT
Definition: device.hh:145
sotDEBUGIN
#define sotDEBUGIN(level)
Definition: debug.hh:214
size
FCL_REAL size() const
dynamicgraph::sot::Device::motorcontrolSOUT
dynamicgraph::Signal< dynamicgraph::Vector, sigtime_t > motorcontrolSOUT
The current state of the robot from the command viewpoint.
Definition: device.hh:125
dynamicgraph::sot::Device::getControlSize
size_type getControlSize() const
Definition: device.cpp:261
q
q
dynamicgraph::size_type
Matrix::Index size_type
integrator.hh
dynamicgraph::Vector
Eigen::VectorXd Vector
dynamicgraph::sot::POSITION_CONTROL
@ POSITION_CONTROL
Definition: device.hh:35
dynamicgraph::sot::Device::upperPosition_
Vector upperPosition_
Definition: device.hh:70
dynamicgraph::sot::Device::previousControlSOUT
dynamicgraph::Signal< dynamicgraph::Vector, sigtime_t > previousControlSOUT
Definition: device.hh:126
dynamicgraph::sot::Device::setNoIntegration
virtual void setNoIntegration()
Definition: device.cpp:291
dynamicgraph::Signal::setConstant
virtual void setConstant(const T &t)
dynamicgraph::command::makeCommandVoid2
CommandVoid2< E, T1, T2 > * makeCommandVoid2(E &entity, boost::function< void(const T1 &, const T2 &)> function, const std::string &docString)
real-time-logger.h
dynamicgraph::command::docCommandVoid2
std::string docCommandVoid2(const std::string &doc, const std::string &type1, const std::string &type2)
dynamicgraph::sot::Device::sanityCheck_
bool sanityCheck_
Definition: device.hh:62
dynamicgraph::sot::Device::timestep_
double timestep_
Definition: device.hh:66
dynamicgraph::sot::Device::setVelocitySize
void setVelocitySize(const size_type &size)
Definition: device.cpp:263
linear-algebra.h
dynamicgraph::sot::Device::attitudeSIN
dynamicgraph::SignalPtr< dynamicgraph::Vector, sigtime_t > attitudeSIN
Definition: device.hh:116
dynamicgraph::sot::Device::velocity_
dynamicgraph::Vector velocity_
Definition: device.hh:61
dynamicgraph::command::docDirectGetter
std::string docDirectGetter(const std::string &name, const std::string &type)
dynamicgraph::sot::Device::velocitySOUT
dynamicgraph::Signal< dynamicgraph::Vector, sigtime_t > velocitySOUT
Definition: device.hh:122
dynamicgraph::sot::Integrator::dt
static const double dt
Definition: integrator.hh:117
matrix-geometry.hh
dynamicgraph::sot::Device::controlInputType_
ControlInput controlInputType_
Definition: device.hh:64
CHECK_BOUNDS
#define CHECK_BOUNDS(val, lower, upper, what, eps)
Definition: device.cpp:57
dynamicgraph::sot::Device::robotState_
dynamicgraph::Signal< dynamicgraph::Vector, sigtime_t > robotState_
Definition: device.hh:138
dynamicgraph::sot::Device::ZMPPreviousControllerSOUT
dynamicgraph::Signal< dynamicgraph::Vector, sigtime_t > ZMPPreviousControllerSOUT
The ZMP reference send by the previous controller.
Definition: device.hh:129
dynamicgraph::sot::Device::robotVelocity_
dynamicgraph::Signal< dynamicgraph::Vector, sigtime_t > robotVelocity_
Motor velocities.
Definition: device.hh:140
dynamicgraph::sot
dynamicgraph::sot::Device::withForceSignals
bool withForceSignals[4]
Definition: device.hh:65
dynamicgraph::sot::Device::stateSOUT
dynamicgraph::Signal< dynamicgraph::Vector, sigtime_t > stateSOUT
Definition: device.hh:121
dynamicgraph::Entity::addCommand
void addCommand(const std::string &name, command::Command *command)
dynamicgraph::sot::Device::attitudeSOUT
dynamicgraph::Signal< MatrixRotation, sigtime_t > attitudeSOUT
Definition: device.hh:123
dynamicgraph::sot::Device::setState
virtual void setState(const dynamicgraph::Vector &st)
Definition: device.cpp:269
all-commands.h
dynamicgraph::sot::Device::lowerVelocity_
Vector lowerVelocity_
Definition: device.hh:74
dynamicgraph::command::Setter
macros.hh
dynamicgraph::sot::Device::setStateSize
virtual void setStateSize(const size_type &size)
Definition: device.cpp:244
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
n
Vec3f n
dynamicgraph::command::makeCommandVoid1
CommandVoid1< E, T > * makeCommandVoid1(E &entity, boost::function< void(const T &)> function, const std::string &docString)


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Tue Oct 24 2023 02:26:31