summaryrefslogtreecommitdiff
path: root/LocomotorPrimitives.cpp
blob: 3bb8d9714d1b1f12593c46109fa019608304c18c (plain)
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
/**
 * Simulation for the Locomotor Primitives (Dominici et. al 2012) in a Human
 * Leg model with 14 Muscles.
 *
 * Author: Tobias Klauser <tobias.klauser@uzh.ch>
 *
 * Copyright (C) 2013 Tobias Klauser <tobias.klauser@uzh.ch>
 */

#include <OpenSim/OpenSim.h>

#include "LocomotorPrimitivesManager.h"
#include "LocomotorPrimitivesController.h"

#define FIXED_IN_SPACE	1
#define NO_SIM		0

clock_t ts_start;

static const std::string MODEL_NAME = "LocomotorPrimitives";

static void constructModel(OpenSim::Model &model, OpenSim::Storage actData)
{
	std::cout << "Constructing " << MODEL_NAME << " model" << std::endl;

	// Define controller for the model
	LocomotorPrimitivesController *control = new LocomotorPrimitivesController(actData, 0.01);
	control->setActuators(model.updActuators());
	if (control->checkControls() != 0) {
		std::cerr << "Control data for some muscles in the model is missing. ENTER to continue, ^-C to quit." << std::endl;
		std::cin.get();
	}
	model.addController(control);

	// Set default activation and fiber length on all muscles of the model
	const OpenSim::Set<OpenSim::Actuator> &actSet = model.getActuators();
	int sz = actSet.getSize();
	std::cout << " + Defining initial muscle states for " << sz << " actuators:" << std::endl;

	// Set default activation and fiber length of all muscles
	for (int i = 0; i < sz; i++) {
		OpenSim::ActivationFiberLengthMuscle *m = dynamic_cast<OpenSim::ActivationFiberLengthMuscle *>(&actSet.get(i));
		std::cout << "   " << m->getName() << std::endl;
//		m->setDefaultActivation(0.5);
//		m->setDefaultFiberLength(0.1);
	}

	// Create the force reporter for obtaining the forces applied to the model
	// during a forward simulation
	OpenSim::ForceReporter *reporter = new OpenSim::ForceReporter(&model);
	model.addAnalysis(reporter);

	std::cout << "Finished constructing model" << std::endl;
}

void simulateModel(OpenSim::Model &model, const double initialTime, const double finalTime)
{
	if (NO_SIM) {
		std::cout << "Skipping simulation as per NO_SIM=" << NO_SIM << std::endl;
		return;
	}

	std::cout << "Simulating model " << MODEL_NAME << std::endl;
	std::cout << " + Initializing system" << std::endl;
	SimTK::State &si = model.initSystem();

	std::cout << " + Creating integrator" << std::endl;
	// Create the integrator and manager for the simulation
	SimTK::RungeKuttaMersonIntegrator integrator(model.getMultibodySystem());
	integrator.setAccuracy(1.0e-3);
	std::cout << " + Creating simulation manager" << std::endl;

	LocomotorPrimitivesManager manager(model, integrator);

	// Integrate from initial time to final time
	manager.setInitialTime(initialTime);
	manager.setFinalTime(finalTime);

	std::cout << "=== MODEL SUMMARY ===" << std::endl;
	model.printDetailedInfo(si, std::cout);
	std::cout << "=== END MODEL SUMMARY ===" << std::endl << std::endl;
	std::cout << " + Integrating from " << std::fixed << initialTime << " to " << std::fixed << finalTime << std::endl;
	manager.integrate(si);

	// Save the model states from forward integration
	OpenSim::Storage statesDegrees(manager.getStateStorage());
	statesDegrees.print(MODEL_NAME + "_states.sto");

	// Save the forces
	OpenSim::Analysis &reporter = model.getAnalysisSet()[0];
	((OpenSim::ForceReporter &)reporter).getForceStorage().print(MODEL_NAME + "_forces.mot");
}

static void usage()
{
	std::cout << "usage: " << MODEL_NAME << " [OPTION...]" << std::endl << std::endl
		  << "  -d FILE  specify .sto datafile to use for muscle activation data" << std::endl
		  << "  -m FILE  specify .osim to use as model" << std::endl
		  << "  -h       show this help and exit" << std::endl;
}

int main(int argc, char **argv)
{
	ts_start = clock();

	/* set default values (to work on windows) */
	std::string modelFile = FIXED_IN_SPACE ? "../../locomotor-primitives-fixed.osim" : "../../locomotor-primitives.osim";
	std::string dataFile = "../../data/adults.sto";

	for (int i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
		switch (argv[i][1]) {
		case 'd':
			dataFile = argv[++i];
			break;
		case 'm':
			modelFile = argv[++i];
			break;
		case 'h':
			usage();
			exit(0);
		default:
			std::cerr << "unknown option: -" << argv[i][1] << std::endl;
			usage();
			exit(-1);
		}
	}

	OpenSim::Storage actData(dataFile);
	const double initialTime = actData.getFirstTime();
	const double finalTime = actData.getLastTime();

	if (initialTime < 0 || finalTime < 0 || finalTime <= initialTime) {
		std::cerr << "invalid time range in data file: " << initialTime << " - " << finalTime << std::endl;
		exit(-1);
	}

	std::cout << "Starting simulation " + MODEL_NAME << " with (" << modelFile << ", " << dataFile << ") from time "
		  << initialTime << " to " << finalTime << std::endl;

	try {
		// Create an OpenSim model and set its name
		OpenSim::Model osimModel(modelFile);

		osimModel.setName(MODEL_NAME);
		osimModel.setUseVisualizer(false);

		constructModel(osimModel, actData);
		simulateModel(osimModel, initialTime, finalTime);
	} catch (OpenSim::Exception ex) {
		std::cout << ex.getMessage() << std::endl;
		std::cin.get();
		return 1;
	} catch (std::exception ex) {
		std::cout << ex.what() << std::endl;
		std::cin.get();
		return 1;
	} catch (...) {
		std::cout << "UNRECOGNIZED EXCEPTION" << std::endl;
		std::cin.get();
		return 1;
	}

	std::cout << "main() routine time = " << 1.e3*(clock()-ts_start)/CLOCKS_PER_SEC << "ms" << std::endl;

	std::cout << "Simulation " + MODEL_NAME << " completed successfully." << std::endl;
	std::cin.get();
	return 0;
}