File: ExampleIMUTracking.cpp

package info (click to toggle)
simbody 3.7%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 72,896 kB
  • sloc: cpp: 248,827; ansic: 18,240; sh: 29; makefile: 24
file content (197 lines) | stat: -rw-r--r-- 7,559 bytes parent folder | download | duplicates (5)
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
/* -------------------------------------------------------------------------- *
 *                     Simbody(tm) Example: IMU Tracking                      *
 * -------------------------------------------------------------------------- *
 * 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) 2014 Stanford University and the Authors.           *
 * Authors: Michael Sherman                                                   *
 * Contributors:                                                              *
 *                                                                            *
 * 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.                                             *
 * -------------------------------------------------------------------------- */

/* This example shows a simple example using the Assembler for inverse
kinematics tracking where the input source is orientation observations from
an attached IMU (Inertial Measurement Unit).
*/


#include "Simbody.h"

#include <cstdio>
#include <exception>

using std::cout; using std::endl;

using namespace SimTK;

typedef SimTK::Markers::MarkerIx        MarkerIx;
typedef SimTK::Markers::ObservationIx   MarkerObsIx;

typedef SimTK::OrientationSensors::OSensorIx     IMUIx;
typedef SimTK::OrientationSensors::ObservationIx IMUObsIx;

int main() {
  try
  { // Create the system.
    
    MultibodySystem         system;
    SimbodyMatterSubsystem  matter(system);
    GeneralForceSubsystem   forces(system);
    Force::Gravity          gravity(forces, matter, -YAxis, 9.81);

    system.setUseUniformBackground(true);

    // An identity Rotation represents an IMU aligned with the body frame.
    const Rotation bodyAxesIMU;
    const Rotation tiltedIMU(Pi/4, ZAxis);

    const Rotation torsoIMUOri = bodyAxesIMU;
    const Rotation headIMUOri = tiltedIMU;


    const Real mass_torso = 20, mass_head = 2;
    const Vec3 hdims_torso(1,4,2), hdims_head(.3,1,.6);
    const Vec3 marker_head(hdims_head[0],hdims_head[1],0); // top front

    Body::Rigid torsoBody(MassProperties(mass_torso, Vec3(0), 
                                            UnitInertia::brick(hdims_torso)));
    torsoBody.addDecoration(DecorativeBrick(hdims_torso).setOpacity(.4));
    torsoBody.addDecoration(torsoIMUOri, 
        DecorativeFrame(1).setColor(Green).setLineThickness(5));

    Body::Rigid headBody(MassProperties(mass_head, Vec3(0), 
                                        UnitInertia::brick(hdims_head)));
    headBody.addDecoration(DecorativeBrick(hdims_head)
                           .setOpacity(.4).setColor(Cyan));
    headBody.addDecoration(headIMUOri, 
        DecorativeFrame(1).setColor(Green).setLineThickness(5));
    headBody.addDecoration(marker_head,
        DecorativePoint().setColor(Green));

    MobilizedBody::Free torso(matter.Ground(), Vec3(0,hdims_torso[1],0), 
                              torsoBody, Vec3(0,-hdims_torso[1],0));

    MobilizedBody::Ball head(torso, Vec3(0,hdims_torso[1],0),
                             headBody, Vec3(0,-hdims_head[1],0));



    // Initialize the system and state. 
    State state = system.realizeTopology();

    const Real Accuracy = 1e-5;
    Assembler ik(system);
    ik.setAccuracy(Accuracy);
    //ik.setForceNumericalGradient(true);


    Markers*            markers = new Markers();
    OrientationSensors* imus    = new OrientationSensors();
    ik.adoptAssemblyGoal(markers);
    ik.adoptAssemblyGoal(imus);

    const IMUIx    torsoIMU   = imus->addOSensor(torso, torsoIMUOri);
    const IMUIx    headIMU    = imus->addOSensor(head, headIMUOri);
    const MarkerIx headMarker = markers->addMarker(head, marker_head);

    ik.initialize(state);

    const IMUObsIx torsoObsIx = imus->getObservationIxForOSensor(torsoIMU);
    const IMUObsIx headObsIx  = imus->getObservationIxForOSensor(headIMU);
    const MarkerObsIx headMarkerObsIx = 
        markers->getObservationIxForMarker(headMarker);

    // Try an initial assembly to an arbitrary pose.
    imus->moveOneObservation(torsoObsIx, 
                             Rotation(SimTK::BodyRotationSequence,
                                      Pi/8, ZAxis, Pi/8, YAxis));
    imus->moveOneObservation(headObsIx, 
                             Rotation()); // keep aligned with Ground
    markers->moveOneObservation(headMarkerObsIx,
                                Vec3(0, 12, 0));

    for (OrientationSensors::OSensorIx mx(0); 
         mx < imus->getNumOSensors(); ++mx)
    {
        printf("mx=%d ox=%d err=%g\n", 
            (int)mx, (int)imus->getObservationIxForOSensor(mx),
            imus->findCurrentOSensorError(mx));
    }

    Visualizer viz(system);
    // Show initial configuration
    viz.report(state);
    cout << "Initial state. Type any character to continue:\n";
    getchar();

    printf("Using accuracy=%g\n", ik.getAccuracyInUse());
    ik.assemble(state);

    for (OrientationSensors::OSensorIx mx(0); 
         mx < imus->getNumOSensors(); ++mx)
    {
        printf("mx=%d ox=%d err=%g\n", 
            (int)mx, (int)imus->getObservationIxForOSensor(mx),
            imus->findCurrentOSensorError(mx));
    }

    viz.report(state);
    cout << "ASSEMBLED CONFIGURATION\n";     
    cout << "Type any character to start tracking:\n";
    getchar();

    const double startCPU = cpuTime(), startReal = realTime();

    const int NSteps = 200;
    for (int iters=0; iters <= NSteps; ++iters) {
        const Real slow = std::sin(2*Pi*iters/100);
        const Real fast = std::sin(10*Pi*iters/100);
        Rotation torsoObs(SpaceRotationSequence, 
                          (Pi/4)*slow, ZAxis,
                          (Pi/2)*slow, YAxis);
        Rotation headObs((Pi/8)*fast, YAxis); // shake head
        Vec3 markerObs(Vec3(0,12,0) + slow*Vec3(5,0,0));

        imus->moveOneObservation(torsoObsIx, torsoObs);
        imus->moveOneObservation(headObsIx, headObs);
        markers->moveOneObservation(headMarkerObsIx, markerObs);
                                        
        ik.track();
        ik.updateFromInternalState(state);
        viz.report(state);
    }

    cout << "TRACKED " << NSteps << " steps in " 
         << cpuTime()-startCPU   << " CPU s, " 
         << realTime()-startReal << " REAL s\n";

    cout << "DONE TRACKING ...\n";
    viz.report(state);

    cout << "Type any character to continue:\n";
    getchar();


  } catch (const std::exception& e) {
    std::printf("EXCEPTION THROWN: %s\n", e.what());
    exit(1);

  } catch (...) {
    std::printf("UNKNOWN EXCEPTION THROWN\n");
    exit(1);
  }

    return 0;
}