You are viewing the documentation for OpenSim 3.x. Are you looking for the latest OpenSim 4.0 Documentation?

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

The steps covered in part two are:

Initialize the OpenSim Model System and Get the State

An OpenSim model is backed by a SimTK::System (see Chapter 5), which actually performs the computations. As such, the model itself is a stateless object with the state being stored externally in an instance of SimTK::State. To begin simulating the block falling, we initialize the SimTK::System associated with the OpenSim model and create an instance of the system state. After the call to initSystem(), no changes should be made to the structure of the model. For example, adding forces or constraints would require the re-creation of the system and a fresh call to initSystem() since these objects may have a state of their own that needs to be incorporated into the system's state.

// Initialize the system
SimTK::State& si = osimModel.initSystem();

Define Initial Position and Velocity States of the Block

Next, we define the initial position and velocity of the block. For the free joint, the position coordinates and their velocities are ordered 0 (x-rotation), 1 (y-rotation), 2 (z-rotation), 3 (x-translation), 4 (y-translation), and 5 (z-translation).

// Define non-zero (defaults are 0) states for the free joint
CoordinateSet& modelCoordinateSet = osimModel.updCoordinateSet();
 
// set x-translation value
modelCoordinateSet[3].setValue(si, blockSideLength);
 
// set x-speed value
modelCoordinateSet[3].setSpeedValue(si, 0.1);
 
// set y-translation value
modelCoordinateSet[4].setValue(si, blockSideLength/2+0.01); 

Create the Integrator and Manager for the Simulation

We create the integrator and manager for the simulation in order to perform the numerical integration of the system equations of motion during the forward dynamics simulation. An OpenSim Manager object collects together all the resources need to perform a simulation, including the Model, the numerical methods to be employed, the current State, storage for the trajectory, and runtime options for controlling the simulation.

 

// Create the integrator and manager for the simulation.
SimTK::RungeKuttaMersonIntegrator integrator(osimModel.getMultibodySystem());
integrator.setAccuracy(1.0e-4);
Manager manager(osimModel, integrator);

Integrate the System Equations of Motion

We integrate the system equations of motion from the initial time to the final time of the simulation. Depending on your computer speed, this numerical integration could take from a few to several seconds.

// Define the initial and final simulation times
double initialTime = 0.0;
double finalTime = 4.0; 
// Integrate from initial time to final time
manager.setInitialTime(initialTime);
manager.setFinalTime(finalTime);
std::cout<<"\n\nIntegrating from "<<initialTime<<" to " <<finalTime<<std::endl;
manager.integrate(si);

Save the Simulation Results

After we have performed the integration for the forward dynamics simulation, we save the resulting motion in order to visualize the simulation we have created. Note that OpenSim uses radians internally but degrees are required in a .mot file, so we have to convert to degrees before writing out the .mot file for visualization.

// Save the simulation results
Storage statesDegrees(manager.getStateStorage());
statesDegrees.print("tugOfWar_states.sto");
osimModel.updSimbodyEngine().convertRadiansToDegrees(statesDegrees);
statesDegrees.setWriteSIMMHeader(true);
statesDegrees.print("tugOfWar_states_degrees.mot");

 

Exercise 4: After we compile and run the current main program, we can load the model and the motion in the OpenSim GUI and visualize the simulation. (To load the motion, go to File → Load Motion. Select the motion file tugOfWar_states_degrees.mot that we just wrote out.)

Figure: Block is falling in the presence of gravity.

Except for the colors, you should see the above in the GUI. Using the motion slider and video controls, visualize the motion. You should see the block falling under gravity.

  • No labels