forked from ethz-adrl/towr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hopper_example.cc
126 lines (100 loc) · 5.25 KB
/
hopper_example.cc
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/******************************************************************************
Copyright (c) 2018, Alexander W. Winkler. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
#include <cmath>
#include <iostream>
#include <towr/terrain/examples/height_map_examples.h>
#include <towr/nlp_formulation.h>
#include <ifopt/ipopt_solver.h>
using namespace towr;
// A minimal example how to build a trajectory optimization problem using TOWR.
//
// The more advanced example that includes ROS integration, GUI, rviz
// visualization and plotting can be found here:
// towr_ros/src/towr_ros_app.cc
int main()
{
NlpFormulation formulation;
// terrain
formulation.terrain_ = std::make_shared<FlatGround>(0.0);
// Kinematic limits and dynamic parameters of the hopper
formulation.model_ = RobotModel(RobotModel::Monoped);
// set the initial position of the hopper
formulation.initial_base_.lin.at(kPos).z() = 0.5;
formulation.initial_ee_W_.push_back(Eigen::Vector3d::Zero());
// define the desired goal state of the hopper
formulation.final_base_.lin.at(towr::kPos) << 1.0, 0.0, 0.5;
// Parameters that define the motion. See c'tor for default values or
// other values that can be modified.
// First we define the initial phase durations, that can however be changed
// by the optimizer. The number of swing and stance phases however is fixed.
// alternating stance and swing: ____-----_____-----_____-----_____
formulation.params_.ee_phase_durations_.push_back({0.4, 0.2, 0.4, 0.2, 0.4, 0.2, 0.2});
formulation.params_.ee_in_contact_at_start_.push_back(true);
// Initialize the nonlinear-programming problem with the variables,
// constraints and costs.
ifopt::Problem nlp;
SplineHolder solution;
for (auto c : formulation.GetVariableSets(solution))
nlp.AddVariableSet(c);
for (auto c : formulation.GetConstraints(solution))
nlp.AddConstraintSet(c);
for (auto c : formulation.GetCosts())
nlp.AddCostSet(c);
// You can add your own elements to the nlp as well, simply by calling:
// nlp.AddVariablesSet(your_custom_variables);
// nlp.AddConstraintSet(your_custom_constraints);
// Choose ifopt solver (IPOPT or SNOPT), set some parameters and solve.
// solver->SetOption("derivative_test", "first-order");
auto solver = std::make_shared<ifopt::IpoptSolver>();
solver->SetOption("jacobian_approximation", "exact"); // "finite difference-values"
solver->SetOption("max_cpu_time", 20.0);
solver->Solve(nlp);
// Can directly view the optimization variables through:
// Eigen::VectorXd x = nlp.GetVariableValues()
// However, it's more convenient to access the splines constructed from these
// variables and query their values at specific times:
using namespace std;
cout.precision(2);
nlp.PrintCurrent(); // view variable-set, constraint violations, indices,...
cout << fixed;
cout << "\n====================\nMonoped trajectory:\n====================\n";
double t = 0.0;
while (t<=solution.base_linear_->GetTotalTime() + 1e-5) {
cout << "t=" << t << "\n";
cout << "Base linear position x,y,z: \t";
cout << solution.base_linear_->GetPoint(t).p().transpose() << "\t[m]" << endl;
cout << "Base Euler roll, pitch, yaw: \t";
Eigen::Vector3d rad = solution.base_angular_->GetPoint(t).p();
cout << (rad/M_PI*180).transpose() << "\t[deg]" << endl;
cout << "Foot position x,y,z: \t";
cout << solution.ee_motion_.at(0)->GetPoint(t).p().transpose() << "\t[m]" << endl;
cout << "Contact force x,y,z: \t";
cout << solution.ee_force_.at(0)->GetPoint(t).p().transpose() << "\t[N]" << endl;
bool contact = solution.phase_durations_.at(0)->IsContactPhase(t);
std::string foot_in_contact = contact? "yes" : "no";
cout << "Foot in contact: \t" + foot_in_contact << endl;
cout << endl;
t += 0.2;
}
}