simpleseqplay.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2012, 2013, 2017 LAAS-CNRS
3 //
4 // From Author: Florent Lamiraux, Mehdi Benallegue,
5 // Author: Olivier Stasse, Rohan Budhiraja
6 // Simple sequence player just playing back a set of poses.
7 //
8 
10 
15 
16 #include <boost/tokenizer.hpp>
17 #include <iostream>
18 #include <stdexcept>
19 #include <vector>
20 
21 namespace dynamicgraph {
22 namespace sot {
27 
28 namespace tools {
34 
36  : Entity(name),
37  firstSINTERN(NULL, sotNOSIGNAL,
38  "SimpleSeqPlay(" + name + ")::intern(dummy)::init"),
39  postureSOUT_(boost::bind(&SimpleSeqPlay::computePosture, this, _1, _2),
40  currentPostureSIN_,
41  "SimpleSeqPlay(" + name + ")::output(vector)::posture"),
42  currentPostureSIN_(
43  NULL, "SimpleSeqPlay(" + name + ")::input(vector)::currentPosture"),
44  state_(0),
45  startTime_(0),
46  posture_(),
47  time_(0),
48  dt_(0.001),
49  time_to_start_(3.0),
50  it_nbs_in_state1_(0) {
51  firstSINTERN.setConstant(0);
54 
55  std::string docstring =
56  "Load files describing a whole-body motion as reference feature "
57  "trajectories\n"
58  "\n"
59  " Input:\n"
60  " - string filename without extension\n"
61  "\n"
62  " Data is read from the following files:\n"
63  " - posture from file \"filename.posture\",\n"
64  " The file should contain one column for time and as many columns as"
65  " required\n"
66  " depending of degree-of-freedrom.\n"
67  "\n";
68  addCommand("load", makeCommandVoid1(*this, &SimpleSeqPlay::load, docstring));
69 
71  docCommandVoid0("Start motion")));
72 
73  docstring =
74  "Set the time between the robot current pose and the starting of the "
75  "buffer \n";
76 
77  addCommand("setTimeToStart",
79  *this, &time_to_start_,
80  docDirectSetter("Time to start of the buffer", "double")));
81 }
82 
83 void SimpleSeqPlay::load(const std::string& filename) {
84  state_ = 0;
85 
86  using boost::escaped_list_separator;
87  typedef boost::tokenizer<escaped_list_separator<char> > tokenizer_t;
88  std::string line;
89  std::string fn;
90 
91  std::ifstream file;
92  unsigned int lineNumber = 0;
93  int postureSize = -2;
94  fn = filename + ".posture";
95  // Open file
96  file.open(fn.c_str());
97  if (!file.is_open()) {
98  throw std::runtime_error(std::string("Failed to open file ") + fn);
99  }
100 
101  posture_.clear();
102 
103  // Read posture
104  while (file.good()) {
105  std::getline(file, line);
106  ++lineNumber;
107  tokenizer_t tok(line, escaped_list_separator<char>('\\', ' ', '\"'));
108  std::vector<double> components;
109  for (tokenizer_t::iterator it = tok.begin(); it != tok.end(); ++it) {
110  components.push_back(atof(it->c_str()));
111  }
112  if (components.size() == 0) {
113  break;
114  }
115  if (postureSize == -2) {
116  postureSize = static_cast<int>(components.size() - 1);
117  } else {
118  if (postureSize != static_cast<int>(components.size()) - 1) {
119  std::ostringstream oss;
120  oss << fn << ", line " << lineNumber << ": config of size "
121  << components.size() - 1 << ". Expecting " << postureSize << ".";
122  throw std::runtime_error(oss.str());
123  }
124  }
125  dg::Vector config(static_cast<unsigned>(components.size() - 1));
126  for (unsigned i = 1; i < components.size(); ++i) {
127  config(i - 1) = components[i];
128  }
129  posture_.push_back(config);
130  }
131  file.close();
132 }
133 
135  if (state_ == 0) {
136  state_ = 1;
137  startTime_ = postureSOUT_.getTime();
138  }
139 }
140 
142  std::size_t configId;
143  // If we are still waiting to start
144  if (state_ == 0) {
145  // return the current posture.
147  return pos;
148  }
149  if (posture_.size() == 0) {
150  throw std::runtime_error(
151  "SimpleSeqPlay posture: Signals not initialized. read files first.");
152  }
153 
154  // Going to the first position
155  if (state_ == 1) {
156  // Compute the difference between current posture and desired one.
157  dg::Vector deltapos = posture_[0] - currentPostureSIN_.access(t);
158 
159  // If sufficiently closed to the first posture of the seqplay.
160  if ((deltapos.norm() < 1e-4) ||
161  (((dt_ + 1) * it_nbs_in_state1_) > time_to_start_)) {
162  // Switch to the next state.
163  state_ = 2;
164  startTime_ = postureSOUT_.getTime();
165  pos = posture_[0];
166  } else {
167  // Tries to go closer to the first posture.
168  deltapos = (deltapos * dt_) / (time_to_start_ - dt_ * it_nbs_in_state1_);
169  pos = currentPostureSIN_.access(t) + deltapos;
171  }
172  return pos;
173  }
174  // Tries to go through the list of postures.
175  else if (state_ == 2) {
176  configId = t - startTime_;
177  if (configId == posture_.size() - 1) {
178  state_ = 3;
179  }
180  } else {
181  configId = posture_.size() - 1;
182  }
183  pos = posture_[configId];
184  return pos;
185 }
186 
187 bool SimpleSeqPlay::waiting() const { return state_ == 0; }
188 bool SimpleSeqPlay::initializing() const { return state_ == 1; }
189 bool SimpleSeqPlay::executing() const { return state_ == 2; }
190 bool SimpleSeqPlay::finished() const { return state_ == 3; }
191 
192 std::string SimpleSeqPlay::getDocString() const {
193  return "Provide joint references for a whole-body motion\n"
194  "\n"
195  " The reference trajectories of the joints are loaded from the file\n"
196  " using command load.\n"
197  "\n"
198  " To use this entity,\n"
199  " 1. call method load,\n"
200  " 2. plug reference signals into robot signals and\n"
201  " 3. call method start.\n"
202  " Warning: pluging signals before loading trajectories will fail.\n";
203 }
204 
206 } // namespace tools
207 } // namespace sot
208 } // namespace dynamicgraph
dynamicgraph::sot::tools::SimpleSeqPlay::startTime_
sigtime_t startTime_
Definition: simpleseqplay.hh:54
dynamicgraph
dynamicgraph::sot::tools::SimpleSeqPlay::state_
unsigned int state_
Definition: simpleseqplay.hh:53
i
int i
dynamicgraph::Entity
dynamicgraph::sot::tools::SimpleSeqPlay::initializing
bool initializing() const
Definition: simpleseqplay.cc:188
boost
dynamicgraph::command::docCommandVoid0
std::string docCommandVoid0(const std::string &doc)
dynamicgraph::sot::tools::SimpleSeqPlay::dt_
double dt_
Definition: simpleseqplay.hh:60
dynamicgraph::sot::tools::SimpleSeqPlay
Definition: simpleseqplay.hh:28
dynamicgraph::command::makeCommandVoid0
CommandVoid0< E > * makeCommandVoid0(E &entity, boost::function< void(E *)> function, const std::string &docString)
command-bind.h
dynamicgraph::command::makeDirectGetter
DirectGetter< E, T > * makeDirectGetter(E &entity, T *ptr, const std::string &docString)
dynamicgraph::sot::tools::SimpleSeqPlay::start
void start()
Definition: simpleseqplay.cc:134
dynamicgraph::SignalPtr::access
virtual const T & access(const Time &t)
command-setter.h
dynamicgraph::command::docCommandVoid1
std::string docCommandVoid1(const std::string &doc, const std::string &type)
dynamicgraph::sot::tools::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CubicInterpolationSE3, "CubicInterpolationSE3")
dynamicgraph::sot::tools::SimpleSeqPlay::SimpleSeqPlay
SimpleSeqPlay(const std::string &name)
Definition: simpleseqplay.cc:35
simpleseqplay.hh
command-direct-setter.h
filename
filename
dynamicgraph::sot::tools::SimpleSeqPlay::finished
bool finished() const
Definition: simpleseqplay.cc:190
pos
pos
dynamicgraph::sotNOSIGNAL
SignalArray< sigtime_t > sotNOSIGNAL(0)
dynamicgraph::command::docDirectSetter
std::string docDirectSetter(const std::string &name, const std::string &type)
dynamicgraph::Vector
Eigen::VectorXd Vector
dynamicgraph::sot::tools::SimpleSeqPlay::postureSOUT_
dg::SignalTimeDependent< dg::Vector, sigtime_t > postureSOUT_
Definition: simpleseqplay.hh:32
dynamicgraph::command::docDirectGetter
std::string docDirectGetter(const std::string &name, const std::string &type)
dynamicgraph::sot::tools::SimpleSeqPlay::waiting
bool waiting() const
Definition: simpleseqplay.cc:187
dynamicgraph::sot::tools::SimpleSeqPlay::firstSINTERN
dg::SignalTimeDependent< Dummy, sigtime_t > firstSINTERN
Definition: simpleseqplay.hh:31
dynamicgraph::sot::tools::SimpleSeqPlay::posture_
std::vector< dg::Vector > posture_
Definition: simpleseqplay.hh:56
dynamicgraph::sot::tools::SimpleSeqPlay::currentPostureSIN_
dg::SignalPtr< dg::Vector, sigtime_t > currentPostureSIN_
Definition: simpleseqplay.hh:34
dynamicgraph::sot::tools::SimpleSeqPlay::it_nbs_in_state1_
int it_nbs_in_state1_
Definition: simpleseqplay.hh:64
dynamicgraph::sot::tools::SimpleSeqPlay::getDocString
virtual std::string getDocString() const
Definition: simpleseqplay.cc:192
dynamicgraph::command::makeDirectSetter
DirectSetter< E, T > * makeDirectSetter(E &entity, T *ptr, const std::string &docString)
dynamicgraph::sot::tools::SimpleSeqPlay::time_to_start_
double time_to_start_
Time to start.
Definition: simpleseqplay.hh:62
dynamicgraph::sot::tools::SimpleSeqPlay::computePosture
dg::Vector & computePosture(dg::Vector &pos, int t)
Definition: simpleseqplay.cc:141
dynamicgraph::Entity::addCommand
void addCommand(const std::string &name, command::Command *command)
t
Transform3f t
dynamicgraph::sot::tools::SimpleSeqPlay::executing
bool executing() const
Definition: simpleseqplay.cc:189
command-direct-getter.h
setup.config
config
Definition: setup.in.py:103
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
line
int line
dynamicgraph::command::makeCommandVoid1
CommandVoid1< E, T > * makeCommandVoid1(E &entity, boost::function< void(const T &)> function, const std::string &docString)
compile.name
name
Definition: compile.py:23
dynamicgraph::sot::tools::SimpleSeqPlay::load
void load(const std::string &filename)
Definition: simpleseqplay.cc:83


sot-tools
Author(s): Mehdi Benallegue, Francois Keith, Florent Lamiraux, Thomas Moulard, Olivier Stasse, Jorrit T'Hooft
autogenerated on Wed Aug 2 2023 02:35:13