You are viewing the documentation for OpenSim 2.4. Are you looking for the latest OpenSim 4.0 Documentation?
Creating an Actuator Part Two
The steps covered in part two are:
Using the ControllableSpring (toyLeg_example.cpp)
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
Ready toyLeg_example.cpp to use the ControllableSpring
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.
Add a ControllableSpring to the model
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);
Modify the control values given to the actuator
Comment out the line defining the control values for the piston. Below it, add a series of control values that will be applied to the spring.
// Define the control values for the piston // double controlT0[1] = {0.982}, controlTf[1] = {0.978}; // Define the control values for the spring double controlT0[1] = {1.0}, controlT1[1] = {1.0}, controlT2[1] = {0.25}, controlT3[1] = {.25}, controlT4[1] = {5};
Point the controls to the spring
After the definition of control1, modify the setName call to apply control1 to the spring instead of the actuator.
ControlLinear *control1 = new ControlLinear(); control1->setName("spring"); //change this from 'piston' to 'spring'
Point the control set to the new control values
Comment out the section that sets the controlSet values to the piston controls and then point controlSet to the spring controls you just defined.
// set control values for the piston /*controlSet->setControlValues(t0, controlT0); controlSet->setControlValues(tf, controlTf);*/ // set control values for the spring controlSet->setControlValues(t0, controlT0); controlSet->setControlValues(4.0, controlT1); controlSet->setControlValues(7.0, controlT2); controlSet->setControlValues(10.0, controlT3); controlSet->setControlValues(tf, controlT4);
Save the resulting motion as a different file
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");
Build and run the example
- Build the Actuator_examples project again (see Section 4.3.3.1 above).
- Then build the INSTALL project.
- Make sure that the <OpenSim2.0_intall_dir>/bin directory appears at the front of your PATH. To check and/or set your PATH, go to Start -> System Properties (or System). Click on the Advanced tab and then select the Environment Variables button.
- Navigate to the install directory and run the executable file, toyLeg_example. After running the executable, use the GUI to open the model toyLeg.osim and load the new motion file (SpringActuatedLeg_states_degrees.mot). Upon visualizing the motion, you should see the block oscillate at different magnitudes and frequencies as the spring stiffness is varied over time.
OpenSim is supported by the Mobilize Center , an NIH Biomedical Technology Resource Center (grant P41 EB027060); the Restore Center , an NIH-funded Medical Rehabilitation Research Resource Network Center (grant P2C HD101913); and the Wu Tsai Human Performance Alliance through the Joe and Clara Tsai Foundation. See the People page for a list of the many people who have contributed to the OpenSim project over the years. ©2010-2024 OpenSim. All rights reserved.