|
a |
|
b/arm_model/main.py |
|
|
1 |
import sympy as sp |
|
|
2 |
import numpy as np |
|
|
3 |
import pylab as plt |
|
|
4 |
from model import ArmModel |
|
|
5 |
from simulation import Simulation |
|
|
6 |
from projection import TaskSpace, MuscleSpace |
|
|
7 |
from controller import JointSpaceController, TaskSpaceController,\ |
|
|
8 |
MuscleSpaceControllerJS, PosturalMuscleSpaceController |
|
|
9 |
from analysis import FeasibleMuscleSetAnalysis |
|
|
10 |
from util import calculate_feasible_muscle_set |
|
|
11 |
from sympy import init_printing |
|
|
12 |
from IPython.display import display |
|
|
13 |
|
|
|
14 |
import logging |
|
|
15 |
logging.basicConfig(level=logging.DEBUG) |
|
|
16 |
|
|
|
17 |
# basic configuration |
|
|
18 |
init_printing(use_unicode=True, wrap_line=False, |
|
|
19 |
no_global=True, use_latex=True) # 'mathjax' |
|
|
20 |
# np.set_printoptions(precision=3) |
|
|
21 |
plt.ioff() |
|
|
22 |
# plt.ion() |
|
|
23 |
|
|
|
24 |
# ------------------------------------------------------------------------ |
|
|
25 |
# utilities |
|
|
26 |
# ------------------------------------------------------------------------ |
|
|
27 |
|
|
|
28 |
|
|
|
29 |
def joint_space_control(model, fig_name='results/js_control', format='pdf'): |
|
|
30 |
"""Make use of the joint space controller to simulate a movement. |
|
|
31 |
|
|
|
32 |
""" |
|
|
33 |
# b = 0.05 produces smooth profiles |
|
|
34 |
t_end = 5.0 |
|
|
35 |
fig, ax = plt.subplots(2, 3, figsize=(15, 10)) |
|
|
36 |
|
|
|
37 |
# controller |
|
|
38 |
controller = JointSpaceController(model) |
|
|
39 |
|
|
|
40 |
# numerical integration |
|
|
41 |
simulation = Simulation(model, controller) |
|
|
42 |
simulation.integrate(t_end) |
|
|
43 |
simulation.plot_simulation(ax[0]) |
|
|
44 |
controller.reporter.plot_joint_space_data(ax[1]) |
|
|
45 |
|
|
|
46 |
fig.tight_layout() |
|
|
47 |
fig.savefig(fig_name + '.' + format, format=format, dpi=300) |
|
|
48 |
fig.savefig(fig_name + '.' + 'eps', format='eps', dpi=300) |
|
|
49 |
fig.show() |
|
|
50 |
|
|
|
51 |
return controller, t_end |
|
|
52 |
|
|
|
53 |
|
|
|
54 |
def task_space_control(model, angle, evaluate_muscle_forces, |
|
|
55 |
fig_name='results/ts_control', format='pdf'): |
|
|
56 |
"""Make use of the task space controller to simulate a movement. |
|
|
57 |
|
|
|
58 |
""" |
|
|
59 |
t_end = 2.0 |
|
|
60 |
fig, ax = plt.subplots(2, 3, figsize=(15, 10)) |
|
|
61 |
|
|
|
62 |
# define the end effector position in terms of q's |
|
|
63 |
end_effector = sp.Matrix(model.ee) |
|
|
64 |
display('x_t = ', end_effector) |
|
|
65 |
|
|
|
66 |
# task space |
|
|
67 |
task = TaskSpace(model, end_effector) |
|
|
68 |
controller = TaskSpaceController( |
|
|
69 |
model, task, angle, evaluate_muscle_forces) |
|
|
70 |
|
|
|
71 |
# numerical integration |
|
|
72 |
simulation = Simulation(model, controller) |
|
|
73 |
simulation.integrate(t_end) |
|
|
74 |
simulation.plot_simulation(ax[0]) |
|
|
75 |
controller.reporter.plot_task_space_data(ax[1]) |
|
|
76 |
|
|
|
77 |
fig.tight_layout() |
|
|
78 |
fig.savefig(fig_name + '.' + format, format=format, dpi=300) |
|
|
79 |
fig.savefig(fig_name + '.' + 'eps', format='eps', dpi=300) |
|
|
80 |
fig.show() |
|
|
81 |
|
|
|
82 |
return controller, t_end, task |
|
|
83 |
|
|
|
84 |
|
|
|
85 |
def muscle_space_control(model, use_optimization, |
|
|
86 |
fig_name='results/ms_control', format='pdf'): |
|
|
87 |
"""Make use of the muscle space controller to simulate a movement. |
|
|
88 |
|
|
|
89 |
""" |
|
|
90 |
# requires small b = 0.01 or 0 |
|
|
91 |
t_end = 3.0 |
|
|
92 |
fig, ax = plt.subplots(2, 3, figsize=(15, 10)) |
|
|
93 |
|
|
|
94 |
# muscle space |
|
|
95 |
muscle_space = MuscleSpace(model, use_optimization) |
|
|
96 |
controller = MuscleSpaceControllerJS(model, muscle_space) |
|
|
97 |
|
|
|
98 |
# numerical integration |
|
|
99 |
simulation = Simulation(model, controller) |
|
|
100 |
simulation.integrate(t_end) |
|
|
101 |
simulation.plot_simulation(ax[0]) |
|
|
102 |
controller.reporter.plot_muscle_space_data_js(ax[1]) |
|
|
103 |
|
|
|
104 |
fig.tight_layout() |
|
|
105 |
fig.savefig(fig_name + '.' + format, format=format, dpi=300) |
|
|
106 |
fig.savefig(fig_name + '.' + 'eps', format='eps', dpi=300) |
|
|
107 |
fig.show() |
|
|
108 |
|
|
|
109 |
|
|
|
110 |
def postural_muscle_space_control(model, kp, use_optimization, |
|
|
111 |
fig_name='results/pc_control', format='pdf'): |
|
|
112 |
"""Make use of the muscle space controller for posture control. |
|
|
113 |
|
|
|
114 |
""" |
|
|
115 |
t_end = 3.0 |
|
|
116 |
kd = 10 |
|
|
117 |
delay = 0.02 |
|
|
118 |
a = 15.0 |
|
|
119 |
t0 = 0.1 |
|
|
120 |
sigma = 0.01 |
|
|
121 |
gamma = np.pi |
|
|
122 |
fig, ax = plt.subplots(2, 3, figsize=(15, 10)) |
|
|
123 |
|
|
|
124 |
# muscle space |
|
|
125 |
muscle_space = MuscleSpace(model, use_optimization=use_optimization) |
|
|
126 |
controller = PosturalMuscleSpaceController(model, muscle_space, kp, kd, |
|
|
127 |
delay, a, t0, sigma, gamma) |
|
|
128 |
|
|
|
129 |
# numerical integration |
|
|
130 |
simulation = Simulation(model, controller) |
|
|
131 |
simulation.integrate(t_end) |
|
|
132 |
simulation.plot_simulation(ax[0]) |
|
|
133 |
controller.reporter.plot_postural_muscle_space_data(ax[1]) |
|
|
134 |
|
|
|
135 |
fig.tight_layout() |
|
|
136 |
fig.savefig(fig_name + '.' + format, format=format, dpi=300) |
|
|
137 |
fig.savefig(fig_name + '.' + 'eps', format='eps', dpi=300) |
|
|
138 |
fig.show() |
|
|
139 |
|
|
|
140 |
|
|
|
141 |
def export_eom(model): |
|
|
142 |
"""Exports equations of motion of the model in a latex format. |
|
|
143 |
|
|
|
144 |
""" |
|
|
145 |
M = model.M |
|
|
146 |
f = model.f |
|
|
147 |
R = model.R |
|
|
148 |
for i in range(0, M.shape[0]): |
|
|
149 |
for j in range(0, M.shape[1]): |
|
|
150 |
print('\\begin{dmath}') |
|
|
151 |
print('M_{' + str(i + 1) + ',' + str(j + 1) + '} = ' + |
|
|
152 |
sp.latex(M[i, j], mode='plain')) |
|
|
153 |
print('\\end{dmath}') |
|
|
154 |
|
|
|
155 |
for i in range(0, f.shape[0]): |
|
|
156 |
print('\\begin{dmath}') |
|
|
157 |
print('f_' + str(i + 1) + ' = ' + sp.latex(f[i], mode='plain')) |
|
|
158 |
print('\\end{dmath}') |
|
|
159 |
|
|
|
160 |
for i in range(0, R.shape[0]): |
|
|
161 |
for j in range(0, R.shape[1]): |
|
|
162 |
print('\\begin{dmath}') |
|
|
163 |
print('R_{' + str(i + 1) + ',' + str(j + 1) + '} = ' + |
|
|
164 |
sp.latex(R[i, j], mode='plain')) |
|
|
165 |
print('\\end{dmath}') |
|
|
166 |
|
|
|
167 |
|
|
|
168 |
def draw_model(model): |
|
|
169 |
"""Draw a model in a pre-defined pose. |
|
|
170 |
|
|
|
171 |
""" |
|
|
172 |
fig, ax = plt.subplots(1, 1, figsize=(10, 10), frameon=False) |
|
|
173 |
model.draw_model([60, 70, 50], True, ax, 1, False) |
|
|
174 |
fig.tight_layout() |
|
|
175 |
fig.savefig('results/arm_model.pdf', dpi=600, format='pdf', |
|
|
176 |
transparent=True, pad_inches=0, bbox_inches='tight') |
|
|
177 |
plt.show() |
|
|
178 |
|
|
|
179 |
|
|
|
180 |
# ------------------------------------------------------------------------ |
|
|
181 |
# main |
|
|
182 |
# ------------------------------------------------------------------------ |
|
|
183 |
|
|
|
184 |
plt.close('all') |
|
|
185 |
case_study = 1 |
|
|
186 |
if case_study == 0: # joint space |
|
|
187 |
model = ArmModel(use_gravity=0, use_coordinate_limits=0, use_viscosity=0) |
|
|
188 |
model.pre_substitute_parameters() |
|
|
189 |
base_name = 'results/joint_space_control/joint_space' |
|
|
190 |
controller, t_end = joint_space_control(model, fig_name=base_name) |
|
|
191 |
elif case_study == 1: # task space and feasible muscle forces |
|
|
192 |
model = ArmModel(use_gravity=0, use_coordinate_limits=1, use_viscosity=1) |
|
|
193 |
model.pre_substitute_parameters() |
|
|
194 |
base_name = 'results/task_space_control/task_space' |
|
|
195 |
controller, t_end, task = task_space_control(model, np.pi, False, |
|
|
196 |
fig_name=base_name) |
|
|
197 |
feasible_muscle_set = FeasibleMuscleSetAnalysis(model, controller.reporter) |
|
|
198 |
base_name = 'results/feasible_muscle_forces/feasible_forces_ts180_' |
|
|
199 |
calculate_feasible_muscle_set(feasible_muscle_set, |
|
|
200 |
base_name, 0.0, t_end, |
|
|
201 |
0.1, 500) |
|
|
202 |
elif case_study == 2: # posture |
|
|
203 |
model = ArmModel(use_gravity=0, use_coordinate_limits=0, use_viscosity=0) |
|
|
204 |
model.pre_substitute_parameters() |
|
|
205 |
base_name = 'results/posture_control/posture_full' |
|
|
206 |
postural_muscle_space_control(model, 10, True, fig_name=base_name) |
|
|
207 |
base_name = 'results/posture_control/posture_reflex' |
|
|
208 |
postural_muscle_space_control(model, 0, True, fig_name=base_name) |
|
|
209 |
elif case_study == 3: # muscle space |
|
|
210 |
model = ArmModel(use_gravity=0, use_coordinate_limits=0, use_viscosity=0) |
|
|
211 |
model.pre_substitute_parameters() |
|
|
212 |
base_name = 'results/muscle_space_control/muscle_space' |
|
|
213 |
muscle_space_control(model, False, fig_name=base_name) |
|
|
214 |
elif case_study == 4: # model export |
|
|
215 |
model = ArmModel(use_gravity=1, use_coordinate_limits=0, use_viscosity=0) |
|
|
216 |
draw_model(model) |
|
|
217 |
export_eom(model) |
|
|
218 |
else: |
|
|
219 |
print('Undefined case') |