1% -------------------------------------------------------------------------- %
2%                simulate_double_pendulum_and_write_tables.m                 %
3% -------------------------------------------------------------------------- %
4% The OpenSim API is a toolkit for musculoskeletal modeling and simulation.  %
5% See http://opensim.stanford.edu and the NOTICE file for more information.  %
6% OpenSim is developed at Stanford University and supported by the US        %
7% National Institutes of Health (U54 GM072970, R24 HD065690) and by DARPA    %
8% through the Warrior Web program.                                           %
9%                                                                            %
10% Copyright (c) 2005-2017 Stanford University and the Authors                %
11% Author(s): James Dunne, Tom Uchida, Chris Dembia                           %
12%                                                                            %
13% Licensed under the Apache License, Version 2.0 (the "License"); you may    %
14% not use this file except in compliance with the License. You may obtain a  %
15% copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         %
16%                                                                            %
17% Unless required by applicable law or agreed to in writing, software        %
18% distributed under the License is distributed on an "AS IS" BASIS,          %
19% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
20% See the License for the specific language governing permissions and        %
21% limitations under the License.                                             %
22% -------------------------------------------------------------------------- %
23
24% This API example runs a forward simulation of a double-pendulum model and
25% writes the results to .sto and .trc files.
26
27import org.opensim.modeling.*
28
29% Read double-pendulum model.
30model = Model('double_pendulum_markers.osim');
31model.setUseVisualizer(false);
32
33% Set the time interval (in seconds) between consecutive data points stored by
34% the reporters.
35reportTimeInterval = 0.1;
36
37% Add a console reporter to print the joint angles. The output will be written
38% to the log file (out.log) in the current directory.
39consoleReporter = ConsoleReporter();
40consoleReporter.set_report_time_interval(reportTimeInterval);
41% When connecting the outputs, set the alias names to 'pin1_angle' and
42% 'pin2_angle'. The aliases will appear as the column labels.
43consoleReporter.addToReport( ...
44    model.getCoordinateSet().get(0).getOutput('value'), 'pin1_angle');
45consoleReporter.addToReport( ...
46    model.getCoordinateSet().get(1).getOutput('value'), 'pin2_angle');
47model.addComponent(consoleReporter);
48
49% Add a table reporter to record the joint angles.
50tableReporterForAngles = TableReporter();
51tableReporterForAngles.set_report_time_interval(reportTimeInterval);
52tableReporterForAngles.addToReport( ...
53    model.getCoordinateSet().get(0).getOutput('value'), 'pin1_angle');
54tableReporterForAngles.addToReport( ...
55    model.getCoordinateSet().get(1).getOutput('value'), 'pin2_angle');
56model.addComponent(tableReporterForAngles);
57
58% Add a table reporter to record the marker locations, which are of type Vec3.
59tableReporterForMarkers = TableReporterVec3();
60tableReporterForMarkers.set_report_time_interval(reportTimeInterval);
61tableReporterForMarkers.addToReport( ...
62    model.getMarkerSet().get(0).getOutput('location'), 'marker_1');
63tableReporterForMarkers.addToReport( ...
64    model.getMarkerSet().get(1).getOutput('location'), 'marker_2');
65model.addComponent(tableReporterForMarkers);
66
67% Run a forward simulation using the Manager.
68state = model.initSystem();
69manager = Manager(model);
70manager.setInitialTime(0);
71manager.setFinalTime(2);
72manager.integrate(state);
73
74% Display results recorded by the console reporter.
75if (exist('out.log', 'file') ~= 2), error('log file (out.log) not found'); end
76type('out.log');
77
78% Write joint angles to .sto file.
79filename = 'pendulum_coordinates.sto';
80STOFileAdapter.write(tableReporterForAngles.getTable(), filename);
81fprintf('Joint angles written to %s\n', filename);
82
83% Write marker locations to .sto file.
84filename = 'marker_locations.sto';
85markerTable = tableReporterForMarkers.getTable();
86STOFileAdapterVec3.write(markerTable, filename);
87fprintf('Marker locations written to %s\n', filename);
88
89% Write marker locations to .trc file. The TRCFileAdapter requires DataRate and
90% Units be included in the table metadata.
91filename = 'marker_locations.trc';
92markerTable.addTableMetaDataString('DataRate', num2str(reportTimeInterval));
93markerTable.addTableMetaDataString('Units', 'm');
94TRCFileAdapter.write(markerTable, filename);
95fprintf('Marker locations written to %s\n', filename);
96
97fprintf('Success!\n');
98