summaryrefslogtreecommitdiff
path: root/LocomotorPrimitives.cpp
blob: 967513e0f1b0b5a9e4949cdb0256dc5bc13d3d72 (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
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
/**
 * 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 <signal.h>

#include <OpenSim/OpenSim.h>
#include <OpenSim/Common/IO.h>

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

#define FIXED_IN_SPACE	1
#define NO_SIM		0

static const std::string MODEL_NAME = "LocomotorPrimitives";
static const double INTEGRATOR_ACCURACY = 1.0e-6;

static Logger &logger = Logger::getInstance();
static LocomotorPrimitivesManager *manager = NULL;

static bool yes = false;

static void signal_handler(int signr)
{
	switch (signr) {
	case SIGINT:
		logger.log("Got signal SIGINT. Quitting...\n");
		if (manager)
			manager->halt();
	default:
		break;
	}
}

static void constructModel(OpenSim::Model &model, OpenSim::Storage actData)
{
	logger.log("Constructing model %s\n", MODEL_NAME.c_str());

	// Define controller for the model
	LocomotorPrimitivesController *control = new LocomotorPrimitivesController(actData, 0.01);
	control->setActuators(model.updActuators());
	if (control->checkControls() != 0) {
		logger.err("Control data for some muscles in the model is missing. Press ENTER to continue, ^-C to quit.");
		if (!yes)
			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();
	logger.log("+ Defining initial muscle states for %d actuators:\n", sz);

	// 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));
		logger.log("  => %s\n", m->getName().c_str());
		m->setDefaultActivation(0.01);
		m->setDefaultFiberLength(0.1);
	}

//	logger.log("+ Adding Actuation Analysis\n");
//	OpenSim::Actuation *actuation = new OpenSim::Actuation(&model);
//	actuation->setStepInterval(1);
//	model.addAnalysis(actuation);

	// Create the force reporter for obtaining the forces applied to the model
	// during a forward simulation
	logger.log("+ Adding Force Reporter\n");
	OpenSim::ForceReporter *reporter = new OpenSim::ForceReporter(&model);
	model.addAnalysis(reporter);

	// Muscle analysis for recording and computting basic quantities
	// (length, shortening velocity, tendon length, ...) for muscles during
	// a simulation
//	logger.log("+ Adding Muscle Analysis\n");
//	OpenSim::MuscleAnalysis *analysis = new OpenSim::MuscleAnalysis(&model);
//	model.addAnalysis(analysis);

	logger.log("Finished constructing model\n");
}

int simulateModel(OpenSim::Model &model, const double initial_time,
		  const double final_time, double fixed_step_size,
		  std::string output_dir)
{
	if (NO_SIM) {
		logger.log("Skipping simulation as per NO_SIM=%d\n", NO_SIM);
		return 1;
	}

	std::string cwd = OpenSim::IO::getCwd();
	if (OpenSim::IO::chDir(output_dir) != 0) {
		logger.err("Failed to change to output directory %s", output_dir.c_str());
		return 1;
	}

	logger.log("Simulating model %s\n", MODEL_NAME.c_str());

	logger.log("+ Initializing system\n");
	SimTK::State &si = model.initSystem();

	if (!model.isValidSystem()) {
		logger.err("Loaded invalid model. Press ENTER to quit");
		if (!yes)
			std::cin.get();
		return 1;
	}

//	std::cout << "=== MODEL SUMMARY ===" << std::endl;
//	model.printDetailedInfo(si, std::cout);
//	std::cout << "=== END MODEL SUMMARY ===" << std::endl << std::endl;

	logger.log("+ Creating integrator (RungeKuttaMerson) with accuracy %e and step size %f (0.0 means dynamic)\n",
			INTEGRATOR_ACCURACY, fixed_step_size);
	// Create the integrator and manager for the simulation
	SimTK::RungeKuttaMersonIntegrator integrator(model.getMultibodySystem());
	if (fixed_step_size > 0)
		integrator.setFixedStepSize(fixed_step_size);
	integrator.setAccuracy(INTEGRATOR_ACCURACY);

	logger.log("+ Creating simulation manager\n");
	manager = new LocomotorPrimitivesManager(model, integrator);
	// Integrate from initial time to final time
	manager->setInitialTime(initial_time);
	manager->setFinalTime(final_time);
	logger.log("+ Integrating from time %f to %f\n", initial_time, final_time);
	try {
		manager->integrate(si);
	} catch (...) {
		logger.log("Got exception during integration\n");
		return 1;
	}

	// Save the model states from forward integration
	logger.log("+ Writing states to %s_states.sto\n", MODEL_NAME.c_str());
	OpenSim::Storage statesDegrees(manager->getStateStorage());
	statesDegrees.print(MODEL_NAME + "_states.sto");

	logger.log("+ Writing states (degrees) to %s_states_degrees.mot\n", MODEL_NAME.c_str());
	model.updSimbodyEngine().convertRadiansToDegrees(statesDegrees);
	statesDegrees.setWriteSIMMHeader(true);
	statesDegrees.print(MODEL_NAME + "_states_degrees.mot");

	// Save the forces
	logger.log("+ Writing forces to %s_forces.mot\n", MODEL_NAME.c_str());
	const OpenSim::Analysis &a = model.getAnalysisSet().get("ForceReporter");
	((OpenSim::ForceReporter &) a).getForceStorage().print(MODEL_NAME + "_forces.mot");

	// Save the actuation
//	logger.log("+ Writing actuation to %s_actuation.sto\n", MODEL_NAME.c_str());
	// TODO

//	logger.log("+ Writing muscle analysis to %s_muscles\n", MODEL_NAME.c_str());
	// TODO

	logger.log("Finished simulating model\n");

	return 0;
}

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
		  << "  -o DIR   set output directory to DIR (default: current directory)" << std::endl
		  << "  -s N     set fixed step size for integrator to N (default: dynamic)" << std::endl
		  << "  -y       don't prompt for any input on errors and finishing" << std::endl
		  << "  -h       show this help and exit" << std::endl;
}

int main(int argc, char **argv)
{
	clock_t ts_start = clock();
	double fixed_step_size = 0.0;
	/* set default values (to work on windows) */
	std::string model_file = FIXED_IN_SPACE ? "../../locomotor-primitives-fixed.osim" : "../../locomotor-primitives.osim";
	std::string data_file = "../../data/adults.sto";
	std::string output_dir = ".";

	sigset_t block_mask;
	struct sigaction saction;

	for (int i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
		switch (argv[i][1]) {
		case 'd':
			data_file = argv[++i];
			break;
		case 'm':
			model_file = argv[++i];
			break;
		case 'o':
			output_dir = argv[++i];
			break;
		case 's':
			fixed_step_size = strtod(argv[++i], NULL);
			break;
		case 'y':
			yes = true;
			break;
		case 'h':
			usage();
			exit(0);
		default:
			std::cerr << "unknown option: -" << argv[i][1] << std::endl;
			usage();
			exit(-1);
		}
	}

	OpenSim::Storage actData(data_file);
	const double initial_time = actData.getFirstTime();
	const double final_time = actData.getLastTime();

	if (initial_time < 0 || final_time < 0 || final_time <= initial_time) {
		logger.err("invalid time range in data file: %f - %f\n", initial_time, final_time);
		exit(-1);
	}

	/* Register signal handler */
	sigfillset(&block_mask);
	saction.sa_handler = signal_handler;
	saction.sa_mask = block_mask;
	saction.sa_flags = SA_RESTART;
	sigaction(SIGINT, &saction, NULL);

	logger.log("Starting simulation %s with model file %s, actuation data file %s, fixed step size: %f\n", MODEL_NAME.c_str(), model_file.c_str(), data_file.c_str(), fixed_step_size);

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

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

		constructModel(osimModel, actData);
		if (simulateModel(osimModel, initial_time, final_time, fixed_step_size, output_dir)) {
			logger.err("Something went wrong during simulation. Press ENTER to quit");
			if (!yes)
				std::cin.get();
			return 1;
		}
	} catch (OpenSim::Exception ex) {
		logger.err("Exception: %s. Press ENTER to quit", ex.getMessage());
		if (!yes)
			std::cin.get();
		return 1;
	} catch (std::exception ex) {
		logger.err("Exception: %s. Press ENTER to quit", ex.what());
		if (!yes)
			std::cin.get();
		return 1;
	} catch (...) {
		logger.err("Unrecognized Exception. Press ENTER to quit");
		if (!yes)
			std::cin.get();
		return 1;
	}

	logger.log("Total runtime = %fms\n", 1.e3 * (clock() - ts_start) / CLOCKS_PER_SEC);
	logger.log("Simulation %s completed successfully. Press ENTER to quit", MODEL_NAME.c_str());
	if (!yes)
		std::cin.get();
	return 0;
}