In this section, we will add to the tug-of-war example from Chapter 2 by creating a controller that will calculate excitations for the two muscles in the model. The controller we will build computes excitations that naively try to track a desired trajectory of the block in the tug-of-war model. Through this example, you will see how to implement a proportional-derivative (PD) like muscle controller to control a model using the OpenSim API.

We will build the example up in pieces. First, we will write a function that returns the desired position of the model in the z-direction (the direction of the line connecting the origins of both muscles in the ground) at a given time. Then, we will implement a PD controller that extends OpenSim's base Controller class to calculate excitations for the muscles based on the desired position of the model and the model's current state. Finally, we will connect the controller to the model to run a forward dynamics simulation that attempts to make the model follow the desired trajectory by controlling the model's muscle excitations. We can observe the final trajectory of the model to assess how well this controller is able to reproduce the model's desired motion. The resulting source code and associated files for this example come with the OpenSim 2.0 distribution under the directory:


The following sections will describe the blocks of code needed to implement this example: