milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 1 | import abc |
| 2 | import numpy as np |
| 3 | import sys |
| 4 | import traceback |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 5 | |
| 6 | # joint_center in x-y space. |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 7 | IN_TO_M = 0.0254 |
| 8 | joint_center = (-0.203, 0.787) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 9 | |
| 10 | # Joint distances (l1 = "proximal", l2 = "distal") |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 11 | l1 = 20.0 * IN_TO_M |
| 12 | l2 = 31.5 * IN_TO_M |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 13 | |
| 14 | max_dist = 0.01 |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 15 | max_dist_theta = np.pi / 64 |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 16 | xy_end_circle_size = 0.01 |
| 17 | theta_end_circle_size = 0.07 |
| 18 | |
| 19 | |
milind-u | 060e4cf | 2023-02-22 00:08:52 -0800 | [diff] [blame] | 20 | # Shift the angle between the convention used for input/output and the convention we use for some computations here |
| 21 | def shift_angle(theta): |
| 22 | return np.pi / 2 - theta |
| 23 | |
| 24 | |
| 25 | def shift_angles(thetas): |
| 26 | return [shift_angle(theta) for theta in thetas] |
| 27 | |
| 28 | |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 29 | # Convert from x-y coordinates to theta coordinates. |
| 30 | # orientation is a bool. This orientation is circular_index mod 2. |
| 31 | # where circular_index is the circular index, or the position in the |
| 32 | # "hyperextension" zones. "cross_point" allows shifting the place where |
| 33 | # it rounds the result so that it draws nicer (no other functional differences). |
milind-u | 600738b | 2023-02-22 14:42:19 -0800 | [diff] [blame] | 34 | def to_theta(pt, circular_index, cross_point=-np.pi, die=True): |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 35 | orient = (circular_index % 2) == 0 |
| 36 | x = pt[0] |
| 37 | y = pt[1] |
| 38 | x -= joint_center[0] |
| 39 | y -= joint_center[1] |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 40 | l3 = np.hypot(x, y) |
| 41 | t3 = np.arctan2(y, x) |
| 42 | theta1 = np.arccos((l1**2 + l3**2 - l2**2) / (2 * l1 * l3)) |
| 43 | if np.isnan(theta1): |
milind-u | 600738b | 2023-02-22 14:42:19 -0800 | [diff] [blame] | 44 | print(("Couldn't fit triangle to %f, %f, %f" % (l1, l2, l3))) |
| 45 | if die: |
| 46 | traceback.print_stack() |
| 47 | sys.exit(1) |
| 48 | return None |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 49 | |
| 50 | if orient: |
| 51 | theta1 = -theta1 |
| 52 | theta1 += t3 |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 53 | theta1 = (theta1 - cross_point) % (2 * np.pi) + cross_point |
| 54 | theta2 = np.arctan2(y - l1 * np.sin(theta1), x - l1 * np.cos(theta1)) |
| 55 | return np.array((theta1, theta2)) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 56 | |
| 57 | |
| 58 | # Simple trig to go back from theta1, theta2 to x-y |
| 59 | def to_xy(theta1, theta2): |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 60 | x = np.cos(theta1) * l1 + np.cos(theta2) * l2 + joint_center[0] |
| 61 | y = np.sin(theta1) * l1 + np.sin(theta2) * l2 + joint_center[1] |
| 62 | orient = ((theta2 - theta1) % (2.0 * np.pi)) < np.pi |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 63 | return (x, y, orient) |
| 64 | |
| 65 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 66 | END_EFFECTOR_X_LEN = (-1.0 * IN_TO_M, 10.425 * IN_TO_M) |
| 67 | END_EFFECTOR_Y_LEN = (-4.875 * IN_TO_M, 7.325 * IN_TO_M) |
| 68 | END_EFFECTOR_Z_LEN = (-11.0 * IN_TO_M, 11.0 * IN_TO_M) |
| 69 | |
| 70 | |
| 71 | def abs_sum(l): |
| 72 | result = 0 |
| 73 | for e in l: |
| 74 | result += abs(e) |
| 75 | return result |
| 76 | |
| 77 | |
| 78 | def affine_3d(R, T): |
| 79 | H = np.eye(4) |
| 80 | H[:3, 3] = T |
| 81 | H[:3, :3] = R |
| 82 | return H |
| 83 | |
| 84 | |
| 85 | # Simple trig to go back from theta1, theta2, and theta3 to |
| 86 | # the 8 corners on the roll joint x-y-z |
| 87 | def to_end_effector_points(theta1, theta2, theta3): |
| 88 | x, y, _ = to_xy(theta1, theta2) |
| 89 | # Homogeneous end effector points relative to the end_effector |
| 90 | # ee = end effector |
| 91 | endpoints_ee = [] |
| 92 | for i in range(2): |
| 93 | for j in range(2): |
| 94 | for k in range(2): |
| 95 | endpoints_ee.append( |
| 96 | np.array((END_EFFECTOR_X_LEN[i], END_EFFECTOR_Y_LEN[j], |
| 97 | END_EFFECTOR_Z_LEN[k], 1.0))) |
| 98 | |
| 99 | # Only roll. |
| 100 | # rj = roll joint |
| 101 | roll = theta3 |
| 102 | T_rj_ee = np.zeros(3) |
| 103 | R_rj_ee = np.array([[1.0, 0.0, 0.0], [0.0, |
| 104 | np.cos(roll), -np.sin(roll)], |
| 105 | [0.0, np.sin(roll), np.cos(roll)]]) |
| 106 | H_rj_ee = affine_3d(R_rj_ee, T_rj_ee) |
| 107 | |
| 108 | # Roll joint pose relative to the origin |
| 109 | # o = origin |
| 110 | T_o_rj = np.array((x, y, 0)) |
| 111 | # Only yaw |
| 112 | yaw = theta1 + theta2 |
| 113 | R_o_rj = [[np.cos(yaw), -np.sin(yaw), 0.0], |
| 114 | [np.sin(yaw), np.cos(yaw), 0.0], [0.0, 0.0, 1.0]] |
| 115 | H_o_rj = affine_3d(R_o_rj, T_o_rj) |
| 116 | |
| 117 | # Now compute the pose of the end effector relative to the origin |
| 118 | H_o_ee = H_o_rj @ H_rj_ee |
| 119 | |
| 120 | # Get the translation from these transforms |
| 121 | endpoints_o = [(H_o_ee @ endpoint_ee)[:3] for endpoint_ee in endpoints_ee] |
| 122 | |
| 123 | diagonal_distance = np.linalg.norm( |
| 124 | np.array(endpoints_o[0]) - np.array(endpoints_o[-1])) |
| 125 | actual_diagonal_distance = np.linalg.norm( |
| 126 | np.array((abs_sum(END_EFFECTOR_X_LEN), abs_sum(END_EFFECTOR_Y_LEN), |
| 127 | abs_sum(END_EFFECTOR_Z_LEN)))) |
| 128 | assert abs(diagonal_distance - actual_diagonal_distance) < 1e-5 |
| 129 | |
| 130 | return np.array(endpoints_o) |
| 131 | |
| 132 | |
| 133 | # Returns all permutations of rectangle points given two opposite corners. |
| 134 | # x is the two x values, y is the two y values, z is the two z values |
| 135 | def rect_points(x, y, z): |
| 136 | points = [] |
| 137 | for i in range(2): |
| 138 | for j in range(2): |
| 139 | for k in range(2): |
| 140 | points.append((x[i], y[j], z[k])) |
| 141 | return np.array(points) |
| 142 | |
| 143 | |
| 144 | DRIVER_CAM_Z_OFFSET = 3.225 * IN_TO_M |
| 145 | DRIVER_CAM_POINTS = rect_points( |
| 146 | (-5.126 * IN_TO_M + joint_center[0], 0.393 * IN_TO_M + joint_center[0]), |
| 147 | (5.125 * IN_TO_M + joint_center[1], 17.375 * IN_TO_M + joint_center[1]), |
| 148 | (-8.475 * IN_TO_M - DRIVER_CAM_Z_OFFSET, |
| 149 | -4.350 * IN_TO_M - DRIVER_CAM_Z_OFFSET)) |
| 150 | |
| 151 | |
| 152 | def compute_face_normals(points): |
| 153 | # Return the normal vectors of all the faces |
| 154 | normals = [] |
| 155 | for i in range(points.shape[0]): |
| 156 | v1 = points[i] |
| 157 | v2 = points[(i + 1) % points.shape[0]] |
| 158 | normal = np.cross(v1, v2) |
| 159 | normals.append(normal) |
| 160 | return np.array(normals) |
| 161 | |
| 162 | |
| 163 | def project_points_onto_axis(points, axis): |
| 164 | projections = np.dot(points, axis) |
| 165 | return np.min(projections), np.max(projections) |
| 166 | |
| 167 | |
| 168 | def roll_joint_collision(theta1, theta2, theta3): |
milind-u | 060e4cf | 2023-02-22 00:08:52 -0800 | [diff] [blame] | 169 | theta1 = shift_angle(theta1) |
| 170 | theta2 = shift_angle(theta2) |
| 171 | theta3 = shift_angle(theta3) |
| 172 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 173 | end_effector_points = to_end_effector_points(theta1, theta2, theta3) |
| 174 | |
| 175 | assert len(end_effector_points) == 8 and len(end_effector_points[0]) == 3 |
| 176 | assert len(DRIVER_CAM_POINTS) == 8 and len(DRIVER_CAM_POINTS[0]) == 3 |
| 177 | |
| 178 | # Use the Separating Axis Theorem to check for collision |
| 179 | end_effector_normals = compute_face_normals(end_effector_points) |
| 180 | driver_cam_normals = compute_face_normals(DRIVER_CAM_POINTS) |
| 181 | |
| 182 | collision = True |
| 183 | # Check for separating axes |
| 184 | for normal in np.concatenate((end_effector_normals, driver_cam_normals)): |
| 185 | min_ee, max_ee = project_points_onto_axis(end_effector_points, normal) |
| 186 | min_dc, max_dc = project_points_onto_axis(DRIVER_CAM_POINTS, normal) |
| 187 | if max_ee < min_dc or min_ee > max_dc: |
| 188 | # Separating axis found, rectangles don't intersect |
| 189 | collision = False |
| 190 | break |
| 191 | |
| 192 | return collision |
| 193 | |
| 194 | |
milind-u | eeb08c5 | 2023-02-21 22:30:16 -0800 | [diff] [blame] | 195 | # Delta limit means theta2 - theta1. |
| 196 | # The limit for the proximal and distal is relative, |
| 197 | # so define constraints for this delta. |
| 198 | UPPER_DELTA_LIMIT = 0.0 |
| 199 | LOWER_DELTA_LIMIT = -1.9 * np.pi |
| 200 | |
| 201 | # TODO(milind): put actual proximal limits |
| 202 | UPPER_PROXIMAL_LIMIT = np.pi * 1.5 |
| 203 | LOWER_PROXIMAL_LIMIT = -np.pi |
| 204 | |
Austin Schuh | 8edaf3e | 2023-02-22 21:20:52 -0800 | [diff] [blame^] | 205 | UPPER_DISTAL_LIMIT = 0.75 * np.pi |
| 206 | LOWER_DISTAL_LIMIT = -0.75 * np.pi |
| 207 | |
milind-u | eeb08c5 | 2023-02-21 22:30:16 -0800 | [diff] [blame] | 208 | UPPER_ROLL_JOINT_LIMIT = 0.75 * np.pi |
| 209 | LOWER_ROLL_JOINT_LIMIT = -0.75 * np.pi |
| 210 | |
| 211 | |
| 212 | def arm_past_limit(theta1, theta2, theta3): |
| 213 | delta = theta2 - theta1 |
Austin Schuh | 8edaf3e | 2023-02-22 21:20:52 -0800 | [diff] [blame^] | 214 | return delta > UPPER_DELTA_LIMIT or delta < LOWER_DELTA_LIMIT or \ |
| 215 | theta1 > UPPER_PROXIMAL_LIMIT or theta1 < LOWER_PROXIMAL_LIMIT or \ |
| 216 | theta2 > UPPER_DISTAL_LIMIT or theta2 < LOWER_DISTAL_LIMIT or \ |
| 217 | theta3 > UPPER_ROLL_JOINT_LIMIT or theta3 < LOWER_ROLL_JOINT_LIMIT |
milind-u | eeb08c5 | 2023-02-21 22:30:16 -0800 | [diff] [blame] | 218 | |
| 219 | |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 220 | def get_circular_index(theta): |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 221 | return int(np.floor((theta[1] - theta[0]) / np.pi)) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 222 | |
| 223 | |
| 224 | def get_xy(theta): |
milind-u | 060e4cf | 2023-02-22 00:08:52 -0800 | [diff] [blame] | 225 | theta1 = shift_angle(theta[0]) |
| 226 | theta2 = shift_angle(theta[1]) |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 227 | x = np.cos(theta1) * l1 + np.cos(theta2) * l2 + joint_center[0] |
| 228 | y = np.sin(theta1) * l1 + np.sin(theta2) * l2 + joint_center[1] |
| 229 | return np.array((x, y)) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 230 | |
| 231 | |
| 232 | # Subdivide in theta space. |
| 233 | def subdivide_theta(lines): |
| 234 | out = [] |
| 235 | last_pt = lines[0] |
| 236 | out.append(last_pt) |
| 237 | for n_pt in lines[1:]: |
| 238 | for pt in subdivide(last_pt, n_pt, max_dist_theta): |
| 239 | out.append(pt) |
| 240 | last_pt = n_pt |
| 241 | |
| 242 | return out |
| 243 | |
| 244 | |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 245 | def to_theta_with_ci(pt, circular_index): |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 246 | return (to_theta_with_circular_index(pt[0], pt[1], circular_index)) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 247 | |
| 248 | |
| 249 | # to_theta, but distinguishes between |
| 250 | def to_theta_with_circular_index(x, y, circular_index): |
| 251 | theta1, theta2 = to_theta((x, y), circular_index) |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 252 | n_circular_index = int(np.floor((theta2 - theta1) / np.pi)) |
| 253 | theta2 = theta2 + ((circular_index - n_circular_index)) * np.pi |
milind-u | 060e4cf | 2023-02-22 00:08:52 -0800 | [diff] [blame] | 254 | return np.array((shift_angle(theta1), shift_angle(theta2))) |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 255 | |
| 256 | |
| 257 | # to_theta, but distinguishes between |
| 258 | def to_theta_with_circular_index_and_roll(x, y, roll, circular_index): |
| 259 | theta12 = to_theta_with_circular_index(x, y, circular_index) |
| 260 | theta3 = roll |
| 261 | return np.array((theta12[0], theta12[1], theta3)) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 262 | |
| 263 | |
| 264 | # alpha is in [0, 1] and is the weight to merge a and b. |
| 265 | def alpha_blend(a, b, alpha): |
| 266 | """Blends a and b. |
| 267 | |
| 268 | Args: |
| 269 | alpha: double, Ratio. Needs to be in [0, 1] and is the weight to blend a |
| 270 | and b. |
| 271 | """ |
| 272 | return b * alpha + (1.0 - alpha) * a |
| 273 | |
| 274 | |
| 275 | def normalize(v): |
| 276 | """Normalize a vector while handling 0 length vectors.""" |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 277 | norm = np.linalg.norm(v) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 278 | if norm == 0: |
| 279 | return v |
| 280 | return v / norm |
| 281 | |
| 282 | |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 283 | # Generic subdivision algorithm. |
| 284 | def subdivide(p1, p2, max_dist): |
| 285 | dx = p2[0] - p1[0] |
| 286 | dy = p2[1] - p1[1] |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 287 | dist = np.sqrt(dx**2 + dy**2) |
| 288 | n = int(np.ceil(dist / max_dist)) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 289 | return [(alpha_blend(p1[0], p2[0], |
| 290 | float(i) / n), alpha_blend(p1[1], p2[1], |
| 291 | float(i) / n)) |
| 292 | for i in range(1, n + 1)] |
| 293 | |
| 294 | |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 295 | def spline_eval(start, control1, control2, end, alpha): |
| 296 | a = alpha_blend(start, control1, alpha) |
| 297 | b = alpha_blend(control1, control2, alpha) |
| 298 | c = alpha_blend(control2, end, alpha) |
| 299 | return alpha_blend(alpha_blend(a, b, alpha), alpha_blend(b, c, alpha), |
| 300 | alpha) |
| 301 | |
| 302 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 303 | SPLINE_SUBDIVISIONS = 100 |
| 304 | |
| 305 | |
| 306 | def subdivide_multistep(): |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 307 | # TODO: pick N based on spline parameters? or otherwise change it to be more evenly spaced? |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 308 | for i in range(0, SPLINE_SUBDIVISIONS + 1): |
| 309 | yield i / float(SPLINE_SUBDIVISIONS) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 310 | |
| 311 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 312 | def get_proximal_distal_derivs(t_prev, t, t_next): |
| 313 | d_prev = normalize(t - t_prev) |
| 314 | d_next = normalize(t_next - t) |
| 315 | accel = (d_next - d_prev) / np.linalg.norm(t - t_next) |
| 316 | return (ThetaPoint(t[0], d_next[0], |
| 317 | accel[0]), ThetaPoint(t[1], d_next[1], accel[1])) |
| 318 | |
| 319 | |
| 320 | def get_roll_joint_theta(theta_i, theta_f, t): |
| 321 | # Fit a theta(t) = (1 - cos(pi*t)) / 2, |
| 322 | # so that theta(0) = theta_i, and theta(1) = theta_f |
| 323 | offset = theta_i |
| 324 | scalar = (theta_f - theta_i) / 2.0 |
| 325 | freq = np.pi |
| 326 | theta_curve = lambda t: scalar * (1 - np.cos(freq * t)) + offset |
| 327 | |
| 328 | return theta_curve(t) |
| 329 | |
| 330 | |
| 331 | def get_roll_joint_theta_multistep(alpha_rolls, alpha): |
| 332 | # Figure out which segment in the motion we're in |
| 333 | theta_i = None |
| 334 | theta_f = None |
| 335 | t = None |
| 336 | |
| 337 | for i in range(len(alpha_rolls) - 1): |
| 338 | # Find the alpha segment we're in |
| 339 | if alpha_rolls[i][0] <= alpha <= alpha_rolls[i + 1][0]: |
| 340 | theta_i = alpha_rolls[i][1] |
| 341 | theta_f = alpha_rolls[i + 1][1] |
| 342 | |
| 343 | total_dalpha = alpha_rolls[-1][0] - alpha_rolls[0][0] |
| 344 | assert total_dalpha == 1.0 |
| 345 | dalpha = alpha_rolls[i + 1][0] - alpha_rolls[i][0] |
| 346 | t = (alpha - alpha_rolls[i][0]) * (total_dalpha / dalpha) |
| 347 | break |
| 348 | assert theta_i is not None |
| 349 | assert theta_f is not None |
| 350 | assert t is not None |
| 351 | |
| 352 | return get_roll_joint_theta(theta_i, theta_f, t) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 353 | |
| 354 | |
Maxwell Henderson | 83cf6d6 | 2023-02-10 20:29:26 -0800 | [diff] [blame] | 355 | # Draw a list of lines to a cairo context. |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 356 | def draw_lines(cr, lines): |
| 357 | cr.move_to(lines[0][0], lines[0][1]) |
| 358 | for pt in lines[1:]: |
| 359 | cr.line_to(pt[0], pt[1]) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 360 | |
| 361 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 362 | class Path(abc.ABC): |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 363 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 364 | def __init__(self, name): |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 365 | self.name = name |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 366 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 367 | @abc.abstractmethod |
| 368 | def DoToThetaPoints(self): |
| 369 | pass |
| 370 | |
| 371 | @abc.abstractmethod |
| 372 | def DoDrawTo(self): |
| 373 | pass |
| 374 | |
| 375 | @abc.abstractmethod |
| 376 | def roll_joint_thetas(self): |
| 377 | pass |
| 378 | |
| 379 | @abc.abstractmethod |
| 380 | def intersection(self, event): |
| 381 | pass |
| 382 | |
| 383 | def roll_joint_collision(self, points, verbose=False): |
| 384 | for point in points: |
| 385 | if roll_joint_collision(*point): |
| 386 | if verbose: |
| 387 | print("Roll joint collision for path %s in point %s" % |
| 388 | (self.name, point)) |
| 389 | return True |
| 390 | return False |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 391 | |
milind-u | eeb08c5 | 2023-02-21 22:30:16 -0800 | [diff] [blame] | 392 | def arm_past_limit(self, points, verbose=True): |
| 393 | for point in points: |
| 394 | if arm_past_limit(*point): |
| 395 | if verbose: |
| 396 | print("Arm past limit for path %s in point %s" % |
| 397 | (self.name, point)) |
| 398 | return True |
| 399 | return False |
| 400 | |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 401 | def DrawTo(self, cr, theta_version): |
milind-u | eeb08c5 | 2023-02-21 22:30:16 -0800 | [diff] [blame] | 402 | points = self.DoToThetaPoints() |
| 403 | if self.roll_joint_collision(points): |
| 404 | # Draw the spline red |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 405 | cr.set_source_rgb(1.0, 0.0, 0.0) |
milind-u | eeb08c5 | 2023-02-21 22:30:16 -0800 | [diff] [blame] | 406 | elif self.arm_past_limit(points): |
| 407 | # Draw the spline orange |
| 408 | cr.set_source_rgb(1.0, 0.5, 0.0) |
| 409 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 410 | self.DoDrawTo(cr, theta_version) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 411 | |
| 412 | def ToThetaPoints(self): |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 413 | points = self.DoToThetaPoints() |
milind-u | eeb08c5 | 2023-02-21 22:30:16 -0800 | [diff] [blame] | 414 | if self.roll_joint_collision(points, verbose=True) or \ |
| 415 | self.arm_past_limit(points, verbose=True): |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 416 | sys.exit(1) |
| 417 | return points |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 418 | |
| 419 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 420 | class SplineSegmentBase(Path): |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 421 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 422 | def __init__(self, name): |
| 423 | super().__init__(name) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 424 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 425 | @abc.abstractmethod |
| 426 | # Returns (start, control1, control2, end), each in the form |
| 427 | # (theta1, theta2, theta3) |
| 428 | def get_controls_theta(self): |
| 429 | pass |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 430 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 431 | def intersection(self, event): |
| 432 | start, control1, control2, end = self.get_controls_theta() |
| 433 | for alpha in subdivide_multistep(): |
| 434 | x, y = get_xy(spline_eval(start, control1, control2, end, alpha)) |
| 435 | spline_point = np.array([x, y]) |
| 436 | hovered_point = np.array([event.x, event.y]) |
| 437 | if np.linalg.norm(hovered_point - spline_point) < 0.03: |
| 438 | return alpha |
| 439 | return None |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 440 | |
| 441 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 442 | class ThetaSplineSegment(SplineSegmentBase): |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 443 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 444 | # start and end are [theta1, theta2, theta3]. |
| 445 | # controls are just [theta1, theta2]. |
| 446 | # control_alpha_rolls are a list of [alpha, roll] |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 447 | def __init__(self, |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 448 | name, |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 449 | start, |
| 450 | control1, |
| 451 | control2, |
| 452 | end, |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 453 | control_alpha_rolls=[], |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 454 | alpha_unitizer=None, |
| 455 | vmax=None): |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 456 | super().__init__(name) |
| 457 | self.start = start[:2] |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 458 | self.control1 = control1 |
| 459 | self.control2 = control2 |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 460 | self.end = end[:2] |
| 461 | # There will always be roll at alpha = 0 and 1 |
| 462 | self.alpha_rolls = [[0.0, start[2]] |
| 463 | ] + control_alpha_rolls + [[1.0, end[2]]] |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 464 | self.alpha_unitizer = alpha_unitizer |
| 465 | self.vmax = vmax |
| 466 | |
| 467 | def __repr__(self): |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 468 | return "ThetaSplineSegment(%s, %s, %s, %s)" % (repr( |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 469 | self.start), repr(self.control1), repr( |
| 470 | self.control2), repr(self.end)) |
| 471 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 472 | def DoDrawTo(self, cr, theta_version): |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 473 | if (theta_version): |
| 474 | draw_lines(cr, [ |
milind-u | 060e4cf | 2023-02-22 00:08:52 -0800 | [diff] [blame] | 475 | shift_angles( |
| 476 | spline_eval(self.start, self.control1, self.control2, |
| 477 | self.end, alpha)) |
| 478 | for alpha in subdivide_multistep() |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 479 | ]) |
| 480 | else: |
| 481 | start = get_xy(self.start) |
| 482 | end = get_xy(self.end) |
| 483 | |
| 484 | draw_lines(cr, [ |
| 485 | get_xy( |
| 486 | spline_eval(self.start, self.control1, self.control2, |
| 487 | self.end, alpha)) |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 488 | for alpha in subdivide_multistep() |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 489 | ]) |
| 490 | |
| 491 | cr.move_to(start[0] + xy_end_circle_size, start[1]) |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 492 | cr.arc(start[0], start[1], xy_end_circle_size, 0, 2.0 * np.pi) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 493 | cr.move_to(end[0] + xy_end_circle_size, end[1]) |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 494 | cr.arc(end[0], end[1], xy_end_circle_size, 0, 2.0 * np.pi) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 495 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 496 | def DoToThetaPoints(self): |
| 497 | points = [] |
| 498 | for alpha in subdivide_multistep(): |
| 499 | proximal, distal = spline_eval(self.start, self.control1, |
| 500 | self.control2, self.end, alpha) |
| 501 | roll_joint = get_roll_joint_theta_multistep( |
| 502 | self.alpha_rolls, alpha) |
| 503 | points.append((proximal, distal, roll_joint)) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 504 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 505 | return points |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 506 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 507 | def get_controls_theta(self): |
| 508 | return (self.start, self.control1, self.control2, self.end) |
Maxwell Henderson | f5123fe | 2023-02-04 13:44:41 -0800 | [diff] [blame] | 509 | |
milind-u | 18a901d | 2023-02-17 21:51:55 -0800 | [diff] [blame] | 510 | def roll_joint_thetas(self): |
| 511 | ts = [] |
| 512 | thetas = [] |
| 513 | for alpha in subdivide_multistep(): |
| 514 | roll_joint = get_roll_joint_theta_multistep( |
| 515 | self.alpha_rolls, alpha) |
| 516 | thetas.append(roll_joint) |
| 517 | ts.append(alpha) |
| 518 | return ts, thetas |