-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunction.py
308 lines (224 loc) · 11.1 KB
/
function.py
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import os
from sympy import cse
from sympy.printing.numpy import NumPyPrinter
from sympy.printing.cxx import CXX11CodePrinter
from sympy.printing.julia import JuliaCodePrinter
class Function:
def __init__(self, name, path, input_args, output_args, expr):
self.function_name = name
self.file_path = path
self.input_args = input_args
self.output_args = output_args
self.expr = cse(expr)
self.code_printer = None
self.file_handle = None
def open_file(self, file_extension):
self.file_handle = open(os.path.join(self.file_path, self.function_name + f'.{file_extension}'), 'w')
def close_file(self):
self.file_handle.close()
def _four_spaces(self):
return ' '
def _two_spaces(self):
return ' '
def _tab_space(self):
return '\t'
def _newline(self):
return '\n'
class PythonFunction(Function):
def __init__(self, name, path, input_args, output_args, expr):
super().__init__(name, path, input_args, output_args, expr)
self.code_printer = NumPyPrinter
def print(self):
self.open_file('py')
print(f'Printing the function: {self.function_name}')
print(f'{self._import_str()}', file=self.file_handle)
print(f'def {self.function_name}({self._input_argument_str()}):', file=self.file_handle)
# print(self._q_array_to_local_var(), file=self.file_handle)
for input_arg in self.input_args:
print(self._array_to_local_var(input_arg), file=self.file_handle)
print(self._auto_x_to_code(), file=self.file_handle)
print(self._output_statement(), file=self.file_handle)
print(self._return_statement(), file=self.file_handle)
self.close_file()
def _import_str(self):
imp_str = 'import numpy\n\n'
return imp_str
def _input_argument_str(self):
return ', '.join(self.input_args)
def _return_statement(self):
return self._four_spaces() + 'return ' + ', '.join([out_args for out_args in self.output_args])
def _array_to_local_var(self, var_name):
local_var_str = ''
for i in range(7):
local_var_str += self._four_spaces() + f'{var_name}{i+1} = {var_name}[{i}]\n'
return local_var_str
def _output_statement(self):
output_expression = self.expr[1]
output_str = ''
for oe, oa in zip(output_expression, self.output_args):
output_str += self._four_spaces() + oa + ' = ' + self.code_printer().doprint(oe) + '\n'
return output_str
def _auto_x_to_code(self):
auto_x = self.expr[0]
auto_x_str = ''
for ax in auto_x:
auto_x_str += self._four_spaces() + self.code_printer().doprint(ax[0]) + ' = ' + self.code_printer().doprint(ax[1]) + '\n'
return auto_x_str
class CppFunction(Function):
def __init__(self, name, path, input_args, output_arg, aux_output_args, expr):
super().__init__(name, path, input_args, output_arg, expr)
self.code_printer = CXX11CodePrinter
self.aux_output_args = aux_output_args
def _array_to_local_var(self, var_name):
local_var_str = ''
for i in range(7):
local_var_str += f'{self._tab_space()}const double {var_name}{i+1} = {var_name}[{i}];\n'
return local_var_str
def _output_statement(self):
output_expression = self.expr[1]
output_str = ''
for oe, oa in zip(output_expression, self.output_args):
output_str += self._tab_space() + oa + ' << '
output_str += ', '.join([self.code_printer().doprint(e) for e in oe]) + ';\n'
return output_str
def _aux_output_statement(self):
output_expression = self.expr[1]
output_str = ''
for oe, oa in zip(output_expression, self.aux_output_args):
output_str += self._tab_space() + oa + ' << '
output_str += ', '.join([self.code_printer().doprint(e) for e in oe]) + ';\n'
return output_str
def _auto_x_to_code(self):
auto_x = self.expr[0]
auto_x_str = ''
for ax in auto_x:
auto_x_str += self._tab_space() + 'const double ' + self.code_printer().doprint(ax[0]) + ' = ' + self.code_printer().doprint(ax[1]) + ';\n'
return auto_x_str
def print(self):
self.open_file('cpp')
print(f'Printing the function: {self.function_name}')
print(f'{self._header_str()}', file=self.file_handle)
print(f'{self.output_args[list(self.output_args.keys())[0]]} {self.function_name}({self._input_argument_str()})', file=self.file_handle)
print('{\n', file=self.file_handle)
for out_arg in self.aux_output_args:
print(self._tab_space() + self.aux_output_args[out_arg] + ' ' + out_arg + ';', file=self.file_handle)
print(f'{self._tab_space()}{self.output_args[list(self.output_args.keys())[0]]} {list(self.output_args.keys())[0]};', file=self.file_handle)
print('\n', file=self.file_handle)
for input_arg in self.input_args:
print(self._array_to_local_var(input_arg), file=self.file_handle)
print(self._auto_x_to_code(), file=self.file_handle)
print(self._aux_output_statement(), file=self.file_handle)
print(self._output_statement(), file=self.file_handle)
print(self._return_statement(), file=self.file_handle)
print('}\n', file=self.file_handle)
self.close_file()
def _header_str(self):
header_str = '#include "Eigen/Dense"\n' + '#include <cmath>\n'
return header_str
def _input_argument_str(self):
return ', '.join([f'{self.input_args[s]} {s}' for s in self.input_args])
def _return_statement(self):
return f'{self._tab_space()}return {list(self.output_args.keys())[0]};'
class JuliaFunction(Function):
def __init__(self, name, path, input_args, output_arg, expr):
super().__init__(name, path, input_args, output_arg, expr)
self.code_printer = JuliaCodePrinter
def print(self):
self.open_file('jl')
print(f'Printing the function: {self.function_name}')
print(f'{self._import_str()}', file=self.file_handle)
print(f'function {self.function_name}({self._input_argument_str()})', file=self.file_handle)
for input_arg in self.input_args:
print(self._array_to_local_var(input_arg), file=self.file_handle)
print(self._auto_x_to_code(), file=self.file_handle)
print(self._output_statement(), file=self.file_handle)
print(self._return_statement(), file=self.file_handle)
print('end', file=self.file_handle)
self.close_file()
def _import_str(self):
imp_str = 'using StaticArrays\n\n'
return imp_str
def _input_argument_str(self):
return ', '.join([f'{s}::{self.input_args[s]}' for s in self.input_args])
def _array_to_local_var(self, var_name):
local_var_str = ''
for i in range(7):
local_var_str += self._four_spaces() + f'{var_name}{i+1} = {var_name}[{i+1}]\n'
return local_var_str
def _auto_x_to_code(self):
auto_x = self.expr[0]
auto_x_str = ''
for ax in auto_x:
auto_x_str += self._four_spaces() + self.code_printer().doprint(ax[0]) + ' = ' + self.code_printer().doprint(ax[1]) + '\n'
return auto_x_str
def _output_statement(self):
output_expression = self.expr[1]
output_str = ''
# for oe, oa in zip(output_expression, self.output_args):
# output_str += self._four_spaces() + oa + ' = ' + self.code_printer().doprint(oe) + '\n'
# TODO the first index for matrix printing is not working, temporary solution is to set the index to 1 for all elements
# TODO the second index for matrix printing is not working, temporary solution is to use an index starting from 1 and increment
i = 1
for oe, oa in zip(output_expression, self.output_args):
# output_str += self._four_spaces() + oa + ' = ' + self.code_printer().doprint(oe) + '\n'
j = 1
# Check if the expression is (mathematically) a vector or a matrix
if oe.shape[1] == 1:
for oei in oe:
output_str += self._four_spaces() + f'{oa}[{j}] = ' + self.code_printer().doprint(oei) + '\n'
j += 1
else:
for oei in oe:
output_str += self._four_spaces() + f'{oa}[{i}, {j}] = ' + self.code_printer().doprint(oei) + '\n'
j += 1
return output_str
def _return_statement(self):
return_statement = ', '.join([out_args for out_args in self.output_args])
return f'{self._four_spaces()}return ({return_statement})'
if __name__ == '__main__':
# import os
# from pathlib import Path
from sympy import Matrix, pi, symbols
from sympy.physics.mechanics import dynamicsymbols, mechanics_printing, Point, ReferenceFrame, \
RigidBody, inertia, KanesMethod
# from function import PythonFunction
# from function import CppFunction
# from function import JuliaFunction
mechanics_printing(pretty_print=True)
q1, q2, q3, q4, q5, q6, q7 = dynamicsymbols("q1 q2 q3 q4 q5 q6 q7")
q1p, q2p, q3p, q4p, q5p, q6p, q7p = dynamicsymbols("q1 q2 q3 q4 q5 q6 q7", 1)
u1, u2, u3, u4, u5, u6, u7 = dynamicsymbols("u1 u2 u3 u4 u5 u6 u7")
u1p, u2p, u3p, u4p, u5p, u6p, u7p = dynamicsymbols("u1 u2 u3 u4 u5 u6 u7", 1)
# Lists of generalized coordinates and speeds
q = [q1, q2, q3, q4, q5, q6, q7]
qp = [q1p, q2p, q3p, q4p, q5p, q6p, q7p]
u = [u1, u2, u3, u4, u5, u6, u7]
dummy_dict = dict(zip(
q + qp + u,
["q1", "q2", "q3", "q4", "q5", "q6", "q7"] +
["qp1", "qp2", "qp3", "qp4", "qp5", "qp6", "qp7"] +
["u1", "u2", "u3", "u4", "u5", "u6", "u7"]
)
)
N = ReferenceFrame("N")
A = N.orientnew("A", "Body", [pi, 0, q1], "123")
B = A.orientnew("B", "Body", [pi / 2, 0, q2], "123")
C = B.orientnew("C", "Body", [-pi / 2, 0, q3], "123")
D = C.orientnew("D", "Body", [pi / 2, 0, q4], "123")
E = D.orientnew("E", "Body", [-pi / 2, 0, q5], "123")
F = E.orientnew("F", "Body", [pi / 2, 0, q6], "123")
G = F.orientnew("G", "Body", [-pi / 2, 0, q7], "123")
H = G.orientnew("H", "Body", [pi, 0, 0], "123")
P0 = Point("O")
P1 = P0.locatenew("P1", 0.15643 * N.z)
P2 = P1.locatenew("P2", 0.005375 * A.y - 0.12838 * A.z)
P3 = P2.locatenew("P3", -0.21038 * B.y - 0.006375 * B.z)
P4 = P3.locatenew("P4", 0.006375 * C.y - 0.21038 * C.z)
P5 = P4.locatenew("P5", -0.20843 * D.y - 0.006375 * D.z)
P6 = P5.locatenew("P6", 0.00017505 * E.y - 0.10593 * E.z)
P7 = P6.locatenew("P7", -0.10593 * F.y - 0.00017505 * F.z)
P8 = P7.locatenew("P8", -0.0615 * G.z)
x = P8.pos_from(P0).express(N).subs(dummy_dict).to_matrix(N)
R = N.dcm(H).subs(dummy_dict)
forward_kinematics_printer_julia = JuliaFunction('forward_kinematics', '.', {'q':'SVector{7, Float64}'}, {'x': 'SVector{3, Float64}', 'R': 'SMatrix{3, 3, Float64}'}, [x, R])
forward_kinematics_printer_julia.print()