Skip to content

Commit a457c27

Browse files
fix: rounding error in test cases
1 parent 6e97a75 commit a457c27

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

arm_control/src/arm_unit_tests.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,15 @@ def test_pathfind(num_samples = 1000, max_velocities=[0.1, 0.1, 0.1, 0.1, 0.1],
115115
print("------------------------------------------------------------------")
116116
print("-------------------------test_pathfind----------------------------")
117117
print("------------------------------------------------------------------")
118+
119+
joints = ["Waist", "Shoulder", "Elbow", "Wrist", "Hand"]
118120
num_failed = 0
119121
for i in range(num_samples):
120122
failed = False
121123

122124
start_joints = [np.random.random() * (jointUpperLimits[i]-jointLowerLimits[i]) + jointLowerLimits[i] for i in range(5)]
123125
end_joints = [np.random.random() * (jointUpperLimits[i]-jointLowerLimits[i]) + jointLowerLimits[i] for i in range(5)]
124-
differences = [start_joints[i] - end_joints[i] for i in range(5)]
126+
differences = [end_joints[i] - start_joints[i] for i in range(5)]
125127
min_time = max([abs((differences[i])/max_velocities[i]) for i in range(5)])
126128
# TODO: figure out how to calculate max time???
127129
max_distance = max(abs(differences[i]) for i in range(5)) # Using furthest distance to get a reasonable max time
@@ -132,66 +134,66 @@ def test_pathfind(num_samples = 1000, max_velocities=[0.1, 0.1, 0.1, 0.1, 0.1],
132134

133135
polynomials = arm_pathfinding.pathfiningPolynomial(start_joints, end_joints, time)
134136
for k, polynomial in enumerate(polynomials):
135-
if sum([polynomial[j] * math.pow(time, 6 - j) for j in range(4)]) != differences[k]: # Checking final position
137+
if abs(sum([polynomial[j] * math.pow(time, 6 - j) for j in range(4)]) - differences[k]) > 0.001: # Checking final position
136138
failed = True
137139
if verbose:
138140
given = sum([polynomial[j] * math.pow(time, 6 - j) for j in range(4)])
139-
print("Polynoial failed final position.")
140-
print(f"Result: {given} Difference: {differences[k] - given}")
141+
print(f"{joints[k]} polynomial failed final position.")
142+
print(f"Result: {given} Expected: {differences[k]} Difference: {differences[k] - given}")
141143
break
142144

143-
if sum([polynomial[j] * (6 - j) * math.pow(time/2, 5 - j) for j in range(4)]) != max_velocities[k]: # Checking max velocity
145+
if abs(sum([polynomial[j] * (6 - j) * math.pow(time/2, 5 - j) for j in range(4)]) - max_velocities[k]) > 0.001: # Checking max velocity
144146
failed = True
145147
if verbose:
146148
given = sum([polynomial[j] * (6 - j) * math.pow(time/2, 5 - j) for j in range(4)])
147149
print("Polynoial failed midway velocity.")
148150
print(f"Result: {given} Difference: {max_velocities[k] - given}")
149151
break
150152

151-
if sum([polynomial[j] * (6 - j) * math.pow(time, 5 - j) for j in range(4)]) != 0: # Checking final velocity
153+
if abs(sum([polynomial[j] * (6 - j) * math.pow(time, 5 - j) for j in range(4)])) > 0.001: # Checking final velocity
152154
failed = True
153155
if verbose:
154156
given = sum([polynomial[j] * (6 - j) * math.pow(time, 5 - j) for j in range(4)])
155157
print("Polynoial failed final velocity.")
156158
print(f"Result: {given}")
157159
break
158160

159-
if sum([polynomial[j] * (6 - j) * (5 - j) * math.pow(time/2, 4 - j) for j in range(4)]) != 0: # Check halfway acceleration
161+
if abs(sum([polynomial[j] * (6 - j) * (5 - j) * math.pow(time/2, 4 - j) for j in range(4)])) > 0.001: # Check halfway acceleration
160162
failed = True
161163
if verbose:
162164
given = sum([polynomial[j] * (6 - j) * (5 - j) * math.pow(time/2, 4 - j) for j in range(4)])
163165
print("Polynoial failed midway acceleration.")
164166
print(f"Result: {given}")
165167
break
166168

167-
if not failed and end_joints != arm_pathfinding.nextJointPosition(start_joints, time, polynomials):
168-
failed = True
169-
if verbose:
170-
given = arm_pathfinding.nextJointPosition(start_joints, time, polynomials)
171-
print("nextJointPosition failed final position.")
172-
print(f"Result: {given} Difference: {[end_joints[j] - given[j] for j in range(5)]}")
173-
174-
if not failed and end_joints != arm_pathfinding.pathfind(start_joints, end_joints, time):
175-
failed = True
176-
if verbose:
177-
given = arm_pathfinding.pathfind(start_joints, end_joints, time)
178-
print("Pathfind failed final position.")
179-
print(f"Result: {given} Difference: {[end_joints[j] - given[j] for j in range(5)]}")
180-
181-
if not failed and start_joints != arm_pathfinding.nextJointPosition(start_joints, 0, polynomials):
169+
if not failed and abs(sum(start_joints[j] - arm_pathfinding.nextJointPosition(start_joints, 0, polynomials)[j] for j in range(5))) > 0.001:
182170
failed = True
183171
if verbose:
184172
given = arm_pathfinding.nextJointPosition(start_joints, 0, polynomials)
185173
print("nextJointPosition failed initial position.")
186174
print(f"Result: {given} Difference: {[start_joints[j] - given[j] for j in range(5)]}")
187175

188-
if not failed and start_joints != arm_pathfinding.pathfind(start_joints, end_joints, 0):
176+
if not failed and abs(sum(start_joints[j] - arm_pathfinding.pathfind(start_joints, end_joints, time)[j] for j in range(5))) > 0.001:
189177
failed = True
190178
if verbose:
191179
given = arm_pathfinding.pathfind(start_joints, end_joints, 0)
192180
print("Pathfnd failed initial position.")
193181
print(f"Result: {given} Difference: {[start_joints[j] - given[j] for j in range(5)]}")
194182

183+
if not failed and abs(sum(end_joints[j] - arm_pathfinding.nextJointPosition(start_joints, time, polynomials)[j] for j in range(5))) > 0.001:
184+
failed = True
185+
if verbose:
186+
given = arm_pathfinding.nextJointPosition(start_joints, time, polynomials)
187+
print("nextJointPosition failed final position.")
188+
print(f"Result: {given} Difference: {[end_joints[j] - given[j] for j in range(5)]}")
189+
190+
if not failed and abs(sum(end_joints[j] - arm_pathfinding.pathfind(start_joints, end_joints, 0)[j] for j in range(5))) > 0.001:
191+
failed = True
192+
if verbose:
193+
given = arm_pathfinding.pathfind(start_joints, end_joints, time)
194+
print("Pathfind failed final position.")
195+
print(f"Result: {given} Difference: {[end_joints[j] - given[j] for j in range(5)]}")
196+
195197
if failed:
196198
num_failed += 1
197199
print("------------------------------------------------------------------")

0 commit comments

Comments
 (0)