/
tutorialBiped.cpp
336 lines (272 loc) · 9 KB
/
tutorialBiped.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright (c) 2015, Georgia Tech Research Corporation
* All rights reserved.
*
* Author(s): Karen Liu <karenliu@cc.gatech.edu>
*
* Georgia Tech Graphics Lab and Humanoid Robotics Lab
*
* Directed by Prof. C. Karen Liu and Prof. Mike Stilman
* <karenliu@cc.gatech.edu> <mstilman@cc.gatech.edu>
*
* This file is provided under the following "BSD-style" License:
* 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.
* 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.
*/
const double default_speed_increment = 0.5;
const int default_ik_iterations = 4500;
const double default_force = 50.0; // N
const int default_countdown = 100; // Number of timesteps for applying force
#include "dart/dart.h"
using namespace dart::common;
using namespace dart::dynamics;
using namespace dart::simulation;
using namespace dart::gui;
using namespace dart::utils;
using namespace dart::math;
class Controller
{
public:
/// Constructor
Controller(const SkeletonPtr& biped)
: mBiped(biped),
mPreOffset(0.0),
mSpeed(0.0)
{
int nDofs = mBiped->getNumDofs();
mForces = Eigen::VectorXd::Zero(nDofs);
mKp = Eigen::MatrixXd::Identity(nDofs, nDofs);
mKd = Eigen::MatrixXd::Identity(nDofs, nDofs);
for(size_t i = 0; i < 6; ++i)
{
mKp(i, i) = 0.0;
mKd(i, i) = 0.0;
}
for(size_t i = 6; i < biped->getNumDofs(); ++i)
{
mKp(i, i) = 1000;
mKd(i, i) = 50;
}
setTargetPositions(mBiped->getPositions());
}
/// Reset the desired dof position to the current position
void setTargetPositions(const Eigen::VectorXd& pose)
{
mTargetPositions = pose;
}
/// Clear commanding forces
void clearForces()
{
mForces.setZero();
}
/// Add commanding forces from PD controllers
void addPDForces()
{
// Lesson 2
}
/// Add commanind forces from Stable-PD controllers
void addSPDForces()
{
// Lesson 3
}
/// add commanding forces from ankle strategy
void addAnkleStrategyForces()
{
// Lesson 4
}
// Send velocity commands on wheel actuators
void setWheelCommands()
{
// Lesson 6
}
void changeWheelSpeed(double increment)
{
mSpeed += increment;
std::cout << "wheel speed = " << mSpeed << std::endl;
}
protected:
/// The biped Skeleton that we will be controlling
SkeletonPtr mBiped;
/// Joint forces for the biped (output of the Controller)
Eigen::VectorXd mForces;
/// Control gains for the proportional error terms in the PD controller
Eigen::MatrixXd mKp;
/// Control gains for the derivative error terms in the PD controller
Eigen::MatrixXd mKd;
/// Target positions for the PD controllers
Eigen::VectorXd mTargetPositions;
/// For ankle strategy: Error in the previous timestep
double mPreOffset;
/// For velocity actuator: Current speed of the skateboard
double mSpeed;
};
class MyWindow : public SimWindow
{
public:
/// Constructor
MyWindow(const WorldPtr& world)
: mForceCountDown(0),
mPositiveSign(true)
{
setWorld(world);
mController = std::make_unique<Controller>(mWorld->getSkeleton("biped"));
}
/// Handle keyboard input
void keyboard(unsigned char key, int x, int y) override
{
switch(key)
{
case ',':
mForceCountDown = default_countdown;
mPositiveSign = false;
break;
case '.':
mForceCountDown = default_countdown;
mPositiveSign = true;
break;
case 'a':
mController->changeWheelSpeed(default_speed_increment);
break;
case 's':
mController->changeWheelSpeed(-default_speed_increment);
break;
default:
SimWindow::keyboard(key, x, y);
}
}
void timeStepping() override
{
mController->clearForces();
// Lesson 3
mController->addSPDForces();
// Lesson 4
mController->addAnkleStrategyForces();
// Lesson 6
mController->setWheelCommands();
// Apply body forces based on user input, and color the body shape red
if(mForceCountDown > 0)
{
BodyNode* bn = mWorld->getSkeleton("biped")->getBodyNode("h_abdomen");
auto shapeNodes = bn->getShapeNodesWith<VisualAddon>();
shapeNodes[0]->getVisualAddon()->setColor(dart::Color::Red());
if(mPositiveSign)
bn->addExtForce(default_force * Eigen::Vector3d::UnitX(),
bn->getCOM(), false, false);
else
bn->addExtForce(-default_force*Eigen::Vector3d::UnitX(),
bn->getCOM(), false, false);
--mForceCountDown;
}
// Step the simulation forward
SimWindow::timeStepping();
}
protected:
std::unique_ptr<Controller> mController;
/// Number of iterations before clearing a force entry
int mForceCountDown;
/// Whether a force should be applied in the positive or negative direction
bool mPositiveSign;
};
// Load a biped model and enable joint limits and self-collision
SkeletonPtr loadBiped()
{
// Lesson 1
// Create the world with a skeleton
WorldPtr world = SkelParser::readWorld(DART_DATA_PATH"skel/biped.skel");
assert(world != nullptr);
SkeletonPtr biped = world->getSkeleton("biped");
return biped;
}
// Load a skateboard model and connect it to the biped model via an Euler joint
void modifyBipedWithSkateboard(SkeletonPtr /*biped*/)
{
// Lesson 5
}
// Set the actuator type for four wheel joints to "VELOCITY"
void setVelocityAccuators(SkeletonPtr /*biped*/)
{
// Lesson 6
}
// Solve for a balanced pose using IK
Eigen::VectorXd solveIK(SkeletonPtr biped)
{
// Lesson 7
Eigen::VectorXd newPose = biped->getPositions();
return newPose;
}
SkeletonPtr createFloor()
{
SkeletonPtr floor = Skeleton::create("floor");
// Give the floor a body
BodyNodePtr body =
floor->createJointAndBodyNodePair<WeldJoint>(nullptr).second;
// Give the body a shape
double floor_width = 10.0;
double floor_height = 0.01;
std::shared_ptr<BoxShape> box(
new BoxShape(Eigen::Vector3d(floor_width, floor_height, floor_width)));
auto shapeNode
= body->createShapeNodeWith<VisualAddon, CollisionAddon, DynamicsAddon>(box);
shapeNode->getVisualAddon()->setColor(dart::Color::Black());
// Put the body into position
Eigen::Isometry3d tf(Eigen::Isometry3d::Identity());
tf.translation() = Eigen::Vector3d(0.0, -1.0, 0.0);
body->getParentJoint()->setTransformFromParentBodyNode(tf);
return floor;
}
int main(int argc, char* argv[])
{
SkeletonPtr floor = createFloor();
// Lesson 1
SkeletonPtr biped = loadBiped();
// Lesson 5
modifyBipedWithSkateboard(biped);
// Lesson 6
setVelocityAccuators(biped);
// Lesson 7
Eigen::VectorXd balancedPose = solveIK(biped);
biped->setPositions(balancedPose);
WorldPtr world = std::make_shared<World>();
world->setGravity(Eigen::Vector3d(0.0, -9.81, 0.0));
#ifdef HAVE_BULLET_COLLISION
world->getConstraintSolver()->setCollisionDetector(
std::make_unique<dart::collision::BulletCollisionDetector>());
#endif
world->addSkeleton(floor);
world->addSkeleton(biped);
// Create a window for rendering the world and handling user input
MyWindow window(world);
// Print instructions
std::cout << "'.': forward push" << std::endl;
std::cout << "',': backward push" << std::endl;
std::cout << "'s': increase skateboard forward speed" << std::endl;
std::cout << "'a': increase skateboard backward speed" << std::endl;
std::cout << "space bar: simulation on/off" << std::endl;
std::cout << "'p': replay simulation" << std::endl;
std::cout << "'v': Turn contact force visualization on/off" << std::endl;
std::cout << "'[' and ']': replay one frame backward and forward" << std::endl;
// Initialize glut, initialize the window, and begin the glut event loop
glutInit(&argc, argv);
window.initWindow(640, 480, "Multi-Pendulum Tutorial");
glutMainLoop();
}