summaryrefslogtreecommitdiff
path: root/LocomotorPrimitives.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'LocomotorPrimitives.cpp')
-rw-r--r--LocomotorPrimitives.cpp62
1 files changed, 33 insertions, 29 deletions
diff --git a/LocomotorPrimitives.cpp b/LocomotorPrimitives.cpp
index 3bb8d97..e5ff7f6 100644
--- a/LocomotorPrimitives.cpp
+++ b/LocomotorPrimitives.cpp
@@ -11,23 +11,25 @@
#include "LocomotorPrimitivesManager.h"
#include "LocomotorPrimitivesController.h"
+#include "Logger.h"
#define FIXED_IN_SPACE 1
#define NO_SIM 0
-clock_t ts_start;
-
static const std::string MODEL_NAME = "LocomotorPrimitives";
+static const double INTEGRATOR_ACCURACY = 1.0e-3;
+
+static Logger &logger = Logger::getInstance();
static void constructModel(OpenSim::Model &model, OpenSim::Storage actData)
{
- std::cout << "Constructing " << MODEL_NAME << " model" << std::endl;
+ 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) {
- std::cerr << "Control data for some muscles in the model is missing. ENTER to continue, ^-C to quit." << std::endl;
+ logger.err("Control data for some muscles in the model is missing. Press ENTER to continue, ^-C to quit.");
std::cin.get();
}
model.addController(control);
@@ -35,12 +37,12 @@ static void constructModel(OpenSim::Model &model, OpenSim::Storage actData)
// 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;
+ 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));
- std::cout << " " << m->getName() << std::endl;
+ logger.log(" => %s\n", m->getName().c_str());
// m->setDefaultActivation(0.5);
// m->setDefaultFiberLength(0.1);
}
@@ -50,45 +52,49 @@ static void constructModel(OpenSim::Model &model, OpenSim::Storage actData)
OpenSim::ForceReporter *reporter = new OpenSim::ForceReporter(&model);
model.addAnalysis(reporter);
- std::cout << "Finished constructing model" << std::endl;
+ logger.log("Finished constructing model\n");
}
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;
+ logger.log("Skipping simulation as per NO_SIM=%d\n", NO_SIM);
return;
}
- std::cout << "Simulating model " << MODEL_NAME << std::endl;
- std::cout << " + Initializing system" << std::endl;
+ logger.log("Simulating model %s\n", MODEL_NAME.c_str());
+
+ logger.log("+ Initializing system\n");
SimTK::State &si = model.initSystem();
- std::cout << " + Creating integrator" << std::endl;
+// 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\n", INTEGRATOR_ACCURACY);
// 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;
+ integrator.setAccuracy(INTEGRATOR_ACCURACY);
+ logger.log("+ Creating simulation manager\n");
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;
+ logger.log("+ Integrating from time %f to %f\n", initialTime, finalTime);
manager.integrate(si);
// 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");
// Save the forces
+ logger.log("+ Writing forces to %s_forces.mot\n", MODEL_NAME.c_str());
OpenSim::Analysis &reporter = model.getAnalysisSet()[0];
((OpenSim::ForceReporter &)reporter).getForceStorage().print(MODEL_NAME + "_forces.mot");
+
+ logger.log("Finished simulating model\n");
}
static void usage()
@@ -101,7 +107,7 @@ static void usage()
int main(int argc, char **argv)
{
- ts_start = clock();
+ clock_t ts_start = clock();
/* set default values (to work on windows) */
std::string modelFile = FIXED_IN_SPACE ? "../../locomotor-primitives-fixed.osim" : "../../locomotor-primitives.osim";
@@ -130,12 +136,11 @@ int main(int argc, char **argv)
const double finalTime = actData.getLastTime();
if (initialTime < 0 || finalTime < 0 || finalTime <= initialTime) {
- std::cerr << "invalid time range in data file: " << initialTime << " - " << finalTime << std::endl;
+ logger.err("invalid time range in data file: %f - %f\n", initialTime, finalTime);
exit(-1);
}
- std::cout << "Starting simulation " + MODEL_NAME << " with (" << modelFile << ", " << dataFile << ") from time "
- << initialTime << " to " << finalTime << std::endl;
+ logger.log("Starting simulation %s with model file %s, actuation data file %s\n", MODEL_NAME.c_str(), modelFile.c_str(), dataFile.c_str());
try {
// Create an OpenSim model and set its name
@@ -147,22 +152,21 @@ int main(int argc, char **argv)
constructModel(osimModel, actData);
simulateModel(osimModel, initialTime, finalTime);
} catch (OpenSim::Exception ex) {
- std::cout << ex.getMessage() << std::endl;
+ logger.err("Exception: %s. Press ENTER to quit", ex.getMessage());
std::cin.get();
return 1;
} catch (std::exception ex) {
- std::cout << ex.what() << std::endl;
+ logger.err("Exception: %s. Press ENTER to quit", ex.what());
std::cin.get();
return 1;
} catch (...) {
- std::cout << "UNRECOGNIZED EXCEPTION" << std::endl;
+ logger.err("Unrecognized Exception. Press ENTER to quit");
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;
+ 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());
std::cin.get();
return 0;
}