The steps covered in part two are:
We can now use the ControllableSpring class in an example to see its effects. The toyLeg_example.cpp file we have provided implements a toy leg model that is driven by a PistonActuator (see toyLeg figure below).The model is built up in the sequence ground->linkage1->linkage2->block with pin joints between all the segments. The block is constrained to move only in the vertical direction. A PistonActuator called "piston" acts between the distal end of linkage1 and the center of the block. We will modify the main() routine to replace the piston actuator with a variable stiffness spring.
toyLeg example
Open the toyLeg_example.cpp file, if you have not already done so. Add the ControllableSpring class to the included files as shown below.
#include "PistonActuator.h" #include "ControllableSpring.h" #include <OpenSim/OpenSim.h> using namespace OpenSim; using namespace SimTK; |
Within Visual Studios, locate the Actuators_examples project. Right click it and select "Build" in order to rebuild toyLeg_example.cpp and force the first build of ControllableSpring.h. You will need to switch from "Debug" to either "Release" or "RelWithDebInfo" if you do not have debuggable OpenSim libraries with which to link.
Find the line after the piston is added to the model. At this location, create a ControllableSpring. Set it up to have the identical geometry as the piston, and add it to the model.
osimModel.addForce(piston); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Add ControllableSpring between the first linkage and the second block //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ControllableSpring *spring = new ControllableSpring; spring->setName("spring"); spring->setBodyA(block); spring->setBodyB(&ground); spring->setPointA(pointOnBodies); spring->setPointB(pointOnBodies); spring->setOptimalForce(2000.0); spring->setPointsAreGlobal(false); spring->setRestLength(0.8); osimModel.addForce(spring); |
We will use prescribed controller for the toy leg model, we give the piston actuator a constant function and the spring actuator a piece-wise linear function to simulate change in stiffness.
PrescribedController *legController = new PrescribedController(); legController->setActuators(osimModel.updActuators()); // specify some control nodes for spring stiffness control double t[] = {0.0, 4.0, 7.0, 10.0, 15.0}; double x[] = {1.0, 1.0, 0.25, 0.25, 5.0}; // specify the control function for each actuator legController->prescribeControlForActuator("piston", new Constant(0.982)); legController->prescribeControlForActuator("spring", new PiecewiseLinearFunction(5, t, x)); osimModel.addController(legController); |
Change the Save Results section in order to print the resulting toyLeg kinematics under a new file name.
// Save results Storage statesDegrees(manager.getStateStorage()); osimModel.updSimbodyEngine().convertRadiansToDegrees(statesDegrees); //statesDegrees.print("PistonActuatedLeg_states_degrees.mot"); statesDegrees.print("SpringActuatedLeg_states_degrees.mot"); |
Next: Creating a Customized Muscle Model Previous: Creating an Actuator Part One Home: Scripting and Development | Developer's Guide | Adding New Functionality |