-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathrobotExecuteTrajectory.cpp
95 lines (82 loc) · 2.7 KB
/
robotExecuteTrajectory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* \file robotExecuteTrajectory.cpp
* \author Freek Stulp
*
* \ingroup Demos
*
* This file is part of DmpBbo, a set of libraries and programs for the
* black-box optimization of dynamical movement primitives.
* Copyright (C) 2014 Freek Stulp, ENSTA-ParisTech
*
* DmpBbo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* DmpBbo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with DmpBbo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/filesystem.hpp>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include "dmp/Trajectory.hpp"
#include "eigenutils/eigen_file_io.hpp"
#include "runSimulationThrowBall.hpp"
using namespace nlohmann;
using namespace std;
using namespace Eigen;
using namespace DmpBbo;
void help(char* binary_name)
{
cout << "Usage: " << binary_name
<< " <dmp filename.xml> <output trajectory filename.txt>" << endl;
}
/** Main function
* \param[in] n_args Number of arguments
* \param[in] args Arguments themselves
* \return Success of exection. 0 if successful.
*/
int main(int n_args, char** args)
{
if (n_args != 3) {
help(args[0]);
return -1;
}
if (string(args[1]).compare("--help") == 0) {
help(args[0]);
return 0;
}
string traj_filename = string(args[1]);
string cost_vars_filename = string(args[2]);
cout << "C++ | Reading traj from file '" << traj_filename << "'"
<< endl;
// Load trajectory
Trajectory trajectory;
trajectory = Trajectory::readFromFile(traj_filename);
// Prepare simulation
VectorXd ts = trajectory.ts();
MatrixXd y_des = trajectory.ys();
MatrixXd yd_des = trajectory.yds();
MatrixXd ydd_des = trajectory.ydds();
int n_time_steps = trajectory.length();
MatrixXd cost_vars = MatrixXd(n_time_steps, 1 + 6 * 2 + 1);
// Run simulation
ThrowBallSimulator simulator;
double dt = ts[1] - ts[0];
for (int ii = 0; ii < n_time_steps; ii++) {
if (ii >= 1) dt = ts[ii] - ts[ii - 1];
simulator.integrateStep(dt, y_des.row(ii), yd_des.row(ii), ydd_des.row(ii));
cost_vars.row(ii) = simulator.getState();
}
// Save cost_vars to file
bool overwrite = true;
cout << "C++ Writing -> " << cost_vars_filename << endl;
saveMatrix("./", cost_vars_filename, cost_vars, overwrite);
return 0;
}