diff options
Diffstat (limited to 'LocomotorPrimitives.cpp')
-rw-r--r-- | LocomotorPrimitives.cpp | 52 |
1 files changed, 39 insertions, 13 deletions
diff --git a/LocomotorPrimitives.cpp b/LocomotorPrimitives.cpp index cf6ed91..967513e 100644 --- a/LocomotorPrimitives.cpp +++ b/LocomotorPrimitives.cpp @@ -20,11 +20,13 @@ #define NO_SIM 0 static const std::string MODEL_NAME = "LocomotorPrimitives"; -static const double INTEGRATOR_ACCURACY = 1.0e-3; +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) { @@ -46,7 +48,8 @@ static void constructModel(OpenSim::Model &model, OpenSim::Storage actData) 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."); - std::cin.get(); + if (!yes) + std::cin.get(); } model.addController(control); @@ -84,19 +87,19 @@ static void constructModel(OpenSim::Model &model, OpenSim::Storage actData) logger.log("Finished constructing model\n"); } -void simulateModel(OpenSim::Model &model, const double initial_time, - const double final_time, double fixed_step_size, - std::string output_dir) +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; + 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; + return 1; } logger.log("Simulating model %s\n", MODEL_NAME.c_str()); @@ -104,6 +107,13 @@ void simulateModel(OpenSim::Model &model, const double initial_time, 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; @@ -126,6 +136,7 @@ void simulateModel(OpenSim::Model &model, const double initial_time, manager->integrate(si); } catch (...) { logger.log("Got exception during integration\n"); + return 1; } // Save the model states from forward integration @@ -151,6 +162,8 @@ void simulateModel(OpenSim::Model &model, const double initial_time, // TODO logger.log("Finished simulating model\n"); + + return 0; } static void usage() @@ -160,6 +173,7 @@ static void usage() << " -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; } @@ -189,6 +203,9 @@ int main(int argc, char **argv) case 's': fixed_step_size = strtod(argv[++i], NULL); break; + case 'y': + yes = true; + break; case 'h': usage(); exit(0); @@ -215,7 +232,7 @@ int main(int argc, char **argv) saction.sa_flags = SA_RESTART; sigaction(SIGINT, &saction, NULL); - logger.log("Starting simulation %s with model file %s, actuation data file %s\n, fixed step size: %f\n", MODEL_NAME.c_str(), model_file.c_str(), data_file.c_str(), fixed_step_size); + 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 @@ -225,23 +242,32 @@ int main(int argc, char **argv) osimModel.setUseVisualizer(false); constructModel(osimModel, actData); - simulateModel(osimModel, initial_time, final_time, fixed_step_size, output_dir); + 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()); - std::cin.get(); + if (!yes) + std::cin.get(); return 1; } catch (std::exception ex) { logger.err("Exception: %s. Press ENTER to quit", ex.what()); - std::cin.get(); + if (!yes) + std::cin.get(); return 1; } catch (...) { logger.err("Unrecognized Exception. Press ENTER to quit"); - std::cin.get(); + 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()); - std::cin.get(); + if (!yes) + std::cin.get(); return 0; } |