-
Notifications
You must be signed in to change notification settings - Fork 308
/
OpenSimCreateTugOfWarModel.m
154 lines (123 loc) · 6.03 KB
/
OpenSimCreateTugOfWarModel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
% ----------------------------------------------------------------------- %
% The OpenSim API is a toolkit for musculoskeletal modeling and %
% simulation. See http://opensim.stanford.edu and the NOTICE file %
% for more information. OpenSim is developed at Stanford University %
% and supported by the US National Institutes of Health (U54 GM072970, %
% R24 HD065690) and by DARPA through the Warrior Web program. %
% %
% Copyright (c) 2005-2017 Stanford University and the Authors %
% %
% Licensed under the Apache License, Version 2.0 (the "License"); %
% you may not use this file except in compliance with the License. %
% You may obtain a copy of the License at %
% http://www.apache.org/licenses/LICENSE-2.0. %
% %
% Unless required by applicable law or agreed to in writing, software %
% distributed under the License is distributed on an "AS IS" BASIS, %
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or %
% implied. See the License for the specific language governing %
% permissions and limitations under the License. %
% ----------------------------------------------------------------------- %
% OpenSimCreateTugOfWarModel.m - script to perform the same model building and
% simulation tasks as the MainExample from the SDK examples
% This example script creates a model similar to the TugofWar API example.
% Two muscles are created and attached to a block. Linear controllers are
% defined for the muscles.
% Pull in the modeling classes straight from the OpenSim distribution
import org.opensim.modeling.*
%///////////////////////////////////////////
%// DEFINE BODIES AND JOINTS OF THE MODEL //
%///////////////////////////////////////////
% Create a blank model
model = Model();
model.setName('TugOfWar')
% Convenience - create a zero value Vec3
zeroVec3 = ArrayDouble.createVec3(0);
% Set gravity as 0 since there is no ground contact model in this version
% of the example
model.setGravity(zeroVec3);
% GROUND BODY
% Get a reference to the model's ground body
ground = model.getGround();
% Add display geometry to the ground to visualize in the GUI
ground.attachGeometry(Mesh('ground.vtp'));
ground.attachGeometry(Mesh('anchor1.vtp'));
ground.attachGeometry(Mesh('anchor2.vtp'));
% "BLOCK" BODY
% Create a block Body with associate dimensions, mass properties, and DisplayGeometry
block = Body();
block.setName('Block');
block.setMass(20);
block.setMassCenter(zeroVec3);
% Need to set inertia
block.attachGeometry(Mesh('block.vtp'));
% FREE JOINT
% Create a new free joint with 6 degrees-of-freedom (coordinates) between the block and ground bodies
blockSideLength = 0.1;
locationInParentVec3 = ArrayDouble.createVec3([0, blockSideLength/2, 0]);
blockToGround = FreeJoint('blockToGround', ...
ground, locationInParentVec3, zeroVec3, ...
block, zeroVec3, zeroVec3);
% Set bounds on coordinates
angleRange = [-pi/2, pi/2];
positionRange = [-1, 1];
blockToGround.upd_coordinates(0).setRange(angleRange);
blockToGround.upd_coordinates(1).setRange(angleRange);
blockToGround.upd_coordinates(2).setRange(angleRange);
blockToGround.upd_coordinates(3).setRange(positionRange);
blockToGround.upd_coordinates(4).setRange(positionRange);
blockToGround.upd_coordinates(5).setRange(positionRange);
% Add the block body to the model
model.addBody(block)
%///////////////////////////////////////
%// DEFINE FORCES ACTING ON THE MODEL //
%///////////////////////////////////////
% Set muscle parameters
maxIsometricForce = 1000.0;
optimalFiberLength = 0.25;
tendonSlackLength = 0.1;
pennationAngle = 0.0;
% Create new muscles
muscle1 = Thelen2003Muscle();
muscle1.setName('muscle1')
muscle1.setMaxIsometricForce(maxIsometricForce)
muscle1.setOptimalFiberLength(optimalFiberLength)
muscle1.setTendonSlackLength(tendonSlackLength);
muscle1.setPennationAngleAtOptimalFiberLength(pennationAngle)
% Path for muscle 1
muscle1.addNewPathPoint('muscle1-point1', ground, ArrayDouble.createVec3([0.0,0.05,-0.35]))
muscle1.addNewPathPoint('muscle1-point2', block, ArrayDouble.createVec3([0.0,0.0,-0.05]))
% Repeat for Muscle 2
muscle2 = Thelen2003Muscle();
muscle2.setName('muscle2');
muscle2.setMaxIsometricForce(maxIsometricForce)
muscle2.setOptimalFiberLength(optimalFiberLength)
muscle2.setTendonSlackLength(tendonSlackLength)
muscle2.setPennationAngleAtOptimalFiberLength(pennationAngle)
% Path for muscle 2
muscle2.addNewPathPoint('muscle2-point1', ground, ArrayDouble.createVec3([0.0,0.05,0.35]))
muscle2.addNewPathPoint('muscle2-point2', block, ArrayDouble.createVec3([0.0,0.0,0.05]))
% Add the two muscles (as forces) to the model
model.addForce(muscle1)
model.addForce(muscle2);
%Set up Controller
initialTime = 0.0;
finalTime = 3.0;
muscleController = PrescribedController();
muscleController.setName('LinearRamp_Controller')
muscleController.setActuators(model.updActuators())
% Define linear functions for the control values for the two muscles
slopeAndIntercept1=ArrayDouble(0.0, 2);
slopeAndIntercept2=ArrayDouble(0.0, 2);
% Muscle1 control has slope of -1 starting 1 at t = 0
slopeAndIntercept1.setitem(0, -1.0/(finalTime-initialTime));
slopeAndIntercept1.setitem(1, 1.0);
% Muscle2 control has slope of 0.95 starting 0.05 at t = 0
slopeAndIntercept2.setitem(0, 0.95/(finalTime-initialTime));
slopeAndIntercept2.setitem(1, 0.05);
% Set the indiviudal muscle control functions for the prescribed muscle controller
muscleController.prescribeControlForActuator('muscle1', LinearFunction(slopeAndIntercept1));
muscleController.prescribeControlForActuator('muscle2', LinearFunction(slopeAndIntercept2));
% Add the control set controller to the model
model.addController(muscleController);
model.print('tug_of_war_muscles_controller.osim');