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
|
/* -------------------------------------------------------------------------- *
* Simbody(tm) Example: Bricard Mechanism *
* -------------------------------------------------------------------------- *
* This is part of the SimTK biosimulation toolkit originating from *
* Simbios, the NIH National Center for Physics-Based Simulation of *
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
* *
* Portions copyright (c) 2010-12 Stanford University and the Authors. *
* Authors: Michael Sherman *
* Contributors: Moonki Jung *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
* not use this file except in compliance with the License. You may obtain a *
* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* -------------------------------------------------------------------------- */
#include "Simbody.h"
#include <fstream>
#include <iostream>
using std::cout; using std::endl;
using namespace SimTK;
/* Note: this example is due to Moonki Jung from Seoul National University's
Human-Centered CAD Laboratory. See SimTK Core open-discussion forum startin
g 2/4/2010 with message 3292. Moonki kindly gave us permission to include this
very difficult mechanism as a test case for Simbody and as a user example. The
Bricard mechanism is particularly difficult because it has six mobilities and
six constraints but has one net degree of freedom. Thus one of the constraints
is redundant; but it is not any particular one. The mechanism must be perfectly
aligned in order to move, but in practice we can only satisfy the position
constraints approximately. This can hide the redundancy, causing the mechanism
to appear to have zero dofs and thus lock up. */
class EnergyReport : public PeriodicEventReporter {
public:
EnergyReport(const MultibodySystem& system, Real interval)
: PeriodicEventReporter(interval), system(system) {}
void handleEvent(const State& s) const {
cout << "\n*** t=" << s.getTime()
<< " ke=" << system.calcKineticEnergy(s)
<< " E=" << system.calcEnergy(s)
<< endl;
cout << " u=" << s.getU() << "\n";
cout << " uerr=" << s.getUErr() << "\n\n";
}
private:
const MultibodySystem& system;
};
int main()
{
try {
const String currentWorkingDir = Pathname::getCurrentWorkingDirectory();
std::cout << "Current working directory: " << currentWorkingDir << std::endl;
MultibodySystem system;
SimbodyMatterSubsystem matter(system);
GeneralForceSubsystem forces(system);
Force::Gravity gravity(forces, matter, UnitVec3(0, -1, 0), 9.8);
const Real Mass = 20;
const Vec3 EvenCOM(1.00000000, -0.16416667, -0.16416667);
const Vec3 OddCOM(1.00000000, 0.16416667, -0.16416667);
const Inertia EvenCentralInertia
( 3.33400000, 28.33366667, 28.33366667, // xx, yy, zz
-4.96666667, -1.60000000, -0.03333333); // xy, xz, yz
const Inertia OddCentralInertia
( 3.33400000, 28.33366667, 28.33366667, // xx, yy, zz
4.96666667, -1.60000000, 0.03333333); // xy, xz, yz
// Inertias must be given about the body origin.
const Inertia EvenBodyInertia =
EvenCentralInertia.shiftFromMassCenter(-EvenCOM, Mass);
const Inertia OddBodyInertia =
OddCentralInertia.shiftFromMassCenter(-OddCOM, Mass);
Body::Rigid EVEN_PART_1(MassProperties(Mass, EvenCOM, EvenBodyInertia));
Body::Rigid EVEN_PART_2(MassProperties(Mass, EvenCOM, EvenBodyInertia));
Body::Rigid EVEN_PART_3(MassProperties(Mass, EvenCOM, EvenBodyInertia));
Body::Rigid ODD_PART_1(MassProperties(Mass, OddCOM, OddBodyInertia));
Body::Rigid ODD_PART_2(MassProperties(Mass, OddCOM, OddBodyInertia));
// Split the last body and weld back together to close loop.
Body::Rigid ODD_PART_3_HALF1(MassProperties(Mass/2, OddCOM,
OddBodyInertia/2));
Body::Rigid ODD_PART_3_HALF2(MassProperties(Mass/2, OddCOM,
OddBodyInertia/2));
std::ifstream file1, file2;
PolygonalMesh Mesh1; file1.open("Bricard_EVEN_PART.obj");
if (!file1.good()) {
std::cout << "Couldn't open file 'Bricard_EVEN_PART.obj' in current working directory "
<< currentWorkingDir << std::endl;
exit(1);
}
Mesh1.loadObjFile(file1); file1.close();
PolygonalMesh Mesh2; file2.open("Bricard_ODD_PART.obj");
if (!file2.good()) {
std::cout << "Couldn't open file 'Bricard_ODD_PART.obj' in current working directory "
<< currentWorkingDir << std::endl;
exit(1);
}
Mesh2.loadObjFile(file2); file2.close();
EVEN_PART_1.addDecoration(Transform(), DecorativeMesh(Mesh1).setColor(Vec3(0.00000000, 1.00000000, 0.00000000)));
EVEN_PART_2.addDecoration(Transform(), DecorativeMesh(Mesh1).setColor(Vec3(1.00000000, 0.00000000, 1.00000000)));
EVEN_PART_3.addDecoration(Transform(), DecorativeMesh(Mesh1).setColor(Vec3(1.00000000, 1.00000000, 0.00000000)));
ODD_PART_1.addDecoration(Transform(), DecorativeMesh(Mesh2).setColor(Vec3(1.00000000, 0.00000000, 0.00000000)));
ODD_PART_2.addDecoration(Transform(), DecorativeMesh(Mesh2).setColor(Vec3(0.00000000, 0.00000000, 1.00000000)));
ODD_PART_3_HALF1.addDecoration(Transform(), DecorativeMesh(Mesh2).setColor(Vec3(0.00000000, 1.00000000, 1.00000000)));
MobilizedBody::Weld EVEN_PART_1_body(matter.updGround(), Transform(Rotation(Mat33(1,0,0,0,-1,0,0,0,-1)), Vec3(0, 0, 0))
,EVEN_PART_1, Transform());
MobilizedBody::Pin ODD_PART_1_body(EVEN_PART_1_body, Transform(Rotation(Mat33(0,-1,0,1,0,0,0,0,1)), Vec3(0, 0, 0))
,ODD_PART_1, Transform(Rotation(Mat33(0,-1,0,1,0,0,0,0,1)), Vec3(0,0,0)));
MobilizedBody::Pin EVEN_PART_2_body(ODD_PART_1_body, Transform(Rotation(Mat33(0,-1,0,0,0,1,-1,0,0)), Vec3(2, 0, 0))
,EVEN_PART_2, Transform(Rotation(Mat33(0,-1,0,-1,0,0,0,0,-1)), Vec3(0,0,0)));
MobilizedBody::Pin ODD_PART_2_body(EVEN_PART_1_body, Transform(Rotation(Mat33(0,-1,0,0,0,1,-1,0,0)), Vec3(2, 0, 0))
,ODD_PART_2, Transform(Rotation(Mat33(0,-1,0,1,0,0,0,0,1)), Vec3(0,0,0)));
MobilizedBody::Pin EVEN_PART_3_body(ODD_PART_2_body, Transform(Rotation(Mat33(0,-1,0,0,0,1,-1,0,0)), Vec3(2, 0, 0))
,EVEN_PART_3, Transform(Rotation(Mat33(0,-1,0,0,0,-1,1,0,0)), Vec3(2,0,0)));
MobilizedBody::Pin ODD_PART_3_HALF1_body(EVEN_PART_3_body, Transform(Rotation(Mat33(0,-1,0,-1,0,0,0,0,-1)), Vec3(0, 0, 0))
,ODD_PART_3_HALF1, Transform(Rotation(Mat33(0,-1,0,0,0,1,-1,0,0)), Vec3(2,0,0)));
MobilizedBody::Pin ODD_PART_3_HALF2_body(EVEN_PART_2_body, Transform(Rotation(Mat33(0,-1,0,0,0,1,-1,0,0)), Vec3(2, 0, 0))
,ODD_PART_3_HALF2, Transform(Rotation(Mat33(0,-1,0,1,0,0,0,0,1)), Vec3(0,0,0)));
Constraint::Weld ODD_PART_3_UNION(ODD_PART_3_HALF1_body, Transform(), ODD_PART_3_HALF2_body, Transform());
//Constraint::ConstantSpeed motion(EVEN_PART_3_body, -.1);
//Force::MobilityLinearSpring frc(forces, EVEN_PART_3_body,
// MobilizerUIndex(0), 100, 0);
Visualizer viz(system);
viz.setCameraTransform(Vec3(0.5,0.5,0.5));
viz.pointCameraAt(Vec3(0), Vec3(0,1,0));
viz.setBackgroundType(Visualizer::SolidColor);
system.addEventReporter(new Visualizer::Reporter(viz, 1./30));
system.addEventReporter(new EnergyReport(system, .01));
system.realizeTopology();
State state = system.getDefaultState();
// Set initial states (Q's and U's)
// Position
ODD_PART_1_body.setOneQ(state, 0, 180.0*Pi/180.0);
EVEN_PART_3_body.setOneQ(state, 0, 180.0*Pi/180.0);
ODD_PART_3_HALF2_body.setOneQ(state, 0, 0.0*Pi/180.0);
EVEN_PART_2_body.setOneQ(state, 0, -120.0*Pi/180.0);
ODD_PART_2_body.setOneQ(state, 0, -120.0*Pi/180.0);
ODD_PART_3_HALF1_body.setOneQ(state, 0, 120.0*Pi/180.0);
// Velocity
ODD_PART_1_body.setOneU(state,0, -11.2);
//RungeKuttaMersonIntegrator integ(system);
RungeKutta3Integrator integ(system);
//RungeKuttaFeldbergIntegrator integ(system);
//VerletIntegrator integ(system);
//CPodesIntegrator integ(system);
// Accuracy needs to be fairly tight to avoid lockup that would occur
// if the constraints were allowed to drift.
integ.setAccuracy(1e-5);
//integ.setConstraintTolerance(1e-8);
integ.initialize(state);
const double startCPU = cpuTime(), startReal = realTime();
TimeStepper ts(system, integ);
ts.initialize(state);
ts.stepTo(20.0);
cout << "DONE. CPU=" << cpuTime()-startCPU
<< "s, REAL=" << realTime()-startReal << "s\n";
printf("Used Integrator %s at accuracy %g:\n",
integ.getMethodName(), integ.getAccuracyInUse());
printf("# STEPS/ATTEMPTS = %d/%d\n", integ.getNumStepsTaken(), integ.getNumStepsAttempted());
printf("# ERR TEST FAILS = %d\n", integ.getNumErrorTestFailures());
printf("# REALIZE/PROJECT = %d/%d\n", integ.getNumRealizations(), integ.getNumProjections());
} catch (const std::exception& e) {
std::cout << "std::exception: " << e.what() << std::endl;
} catch (...) {
std::cout << "UNKNOWN EXCEPTION\n";
}
return 0;
}
|