Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 1 | import numpy |
| 2 | |
| 3 | # joint_center in x-y space. |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 4 | joint_center = (-0.299, 0.299) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 5 | |
| 6 | # Joint distances (l1 = "proximal", l2 = "distal") |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 7 | l1 = 46.25 * 0.0254 |
| 8 | l2 = 43.75 * 0.0254 |
| 9 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 10 | |
| 11 | # Convert from x-y coordinates to theta coordinates. |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 12 | # orientation is a bool. This orientation is circular_index mod 2. |
| 13 | # where circular_index is the circular index, or the position in the |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 14 | # "hyperextension" zones. "cross_point" allows shifting the place where |
| 15 | # it rounds the result so that it draws nicer (no other functional differences). |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 16 | def to_theta(pt, circular_index, cross_point=-numpy.pi): |
| 17 | orient = (circular_index % 2) == 0 |
| 18 | x = pt[0] |
| 19 | y = pt[1] |
| 20 | x -= joint_center[0] |
| 21 | y -= joint_center[1] |
| 22 | l3 = numpy.hypot(x, y) |
| 23 | t3 = numpy.arctan2(y, x) |
| 24 | theta1 = numpy.arccos((l1**2 + l3**2 - l2**2) / (2 * l1 * l3)) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 25 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 26 | if orient: |
| 27 | theta1 = -theta1 |
| 28 | theta1 += t3 |
| 29 | theta1 = (theta1 - cross_point) % (2 * numpy.pi) + cross_point |
| 30 | theta2 = numpy.arctan2(y - l1 * numpy.sin(theta1), |
| 31 | x - l1 * numpy.cos(theta1)) |
| 32 | return numpy.array((theta1, theta2)) |
| 33 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 34 | |
| 35 | # Simple trig to go back from theta1, theta2 to x-y |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 36 | def to_xy(theta1, theta2): |
| 37 | x = numpy.cos(theta1) * l1 + numpy.cos(theta2) * l2 + joint_center[0] |
| 38 | y = numpy.sin(theta1) * l1 + numpy.sin(theta2) * l2 + joint_center[1] |
| 39 | orient = ((theta2 - theta1) % (2.0 * numpy.pi)) < numpy.pi |
| 40 | return (x, y, orient) |
| 41 | |
| 42 | |
| 43 | def get_circular_index(theta): |
| 44 | return int(numpy.floor((theta[1] - theta[0]) / numpy.pi)) |
| 45 | |
| 46 | |
| 47 | def get_xy(theta): |
| 48 | theta1 = theta[0] |
| 49 | theta2 = theta[1] |
| 50 | x = numpy.cos(theta1) * l1 + numpy.cos(theta2) * l2 + joint_center[0] |
| 51 | y = numpy.sin(theta1) * l1 + numpy.sin(theta2) * l2 + joint_center[1] |
| 52 | return numpy.array((x, y)) |
| 53 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 54 | |
| 55 | # Draw a list of lines to a cairo context. |
| 56 | def draw_lines(cr, lines): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 57 | cr.move_to(lines[0][0], lines[0][1]) |
| 58 | for pt in lines[1:]: |
| 59 | cr.line_to(pt[0], pt[1]) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 60 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 61 | |
| 62 | max_dist = 0.01 |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 63 | max_dist_theta = numpy.pi / 64 |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 64 | xy_end_circle_size = 0.01 |
| 65 | theta_end_circle_size = 0.07 |
| 66 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 67 | |
| 68 | # Subdivide in theta space. |
| 69 | def subdivide_theta(lines): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 70 | out = [] |
| 71 | last_pt = lines[0] |
| 72 | out.append(last_pt) |
| 73 | for n_pt in lines[1:]: |
| 74 | for pt in subdivide(last_pt, n_pt, max_dist_theta): |
| 75 | out.append(pt) |
| 76 | last_pt = n_pt |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 77 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 78 | return out |
| 79 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 80 | |
| 81 | # subdivide in xy space. |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 82 | def subdivide_xy(lines, max_dist=max_dist): |
| 83 | out = [] |
| 84 | last_pt = lines[0] |
| 85 | out.append(last_pt) |
| 86 | for n_pt in lines[1:]: |
| 87 | for pt in subdivide(last_pt, n_pt, max_dist): |
| 88 | out.append(pt) |
| 89 | last_pt = n_pt |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 90 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 91 | return out |
| 92 | |
| 93 | |
| 94 | def to_theta_with_ci(pt, circular_index): |
| 95 | return to_theta_with_circular_index(pt[0], pt[1], circular_index) |
| 96 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 97 | |
| 98 | # to_theta, but distinguishes between |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 99 | def to_theta_with_circular_index(x, y, circular_index): |
| 100 | theta1, theta2 = to_theta((x, y), circular_index) |
| 101 | n_circular_index = int(numpy.floor((theta2 - theta1) / numpy.pi)) |
| 102 | theta2 = theta2 + ((circular_index - n_circular_index)) * numpy.pi |
| 103 | return numpy.array((theta1, theta2)) |
| 104 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 105 | |
| 106 | # alpha is in [0, 1] and is the weight to merge a and b. |
| 107 | def alpha_blend(a, b, alpha): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 108 | """Blends a and b. |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 109 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 110 | Args: |
| 111 | alpha: double, Ratio. Needs to be in [0, 1] and is the weight to blend a |
| 112 | and b. |
| 113 | """ |
| 114 | return b * alpha + (1.0 - alpha) * a |
| 115 | |
| 116 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 117 | def normalize(v): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 118 | """Normalize a vector while handling 0 length vectors.""" |
| 119 | norm = numpy.linalg.norm(v) |
| 120 | if norm == 0: |
| 121 | return v |
| 122 | return v / norm |
| 123 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 124 | |
| 125 | # CI is circular index and allows selecting between all the stats that map |
| 126 | # to the same x-y state (by giving them an integer index). |
| 127 | # This will compute approximate first and second derivatives with respect |
| 128 | # to path length. |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 129 | def to_theta_with_circular_index_and_derivs(x, y, dx, dy, |
| 130 | circular_index_select): |
| 131 | a = to_theta_with_circular_index(x, y, circular_index_select) |
| 132 | b = to_theta_with_circular_index(x + dx * 0.0001, y + dy * 0.0001, |
| 133 | circular_index_select) |
| 134 | c = to_theta_with_circular_index(x - dx * 0.0001, y - dy * 0.0001, |
| 135 | circular_index_select) |
| 136 | d1 = normalize(b - a) |
| 137 | d2 = normalize(c - a) |
| 138 | accel = (d1 + d2) / numpy.linalg.norm(a - b) |
| 139 | return (a[0], a[1], d1[0], d1[1], accel[0], accel[1]) |
| 140 | |
| 141 | |
| 142 | def to_theta_with_ci_and_derivs(p_prev, p, p_next, c_i_select): |
| 143 | a = to_theta(p, c_i_select) |
| 144 | b = to_theta(p_next, c_i_select) |
| 145 | c = to_theta(p_prev, c_i_select) |
| 146 | d1 = normalize(b - a) |
| 147 | d2 = normalize(c - a) |
| 148 | accel = (d1 + d2) / numpy.linalg.norm(a - b) |
| 149 | return (a[0], a[1], d1[0], d1[1], accel[0], accel[1]) |
| 150 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 151 | |
| 152 | # Generic subdivision algorithm. |
| 153 | def subdivide(p1, p2, max_dist): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 154 | dx = p2[0] - p1[0] |
| 155 | dy = p2[1] - p1[1] |
| 156 | dist = numpy.sqrt(dx**2 + dy**2) |
| 157 | n = int(numpy.ceil(dist / max_dist)) |
| 158 | return [(alpha_blend(p1[0], p2[0], |
| 159 | float(i) / n), alpha_blend(p1[1], p2[1], |
| 160 | float(i) / n)) |
| 161 | for i in range(1, n + 1)] |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 162 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 163 | |
| 164 | # convert from an xy space loop into a theta loop. |
| 165 | # All segements are expected go from one "hyper-extension" boundary |
| 166 | # to another, thus we must go backwards over the "loop" to get a loop in |
| 167 | # x-y space. |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 168 | def to_theta_loop(lines, cross_point=-numpy.pi): |
| 169 | out = [] |
| 170 | last_pt = lines[0] |
| 171 | for n_pt in lines[1:]: |
| 172 | for pt in subdivide(last_pt, n_pt, max_dist): |
| 173 | out.append(to_theta(pt, 0, cross_point)) |
| 174 | last_pt = n_pt |
| 175 | for n_pt in reversed(lines[:-1]): |
| 176 | for pt in subdivide(last_pt, n_pt, max_dist): |
| 177 | out.append(to_theta(pt, 1, cross_point)) |
| 178 | last_pt = n_pt |
| 179 | return out |
| 180 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 181 | |
| 182 | # Convert a loop (list of line segments) into |
| 183 | # The name incorrectly suggests that it is cyclic. |
| 184 | def back_to_xy_loop(lines): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 185 | out = [] |
| 186 | last_pt = lines[0] |
| 187 | out.append(to_xy(last_pt[0], last_pt[1])) |
| 188 | for n_pt in lines[1:]: |
| 189 | for pt in subdivide(last_pt, n_pt, max_dist_theta): |
| 190 | out.append(to_xy(pt[0], pt[1])) |
| 191 | last_pt = n_pt |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 192 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 193 | return out |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 194 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 195 | |
| 196 | # Segment in angle space. |
| 197 | class AngleSegment: |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 198 | def __init__(self, start, end, name=None, alpha_unitizer=None, vmax=None): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 199 | """Creates an angle segment. |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 200 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 201 | Args: |
| 202 | start: (double, double), The start of the segment in theta1, theta2 |
| 203 | coordinates in radians |
| 204 | end: (double, double), The end of the segment in theta1, theta2 |
| 205 | coordinates in radians |
| 206 | """ |
| 207 | self.start = start |
| 208 | self.end = end |
| 209 | self.name = name |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 210 | self.alpha_unitizer = alpha_unitizer |
| 211 | self.vmax = vmax |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 212 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 213 | def __repr__(self): |
| 214 | return "AngleSegment(%s, %s)" % (repr(self.start), repr(self.end)) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 215 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 216 | def DrawTo(self, cr, theta_version): |
| 217 | if theta_version: |
| 218 | cr.move_to(self.start[0], self.start[1] + theta_end_circle_size) |
| 219 | cr.arc(self.start[0], self.start[1], theta_end_circle_size, 0, |
| 220 | 2.0 * numpy.pi) |
| 221 | cr.move_to(self.end[0], self.end[1] + theta_end_circle_size) |
| 222 | cr.arc(self.end[0], self.end[1], theta_end_circle_size, 0, |
| 223 | 2.0 * numpy.pi) |
| 224 | cr.move_to(self.start[0], self.start[1]) |
| 225 | cr.line_to(self.end[0], self.end[1]) |
| 226 | else: |
| 227 | start_xy = to_xy(self.start[0], self.start[1]) |
| 228 | end_xy = to_xy(self.end[0], self.end[1]) |
| 229 | draw_lines(cr, back_to_xy_loop([self.start, self.end])) |
| 230 | cr.move_to(start_xy[0] + xy_end_circle_size, start_xy[1]) |
| 231 | cr.arc(start_xy[0], start_xy[1], xy_end_circle_size, 0, |
| 232 | 2.0 * numpy.pi) |
| 233 | cr.move_to(end_xy[0] + xy_end_circle_size, end_xy[1]) |
| 234 | cr.arc(end_xy[0], end_xy[1], xy_end_circle_size, 0, 2.0 * numpy.pi) |
| 235 | |
| 236 | def ToThetaPoints(self): |
| 237 | dx = self.end[0] - self.start[0] |
| 238 | dy = self.end[1] - self.start[1] |
| 239 | mag = numpy.hypot(dx, dy) |
| 240 | dx /= mag |
| 241 | dy /= mag |
| 242 | |
| 243 | return [(self.start[0], self.start[1], dx, dy, 0.0, 0.0), |
| 244 | (self.end[0], self.end[1], dx, dy, 0.0, 0.0)] |
| 245 | |
| 246 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 247 | class XYSegment: |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 248 | """Straight line in XY space.""" |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 249 | |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 250 | def __init__(self, start, end, name=None, alpha_unitizer=None, vmax=None): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 251 | """Creates an XY segment. |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 252 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 253 | Args: |
| 254 | start: (double, double), The start of the segment in theta1, theta2 |
| 255 | coordinates in radians |
| 256 | end: (double, double), The end of the segment in theta1, theta2 |
| 257 | coordinates in radians |
| 258 | """ |
| 259 | self.start = start |
| 260 | self.end = end |
| 261 | self.name = name |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 262 | self.alpha_unitizer = alpha_unitizer |
| 263 | self.vmax = vmax |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 264 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 265 | def __repr__(self): |
| 266 | return "XYSegment(%s, %s)" % (repr(self.start), repr(self.end)) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 267 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 268 | def DrawTo(self, cr, theta_version): |
| 269 | if theta_version: |
| 270 | theta1, theta2 = self.start |
| 271 | circular_index_select = int( |
| 272 | numpy.floor((self.start[1] - self.start[0]) / numpy.pi)) |
| 273 | start = get_xy(self.start) |
| 274 | end = get_xy(self.end) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 275 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 276 | ln = [(start[0], start[1]), (end[0], end[1])] |
| 277 | draw_lines(cr, [ |
| 278 | to_theta_with_circular_index(x, y, circular_index_select) |
| 279 | for x, y in subdivide_xy(ln) |
| 280 | ]) |
| 281 | cr.move_to(self.start[0] + theta_end_circle_size, self.start[1]) |
| 282 | cr.arc(self.start[0], self.start[1], theta_end_circle_size, 0, |
| 283 | 2.0 * numpy.pi) |
| 284 | cr.move_to(self.end[0] + theta_end_circle_size, self.end[1]) |
| 285 | cr.arc(self.end[0], self.end[1], theta_end_circle_size, 0, |
| 286 | 2.0 * numpy.pi) |
| 287 | else: |
| 288 | start = get_xy(self.start) |
| 289 | end = get_xy(self.end) |
| 290 | cr.move_to(start[0], start[1]) |
| 291 | cr.line_to(end[0], end[1]) |
| 292 | cr.move_to(start[0] + xy_end_circle_size, start[1]) |
| 293 | cr.arc(start[0], start[1], xy_end_circle_size, 0, 2.0 * numpy.pi) |
| 294 | cr.move_to(end[0] + xy_end_circle_size, end[1]) |
| 295 | cr.arc(end[0], end[1], xy_end_circle_size, 0, 2.0 * numpy.pi) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 296 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 297 | def ToThetaPoints(self): |
| 298 | """ Converts to points in theta space via to_theta_with_circular_index_and_derivs""" |
| 299 | theta1, theta2 = self.start |
| 300 | circular_index_select = int( |
| 301 | numpy.floor((self.start[1] - self.start[0]) / numpy.pi)) |
| 302 | start = get_xy(self.start) |
| 303 | end = get_xy(self.end) |
| 304 | |
| 305 | ln = [(start[0], start[1]), (end[0], end[1])] |
| 306 | |
| 307 | dx = end[0] - start[0] |
| 308 | dy = end[1] - start[1] |
| 309 | mag = numpy.hypot(dx, dy) |
| 310 | dx /= mag |
| 311 | dy /= mag |
| 312 | |
| 313 | return [ |
| 314 | to_theta_with_circular_index_and_derivs(x, y, dx, dy, |
| 315 | circular_index_select) |
| 316 | for x, y in subdivide_xy(ln, 0.01) |
| 317 | ] |
| 318 | |
| 319 | |
| 320 | def spline_eval(start, control1, control2, end, alpha): |
| 321 | a = alpha_blend(start, control1, alpha) |
| 322 | b = alpha_blend(control1, control2, alpha) |
| 323 | c = alpha_blend(control2, end, alpha) |
| 324 | return alpha_blend( |
| 325 | alpha_blend(a, b, alpha), alpha_blend(b, c, alpha), alpha) |
| 326 | |
| 327 | |
| 328 | def subdivide_spline(start, control1, control2, end): |
| 329 | # TODO: pick N based on spline parameters? or otherwise change it to be more evenly spaced? |
| 330 | n = 100 |
| 331 | for i in range(0, n + 1): |
| 332 | yield i / float(n) |
| 333 | |
| 334 | |
| 335 | class SplineSegment: |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 336 | def __init__(self, |
| 337 | start, |
| 338 | control1, |
| 339 | control2, |
| 340 | end, |
| 341 | name=None, |
| 342 | alpha_unitizer=None, |
| 343 | vmax=None): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 344 | self.start = start |
| 345 | self.control1 = control1 |
| 346 | self.control2 = control2 |
| 347 | self.end = end |
| 348 | self.name = name |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 349 | self.alpha_unitizer = alpha_unitizer |
| 350 | self.vmax = vmax |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 351 | |
| 352 | def __repr__(self): |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 353 | return "SplineSegment(%s, %s, %s, %s)" % (repr(self.start), |
| 354 | repr(self.control1), |
| 355 | repr(self.control2), |
| 356 | repr(self.end)) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 357 | |
| 358 | def DrawTo(self, cr, theta_version): |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 359 | if theta_version: |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 360 | c_i_select = get_circular_index(self.start) |
| 361 | start = get_xy(self.start) |
| 362 | control1 = get_xy(self.control1) |
| 363 | control2 = get_xy(self.control2) |
| 364 | end = get_xy(self.end) |
| 365 | |
| 366 | draw_lines(cr, [ |
| 367 | to_theta( |
| 368 | spline_eval(start, control1, control2, end, alpha), |
| 369 | c_i_select) |
| 370 | for alpha in subdivide_spline(start, control1, control2, end) |
| 371 | ]) |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 372 | cr.move_to(self.start[0] + theta_end_circle_size, self.start[1]) |
| 373 | cr.arc(self.start[0], self.start[1], theta_end_circle_size, 0, |
| 374 | 2.0 * numpy.pi) |
| 375 | cr.move_to(self.end[0] + theta_end_circle_size, self.end[1]) |
| 376 | cr.arc(self.end[0], self.end[1], theta_end_circle_size, 0, |
| 377 | 2.0 * numpy.pi) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 378 | else: |
| 379 | start = get_xy(self.start) |
| 380 | control1 = get_xy(self.control1) |
| 381 | control2 = get_xy(self.control2) |
| 382 | end = get_xy(self.end) |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 383 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 384 | draw_lines(cr, [ |
| 385 | spline_eval(start, control1, control2, end, alpha) |
| 386 | for alpha in subdivide_spline(start, control1, control2, end) |
| 387 | ]) |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 388 | |
| 389 | cr.move_to(self.start[0] + xy_end_circle_size, start[1]) |
| 390 | cr.arc(start[0], start[1], xy_end_circle_size, 0, 2.0 * numpy.pi) |
| 391 | cr.move_to(end[0] + xy_end_circle_size, end[1]) |
| 392 | cr.arc(end[0], end[1], xy_end_circle_size, 0, 2.0 * numpy.pi) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 393 | |
| 394 | def ToThetaPoints(self): |
| 395 | t1, t2 = self.start |
| 396 | c_i_select = get_circular_index(self.start) |
| 397 | start = get_xy(self.start) |
| 398 | control1 = get_xy(self.control1) |
| 399 | control2 = get_xy(self.control2) |
| 400 | end = get_xy(self.end) |
| 401 | |
| 402 | return [ |
| 403 | to_theta_with_ci_and_derivs( |
| 404 | spline_eval(start, control1, control2, end, alpha - 0.00001), |
| 405 | spline_eval(start, control1, control2, end, alpha), |
| 406 | spline_eval(start, control1, control2, end, alpha + 0.00001), |
| 407 | c_i_select) |
| 408 | for alpha in subdivide_spline(start, control1, control2, end) |
| 409 | ] |
| 410 | |
| 411 | |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 412 | def get_derivs(t_prev, t, t_next): |
| 413 | c, a, b = t_prev, t, t_next |
| 414 | d1 = normalize(b - a) |
| 415 | d2 = normalize(c - a) |
| 416 | accel = (d1 + d2) / numpy.linalg.norm(a - b) |
| 417 | return (a[0], a[1], d1[0], d1[1], accel[0], accel[1]) |
| 418 | |
| 419 | |
| 420 | class ThetaSplineSegment: |
| 421 | def __init__(self, |
| 422 | start, |
| 423 | control1, |
| 424 | control2, |
| 425 | end, |
| 426 | name=None, |
| 427 | alpha_unitizer=None, |
| 428 | vmax=None): |
| 429 | self.start = start |
| 430 | self.control1 = control1 |
| 431 | self.control2 = control2 |
| 432 | self.end = end |
| 433 | self.name = name |
| 434 | self.alpha_unitizer = alpha_unitizer |
| 435 | self.vmax = vmax |
| 436 | |
| 437 | def __repr__(self): |
| 438 | return "ThetaSplineSegment(%s, %s, &s, %s)" % (repr(self.start), |
| 439 | repr(self.control1), |
| 440 | repr(self.control2), |
| 441 | repr(self.end)) |
| 442 | |
| 443 | def DrawTo(self, cr, theta_version): |
| 444 | if (theta_version): |
| 445 | draw_lines(cr, [ |
| 446 | spline_eval(self.start, self.control1, self.control2, self.end, |
| 447 | alpha) |
| 448 | for alpha in subdivide_spline(self.start, self.control1, |
| 449 | self.control2, self.end) |
| 450 | ]) |
| 451 | else: |
| 452 | start = get_xy(self.start) |
| 453 | end = get_xy(self.end) |
| 454 | |
| 455 | draw_lines(cr, [ |
| 456 | get_xy( |
| 457 | spline_eval(self.start, self.control1, self.control2, |
| 458 | self.end, alpha)) |
| 459 | for alpha in subdivide_spline(self.start, self.control1, |
| 460 | self.control2, self.end) |
| 461 | ]) |
| 462 | |
| 463 | cr.move_to(start[0] + xy_end_circle_size, start[1]) |
| 464 | cr.arc(start[0], start[1], xy_end_circle_size, 0, 2.0 * numpy.pi) |
| 465 | cr.move_to(end[0] + xy_end_circle_size, end[1]) |
| 466 | cr.arc(end[0], end[1], xy_end_circle_size, 0, 2.0 * numpy.pi) |
| 467 | |
| 468 | def ToThetaPoints(self): |
| 469 | return [ |
| 470 | get_derivs( |
| 471 | spline_eval(self.start, self.control1, self.control2, self.end, |
| 472 | alpha - 0.00001), |
| 473 | spline_eval(self.start, self.control1, self.control2, self.end, |
| 474 | alpha), |
| 475 | spline_eval(self.start, self.control1, self.control2, self.end, |
| 476 | alpha + 0.00001)) |
| 477 | for alpha in subdivide_spline(self.start, self.control1, |
| 478 | self.control2, self.end) |
| 479 | ] |
| 480 | |
| 481 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 482 | tall_box_x = 0.411 |
| 483 | tall_box_y = 0.125 |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 484 | |
| 485 | short_box_x = 0.431 |
| 486 | short_box_y = 0.082 |
| 487 | |
| 488 | ready_above_box = to_theta_with_circular_index( |
| 489 | tall_box_x, tall_box_y + 0.08, circular_index=-1) |
| 490 | tall_box_grab = to_theta_with_circular_index( |
| 491 | tall_box_x, tall_box_y, circular_index=-1) |
| 492 | short_box_grab = to_theta_with_circular_index( |
| 493 | short_box_x, short_box_y, circular_index=-1) |
| 494 | |
| 495 | # TODO(austin): Drive the front/back off the same numbers a bit better. |
| 496 | front_high_box = to_theta_with_circular_index(0.378, 2.46, circular_index=-1) |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 497 | front_middle3_box = to_theta_with_circular_index( |
| 498 | 0.700, 2.125, circular_index=-1.000000) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 499 | front_middle2_box = to_theta_with_circular_index( |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 500 | 0.700, 2.268, circular_index=-1) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 501 | front_middle1_box = to_theta_with_circular_index( |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 502 | 0.800, 1.915, circular_index=-1) |
| 503 | front_low_box = to_theta_with_circular_index(0.87, 1.572, circular_index=-1) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 504 | back_high_box = to_theta_with_circular_index(-0.75, 2.48, circular_index=0) |
| 505 | back_middle2_box = to_theta_with_circular_index( |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 506 | -0.700, 2.268, circular_index=0) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 507 | back_middle1_box = to_theta_with_circular_index( |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 508 | -0.800, 1.915, circular_index=0) |
| 509 | back_low_box = to_theta_with_circular_index(-0.87, 1.572, circular_index=0) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 510 | |
| 511 | front_switch = to_theta_with_circular_index(0.88, 0.967, circular_index=-1) |
| 512 | back_switch = to_theta_with_circular_index(-0.88, 0.967, circular_index=-2) |
| 513 | |
| 514 | neutral = to_theta_with_circular_index(0.0, 0.33, circular_index=-1) |
| 515 | |
| 516 | up = to_theta_with_circular_index(0.0, 2.547, circular_index=-1) |
| 517 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 518 | front_switch_auto = to_theta_with_circular_index( |
| 519 | 0.750, 2.20, circular_index=-1.000000) |
| 520 | |
| 521 | starting = numpy.array( |
| 522 | [numpy.pi / 2.0 - 0.593329, numpy.pi / 2.0 - 3.749631]) |
| 523 | vertical_starting = numpy.array( |
| 524 | [numpy.pi / 2.0, -numpy.pi / 2.0]) |
| 525 | |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 526 | self_hang = numpy.array( |
| 527 | [numpy.pi / 2.0 - 0.191611, numpy.pi / 2.0]) |
| 528 | partner_hang = numpy.array( |
| 529 | [numpy.pi / 2.0 - (-0.25), numpy.pi / 2.0]) |
| 530 | |
| 531 | above_hang = numpy.array( |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 532 | [numpy.pi / 2.0 - 0.14, numpy.pi / 2.0 - (-0.165)]) |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 533 | below_hang = numpy.array( |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 534 | [numpy.pi / 2.0 - 0.39, numpy.pi / 2.0 - (-0.517)]) |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 535 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 536 | up_c1 = to_theta((0.63, 1.17), circular_index=-1) |
| 537 | up_c2 = to_theta((0.65, 1.62), circular_index=-1) |
| 538 | |
| 539 | front_high_box_c1 = to_theta((0.63, 1.04), circular_index=-1) |
| 540 | front_high_box_c2 = to_theta((0.50, 1.60), circular_index=-1) |
| 541 | |
| 542 | front_middle2_box_c1 = to_theta((0.41, 0.83), circular_index=-1) |
| 543 | front_middle2_box_c2 = to_theta((0.52, 1.30), circular_index=-1) |
| 544 | |
| 545 | front_middle1_box_c1 = to_theta((0.34, 0.82), circular_index=-1) |
| 546 | front_middle1_box_c2 = to_theta((0.48, 1.15), circular_index=-1) |
| 547 | |
| 548 | ready_above_box_c1 = to_theta((0.38, 0.33), circular_index=-1) |
| 549 | ready_above_box_c2 = to_theta((0.42, 0.51), circular_index=-1) |
| 550 | |
Austin Schuh | 214b91c | 2018-03-10 19:25:23 -0800 | [diff] [blame] | 551 | front_switch_c1 = numpy.array([1.903841, -0.622351]) |
| 552 | front_switch_c2 = numpy.array([1.903841, -0.622351]) |
| 553 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 554 | points = [(ready_above_box, "ReadyAboveBox"), |
| 555 | (tall_box_grab, "TallBoxGrab"), |
| 556 | (short_box_grab, "ShortBoxGrab"), |
| 557 | (front_high_box, "FrontHighBox"), |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 558 | (front_middle3_box, "FrontMiddle3Box"), |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 559 | (front_middle2_box, "FrontMiddle2Box"), |
| 560 | (front_middle1_box, "FrontMiddle1Box"), |
| 561 | (front_low_box, "FrontLowBox"), |
| 562 | (back_high_box, "BackHighBox"), |
| 563 | (back_middle2_box, "BackMiddle2Box"), |
| 564 | (back_middle1_box, "BackMiddle1Box"), |
| 565 | (back_low_box, "BackLowBox"), |
| 566 | (front_switch, "FrontSwitch"), |
| 567 | (back_switch, "BackSwitch"), |
| 568 | (neutral, "Neutral"), |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 569 | (up, "Up"), |
| 570 | (above_hang, "AboveHang"), |
| 571 | (below_hang, "BelowHang"), |
| 572 | (self_hang, "SelfHang"), |
| 573 | (partner_hang, "PartnerHang"), |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 574 | (front_switch_auto, "FrontSwitchAuto"), |
| 575 | (starting, "Starting"), |
| 576 | (vertical_starting, "VerticalStarting"), |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 577 | ] # yapf: disable |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 578 | |
| 579 | # We need to define critical points so we can create paths connecting them. |
| 580 | # TODO(austin): Attach velocities to the slow ones. |
| 581 | named_segments = [ |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 582 | SplineSegment(neutral, ready_above_box_c1, ready_above_box_c2, |
| 583 | ready_above_box, "ReadyToNeutral"), |
Austin Schuh | 41c71e4 | 2018-04-04 20:11:20 -0700 | [diff] [blame^] | 584 | XYSegment(ready_above_box, tall_box_grab, "ReadyToTallBox", vmax=6.0), |
| 585 | XYSegment(ready_above_box, short_box_grab, "ReadyToShortBox", vmax=6.0), |
| 586 | XYSegment(tall_box_grab, short_box_grab, "TallToShortBox", vmax=6.0), |
Austin Schuh | b874fd3 | 2018-03-05 00:27:10 -0800 | [diff] [blame] | 587 | SplineSegment(neutral, ready_above_box_c1, ready_above_box_c2, |
| 588 | tall_box_grab, "TallToNeutral"), |
| 589 | SplineSegment(neutral, ready_above_box_c1, ready_above_box_c2, |
| 590 | short_box_grab, "ShortToNeutral"), |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 591 | SplineSegment(neutral, up_c1, up_c2, up, "NeutralToUp"), |
| 592 | SplineSegment(neutral, front_high_box_c1, front_high_box_c2, |
| 593 | front_high_box, "NeutralToFrontHigh"), |
| 594 | SplineSegment(neutral, front_middle2_box_c1, front_middle2_box_c2, |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 595 | front_middle2_box, "NeutralToFrontMiddle2"), |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 596 | SplineSegment(neutral, front_middle1_box_c1, front_middle1_box_c2, |
Austin Schuh | 7dfccf6 | 2018-03-03 21:28:14 -0800 | [diff] [blame] | 597 | front_middle1_box, "NeutralToFrontMiddle1"), |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 598 | ] |
| 599 | |
| 600 | unnamed_segments = [ |
| 601 | AngleSegment(neutral, back_switch), |
Austin Schuh | 214b91c | 2018-03-10 19:25:23 -0800 | [diff] [blame] | 602 | SplineSegment(neutral, front_switch_c1, front_switch_c2, front_switch), |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 603 | |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 604 | AngleSegment(starting, vertical_starting), |
| 605 | AngleSegment(vertical_starting, neutral), |
| 606 | |
Austin Schuh | 214b91c | 2018-03-10 19:25:23 -0800 | [diff] [blame] | 607 | XYSegment(neutral, front_low_box), |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 608 | XYSegment(up, front_high_box), |
| 609 | XYSegment(up, front_middle2_box), |
Austin Schuh | 83cdd8a | 2018-03-21 20:49:02 -0700 | [diff] [blame] | 610 | |
| 611 | XYSegment(front_middle3_box, up), |
| 612 | XYSegment(front_middle3_box, front_high_box), |
| 613 | XYSegment(front_middle3_box, front_middle2_box), |
| 614 | XYSegment(front_middle3_box, front_middle1_box), |
| 615 | |
| 616 | XYSegment(neutral, front_switch_auto), |
| 617 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 618 | XYSegment(up, front_middle1_box), |
| 619 | XYSegment(up, front_low_box), |
| 620 | XYSegment(front_high_box, front_middle2_box), |
| 621 | XYSegment(front_high_box, front_middle1_box), |
| 622 | XYSegment(front_high_box, front_low_box), |
| 623 | XYSegment(front_middle2_box, front_middle1_box), |
| 624 | XYSegment(front_middle2_box, front_low_box), |
| 625 | XYSegment(front_middle1_box, front_low_box), |
| 626 | XYSegment(front_switch, front_low_box), |
| 627 | XYSegment(front_switch, up), |
| 628 | XYSegment(front_switch, front_high_box), |
| 629 | AngleSegment(up, back_high_box), |
| 630 | AngleSegment(up, back_middle2_box), |
| 631 | AngleSegment(up, back_middle1_box), |
| 632 | XYSegment(back_high_box, back_middle2_box), |
| 633 | XYSegment(back_high_box, back_middle1_box), |
| 634 | XYSegment(back_high_box, back_low_box), |
| 635 | XYSegment(back_middle2_box, back_middle1_box), |
| 636 | XYSegment(back_middle2_box, back_low_box), |
| 637 | XYSegment(back_middle1_box, back_low_box), |
Austin Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 638 | |
| 639 | AngleSegment(up, above_hang), |
| 640 | AngleSegment(above_hang, below_hang), |
| 641 | AngleSegment(up, below_hang), |
| 642 | AngleSegment(up, self_hang), |
| 643 | AngleSegment(up, partner_hang), |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 644 | ] |
| 645 | |
| 646 | segments = named_segments + unnamed_segments |