feature-posture.cpp
Go to the documentation of this file.
1 // Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
2 // JRL, CNRS/AIST.
3 
5 #include <dynamic-graph/factory.h>
6 #include <dynamic-graph/pool.h>
7 
8 #include <boost/assign/list_of.hpp>
9 #include <boost/numeric/conversion/cast.hpp>
11 #include <string>
12 namespace dg = ::dynamicgraph;
13 
15 
16 namespace dynamicgraph {
17 namespace sot {
18 using command::Command;
19 using command::Value;
20 
22  public:
23  virtual ~SelectDof() {}
24  SelectDof(FeaturePosture &entity, const std::string &docstring)
25  : Command(entity,
26  boost::assign::list_of(Value::UNSIGNEDLONGINT)(Value::BOOL),
27  docstring) {}
28  virtual Value doExecute() {
29  FeaturePosture &feature = static_cast<FeaturePosture &>(owner());
30  std::vector<Value> values = getParameterValues();
31  std::size_t dofId = values[0].value();
32  bool control = values[1].value();
33  feature.selectDof(dofId, control);
34  return Value();
35  }
36 }; // class SelectDof
37 
40  state_(NULL, "FeaturePosture(" + name + ")::input(Vector)::state"),
41  posture_(0, "FeaturePosture(" + name + ")::input(Vector)::posture"),
42  postureDot_(0, "FeaturePosture(" + name + ")::input(Vector)::postureDot"),
43  activeDofs_(),
44  nbActiveDofs_(0) {
46 
48  jacobianSOUT.setConstant(Matrix());
49 
50  std::string docstring;
51  docstring =
52  " \n"
53  " Select degree of freedom to control\n"
54  " \n"
55  " input:\n"
56  " - positive integer: rank of degree of freedom,\n"
57  " - boolean: whether to control the selected degree of "
58  "freedom.\n"
59  " \n"
60  " Note: rank should be more than 5 since posture is "
61  "independent\n"
62  " from freeflyer position.\n"
63  " \n";
64  addCommand("selectDof", new SelectDof(*this, docstring));
65 }
66 
68 
70  res = static_cast<std::size_t>(nbActiveDofs_);
71  return res;
72 }
73 
75  const dg::Vector &state = state_.access(t);
76  const dg::Vector &posture = posture_.access(t);
77 
78  res.resize(nbActiveDofs_);
79  std::size_t index = 0;
80  for (std::size_t i = 0; i < activeDofs_.size(); ++i) {
81  if (activeDofs_[i]) {
82  res(index) = state(i) - posture(i);
83  index++;
84  }
85  }
86  return res;
87 }
88 
90  throw std::runtime_error(
91  "jacobian signal should be constant."
92  " This function should never be called");
93 }
94 
96  const Vector &postureDot = postureDot_.access(t);
97 
98  res.resize(nbActiveDofs_);
99  std::size_t index = 0;
100  for (std::size_t i = 0; i < activeDofs_.size(); ++i) {
101  if (activeDofs_[i]) res(index++) = -postureDot(i);
102  }
103  return res;
104 }
105 
106 void FeaturePosture::selectDof(std::size_t dofId, bool control) {
107  const Vector &state = state_.accessCopy();
108  const Vector &posture = posture_.accessCopy();
109  std::size_t dim(state.size());
110 
111  if (dim != (std::size_t)posture.size()) {
112  throw std::runtime_error("Posture and State should have same dimension.");
113  }
114  // If activeDof_ vector not initialized, initialize it
115  if (activeDofs_.size() != dim) {
116  activeDofs_ = std::vector<bool>(dim, false);
117  nbActiveDofs_ = 0;
118  }
119 
120  // Check that selected dof id is valid
121  if (dofId >= dim) {
122  std::ostringstream oss;
123  oss << "dof id should less than state dimension: " << dim << ". Received "
124  << dofId << ".";
126  }
127 
128  if (control) {
129  if (!activeDofs_[dofId]) {
130  activeDofs_[dofId] = true;
131  nbActiveDofs_++;
132  }
133  } else { // control = false
134  if (activeDofs_[dofId]) {
135  activeDofs_[dofId] = false;
136  nbActiveDofs_--;
137  }
138  }
139  // recompute jacobian
140  Matrix J(Matrix::Zero(nbActiveDofs_, dim));
141 
142  std::size_t index = 0;
143  for (std::size_t i = 0; i < activeDofs_.size(); ++i) {
144  if (activeDofs_[i]) {
145  J(index, i) = 1;
146  index++;
147  }
148  }
149 
150  jacobianSOUT.setConstant(J);
151 }
152 
154 } // namespace sot
155 } // namespace dynamicgraph
dynamicgraph::sot::FeaturePosture::nbActiveDofs_
std::size_t nbActiveDofs_
Definition: feature-posture.hh:82
dynamicgraph::sot::FeaturePosture::posture_
signalIn_t posture_
Definition: feature-posture.hh:76
dynamicgraph::sot::FeaturePosture::computeError
virtual dynamicgraph::Vector & computeError(dynamicgraph::Vector &res, sigtime_t)
Compute the error between the desired feature and the current value of the feature measured or deduce...
Definition: feature-posture.cpp:74
dynamicgraph::SignalPtr::accessCopy
virtual const T & accessCopy() const
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
index
index
J
J
i
int i
dynamicgraph::sot::FeaturePosture::computeErrorDot
virtual dynamicgraph::Vector & computeErrorDot(dynamicgraph::Vector &res, sigtime_t time)
Definition: feature-posture.cpp:95
boost
dynamicgraph::command::Command::getParameterValues
const std::vector< Value > & getParameterValues() const
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::FeaturePosture::activeDofs_
std::vector< bool > activeDofs_
Definition: feature-posture.hh:81
dynamicgraph::sot::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(FeaturePosture, "FeaturePosture")
dynamicgraph::sot::FeatureAbstract
This class gives the abstract definition of a feature.
Definition: feature-abstract.hh:76
command-bind.h
feature-posture.hh
res
res
dynamicgraph::SignalPtr::access
virtual const T & access(const Time &t)
dynamicgraph::sot::FeaturePosture::~FeaturePosture
virtual ~FeaturePosture()
Definition: feature-posture.cpp:67
dim
int dim
dynamicgraph::sigtime_t
int64_t sigtime_t
dynamicgraph::ExceptionAbstract::TOOLS
TOOLS
dynamicgraph::ExceptionAbstract
dynamicgraph::SignalTimeDependent::addDependency
virtual void addDependency(const SignalBase< Time > &signal)
dynamicgraph::sot::FeaturePosture::SelectDof
friend class SelectDof
Definition: feature-posture.hh:50
dynamicgraph::command::Command
dynamicgraph::size_type
Matrix::Index size_type
dynamicgraph::Vector
Eigen::VectorXd Vector
dynamicgraph::sot::Matrix
Matrix
Definition: integrator-euler.t.cpp:45
dynamicgraph::sot::FeaturePosture
Definition: feature-posture.hh:49
dynamicgraph::sot::FeaturePosture::FeaturePosture
FeaturePosture(const std::string &name)
Definition: feature-posture.cpp:38
dynamicgraph::sot::FeaturePosture::SelectDof::~SelectDof
virtual ~SelectDof()
Definition: feature-posture.cpp:23
dynamicgraph::sot::FeaturePosture::SelectDof::doExecute
virtual Value doExecute()
Definition: feature-posture.cpp:28
dynamicgraph::sot::FeaturePosture::computeJacobian
virtual dynamicgraph::Matrix & computeJacobian(dynamicgraph::Matrix &res, sigtime_t)
Compute the Jacobian of the error according the robot state.
Definition: feature-posture.cpp:89
values
list values
dynamicgraph::sot::FeatureAbstract::getDimension
size_type getDimension(void) const
Shortest method.
Definition: feature-abstract.hh:121
dynamicgraph::Entity::addCommand
void addCommand(const std::string &name, command::Command *command)
dynamicgraph::sot::FeaturePosture::state_
signalIn_t state_
Definition: feature-posture.hh:75
t
Transform3f t
dynamicgraph::sot::FeaturePosture::SelectDof::SelectDof
SelectDof(FeaturePosture &entity, const std::string &docstring)
Definition: feature-posture.cpp:24
dynamicgraph::sot::FeaturePosture::SelectDof
Definition: feature-posture.cpp:21
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
dynamicgraph::command::Value
dynamicgraph::sot::FeaturePosture::selectDof
void selectDof(std::size_t dofId, bool control)
Definition: feature-posture.cpp:106
dynamicgraph::sot::FeaturePosture::postureDot_
signalIn_t postureDot_
Definition: feature-posture.hh:77
compile.name
name
Definition: compile.py:23
dynamicgraph::command::Command::owner
Entity & owner()


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