11 #include <boost/tokenizer.hpp>
27 postureSOUT_(
"Seqplay(" +
name +
")::output(vector)::posture"),
28 leftAnkleSOUT_(
"Seqplay(" +
name +
")::output(MatrixHomo)::leftAnkle"),
29 rightAnkleSOUT_(
"Seqplay(" +
name +
")::output(MatrixHomo)::rightAnkle"),
30 leftAnkleVelSOUT_(
"Seqplay(" +
name +
")::output(Vector)::leftAnkleVel"),
31 rightAnkleVelSOUT_(
"Seqplay(" +
name +
32 ")::output(Vector)::rightAnkleVel"),
33 comSOUT_(
"Seqplay(" +
name +
")::output(vector)::com"),
34 comdotSOUT_(
"Seqplay(" +
name +
")::output(vector)::comdot"),
35 comddotSOUT_(
"Seqplay(" +
name +
")::output(vector)::comddot"),
36 forceLeftFootSOUT_(
"Seqplay(" +
name +
37 ")::output(vector)::forceLeftFoot"),
38 forceRightFootSOUT_(
"Seqplay(" +
name +
39 ")::output(vector)::forceRightFoot"),
40 zmpSOUT_(
"Seqplay(" +
name +
")::output(vector)::zmp"),
77 std::string docstring =
78 "Load files describing a whole-body motion as reference feature "
82 " - string filename without extension\n"
84 " Data is read from the following files:\n"
85 " - posture from file \"filename.posture\",\n"
86 " - left ankle task from \"filename.la\",\n"
87 " - right ankle task from \"filename.ra\",\n"
88 " - center of mass task from \"filename.com\",\n"
89 " - force and moment in left foot from \"filename.fl\",\n"
90 " - force and moment in right foot from \"filename.fr\".\n"
91 " Each file should contain one column for time and as many columns as"
93 " depending on data-type, i.e.:\n"
94 " - 17 for homogeneous matrices,\n"
95 " - 4 for center of mass.\n"
101 for (
size_t i = 0;
i < 7; ++
i) {
107 using boost::escaped_list_separator;
108 typedef boost::tokenizer<escaped_list_separator<char> > tokenizer_t;
111 std::string facultativefn[7];
113 std::ifstream file[4];
114 std::ifstream facultativeFile[7];
115 unsigned int lineNumber = 0;
116 int postureSize = -2;
124 for (std::size_t
i = 0;
i < 4;
i++) {
125 file[
i].open(fn[
i].c_str());
126 if (!file[
i].is_open()) {
127 throw std::runtime_error(std::string(
"Failed to open file ") + fn[
i]);
131 facultativefn[0] =
filename +
".fl";
132 facultativefn[1] =
filename +
".fr";
133 facultativefn[2] =
filename +
".comdot";
134 facultativefn[3] =
filename +
".comddot";
135 facultativefn[4] =
filename +
".zmp";
136 facultativefn[5] =
filename +
".ladot";
137 facultativefn[6] =
filename +
".radot";
140 for (std::size_t
i = 0;
i < 7;
i++) {
141 facultativeFile[
i].open(facultativefn[
i].c_str());
142 if (facultativeFile[
i].is_open()) {
151 throw std::runtime_error(
152 std::string(
"File ") +
159 throw std::runtime_error(
160 std::string(
"File ") +
179 while (file[0].good()) {
180 std::getline(file[0],
line);
182 tokenizer_t tok(
line, escaped_list_separator<char>(
'\\',
'\t',
'\"'));
183 std::vector<double> components;
184 for (tokenizer_t::iterator it = tok.begin(); it != tok.end(); ++it) {
185 components.push_back(atof(it->c_str()));
187 if (components.size() == 0) {
190 if (postureSize == -2) {
191 postureSize =
static_cast<int>(components.size() - 1);
193 if (postureSize !=
static_cast<int>(components.size()) - 1) {
194 std::ostringstream oss;
195 oss << fn[0] <<
", line " << lineNumber <<
": config of size "
196 << components.size() - 1 <<
". Expecting " << postureSize <<
".";
197 throw std::runtime_error(oss.str());
200 Vector config(
static_cast<unsigned>(components.size() - 1));
201 for (
unsigned i = 1;
i < components.size(); ++
i) {
213 while (file[3].good()) {
214 std::getline(file[3],
line);
216 tokenizer_t tok(
line, escaped_list_separator<char>(
'\\',
'\t',
'\"'));
217 std::vector<double> components;
218 for (tokenizer_t::iterator it = tok.begin(); it != tok.end(); ++it) {
219 components.push_back(atof(it->c_str()));
221 if (components.size() == 0)
break;
223 if (components.size() != 4) {
224 std::ostringstream oss;
225 oss << fn[3] <<
", line " << lineNumber <<
": expecting 4 numbers.";
226 throw std::runtime_error(oss.str());
228 time_.push_back(components[0]);
229 for (
unsigned i = 1;
i < 4; ++
i) {
230 com(
i - 1) = components[
i];
245 while (facultativeFile[2].good()) {
246 std::getline(facultativeFile[2],
line);
248 tokenizer_t tok(
line, escaped_list_separator<char>(
'\\',
'\t',
'\"'));
249 std::vector<double> components;
250 for (tokenizer_t::iterator it = tok.begin(); it != tok.end(); ++it) {
251 components.push_back(atof(it->c_str()));
253 if (components.size() == 0)
break;
255 if (components.size() != 4) {
256 std::ostringstream oss;
257 oss << facultativefn[2] <<
", line " << lineNumber
258 <<
": expecting 4 numbers.";
259 throw std::runtime_error(oss.str());
262 for (
unsigned i = 1;
i < 4; ++
i) {
263 comdot(
i - 1) = components[
i];
267 facultativeFile[2].close();
273 while (facultativeFile[3].good()) {
274 std::getline(facultativeFile[3],
line);
276 tokenizer_t tok(
line, escaped_list_separator<char>(
'\\',
'\t',
'\"'));
277 std::vector<double> components;
278 for (tokenizer_t::iterator it = tok.begin(); it != tok.end(); ++it) {
279 components.push_back(atof(it->c_str()));
281 if (components.size() == 0)
break;
283 if (components.size() != 4) {
284 std::ostringstream oss;
285 oss << facultativefn[3] <<
", line " << lineNumber
286 <<
": expecting 4 numbers.";
287 throw std::runtime_error(oss.str());
290 for (
unsigned i = 1;
i < 4; ++
i) {
291 comddot(
i - 1) = components[
i];
295 facultativeFile[3].close();
301 while (facultativeFile[5].good()) {
302 std::getline(facultativeFile[5],
line);
304 tokenizer_t tok(
line, escaped_list_separator<char>(
'\\',
'\t',
'\"'));
305 std::vector<double> components;
306 for (tokenizer_t::iterator it = tok.begin(); it != tok.end(); ++it) {
307 components.push_back(atof(it->c_str()));
309 if (components.size() == 0)
break;
311 if (components.size() != 7) {
312 std::ostringstream oss;
313 oss << facultativefn[5] <<
", line " << lineNumber
314 <<
": expecting 7 numbers.";
315 throw std::runtime_error(oss.str());
318 for (
unsigned i = 1;
i < 7; ++
i) {
319 ankledot(
i - 1) = components[
i];
323 facultativeFile[5].close();
329 while (facultativeFile[6].good()) {
330 std::getline(facultativeFile[6],
line);
332 tokenizer_t tok(
line, escaped_list_separator<char>(
'\\',
'\t',
'\"'));
333 std::vector<double> components;
334 for (tokenizer_t::iterator it = tok.begin(); it != tok.end(); ++it) {
335 components.push_back(atof(it->c_str()));
337 if (components.size() == 0)
break;
339 if (components.size() != 7) {
340 std::ostringstream oss;
341 oss << facultativefn[6] <<
", line " << lineNumber
342 <<
": expecting 7 numbers.";
343 throw std::runtime_error(oss.str());
346 for (
unsigned i = 1;
i < 7; ++
i) {
347 ankledot(
i - 1) = components[
i];
351 facultativeFile[6].close();
357 while (facultativeFile[4].good()) {
358 std::getline(facultativeFile[4],
line);
360 tokenizer_t tok(
line, escaped_list_separator<char>(
'\\',
'\t',
'\"'));
361 std::vector<double> components;
362 for (tokenizer_t::iterator it = tok.begin(); it != tok.end(); ++it) {
363 components.push_back(atof(it->c_str()));
365 if (components.size() == 0)
break;
367 if (components.size() != 4) {
368 std::ostringstream oss;
369 oss << facultativefn[3] <<
", line " << lineNumber
370 <<
": expecting 4 numbers.";
371 throw std::runtime_error(oss.str());
374 for (
unsigned i = 1;
i < 4; ++
i) {
375 zmp(
i - 1) = components[
i];
379 facultativeFile[4].close();
392 std::ostringstream oss;
393 oss <<
"Seqplay: Files should have the same number of lines. Read"
401 oss <<
" " <<
com_.size() <<
" lines from " <<
filename <<
".com,"
405 <<
".fl" << std::endl;
407 <<
".fr" << std::endl;
411 oss <<
" " <<
comdot_.size() <<
" lines from " <<
filename <<
".comdot"
421 oss <<
" " <<
zmp_.size() <<
" lines from " <<
filename <<
".zmp"
427 <<
".ladot" << std::endl;
429 <<
".radot" << std::endl;
432 throw std::runtime_error(oss.str());
449 throw std::runtime_error(
450 "Seqplay posture: Signals not initialized. read files first.");
452 std::size_t configId;
457 if (configId ==
posture_.size() - 1) {
470 throw std::runtime_error(
471 "Seqplay leftAnkle: Signals not initialized. read files first.");
473 std::size_t configId;
478 if (configId ==
posture_.size() - 1) {
491 throw std::runtime_error(
492 "Seqplay rightAnkle: Signals not initialized. read files first.");
494 std::size_t configId;
499 if (configId ==
posture_.size() - 1) {
510 Vector& velocity,
const std::vector<MatrixHomogeneous>& ankleVector,
514 std::size_t configId;
519 if (configId ==
com_.size() - 1) {
526 if (configId > 0 && configId <
com_.size() - 1) {
529 double dt = 1 / (
time_[configId + 1] -
time_[configId]);
530 for (
unsigned i = 0;
i < 3; ++
i) {
531 velocity(
i) = (M1(
i, 3) - M0(
i, 3)) *
dt;
550 std::size_t configId;
555 if (configId ==
posture_.size() - 1) {
570 std::size_t configId;
575 if (configId ==
posture_.size() - 1) {
587 if (
com_.size() == 0) {
588 throw std::runtime_error(
589 "Seqplay com: Signals not initialized. read files first.");
591 std::size_t configId;
596 if (configId ==
com_.size() - 1) {
607 if (
zmp_.size() == 0) {
608 throw std::runtime_error(
"Seqplay zmp: Signals not initialized.");
610 std::size_t configId;
615 if (configId ==
com_.size() - 1) {
621 zmp =
zmp_[configId];
627 std::size_t configId;
632 if (configId ==
posture_.size() - 1) {
643 if (
com_.size() == 0) {
644 throw std::runtime_error(
645 "Seqplay comdot: Signals not initialized. read files first.");
647 std::size_t configId;
652 if (configId ==
com_.size() - 1) {
660 if (configId > 0 && configId <
com_.size() - 1) {
663 double dt = 1 / (
time_[configId + 1] -
time_[configId]);
664 comdot = (q_1 - q_0) *
dt;
672 std::size_t configId;
677 if (configId ==
posture_.size() - 1) {
687 if (
com_.size() == 0) {
688 throw std::runtime_error(
689 "Seqplay comddot: Signals not initialized. read files first.");
691 std::size_t configId;
696 if (configId ==
com_.size() - 1) {
704 if (configId > 0 && configId <
com_.size() - 2) {
708 double dt_0 = 1 / (
time_[configId + 1] -
time_[configId]);
711 qdot_1 =
comdot_[configId + 1];
717 double dt_1 = 1 / (
time_[configId + 2] -
time_[configId + 1]);
719 qdot_1 = (q_2 - q_1) * dt_1;
720 qdot_0 = (q_1 - q_0) * dt_0;
723 for (
int i = 0;
i < comddot.size(); ++
i) {
724 comddot(
i) = (qdot_1(
i) - qdot_0(
i)) * dt_0;
732 const std::vector<Vector>& forceVector,
734 if (forceVector.size() == 0) {
735 throw std::runtime_error(
736 "Seqplay foot force: Force signals not initialized.");
738 std::size_t configId;
743 if (configId == forceVector.size() - 1) {
749 force = forceVector[configId];
762 return "Provide task references for a whole-body motion\n"
764 " The reference trajectories of the following features is loaded "
766 " using command load:\n"
769 " - right ankle, and\n"
770 " - center of mass.\n"
772 " To use this entity,\n"
773 " 1. call method load,\n"
774 " 2. plug reference signals into robot signals and\n"
775 " 3. call method start.\n"
776 " Warning: pluging signals before loading trajectories will fail.\n";
780 std::vector<MatrixHomogeneous>&
data,
781 const std::string& filename) {
782 using boost::escaped_list_separator;
783 typedef boost::tokenizer<escaped_list_separator<char> > tokenizer_t;
784 unsigned int lineNumber = 0;
787 while (file.good()) {
788 std::getline(file,
line);
790 tokenizer_t tok(
line, escaped_list_separator<char>(
'\\',
'\t',
'\"'));
791 std::vector<double> components;
792 for (tokenizer_t::iterator it = tok.begin(); it != tok.end(); ++it) {
793 components.push_back(atof(it->c_str()));
795 if (components.size() == 0)
break;
796 if (components.size() != 17) {
797 std::ostringstream oss;
798 oss <<
filename <<
", line " << lineNumber
799 <<
": expecting 17 numbers, got " << components.size() <<
".";
800 throw std::runtime_error(oss.str());
804 for (
unsigned row = 0; row < 4; ++row) {
805 for (
unsigned col = 0; col < 4; ++col) {
806 la(row, col) = components[
i];
816 const std::string& filename) {
817 using boost::escaped_list_separator;
818 typedef boost::tokenizer<escaped_list_separator<char> > tokenizer_t;
819 unsigned int lineNumber = 0;
822 while (file.good()) {
823 std::getline(file,
line);
825 tokenizer_t tok(
line, escaped_list_separator<char>(
'\\',
'\t',
'\"'));
826 std::vector<double> components;
827 for (tokenizer_t::iterator it = tok.begin(); it != tok.end(); ++it) {
828 components.push_back(atof(it->c_str()));
830 if (components.size() == 0)
break;
831 if (components.size() != 7) {
832 std::ostringstream oss;
833 oss <<
filename <<
", line " << lineNumber
834 <<
": expecting 7 numbers, got " << components.size() <<
".";
835 throw std::runtime_error(oss.str());
838 for (
unsigned i = 1;
i < 7; ++
i) {
839 force(
i - 1) = components[
i];
841 data.push_back(force);