forked from robot-acceleration/GRiD
-
Notifications
You must be signed in to change notification settings - Fork 6
/
testGRiD.py
225 lines (191 loc) · 8.04 KB
/
testGRiD.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
#!/usr/bin/python3
from URDFParser import URDFParser
from GRiDCodeGenerator import GRiDCodeGenerator
from RBDReference import RBDReference
from util import parseInputs, printUsage, validateRobot, initializeValues
import subprocess
import numpy as np
import re
DIFF = 1e-2 # maximum allowable difference per value in outputs
def testGRiD(URDF_PATH, FLOATING_BASE):
"""
Tests all implemented algorithms in GRiD, and
compares them with RBDReference. Returns a
boolean that signifies whether all tests
have been passed.
"""
parser = URDFParser()
robot = parser.parse(URDF_PATH, floating_base=FLOATING_BASE)
q,qd,u,num_pos = initializeValues(robot, MATCH_CPP_RANDOM = True)
nv = num_pos - 1 * FLOATING_BASE
validateRobot(robot, NO_ARG_OPTION = True)
codegen = GRiDCodeGenerator(robot, False, True, FILE_NAMESPACE = 'grid')
print("-----------------")
print("Generating GRiD.cuh")
print("-----------------")
if FLOATING_BASE: include_homogenous_transforms = False
else: include_homogenous_transforms = True
codegen.gen_all_code(include_homogenous_transforms = include_homogenous_transforms)
print("New code generated and saved to grid.cuh!")
if FLOATING_BASE: filename = 'TestGRiD/testGRiDFB.cu'
else: filename = 'TestGRiD/testGRiD.cu'
print("-----------------")
print("Compiling testGRiD")
print("-----------------")
result = subprocess.run( \
["nvcc", "-o", "testGRiD.exe", filename], \
capture_output=True, text=True \
)
if result.stderr:
print("Compilation errors follow:")
print(result.stderr)
exit()
print("-----------------")
print("Running testGRiD")
print("-----------------")
result = subprocess.run(["./testGRiD.exe"], capture_output=True, text=True)
if result.stderr:
print("Runtime errors follow:")
print(result.stderr)
exit()
# collect output
grid_output = result.stdout.split('\n') # split output into separate strings
grid_output = [data.strip() for data in grid_output if data]
q = grid_output[0].strip().split(' ')
q = np.array([float(i) for i in q])
qd = np.array(grid_output[1].strip().split(' '))
qd = np.array([float(i) for i in qd])
u = np.array(grid_output[2].strip().split(' '))
u = np.array([float(i) for i in u])
grid_output_gradients = grid_output[len(grid_output)-5:] # rnea, & gradients
grid_output = grid_output[3:]
# assign functions
r = RBDReference(robot)
passed_all = True
# inverse dynamics gradient
(dcdq_ref, dcdqd_ref) = r.rnea_grad(q,qd,np.zeros(nv))
dcdq_grid = grid_output[:nv]
dcdqd_grid = grid_output[nv:2*nv]
grid_output = grid_output[2*nv:]
passed, differences, equal, sum_diff = compare_matrix(dcdq_ref, dcdq_grid)
print(f'dc_dq: {equal}\n')
if 'Failed' in equal: passed_all = False
passed, differences, equal, sum_diff = compare_matrix(dcdqd_ref, dcdqd_grid)
print(f'dc_dqd: {equal}\n')
if 'Failed' in equal: passed_all = False
# minv
minv_ref = np.array(r.minv(q))
minv_grid = grid_output[:nv]
grid_output = grid_output[nv:]
passed, differences, equal, sum_diff = compare_matrix(minv_ref, minv_grid, True)
print(f'minv: {equal}\n')
if 'Failed' in equal: passed_all = False
# forward dynamics
fd_ref = np.array(r.forward_dynamics(q,qd,u))
qdd = fd_ref
fd_grid = grid_output[0].strip().split(' ')
grid_output = grid_output[1:]
passed, differences, equal, sum_diff = compare_array(fd_ref,fd_grid)
print(f'forward dynamics: {equal}\n')
if 'Failed' in equal: passed_all = False
# inverse dynamics
rnea_ref = r.rnea(q,qd,u)[0]
rnea_grid = grid_output[0].strip().split(' ')
grid_output = grid_output[1:]
passed, differences, equal, sum_diff = compare_array(rnea_ref,rnea_grid)
print(f'inverse dynamics: {equal}\n')
if 'Failed' in equal: passed_all = False
# forward dynamics gradient
(dfdq_ref, dfdqd_ref) = r.forward_dynamics_grad(q,qd,fd_ref)
dfdq_grid = grid_output[:nv]
dfdqd_grid = grid_output[nv:2*nv]
grid_output = grid_output[2*nv:]
passed, differences, equal, sum_diff = compare_matrix(dfdq_ref, dfdq_grid)
print(f'df_dq: {equal}\n')
if 'Failed' in equal: passed_all = False
passed, differences, equal, sum_diff = compare_matrix(dfdqd_ref, dfdqd_grid)
print(f'df_dqd: {equal}\n')
if 'Failed' in equal: passed_all = False
if not FLOATING_BASE:
# eepos
eepos_ref = r.end_effector_positions(q)
for i in range(len(eepos_ref)): eepos_ref[i] = eepos_ref[i].flatten()
eepos_ref = np.concatenate(eepos_ref).flatten().tolist()[0]
eepos_grid = grid_output[0].strip().split(' ')
eepos_grid = np.array([float(i) for i in eepos_grid])
passed, differences, equal, sum_diff = compare_array(eepos_ref,eepos_grid)
print(f'eepos: {equal}\n')
if 'Failed' in equal: passed_all = False
# eepos grad
eeposgrad_ref = np.array(r.end_effector_position_gradients(q))[0]
eeposgrad_grid = grid_output[1:7]
grid_output = grid_output[7:]
passed, differences, equal, sum_diff = compare_matrix(eeposgrad_ref, eeposgrad_grid)
print(f'eeposgrad: {equal}\n')
if 'Failed' in equal: passed_all = False
# aba
aba_grid = grid_output[0].strip().split(' ')
grid_output = grid_output[1:]
passed, differences, equal, sum_diff = compare_array(fd_ref,aba_grid)
print(f'aba: {equal}\n')
if 'Failed' in equal: passed_all = False
# crba
crba_ref = np.array(r.crba(q,np.zeros(len(qd))))
crba_grid = grid_output[:nv]
grid_output = grid_output[nv:]
passed, differences, equal, sum_diff = compare_matrix(crba_ref,crba_grid)
print(f'crba: {equal}\n')
if 'Failed' in equal: passed_all = False
return passed_all
def compare_matrix(ref, grid, minv=False):
passed = []
differences = []
equal = True
for (i, row) in enumerate(ref):
passed.append([])
differences.append([])
grid_row = grid[i].strip().split(' ')
for (j, data) in enumerate(row):
if j < i and minv: # cuda lower triangle not filled in
if float(grid_row[j]) == 0:
passed[i].append(True)
differences[i].append(0)
elif abs(data - float(grid_row[j])) < DIFF:
passed[i].append(True)
differences[i].append(abs(data - float(grid_row[j])))
else:
passed[i].append(False)
equal = False
differences[i].append(abs(data - float(grid_row[j])))
else:
if abs(data - float(grid_row[j])) < DIFF: passed[i].append(True)
else:
passed[i].append(False)
equal = False
differences[i].append(abs(data - float(grid_row[j])))
sum_diff = 0
for i in differences: sum_diff += sum(i)
if sum_diff < DIFF * len(differences[0]) * len(differences): equal = True
else: equal = False
if equal: equal = "\033[92mPassed\033[0m" # Green
else:
equal = "\033[91mFailed\033[0m" # Red
return np.array(passed), np.array(differences), equal, sum_diff
def compare_array(ref, grid):
passed = []
differences = []
for (idx, val) in enumerate(grid):
if abs(float(val) - ref[idx]) < DIFF: passed.append(True)
else: passed.append(False)
differences.append(abs(float(val) - ref[idx]))
if sum(differences) < len(differences) * DIFF: equal = "\033[92mPassed\033[0m" # Green
else: equal = "\033[91mFailed\033[0m" # Red
return passed, differences, equal, sum(differences)
if __name__ == "__main__":
inputs = parseInputs(NO_ARG_OPTION = True)
if not inputs is None:
URDF_PATH, DEBUG_MODE, FILE_NAMESPACE_NAME, FLOATING_BASE = parseInputs()
else:
print(f'Usage: printGRiD.py URDF_PATH FILE_NAMESPACE_NAME (-f) (-d)')
exit()
testGRiD(URDF_PATH, FLOATING_BASE)