I'm currently adding a ControlSetController to an existing model (loaded from an .osim file) and forward simulating. The simulation (with the controller) runs fine but issues arise when exciting the program and the destructor is called.
The addition of the controller is performed by;
Code: Select all
// Add
createControlFileXml(controlsPath,i);
OpenSim::ControlSet controlSet(absPath + "/_controls.xml");
OpenSim::ControlSetController controller;
controller.setControlSet((OpenSim::ControlSet*)OpenSim::Object::SafeCopy(&controlSet));
model.addController(&controller);
// Set the model to the proper state again after all the changes
model.finalizeFromProperties();
Code: Select all
int main(){
std::string modelPath = absPath + "/subject5_final.osim";
std::string statesPath = absPath + "/scaled_states.sto";
std::string controlsPath = absPath + "/scaled_controls.sto";
for(int i=0; i<1; i++){
// Load and initialize model
OpenSim::Model model(modelPath);
model.initSystem();
// load in state data and create 'StatesTrajectory' object which can be
// indexed to extract the correct state.
OpenSim::Storage storage(statesPath);
OpenSim::StatesTrajectory stateTrajectory =
OpenSim::StatesTrajectory::createFromStatesStorage(model,storage,false,false,true);
// The state of this iteration
SimTK::State state = stateTrajectory[i];
state.setTime(0.0);
// Set the model to the correct state
model.realizeAcceleration(state);
// Add controls
createControlFileXml(controlsPath,i);
OpenSim::ControlSet controlSet(absPath + "/_controls.xml");
OpenSim::ControlSetController controller;
controller.setControlSet((OpenSim::ControlSet*)OpenSim::Object::SafeCopy(&controlSet));
model.addController(&controller);
// Set the model to the proper state again after all the changes
model.finalizeFromProperties();
// Create a forward tool and connect the controls file, the initial
// state file, and the external force file
OpenSim::ForwardTool fwdTool;
fwdTool.setModel(model);
fwdTool.setResultsDir(absPath + "/forward_out/");
fwdTool.setFinalTime(finalTime);
fwdTool.setStatesFileName(absPath + "state.sto");
fwdTool.print("fwd_setup.xml");
model.setUseVisualizer(false);
// Run forward simulation
fwdTool.run();
}
return 0;
}
I've tried
- `controller.setControlSet(&controlSet)'
- model.removeController() at the end of the for-loop
but this all did not work. The only way to resolve the issue is by not doing "model.addController(&controller)" which is obviously not an option. I'm wondering what the issue could be or what I am missing in my code. Please let me know if you can help me out.
Thanks!