-
Notifications
You must be signed in to change notification settings - Fork 1
/
muscleStatePrescribeGRFPrescribeStrict.m
235 lines (186 loc) · 9.14 KB
/
muscleStatePrescribeGRFPrescribeStrict.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
function [Issues] = muscleStatePrescribeGRFPrescribeStrict(Issues)
import org.opensim.modeling.*;
tag = 'muscleprescribestrict';
% construct MocoInverse tool
inverse = MocoInverse();
% need to get the geometry path for the pathwrapset
% construct ModelProcessor and sit it on the tool.
% replace default muscles with degrootefregly 2016 muscles, and adjust params
modelProcessor = ModelProcessor('simple_model_all_the_probes_adjusted.osim');
% modelProcessor = ModelProcessor("simple_model_all_the_probes.osim");
modelProcessor.append(ModOpAddExternalLoads('grf_walk.xml'));
% now to do stuff with the model
% modelProcessor = ModelProcessor(model);
% need to adjust some of the joints - weld them
weldem = StdVectorString();
% weldem.add('subtalar_r');
weldem.add('mtp_r');
% weldem.add('subtalar_l');
weldem.add('mtp_l');
% weldem.add('radius_hand_r');
% weldem.add('radius_hand_l');
modelProcessor.append(ModOpReplaceJointsWithWelds(weldem));
% model = modelProcessor.process();
% set up the base model
% modelProcessor.append(ModOpIgnoreTendonCompliance());
modelProcessor.append(ModOpReplaceMusclesWithDeGrooteFregly2016());
% only valid for degroote
% modelProcessor.append(ModOpIgnorePassiveFiberForcesDGF());
% only valid for degroote
modelProcessor.append(ModOpScaleActiveFiberForceCurveWidthDGF(1.5));
% modelProcessor.append(ModOpAddReserves(1.0));
modelProcessor.append(ModOpAddReserves(1.0));
% now do tweaks to get tendon compliance
basemodel = modelProcessor.process();
% turn on the probes for the study
basemodel = probeActivate(basemodel);
% updates
basemodel.initSystem();
basemuscles = basemodel.updMuscles();
numBaseMuscles = basemuscles.getSize();
% for m = 0:numBaseMuscles-1
% set tendon compliance on for certain muscles
% if lopt > lst want stiff (ignore)
% get the muscle
% basemusc = basemuscles.get(m);
% get lopt
% baselopt = basemusc.getOptimalFiberLength();
% get lst
% baselst = basemusc.getTendonSlackLength();
% set compliance if lopt > lst
% if baselopt < baselst
% basemusc.set_ignore_tendon_compliance(false)
% end
% end
%% do more model processor stuff
modelProcessorDC = ModelProcessor(basemodel);
modelProcessorDC.append(ModOpFiberDampingDGF(0.01));
% modelProcessorDC.append(ModOpAddReserves(1, 2.5, true));
modelProcessorDC.append(ModOpTendonComplianceDynamicsModeDGF('implicit'));
% need to add the bhargava metabolics probe for cost function
% bhargmet = Bhargava2004SmoothedMuscleMetabolics();
% bhargmet.setName("simmetabolics");
% bhargmet.set_use_smoothing(true);
% modelmet = modelProcessorDC.process();
% modelmetMuscles = modelmet.getMuscles();
% nummodelmetMuscles = modelmetMuscles.getSize();
% for m = 0:nummodelmetMuscles-1
% musc = modelmetMuscles.get(m);
% muscName = musc.getName();
% muscName = char(muscName);
% bhargmet.addMuscle(muscName, musc);
% end
% modelmet.addComponent(bhargmet);
% modelmet.finalizeConnections();
% modelProcessorDC2 = ModelProcessor(modelmet);
% inverse.setModel(modelProcessorDC2);
inverse.setModel(modelProcessorDC);
tempkintable = TableProcessor('torque_statetrack_grfprescribe_strict_solution.sto').process();
% tempkintable = TableProcessor('torque_statetrack_grfprescribe_solution.sto').process();
% tempkintable = TableProcessor('./ResultsRRA_1/subject01_walk1_RRA_states.sto').process();
% tempkintable = TableProcessor('./coordinates_updated.mot').process();
templabels_os = tempkintable.getColumnLabels();
% templabels = []
for i=0:templabels_os.size()-1
% templabels = [templabels, templabels_os.get(i)];
temp = templabels_os.get(i);
if ~startsWith(temp, '/jointset') %~temp.startsWith('/jointset')
tempkintable.removeColumn(temp);
end
end
inverse.setKinematics(TableProcessor(tempkintable));
% inverse.setKinematics(TableProcessor('torque_statetrack_grfprescribe_solution.sto'));
% construct TableProcessor of the coordinate data and pass it to the inverse tool
% if no operators, it returns the base table
% inverse.setKinematics(TableProcessor('torque_statetrack_grfprescribe_solution.sto'));
% get the subject name and gait timings
load 'G:\Shared drives\Exotendon\muscleModel\muscleEnergyModel\subjectGaitCycles.mat';
workdir = pwd;
[~,trialname,~] = fileparts(pwd);
cd ../
[~,conditionname,~] = fileparts(pwd);
cd ../
[~,subjectname,~] = fileparts(pwd);
cd(workdir);
gait_start = subjectgaitcycles.(genvarname(subjectname)).(genvarname(conditionname)).(genvarname(trialname)).initial;
gait_end = subjectgaitcycles.(genvarname(subjectname)).(genvarname(conditionname)).(genvarname(trialname)).final;
% set time and intervals
inverse.set_initial_time(gait_start);
inverse.set_final_time(gait_end);
inverse.set_mesh_interval(0.02); %.05 .02, .01% may need to adjust this
% By default, Moco gives an error if the kinematics contains extra columns.
% Here, we tell Moco to allow (and ignore) those extra columns.
inverse.set_kinematics_allow_extra_columns(true);
% set inverse goals
inverse.set_minimize_sum_squared_activations(true);
inverse.set_reserves_weight(1e-10);% 3e-2 30
study = inverse.initialize();
problem = study.updProblem();
% TODO test
% excitation_effort goal
excitegoal = problem.updGoal('excitation_effort');
excitegoal.setWeight(5e-4); % 9e-1 5e-4 2.5e-4
% 'activation_effort' goal
% activegoal = problem.updGoal('activation_effort');
% activegoal.setWeight(5e-4);
% add metabolic cost goal
% metGoal = MocoOutputGoal("met", 0.01);
% metGoal.setOutputPath("/simmetabolics|total_metabolic_rate");
% metGoal.setDivideByDisplacement(true);
% metGoal.setDivideByMass(true);
% problem.addGoal(metGoal);
% add goals to the problem and scale them to get close to ~1
% effortgoal = MocoControlGoal('effort');
% effortgoal.setWeight(5e-3);
% problem.addGoal(effortgoal);
initactivationgoal = MocoInitialActivationGoal('init_activation');
initactivationgoal.setWeight(1); % 1
problem.addGoal(initactivationgoal);
% for post problem processing
model = modelProcessorDC.process();
model.print('post_simple_model_all_the_probes_muscletrack.osim');
%%% moving on to solve
% set up the solver and solve the problem
solver = MocoCasADiSolver.safeDownCast(study.updSolver());
solver.resetProblem(problem);
solver.set_optim_convergence_tolerance(.001); % 1e-2
solver.set_optim_constraint_tolerance(1e-4); % 1e-2
solution = study.solve();
solution.insertStatesTrajectory(tempkintable);
% solution = MocoTrajectory('muscle_stateprescribe_grfprescribe_solution.sto');
% solution.write('muscleguess.sto');
% study.visualize(solution);
% post processing
% solution.write('muscle_statetrack_grfprescribe_solution.sto');
solution.write('muscle_stateprescribe_grfprescribe_strict_solution.sto')
STOFileAdapter.write(solution.exportToControlsTable(), 'muscleprescribe_strict_controls.sto');
STOFileAdapter.write(solution.exportToStatesTable(), 'muscleprescribe_strict_states.sto');
% STOFileAdapter.write(solution.exportToControlsTable(), 'muscleprescribe_controls_old.sto');
% STOFileAdapter.write(solution.exportToStatesTable(), 'muscleprescribe_states_old.sto');
% Solve the problem and write the solution to a Storage file.
% solution = inverse.solve(true); % to visualize
% solution = inverse.solve();
% solution.getMocoSolution().write('muscle_stateprescribe_grfprescribe_solution.sto');
% STOFileAdapter.write(solution.getMocoSolution().exportToControlsTable(), 'muscleprescribe_controls.sto')
% STOFileAdapter.write(solution.getMocoSolution().exportToStatesTable(), 'muscleprescribe_states.sto')
% Generate a report with plots for the solution trajectory.
% model = modelProcessor.process();
report = osimMocoTrajectoryReport(model, ...
'muscle_stateprescribe_grfprescribe_strict_solution.sto', 'bilateral', true);
% The report is saved to the working directory.
reportFilePath = report.generate();
pdfFilePath = reportFilePath(1:end-2);
pdfFilePath = strcat(pdfFilePath, 'pdf');
ps2pdf('psfile',reportFilePath,'pdffile',pdfFilePath, ...
'gscommand','C:\Program Files\gs\gs9.54.0\bin\gswin64.exe', ...
'gsfontpath','C:\Program Files\gs\gs9.54.0\Resource\Font', ...
'gslibpath','C:\Program Files\gs\gs9.54.0\lib');
% open(pdfFilePath);
% post analysis and validation
Issues = [Issues; [java.lang.String('muscledrivensim'); java.lang.String('inverseproblem')]];
analyzeMetabolicCost(solution, 'muscleprescribe');
% Issues = computeIDFromResult(Issues, solution, tag);
% analyzeMetabolicCost(solution);
% trackorprescribe = 'prescribe';
% computeKinematicDifferences(solution, trackorprescribe);
end