task.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010,
3  * François Bleibel,
4  * Olivier Stasse,
5  *
6  * CNRS/AIST
7  *
8  */
9 
10 /* --------------------------------------------------------------------- */
11 /* --- INCLUDE --------------------------------------------------------- */
12 /* --------------------------------------------------------------------- */
13 
14 /* SOT */
16 
17 #include <sot/core/debug.hh>
18 #include <sot/core/pool.hh>
19 #include <sot/core/task.hh>
20 
21 #include "../src/task/task-command.h"
22 
23 using namespace std;
24 using namespace dynamicgraph::sot;
25 using namespace dynamicgraph;
26 
27 #include <sot/core/factory.hh>
29 
30 /* --------------------------------------------------------------------- */
31 /* --- CLASS ----------------------------------------------------------- */
32 /* --------------------------------------------------------------------- */
33 
34 Task::Task(const std::string &n)
35  : TaskAbstract(n),
36  featureList(),
37  withDerivative(false),
38  controlGainSIN(NULL, "sotTask(" + n + ")::input(double)::controlGain"),
39  dampingGainSINOUT(NULL, "sotTask(" + n + ")::in/output(double)::damping")
40  // TODO As far as I understand, this is not used in this class.
41  ,
42  controlSelectionSIN(NULL,
43  "sotTask(" + n + ")::input(flag)::controlSelec"),
44  errorSOUT(boost::bind(&Task::computeError, this, _1, _2), sotNOSIGNAL,
45  "sotTask(" + n + ")::output(vector)::error"),
46  errorTimeDerivativeSOUT(
47  boost::bind(&Task::computeErrorTimeDerivative, this, _1, _2),
48  SignalArray<int64_t>(errorSOUT),
49  std::string("sotTask(" + n +
50  ")::output(vector)::errorTimeDerivative")) {
51  taskSOUT.setFunction(
52  boost::bind(&Task::computeTaskExponentialDecrease, this, _1, _2));
53  jacobianSOUT.setFunction(boost::bind(&Task::computeJacobian, this, _1, _2));
54 
58 
60 
61  controlSelectionSIN = true;
62 
65 
66  initCommands();
67 }
68 
69 void Task::initCommands(void) {
70  using namespace dynamicgraph::command;
71  //
72  // Commands
73  //
74  std::string docstring;
75  // AddFeature
76  docstring =
77  " \n"
78  " Add a feature to the task\n"
79  " \n"
80  " Input:\n"
81  " - name of the feature\n"
82  " \n";
83  addCommand("add",
84  makeCommandVoid1(*this, &Task::addFeatureFromName, docstring));
85 
86  addCommand("setWithDerivative",
88  docDirectSetter("withDerivative", "bool")));
89  addCommand("getWithDerivative",
91  docDirectGetter("withDerivative", "bool")));
92  // ClearFeatureList
93  docstring =
94  " \n"
95  " Clear the list of features of the task\n"
96  " \n";
97 
98  addCommand("clear",
99  makeCommandVoid0(*this, &Task::clearFeatureList, docstring));
100  // List features
101  docstring =
102  " \n"
103  " Returns the list of features of the task\n"
104  " \n";
105 
106  addCommand("list", new command::task::ListFeatures(*this, docstring));
107 }
108 
110  featureList.push_back(&s);
111  jacobianSOUT.addDependency(s.jacobianSOUT);
112  errorSOUT.addDependency(s.errorSOUT);
113  errorTimeDerivativeSOUT.addDependency(s.getErrorDot());
114 }
115 
116 void Task::addFeatureFromName(const std::string &featureName) {
117  FeatureAbstract &feature =
118  PoolStorage::getInstance()->getFeature(featureName);
119  addFeature(feature);
120 }
121 
123  for (FeatureList_t::iterator iter = featureList.begin();
124  iter != featureList.end(); ++iter) {
125  FeatureAbstract &s = **iter;
126  jacobianSOUT.removeDependency(s.jacobianSOUT);
127  errorSOUT.removeDependency(s.errorSOUT);
129  }
130 
131  featureList.clear();
132 }
133 
136  Flags fl = controlSelectionSIN.accessCopy();
137  fl &= act;
138  controlSelectionSIN = fl;
139 }
141 
142 void Task::setWithDerivative(const bool &s) { withDerivative = s; }
144 
145 /* --- COMPUTATION ---------------------------------------------------------- */
146 /* --- COMPUTATION ---------------------------------------------------------- */
147 /* --- COMPUTATION ---------------------------------------------------------- */
148 
150  sigtime_t time) {
151  sotDEBUG(15) << "# In " << getName() << " {" << endl;
152 
153  if (featureList.empty()) {
154  throw(ExceptionTask(ExceptionTask::EMPTY_LIST, "Empty feature list"));
155  }
156 
157  try {
158  /* The vector dimensions are not known before the affectation loop.
159  * They thus should be allocated on the flight, in the loop.
160  * The first assumption is that the size has not changed. A double
161  * reallocation (realloc(dim*2)) is done if necessary. In particulary,
162  * [log_2(dim)+1] reallocations are done for the first error computation.
163  * If the allocated size is too large, a correction is done after the loop.
164  * The algotithmic cost is linear in affectation, logarthmic in allocation
165  * numbers and linear in allocation size.
166  * No assumptions are made concerning size of each vector: they are
167  * not said equal, and could be different.
168  */
169 
170  /* First assumption: vector dimensions have not changed. If 0, they are
171  * initialized to dim 1.*/
172  dynamicgraph::Vector::Index dimError = error.size();
173  if (0 == dimError) {
174  dimError = 1;
175  error.resize(dimError);
176  error.setZero();
177  }
178 
179  dynamicgraph::Vector vectTmp;
180  size_type cursorError = 0;
181 
182  /* For each cell of the list, recopy value of s, s_star and error. */
183  for (FeatureList_t::iterator iter = featureList.begin();
184  iter != featureList.end(); ++iter) {
185  FeatureAbstract &feature = **iter;
186 
187  /* Get s, and store it in the s vector. */
188  sotDEBUG(45) << "Feature <" << feature.getName() << ">." << std::endl;
189  const dynamicgraph::Vector &partialError = feature.errorSOUT(time);
190 
191  const dynamicgraph::Vector::Index dim = partialError.size();
192  while (cursorError + dim > dimError) // DEBUG It was >=
193  {
194  dimError *= 2;
195  error.resize(dimError);
196  error.setZero();
197  }
198 
199  for (size_type k = 0; k < dim; ++k) {
200  error(cursorError++) = partialError(k);
201  }
202  sotDEBUG(35) << "feature: " << partialError << std::endl;
203  sotDEBUG(35) << "error: " << error << std::endl;
204  }
205 
206  /* If too much memory has been allocated, resize. */
207  error.conservativeResize(cursorError);
208  } catch SOT_RETHROW;
209 
210  sotDEBUG(35) << "error_final: " << error << std::endl;
211  sotDEBUG(15) << "# Out }" << endl;
212  return error;
213 }
214 
216  dynamicgraph::Vector &res, sigtime_t time) {
217  res.resize(errorSOUT(time).size());
218  dynamicgraph::Vector::Index cursor = 0;
219 
220  for (FeatureList_t::iterator iter = featureList.begin();
221  iter != featureList.end(); ++iter) {
222  FeatureAbstract &feature = **iter;
223 
224  const dynamicgraph::Vector &partialErrorDot = feature.getErrorDot()(time);
225  const dynamicgraph::Vector::Index dim = partialErrorDot.size();
226  res.segment(cursor, dim) = partialErrorDot;
227  cursor += dim;
228  }
229 
230  return res;
231 }
232 
234  VectorMultiBound &errorRef, sigtime_t time) {
235  sotDEBUG(15) << "# In {" << endl;
236  const dynamicgraph::Vector &errSingleBound = errorSOUT(time);
237  const double &gain = controlGainSIN(time);
238  errorRef.resize(errSingleBound.size());
239 
240  for (std::size_t i = 0; i < errorRef.size(); ++i)
241  errorRef[i] = -errSingleBound(i) * gain;
242 
243  if (withDerivative) {
245  for (std::size_t i = 0; i < errorRef.size(); ++i)
246  errorRef[i] = errorRef[i].getSingleBound() - de(i);
247  }
248 
249  sotDEBUG(15) << "# Out }" << endl;
250  return errorRef;
251 }
252 
254  sigtime_t time) {
255  sotDEBUG(15) << "# In {" << endl;
256 
257  if (featureList.empty()) {
258  throw(ExceptionTask(ExceptionTask::EMPTY_LIST, "Empty feature list"));
259  }
260 
261  try {
262  dynamicgraph::Matrix::Index dimJ = J.rows();
263  dynamicgraph::Matrix::Index nbc = J.cols();
264  if (0 == dimJ) {
265  dimJ = 1;
266  J.resize(dimJ, nbc);
267  }
268 
269  dynamicgraph::Matrix::Index cursorJ = 0;
270  // const Flags& selection = controlSelectionSIN(time);
271 
272  /* For each cell of the list, recopy value of s, s_star and error. */
273  for (FeatureList_t::iterator iter = featureList.begin();
274  iter != featureList.end(); ++iter) {
275  FeatureAbstract &feature = **iter;
276  sotDEBUG(25) << "Feature <" << feature.getName() << ">" << endl;
277 
278  /* Get s, and store it in the s vector. */
279  const dynamicgraph::Matrix &partialJacobian = feature.jacobianSOUT(time);
280  const dynamicgraph::Matrix::Index nbr = partialJacobian.rows();
281  sotDEBUG(25) << "Jp =" << endl << partialJacobian << endl;
282 
283  if (0 == nbc) {
284  nbc = partialJacobian.cols();
285  J.resize(nbc, dimJ);
286  } else if (partialJacobian.cols() != nbc)
287  throw ExceptionTask(
289  "Features from the list don't have compatible-size jacobians.");
290 
291  while (cursorJ + nbr >= dimJ) {
292  dimJ *= 2;
293  J.conservativeResize(dimJ, nbc);
294  }
295  // TODO If controlSelectionSIN is really to be removed,
296  // then the following loop is equivalent to:
297  // J.middleRows (cursorJ, nbr) = partialJacobian;
298  for (size_type kc = 0; kc < nbc; ++kc) {
299  // if( selection(kc) )
300  for (size_type k = 0; k < nbr; ++k) {
301  J(cursorJ + k, kc) = partialJacobian(k, kc);
302  }
303  // else
304  // for( std::size_t k=0;k<nbr;++k ) J(cursorJ+k,kc) = 0.;
305  }
306  cursorJ += nbr;
307  }
308 
309  /* If too much memory has been allocated, resize. */
310  J.conservativeResize(cursorJ, nbc);
311  } catch SOT_RETHROW;
312 
313  sotDEBUG(15) << "# Out }" << endl;
314  return J;
315 }
316 
317 /* --- DISPLAY ------------------------------------------------------------ */
318 /* --- DISPLAY ------------------------------------------------------------ */
319 /* --- DISPLAY ------------------------------------------------------------ */
320 
321 void Task::display(std::ostream &os) const {
322  os << "Task " << name << ": " << endl;
323  os << "--- LIST --- " << std::endl;
324 
325  for (FeatureList_t::const_iterator iter = featureList.begin();
326  iter != featureList.end(); ++iter) {
327  os << "-> " << (*iter)->getName() << endl;
328  }
329 }
330 
331 std::ostream &Task::writeGraph(std::ostream &os) const {
332  FeatureList_t::const_iterator itFeatureAbstract;
333  itFeatureAbstract = featureList.begin();
334  while (itFeatureAbstract != featureList.end()) {
335  os << "\t\"" << (*itFeatureAbstract)->getName() << "\" -> \"" << getName()
336  << "\"" << endl;
337  itFeatureAbstract++;
338  }
339  return os;
340 }
dynamicgraph::sot::Task::errorTimeDerivativeSOUT
dynamicgraph::SignalTimeDependent< dynamicgraph::Vector, sigtime_t > errorTimeDerivativeSOUT
Definition: task.hh:115
factory.hh
dynamicgraph::sot::Task::writeGraph
virtual std::ostream & writeGraph(std::ostream &os) const
Definition: task.cpp:331
dynamicgraph::sot::Task::controlSelectionSIN
dynamicgraph::SignalPtr< Flags, sigtime_t > controlSelectionSIN
Definition: task.hh:112
dynamicgraph::sot::Task::getWithDerivative
bool getWithDerivative(void)
Definition: task.cpp:143
dynamicgraph::sot::Task::controlGainSIN
dynamicgraph::SignalPtr< double, sigtime_t > controlGainSIN
Definition: task.hh:110
dynamicgraph::sot::Task::setWithDerivative
void setWithDerivative(const bool &s)
Definition: task.cpp:142
dynamicgraph::sot::FeatureAbstract::errorSOUT
SignalTimeDependent< dynamicgraph::Vector, sigtime_t > errorSOUT
This signal returns the error between the desired value and the current value : .
Definition: feature-abstract.hh:185
dynamicgraph::sot::Task::computeTaskExponentialDecrease
VectorMultiBound & computeTaskExponentialDecrease(VectorMultiBound &errorRef, sigtime_t time)
Definition: task.cpp:233
dynamicgraph::sot::Task::withDerivative
bool withDerivative
Definition: task.hh:78
dynamicgraph
J
J
dynamicgraph::sot::VectorMultiBound
std::vector< MultiBound > VectorMultiBound
Definition: multi-bound.hh:72
dynamicgraph::command
dynamicgraph::sot::Task::addControlSelection
void addControlSelection(const Flags &act)
Definition: task.cpp:135
i
int i
dynamicgraph::sot::Task::computeError
dynamicgraph::Vector & computeError(dynamicgraph::Vector &error, sigtime_t time)
Definition: task.cpp:149
task.hh
boost
dynamicgraph::Entity::name
std::string name
dynamicgraph::Matrix
Eigen::MatrixXd Matrix
dynamicgraph::sot::FeatureAbstract::jacobianSOUT
SignalTimeDependent< dynamicgraph::Matrix, sigtime_t > jacobianSOUT
Jacobian of the error wrt the robot state: .
Definition: feature-abstract.hh:193
dynamicgraph::sot::Task::clearControlSelection
void clearControlSelection(void)
Definition: task.cpp:140
dynamicgraph::sot::ExceptionTask::NON_ADEQUATE_FEATURES
@ NON_ADEQUATE_FEATURES
Definition: exception-task.hh:36
dynamicgraph::sot::command::task::ListFeatures
Definition: task-command.h:26
dynamicgraph::sot::TaskAbstract
Definition: task-abstract.hh:51
dynamicgraph::sot::Task::initCommands
void initCommands(void)
Definition: task.cpp:69
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(Task, "Task")
dynamicgraph::sot::FeatureAbstract
This class gives the abstract definition of a feature.
Definition: feature-abstract.hh:76
dynamicgraph::sot::Task::featureList
FeatureList_t featureList
Definition: task.hh:77
dynamicgraph::Entity::getName
const std::string & getName() const
makeCommandVoid0
CommandVoid0< E > * makeCommandVoid0(E &entity, boost::function< void(E *)> function, const std::string &docString)
debug.hh
makeDirectGetter
DirectGetter< E, T > * makeDirectGetter(E &entity, T *ptr, const std::string &docString)
dynamicgraph::sot::Task::computeErrorTimeDerivative
dynamicgraph::Vector & computeErrorTimeDerivative(dynamicgraph::Vector &res, sigtime_t time)
Definition: task.cpp:215
res
res
s
s
SOT_RETHROW
#define SOT_RETHROW
Definition: exception-abstract.hh:123
dim
int dim
dynamicgraph::sigtime_t
int64_t sigtime_t
dynamicgraph::SignalTimeDependent::addDependency
virtual void addDependency(const SignalBase< Time > &signal)
size
FCL_REAL size() const
Index
std::size_t Index
dynamicgraph::sot::TaskAbstract::jacobianSOUT
dynamicgraph::SignalTimeDependent< dynamicgraph::Matrix, sigtime_t > jacobianSOUT
Definition: task-abstract.hh:80
dynamicgraph::SignalTimeDependent::removeDependency
virtual void removeDependency(const SignalBase< Time > &signal)
docDirectSetter
std::string docDirectSetter(const std::string &name, const std::string &type)
dynamicgraph::sot::ExceptionTask
Definition: exception-task.hh:29
dynamicgraph::size_type
Matrix::Index size_type
dynamicgraph::Vector
Eigen::VectorXd Vector
dynamicgraph::sot::Task::errorSOUT
dynamicgraph::SignalTimeDependent< dynamicgraph::Vector, sigtime_t > errorSOUT
Definition: task.hh:113
dynamicgraph::sot::Task::computeJacobian
dynamicgraph::Matrix & computeJacobian(dynamicgraph::Matrix &J, sigtime_t time)
Definition: task.cpp:253
pool.hh
docDirectGetter
std::string docDirectGetter(const std::string &name, const std::string &type)
dynamicgraph::sot::TaskAbstract::taskSOUT
dynamicgraph::SignalTimeDependent< VectorMultiBound, sigtime_t > taskSOUT
Definition: task-abstract.hh:78
dynamicgraph::sot::Task
Class that defines the basic elements of a task.
Definition: task.hh:72
dynamicgraph::sot::Task::display
void display(std::ostream &os) const
Definition: task.cpp:321
dynamicgraph::PoolStorage::getInstance
static PoolStorage * getInstance()
Definition: pool.cpp:147
dynamicgraph::sot
dynamicgraph::sot::ExceptionTask::EMPTY_LIST
@ EMPTY_LIST
Definition: exception-task.hh:35
dynamicgraph::sot::FeatureAbstract::getErrorDot
virtual SignalTimeDependent< dynamicgraph::Vector, sigtime_t > & getErrorDot()
Definition: feature-abstract.hh:202
makeDirectSetter
DirectSetter< E, T > * makeDirectSetter(E &entity, T *ptr, const std::string &docString)
dynamicgraph::Entity::addCommand
void addCommand(const std::string &name, command::Command *command)
dynamicgraph::SignalArray
all-commands.h
dynamicgraph::sot::Flags
Definition: flags.hh:33
dynamicgraph::sot::Task::addFeature
void addFeature(FeatureAbstract &s)
Definition: task.cpp:109
dynamicgraph::sot::Task::clearFeatureList
void clearFeatureList(void)
Definition: task.cpp:122
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
sotNOSIGNAL
DYNAMIC_GRAPH_DLLAPI SignalArray< sigtime_t > sotNOSIGNAL
dynamicgraph::sot::Task::dampingGainSINOUT
dynamicgraph::SignalPtr< double, sigtime_t > dampingGainSINOUT
Definition: task.hh:111
n
Vec3f n
makeCommandVoid1
CommandVoid1< E, T > * makeCommandVoid1(E &entity, boost::function< void(const T &)> function, const std::string &docString)
dynamicgraph::sot::Task::setControlSelection
void setControlSelection(const Flags &act)
Definition: task.cpp:134
dynamicgraph::sot::Task::addFeatureFromName
void addFeatureFromName(const std::string &name)
Definition: task.cpp:116
sotDEBUG
#define sotDEBUG(level)
Definition: debug.hh:168


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