diff options
| author | 2026-11-11 22:28:13 +0000 | |
|---|---|---|
| committer | 2026-11-11 22:47:34 +0000 | |
| commit | cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch) | |
| tree | c7d90c0abe9533c87d51b5196871846855efa1ba /src/motion_planner.c | |
| download | thesis-master.tar.gz | |
Diffstat (limited to 'src/motion_planner.c')
| -rw-r--r-- | src/motion_planner.c | 1245 |
1 files changed, 1245 insertions, 0 deletions
diff --git a/src/motion_planner.c b/src/motion_planner.c new file mode 100644 index 0000000..313e3e9 --- /dev/null +++ b/src/motion_planner.c | |||
| @@ -0,0 +1,1245 @@ | |||
| 1 | #include "motion_planner.h" | ||
| 2 | |||
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <math.h> | ||
| 6 | #include <float.h> | ||
| 7 | |||
| 8 | static double | ||
| 9 | mp_clamp (double x, double lo, double hi) | ||
| 10 | { | ||
| 11 | return x < lo ? lo : (x > hi ? hi : x); | ||
| 12 | } | ||
| 13 | |||
| 14 | static int | ||
| 15 | mp_finite_pos (double x) | ||
| 16 | { | ||
| 17 | return isfinite (x) && x > 0.0; | ||
| 18 | } | ||
| 19 | |||
| 20 | static int | ||
| 21 | mp_limits_valid (const struct mp_limits *l) | ||
| 22 | { | ||
| 23 | return l && | ||
| 24 | mp_finite_pos (l->vmax) && | ||
| 25 | mp_finite_pos (l->amax) && | ||
| 26 | mp_finite_pos (l->jmax) && | ||
| 27 | isfinite (l->rapid_vmax) && | ||
| 28 | isfinite (l->corner_tol) && | ||
| 29 | isfinite (l->min_segment_len) && | ||
| 30 | mp_finite_pos (l->default_feed) && | ||
| 31 | isfinite (l->straight_angle_tol) && | ||
| 32 | isfinite (l->stop_angle) && | ||
| 33 | isfinite (l->corner_speed_deadband) && | ||
| 34 | isfinite (l->curvature_blend_len); | ||
| 35 | } | ||
| 36 | |||
| 37 | static double | ||
| 38 | mp_rapid_vmax (const struct mp_limits *l) | ||
| 39 | { | ||
| 40 | return l->rapid_vmax > 0.0 ? l->rapid_vmax : l->vmax; | ||
| 41 | } | ||
| 42 | |||
| 43 | static double | ||
| 44 | mp_straight_angle_tol (const struct mp_limits *l) | ||
| 45 | { | ||
| 46 | return l->straight_angle_tol > 0.0 ? l->straight_angle_tol : 1e-5; | ||
| 47 | } | ||
| 48 | |||
| 49 | static double | ||
| 50 | mp_stop_angle (const struct mp_limits *l) | ||
| 51 | { | ||
| 52 | if (l->stop_angle > 0.0 && l->stop_angle < M_PI) | ||
| 53 | return l->stop_angle; | ||
| 54 | |||
| 55 | return M_PI; | ||
| 56 | } | ||
| 57 | |||
| 58 | static double | ||
| 59 | mp_corner_deadband (const struct mp_limits *l) | ||
| 60 | { | ||
| 61 | return l->corner_speed_deadband > 0.0 ? l->corner_speed_deadband : 0.0; | ||
| 62 | } | ||
| 63 | |||
| 64 | static double | ||
| 65 | mp_min_segment_len (const struct mp_limits *l) | ||
| 66 | { | ||
| 67 | return l->min_segment_len > 0.0 ? l->min_segment_len : 1e-8; | ||
| 68 | } | ||
| 69 | |||
| 70 | static struct mp_vec2 | ||
| 71 | mp_vec2 (double x, double y) | ||
| 72 | { | ||
| 73 | struct mp_vec2 v; | ||
| 74 | v.x = x; | ||
| 75 | v.y = y; | ||
| 76 | return v; | ||
| 77 | } | ||
| 78 | |||
| 79 | static struct mp_vec2 | ||
| 80 | mp_vec2_add (struct mp_vec2 a, struct mp_vec2 b) | ||
| 81 | { | ||
| 82 | return mp_vec2 (a.x + b.x, a.y + b.y); | ||
| 83 | } | ||
| 84 | |||
| 85 | static struct mp_vec2 | ||
| 86 | mp_vec2_sub (struct mp_vec2 a, struct mp_vec2 b) | ||
| 87 | { | ||
| 88 | return mp_vec2 (a.x - b.x, a.y - b.y); | ||
| 89 | } | ||
| 90 | |||
| 91 | static struct mp_vec2 | ||
| 92 | mp_vec2_mul (struct mp_vec2 a, double k) | ||
| 93 | { | ||
| 94 | return mp_vec2 (a.x*k, a.y*k); | ||
| 95 | } | ||
| 96 | |||
| 97 | static double | ||
| 98 | mp_vec2_dot (struct mp_vec2 a, struct mp_vec2 b) | ||
| 99 | { | ||
| 100 | return a.x*b.x + a.y*b.y; | ||
| 101 | } | ||
| 102 | |||
| 103 | static double | ||
| 104 | mp_vec2_cross (struct mp_vec2 a, struct mp_vec2 b) | ||
| 105 | { | ||
| 106 | return a.x*b.y - a.y*b.x; | ||
| 107 | } | ||
| 108 | |||
| 109 | static double | ||
| 110 | mp_vec2_len (struct mp_vec2 a) | ||
| 111 | { | ||
| 112 | return hypot (a.x, a.y); | ||
| 113 | } | ||
| 114 | |||
| 115 | static double | ||
| 116 | mp_vec2_dist (struct mp_vec2 a, struct mp_vec2 b) | ||
| 117 | { | ||
| 118 | return mp_vec2_len (mp_vec2_sub (a, b)); | ||
| 119 | } | ||
| 120 | |||
| 121 | static struct mp_vec2 | ||
| 122 | mp_vec2_unit (struct mp_vec2 a) | ||
| 123 | { | ||
| 124 | double n = mp_vec2_len (a); | ||
| 125 | |||
| 126 | if (!(n > MP_EPS)) | ||
| 127 | return mp_vec2 (1.0, 0.0); | ||
| 128 | |||
| 129 | return mp_vec2_mul (a, 1.0/n); | ||
| 130 | } | ||
| 131 | |||
| 132 | static int | ||
| 133 | mp_reserve_segments (struct motion_plan *p, uint32_t want) | ||
| 134 | { | ||
| 135 | if (p->segment_capacity >= want) | ||
| 136 | return MP_OK; | ||
| 137 | |||
| 138 | uint32_t cap = p->segment_capacity ? p->segment_capacity : 16u; | ||
| 139 | while (cap < want) | ||
| 140 | cap *= 2u; | ||
| 141 | |||
| 142 | void *mem = realloc (p->segments, (size_t) cap * sizeof p->segments[0]); | ||
| 143 | if (!mem) | ||
| 144 | return MP_ERR_ALLOC; | ||
| 145 | |||
| 146 | p->segments = (struct mp_segment *) mem; | ||
| 147 | p->segment_capacity = cap; | ||
| 148 | return MP_OK; | ||
| 149 | } | ||
| 150 | |||
| 151 | static int | ||
| 152 | mp_reserve_nodes (struct motion_plan *p, uint32_t want) | ||
| 153 | { | ||
| 154 | if (p->node_capacity >= want) | ||
| 155 | return MP_OK; | ||
| 156 | |||
| 157 | uint32_t cap = p->node_capacity ? p->node_capacity : 16u; | ||
| 158 | while (cap < want) | ||
| 159 | cap *= 2u; | ||
| 160 | |||
| 161 | void *mem = realloc (p->nodes, (size_t) cap * sizeof p->nodes[0]); | ||
| 162 | if (!mem) | ||
| 163 | return MP_ERR_ALLOC; | ||
| 164 | |||
| 165 | p->nodes = (struct mp_node *) mem; | ||
| 166 | p->node_capacity = cap; | ||
| 167 | return MP_OK; | ||
| 168 | } | ||
| 169 | |||
| 170 | static int | ||
| 171 | mp_reserve_phases (struct motion_plan *p, uint32_t want) | ||
| 172 | { | ||
| 173 | if (p->phase_capacity >= want) | ||
| 174 | return MP_OK; | ||
| 175 | |||
| 176 | uint32_t cap = p->phase_capacity ? p->phase_capacity : 64u; | ||
| 177 | while (cap < want) | ||
| 178 | cap *= 2u; | ||
| 179 | |||
| 180 | void *mem = realloc (p->phases, (size_t) cap * sizeof p->phases[0]); | ||
| 181 | if (!mem) | ||
| 182 | return MP_ERR_ALLOC; | ||
| 183 | |||
| 184 | p->phases = (struct mp_phase *) mem; | ||
| 185 | p->phase_capacity = cap; | ||
| 186 | return MP_OK; | ||
| 187 | } | ||
| 188 | |||
| 189 | void | ||
| 190 | mp_plan_init (struct motion_plan *plan) | ||
| 191 | { | ||
| 192 | if (plan) | ||
| 193 | memset (plan, 0, sizeof *plan); | ||
| 194 | } | ||
| 195 | |||
| 196 | void | ||
| 197 | mp_plan_free (struct motion_plan *plan) | ||
| 198 | { | ||
| 199 | if (!plan) | ||
| 200 | return; | ||
| 201 | |||
| 202 | free (plan->segments); | ||
| 203 | free (plan->nodes); | ||
| 204 | free (plan->phases); | ||
| 205 | |||
| 206 | memset (plan, 0, sizeof *plan); | ||
| 207 | } | ||
| 208 | |||
| 209 | static void | ||
| 210 | mp_arc_sweep (struct mp_vec2 p0, | ||
| 211 | struct mp_vec2 p1, | ||
| 212 | struct mp_vec2 c, | ||
| 213 | int ccw, | ||
| 214 | double *a0_out, | ||
| 215 | double *sweep_out, | ||
| 216 | double *r_out) | ||
| 217 | { | ||
| 218 | double r0 = mp_vec2_dist (p0, c); | ||
| 219 | double r1 = mp_vec2_dist (p1, c); | ||
| 220 | double a0 = atan2 (p0.y - c.y, p0.x - c.x); | ||
| 221 | double a1 = atan2 (p1.y - c.y, p1.x - c.x); | ||
| 222 | double sweep = a1 - a0; | ||
| 223 | |||
| 224 | if (ccw) { | ||
| 225 | while (sweep <= 0) | ||
| 226 | sweep += 2*M_PI; | ||
| 227 | } else { | ||
| 228 | while (sweep >= 0) | ||
| 229 | sweep -= 2*M_PI; | ||
| 230 | } | ||
| 231 | |||
| 232 | if (r0 > MP_EPS && mp_vec2_dist (p0, p1) <= 1e-7*r0) | ||
| 233 | sweep = ccw ? 2*M_PI : -2*M_PI; | ||
| 234 | |||
| 235 | *a0_out = a0; | ||
| 236 | *sweep_out = sweep; | ||
| 237 | *r_out = (r0 + r1)/2; | ||
| 238 | } | ||
| 239 | |||
| 240 | static struct mp_vec2 | ||
| 241 | mp_tangent_start (const struct mp_segment *g) | ||
| 242 | { | ||
| 243 | if (g->type == MP_GEOM_LINE) | ||
| 244 | return mp_vec2_unit (mp_vec2_sub (g->p1, g->p0)); | ||
| 245 | |||
| 246 | double sgn = g->sweep >= 0 ? 1 : -1; | ||
| 247 | double a = g->a0; | ||
| 248 | |||
| 249 | return mp_vec2 (-sgn*sin (a), sgn*cos (a)); | ||
| 250 | } | ||
| 251 | |||
| 252 | static struct mp_vec2 | ||
| 253 | mp_tangent_end (const struct mp_segment *g) | ||
| 254 | { | ||
| 255 | if (g->type == MP_GEOM_LINE) | ||
| 256 | return mp_vec2_unit (mp_vec2_sub (g->p1, g->p0)); | ||
| 257 | |||
| 258 | double sgn = g->sweep >= 0 ? 1 : -1; | ||
| 259 | double a = g->a0 + g->sweep; | ||
| 260 | |||
| 261 | return mp_vec2 (-sgn*sin (a), sgn*cos (a)); | ||
| 262 | } | ||
| 263 | |||
| 264 | static void | ||
| 265 | mp_geom_eval_frame (const struct mp_segment *g, double s, | ||
| 266 | struct mp_vec2 *pos_out, | ||
| 267 | struct mp_vec2 *tangent_out, | ||
| 268 | struct mp_vec2 *normal_out, | ||
| 269 | double *kappa_out) | ||
| 270 | { | ||
| 271 | s = mp_clamp (s, 0.0, g->length); | ||
| 272 | |||
| 273 | if (g->type == MP_GEOM_ARC) { | ||
| 274 | double u = g->length > MP_EPS ? s/g->length : 1.0; | ||
| 275 | double a = g->a0 + g->sweep*u; | ||
| 276 | double ca = cos (a); | ||
| 277 | double sa = sin (a); | ||
| 278 | double sgn = g->sweep >= 0.0 ? 1.0 : -1.0; | ||
| 279 | |||
| 280 | *pos_out = mp_vec2 (g->c.x + g->r*ca, | ||
| 281 | g->c.y + g->r*sa); | ||
| 282 | *tangent_out = mp_vec2 (-sgn*sa, sgn*ca); | ||
| 283 | *normal_out = mp_vec2 (-ca, -sa); | ||
| 284 | *kappa_out = g->r > MP_EPS ? 1.0/g->r : 0.0; | ||
| 285 | return; | ||
| 286 | } | ||
| 287 | |||
| 288 | double u = g->length > MP_EPS ? s/g->length : 1.0; | ||
| 289 | struct mp_vec2 d = mp_vec2_sub (g->p1, g->p0); | ||
| 290 | struct mp_vec2 t = mp_vec2_unit (d); | ||
| 291 | |||
| 292 | *pos_out = mp_vec2 (g->p0.x + d.x*u, | ||
| 293 | g->p0.y + d.y*u); | ||
| 294 | *tangent_out = t; | ||
| 295 | *normal_out = mp_vec2 (-t.y, t.x); | ||
| 296 | *kappa_out = 0.0; | ||
| 297 | } | ||
| 298 | |||
| 299 | |||
| 300 | static double | ||
| 301 | mp_smoothstep (double x) | ||
| 302 | { | ||
| 303 | x = mp_clamp (x, 0.0, 1.0); | ||
| 304 | return x*x*(3.0 - 2.0*x); | ||
| 305 | } | ||
| 306 | |||
| 307 | static double | ||
| 308 | mp_segment_curvature (const struct mp_segment *g) | ||
| 309 | { | ||
| 310 | if (g && g->type == MP_GEOM_ARC && g->r > MP_EPS) | ||
| 311 | return 1.0/g->r; | ||
| 312 | |||
| 313 | return 0.0; | ||
| 314 | } | ||
| 315 | |||
| 316 | static int | ||
| 317 | mp_node_allows_curvature_blend (const struct mp_node *node) | ||
| 318 | { | ||
| 319 | if (!node) | ||
| 320 | return 0; | ||
| 321 | |||
| 322 | if (node->flags & (MP_NODE_GEOM_GAP | MP_NODE_HARD_STOP)) | ||
| 323 | return 0; | ||
| 324 | |||
| 325 | return 1; | ||
| 326 | } | ||
| 327 | |||
| 328 | static double | ||
| 329 | mp_effective_kappa (const struct motion_plan *plan, | ||
| 330 | uint32_t segment_index, | ||
| 331 | double local_s, | ||
| 332 | double kappa_geom) | ||
| 333 | { | ||
| 334 | if (!plan || !(plan->curvature_blend_len > 0.0) || !isfinite (plan->curvature_blend_len)) | ||
| 335 | return kappa_geom; | ||
| 336 | |||
| 337 | if (segment_index >= plan->segment_count) | ||
| 338 | return kappa_geom; | ||
| 339 | |||
| 340 | const struct mp_segment *cur = &plan->segments[segment_index]; | ||
| 341 | double kappa = kappa_geom; | ||
| 342 | double blend = plan->curvature_blend_len; | ||
| 343 | |||
| 344 | if (segment_index > 0u && local_s < blend) { | ||
| 345 | const struct mp_node *node = &plan->nodes[segment_index]; | ||
| 346 | |||
| 347 | if (mp_node_allows_curvature_blend (node)) { | ||
| 348 | const struct mp_segment *prev = &plan->segments[segment_index - 1u]; | ||
| 349 | double b = fmin (blend, 0.5*fmin (prev->length, cur->length)); | ||
| 350 | |||
| 351 | if (b > MP_EPS && local_s < b) { | ||
| 352 | double k_prev = mp_segment_curvature (prev); | ||
| 353 | double w = mp_smoothstep (local_s/b); | ||
| 354 | kappa = (1.0 - w)*k_prev + w*kappa; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 | if (segment_index + 1u < plan->segment_count && cur->length - local_s < blend) { | ||
| 360 | const struct mp_node *node = &plan->nodes[segment_index + 1u]; | ||
| 361 | |||
| 362 | if (mp_node_allows_curvature_blend (node)) { | ||
| 363 | const struct mp_segment *next = &plan->segments[segment_index + 1u]; | ||
| 364 | double b = fmin (blend, 0.5*fmin (cur->length, next->length)); | ||
| 365 | double d = cur->length - local_s; | ||
| 366 | |||
| 367 | if (b > MP_EPS && d < b) { | ||
| 368 | double k_next = mp_segment_curvature (next); | ||
| 369 | double w = mp_smoothstep (1.0 - d/b); | ||
| 370 | kappa = (1.0 - w)*kappa + w*k_next; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | } | ||
| 374 | |||
| 375 | return kappa; | ||
| 376 | } | ||
| 377 | |||
| 378 | static double | ||
| 379 | mp_segment_speed_cap (const struct mp_segment *g, | ||
| 380 | const struct mp_limits *l, | ||
| 381 | double v) | ||
| 382 | { | ||
| 383 | if (g->type == MP_GEOM_ARC && g->r > MP_EPS) { | ||
| 384 | double vc = sqrt (l->amax * g->r); | ||
| 385 | if (vc > 0.0 && isfinite (vc)) | ||
| 386 | v = fmin (v, vc); | ||
| 387 | } | ||
| 388 | |||
| 389 | return v; | ||
| 390 | } | ||
| 391 | |||
| 392 | static int | ||
| 393 | mp_push_segment (struct motion_plan *p, const struct mp_segment *g) | ||
| 394 | { | ||
| 395 | int rc = mp_reserve_segments (p, p->segment_count + 1u); | ||
| 396 | if (rc != MP_OK) | ||
| 397 | return rc; | ||
| 398 | |||
| 399 | p->segments[p->segment_count++] = *g; | ||
| 400 | return MP_OK; | ||
| 401 | } | ||
| 402 | |||
| 403 | static int | ||
| 404 | mp_build_segments (const struct path *path, | ||
| 405 | double initial_x, | ||
| 406 | double initial_y, | ||
| 407 | const struct mp_limits *limits, | ||
| 408 | struct motion_plan *out) | ||
| 409 | { | ||
| 410 | struct mp_vec2 p0 = mp_vec2 (initial_x, initial_y); | ||
| 411 | double min_len = mp_min_segment_len (limits); | ||
| 412 | double rapid = mp_rapid_vmax (limits); | ||
| 413 | |||
| 414 | if (!(rapid > 0.0) || !isfinite (rapid)) | ||
| 415 | return MP_ERR_LIMITS; | ||
| 416 | |||
| 417 | for (uint32_t i = 0; i < path->count; ++i) { | ||
| 418 | const struct path_element *el = &path->items[i]; | ||
| 419 | struct mp_vec2 p1 = mp_vec2 (el->x, el->y); | ||
| 420 | |||
| 421 | struct mp_segment g; | ||
| 422 | memset (&g, 0, sizeof g); | ||
| 423 | |||
| 424 | g.source_index = i; | ||
| 425 | g.flags = el->flags; | ||
| 426 | g.p0 = p0; | ||
| 427 | g.p1 = p1; | ||
| 428 | |||
| 429 | if (el->flags & PEF_ARC) { | ||
| 430 | g.type = MP_GEOM_ARC; | ||
| 431 | g.c = mp_vec2 (el->i, el->j); | ||
| 432 | mp_arc_sweep (g.p0, g.p1, g.c, el->flags & PEF_CCW, &g.a0, &g.sweep, &g.r); | ||
| 433 | |||
| 434 | if (!(g.r > MP_EPS) || !isfinite (g.r) || !isfinite (g.sweep)) | ||
| 435 | return MP_ERR_GEOMETRY; | ||
| 436 | |||
| 437 | g.length = fabs (g.sweep) * g.r; | ||
| 438 | } else { | ||
| 439 | g.type = MP_GEOM_LINE; | ||
| 440 | g.length = mp_vec2_dist (g.p0, g.p1); | ||
| 441 | } | ||
| 442 | |||
| 443 | p0 = p1; | ||
| 444 | |||
| 445 | if (!(g.length > min_len)) | ||
| 446 | continue; | ||
| 447 | |||
| 448 | if (el->flags & PEF_TRAVERSING) { | ||
| 449 | g.feed = rapid; | ||
| 450 | g.v_nominal = rapid; | ||
| 451 | } else { | ||
| 452 | g.feed = el->f > 0.0 ? el->f : limits->default_feed; | ||
| 453 | g.v_nominal = fmin (g.feed, limits->vmax); | ||
| 454 | } | ||
| 455 | |||
| 456 | g.v_nominal = mp_segment_speed_cap (&g, limits, g.v_nominal); | ||
| 457 | |||
| 458 | if (!(g.v_nominal > 0.0) || !isfinite (g.v_nominal)) | ||
| 459 | return MP_ERR_LIMITS; | ||
| 460 | |||
| 461 | g.s_begin = out->total_length; | ||
| 462 | g.s_end = g.s_begin + g.length; | ||
| 463 | out->total_length = g.s_end; | ||
| 464 | |||
| 465 | int rc = mp_push_segment (out, &g); | ||
| 466 | if (rc != MP_OK) | ||
| 467 | return rc; | ||
| 468 | } | ||
| 469 | |||
| 470 | return MP_OK; | ||
| 471 | } | ||
| 472 | |||
| 473 | static double | ||
| 474 | mp_junction_speed (const struct mp_segment *left, | ||
| 475 | const struct mp_segment *right, | ||
| 476 | const struct mp_limits *limits, | ||
| 477 | double *theta_out, | ||
| 478 | uint32_t *flags_out) | ||
| 479 | { | ||
| 480 | double cap = fmin (left->v_nominal, right->v_nominal); | ||
| 481 | uint32_t flags = 0; | ||
| 482 | |||
| 483 | if (!(cap > 0.0) || !isfinite (cap)) { | ||
| 484 | if (theta_out) *theta_out = 0.0; | ||
| 485 | if (flags_out) *flags_out = MP_NODE_HARD_STOP; | ||
| 486 | return 0.0; | ||
| 487 | } | ||
| 488 | |||
| 489 | if (mp_vec2_dist (left->p1, right->p0) > 1e-5) { | ||
| 490 | if (theta_out) *theta_out = 0.0; | ||
| 491 | if (flags_out) *flags_out = MP_NODE_GEOM_GAP | MP_NODE_HARD_STOP; | ||
| 492 | return 0.0; | ||
| 493 | } | ||
| 494 | |||
| 495 | struct mp_vec2 t0 = mp_tangent_end (left); | ||
| 496 | struct mp_vec2 t1 = mp_tangent_start (right); | ||
| 497 | double dot = mp_clamp (mp_vec2_dot (t0, t1), -1.0, 1.0); | ||
| 498 | double cross = mp_vec2_cross (t0, t1); | ||
| 499 | double theta = fabs (atan2 (cross, dot)); | ||
| 500 | |||
| 501 | if (theta_out) | ||
| 502 | *theta_out = theta; | ||
| 503 | |||
| 504 | if (theta <= mp_straight_angle_tol (limits)) { | ||
| 505 | flags = MP_NODE_STRAIGHT; | ||
| 506 | if (flags_out) *flags_out = flags; | ||
| 507 | return cap; | ||
| 508 | } | ||
| 509 | |||
| 510 | if (theta >= mp_stop_angle (limits)) { | ||
| 511 | flags = MP_NODE_HARD_STOP | MP_NODE_CORNER_LIMIT; | ||
| 512 | if (flags_out) *flags_out = flags; | ||
| 513 | return 0; | ||
| 514 | } | ||
| 515 | |||
| 516 | if (!(limits->corner_tol > 0.0)) { | ||
| 517 | flags = MP_NODE_HARD_STOP | MP_NODE_CORNER_LIMIT; | ||
| 518 | if (flags_out) *flags_out = flags; | ||
| 519 | return 0; | ||
| 520 | } | ||
| 521 | |||
| 522 | double c = cos (theta/2); | ||
| 523 | double denom = 1 - c; | ||
| 524 | |||
| 525 | if (denom <= MP_EPS) { | ||
| 526 | flags = MP_NODE_STRAIGHT; | ||
| 527 | if (flags_out) *flags_out = flags; | ||
| 528 | return cap; | ||
| 529 | } | ||
| 530 | |||
| 531 | double r_virtual = limits->corner_tol*c/denom; | ||
| 532 | if (!(r_virtual > 0.0) || !isfinite (r_virtual)) { | ||
| 533 | flags = MP_NODE_HARD_STOP | MP_NODE_CORNER_LIMIT; | ||
| 534 | if (flags_out) *flags_out = flags; | ||
| 535 | return 0.0; | ||
| 536 | } | ||
| 537 | |||
| 538 | double vj = sqrt (limits->amax * r_virtual); | ||
| 539 | double db = mp_corner_deadband (limits); | ||
| 540 | |||
| 541 | if (vj >= cap*(1 - db)) { | ||
| 542 | flags = MP_NODE_STRAIGHT; | ||
| 543 | if (flags_out) *flags_out = flags; | ||
| 544 | return cap; | ||
| 545 | } | ||
| 546 | |||
| 547 | flags = MP_NODE_CORNER_LIMIT; | ||
| 548 | if (flags_out) *flags_out = flags; | ||
| 549 | return cap; | ||
| 550 | return fmin (cap, vj); | ||
| 551 | } | ||
| 552 | |||
| 553 | static int | ||
| 554 | mp_build_nodes (struct motion_plan *out, const struct mp_limits *limits) | ||
| 555 | { | ||
| 556 | uint32_t nseg = out->segment_count; | ||
| 557 | int rc = mp_reserve_nodes (out, nseg + 1u); | ||
| 558 | if (rc != MP_OK) | ||
| 559 | return rc; | ||
| 560 | |||
| 561 | out->node_count = nseg + 1u; | ||
| 562 | memset (out->nodes, 0, (size_t) out->node_count * sizeof out->nodes[0]); | ||
| 563 | |||
| 564 | out->nodes[0].s = 0.0; | ||
| 565 | out->nodes[0].v_cap = 0.0; | ||
| 566 | out->nodes[0].v = 0.0; | ||
| 567 | out->nodes[0].flags = MP_NODE_START | MP_NODE_HARD_STOP | MP_NODE_KNOT; | ||
| 568 | |||
| 569 | for (uint32_t i = 1; i < nseg; ++i) { | ||
| 570 | struct mp_node *node = &out->nodes[i]; | ||
| 571 | const struct mp_segment *left = &out->segments[i - 1u]; | ||
| 572 | const struct mp_segment *right = &out->segments[i]; | ||
| 573 | |||
| 574 | uint32_t flags = 0; | ||
| 575 | double theta = 0.0; | ||
| 576 | double vcap = mp_junction_speed (left, right, limits, &theta, &flags); | ||
| 577 | |||
| 578 | if (fabs (left->v_nominal - right->v_nominal) > 1e-8*fmax (1.0, fmax (left->v_nominal, right->v_nominal))) | ||
| 579 | flags |= MP_NODE_SPEED_CHANGE; | ||
| 580 | |||
| 581 | node->s = left->s_end; | ||
| 582 | node->v_cap = vcap; | ||
| 583 | node->v = vcap; | ||
| 584 | node->theta = theta; | ||
| 585 | node->flags = flags; | ||
| 586 | } | ||
| 587 | |||
| 588 | out->nodes[nseg].s = out->total_length; | ||
| 589 | out->nodes[nseg].v_cap = 0.0; | ||
| 590 | out->nodes[nseg].v = 0.0; | ||
| 591 | out->nodes[nseg].flags = MP_NODE_END | MP_NODE_HARD_STOP | MP_NODE_KNOT; | ||
| 592 | |||
| 593 | return MP_OK; | ||
| 594 | } | ||
| 595 | |||
| 596 | static double | ||
| 597 | mp_speed_change_distance_scurve (double v_from, | ||
| 598 | double v_to, | ||
| 599 | double amax, | ||
| 600 | double jmax) | ||
| 601 | { | ||
| 602 | if (v_to <= v_from + MP_EPS) | ||
| 603 | return 0.0; | ||
| 604 | |||
| 605 | double dv = v_to - v_from; | ||
| 606 | double dv_full = amax*amax/jmax; | ||
| 607 | double duration; | ||
| 608 | |||
| 609 | if (dv <= dv_full) { | ||
| 610 | double tj = sqrt (dv/jmax); | ||
| 611 | duration = 2.0*tj; | ||
| 612 | } else { | ||
| 613 | double tj = amax/jmax; | ||
| 614 | double tc = dv/amax - tj; | ||
| 615 | duration = 2.0*tj + tc; | ||
| 616 | } | ||
| 617 | |||
| 618 | return 0.5*(v_from + v_to)*duration; | ||
| 619 | } | ||
| 620 | |||
| 621 | static double | ||
| 622 | mp_reachable_speed (double v0, | ||
| 623 | double length, | ||
| 624 | const struct mp_limits *limits, | ||
| 625 | double vcap) | ||
| 626 | { | ||
| 627 | if (vcap <= v0) | ||
| 628 | return vcap; | ||
| 629 | |||
| 630 | if (mp_speed_change_distance_scurve (v0, vcap, limits->amax, limits->jmax) <= length) | ||
| 631 | return vcap; | ||
| 632 | |||
| 633 | double lo = v0; | ||
| 634 | double hi = vcap; | ||
| 635 | |||
| 636 | for (int k = 0; k < 80; ++k) { | ||
| 637 | double mid = 0.5*(lo + hi); | ||
| 638 | double s = mp_speed_change_distance_scurve (v0, mid, limits->amax, limits->jmax); | ||
| 639 | |||
| 640 | if (s <= length) | ||
| 641 | lo = mid; | ||
| 642 | else | ||
| 643 | hi = mid; | ||
| 644 | } | ||
| 645 | |||
| 646 | return lo; | ||
| 647 | } | ||
| 648 | |||
| 649 | static void | ||
| 650 | mp_apply_speed_lookahead (struct motion_plan *out, | ||
| 651 | const struct mp_limits *limits) | ||
| 652 | { | ||
| 653 | uint32_t nseg = out->segment_count; | ||
| 654 | |||
| 655 | for (uint32_t i = nseg; i-- > 0u;) { | ||
| 656 | double cap = out->segments[i].v_nominal; | ||
| 657 | double allowed = mp_reachable_speed (out->nodes[i + 1u].v, | ||
| 658 | out->segments[i].length, | ||
| 659 | limits, | ||
| 660 | cap); | ||
| 661 | if (out->nodes[i].v > allowed) | ||
| 662 | out->nodes[i].v = allowed; | ||
| 663 | } | ||
| 664 | |||
| 665 | for (uint32_t i = 0; i < nseg; ++i) { | ||
| 666 | double cap = out->segments[i].v_nominal; | ||
| 667 | double allowed = mp_reachable_speed (out->nodes[i].v, | ||
| 668 | out->segments[i].length, | ||
| 669 | limits, | ||
| 670 | cap); | ||
| 671 | if (out->nodes[i + 1u].v > allowed) | ||
| 672 | out->nodes[i + 1u].v = allowed; | ||
| 673 | } | ||
| 674 | } | ||
| 675 | |||
| 676 | static int | ||
| 677 | mp_node_should_be_knot (const struct motion_plan *p, | ||
| 678 | uint32_t node_index, | ||
| 679 | const struct mp_limits *limits) | ||
| 680 | { | ||
| 681 | if (node_index == 0u || node_index + 1u == p->node_count) | ||
| 682 | return 1; | ||
| 683 | |||
| 684 | const struct mp_node *node = &p->nodes[node_index]; | ||
| 685 | const struct mp_segment *left = &p->segments[node_index - 1u]; | ||
| 686 | const struct mp_segment *right = &p->segments[node_index]; | ||
| 687 | |||
| 688 | if (node->flags & (MP_NODE_GEOM_GAP | MP_NODE_HARD_STOP)) | ||
| 689 | return 1; | ||
| 690 | |||
| 691 | double cap = fmin (left->v_nominal, right->v_nominal); | ||
| 692 | double db = mp_corner_deadband (limits); | ||
| 693 | |||
| 694 | if (node->v_cap < cap * (1.0 - db)) | ||
| 695 | return 1; | ||
| 696 | |||
| 697 | return 0; | ||
| 698 | } | ||
| 699 | |||
| 700 | static void | ||
| 701 | mp_mark_knots (struct motion_plan *p, const struct mp_limits *limits) | ||
| 702 | { | ||
| 703 | for (uint32_t i = 0; i < p->node_count; ++i) { | ||
| 704 | if (mp_node_should_be_knot (p, i, limits)) | ||
| 705 | p->nodes[i].flags |= MP_NODE_KNOT; | ||
| 706 | else | ||
| 707 | p->nodes[i].flags &= ~((uint32_t) MP_NODE_KNOT); | ||
| 708 | } | ||
| 709 | } | ||
| 710 | |||
| 711 | static double | ||
| 712 | mp_interval_vcap (const struct motion_plan *p, | ||
| 713 | uint32_t node_begin, | ||
| 714 | uint32_t node_end) | ||
| 715 | { | ||
| 716 | double vcap = DBL_MAX; | ||
| 717 | |||
| 718 | for (uint32_t i = node_begin; i < node_end; ++i) | ||
| 719 | vcap = fmin (vcap, p->segments[i].v_nominal); | ||
| 720 | |||
| 721 | if (!(vcap < DBL_MAX)) | ||
| 722 | return 0.0; | ||
| 723 | |||
| 724 | return vcap; | ||
| 725 | } | ||
| 726 | |||
| 727 | static int | ||
| 728 | mp_append_phase (struct motion_plan *p, | ||
| 729 | double dt, | ||
| 730 | double a0, | ||
| 731 | double j, | ||
| 732 | double *s_cursor, | ||
| 733 | double *t_cursor, | ||
| 734 | double *v_cursor, | ||
| 735 | double *a_cursor) | ||
| 736 | { | ||
| 737 | if (dt <= 1e-12) | ||
| 738 | return MP_OK; | ||
| 739 | |||
| 740 | if (!isfinite (dt) || !isfinite (a0) || !isfinite (j)) | ||
| 741 | return MP_ERR_PROFILE; | ||
| 742 | |||
| 743 | int rc = mp_reserve_phases (p, p->phase_count + 1u); | ||
| 744 | if (rc != MP_OK) | ||
| 745 | return rc; | ||
| 746 | |||
| 747 | struct mp_phase *ph = &p->phases[p->phase_count++]; | ||
| 748 | |||
| 749 | ph->t_begin = *t_cursor; | ||
| 750 | ph->dt = dt; | ||
| 751 | ph->s_begin = *s_cursor; | ||
| 752 | ph->v0 = *v_cursor; | ||
| 753 | ph->a0 = a0; | ||
| 754 | ph->j = j; | ||
| 755 | |||
| 756 | double dt2 = dt*dt; | ||
| 757 | double dt3 = dt2*dt; | ||
| 758 | |||
| 759 | double s1 = *s_cursor + (*v_cursor)*dt + 0.5*a0*dt2 + (j*dt3)/6.0; | ||
| 760 | double v1 = *v_cursor + a0*dt + 0.5*j*dt2; | ||
| 761 | double a1 = a0 + j*dt; | ||
| 762 | |||
| 763 | ph->s_end = s1; | ||
| 764 | |||
| 765 | *s_cursor = s1; | ||
| 766 | *t_cursor += dt; | ||
| 767 | *v_cursor = v1; | ||
| 768 | *a_cursor = a1; | ||
| 769 | |||
| 770 | p->total_time = *t_cursor; | ||
| 771 | |||
| 772 | p->v_min = fmin (p->v_min, fmin (ph->v0, v1)); | ||
| 773 | p->v_max = fmax (p->v_max, fmax (ph->v0, v1)); | ||
| 774 | |||
| 775 | p->a_min = fmin (p->a_min, fmin (a0, a1)); | ||
| 776 | p->a_max = fmax (p->a_max, fmax (a0, a1)); | ||
| 777 | |||
| 778 | p->j_min = fmin (p->j_min, j); | ||
| 779 | p->j_max = fmax (p->j_max, j); | ||
| 780 | |||
| 781 | return MP_OK; | ||
| 782 | } | ||
| 783 | |||
| 784 | static int | ||
| 785 | mp_append_speed_change_scurve (struct motion_plan *p, | ||
| 786 | double v_from, | ||
| 787 | double v_to, | ||
| 788 | const struct mp_limits *limits, | ||
| 789 | double *s_cursor, | ||
| 790 | double *t_cursor, | ||
| 791 | double *v_cursor, | ||
| 792 | double *a_cursor) | ||
| 793 | { | ||
| 794 | double dv = fabs (v_to - v_from); | ||
| 795 | |||
| 796 | if (dv <= 1e-10) { | ||
| 797 | *v_cursor = v_to; | ||
| 798 | *a_cursor = 0.0; | ||
| 799 | return MP_OK; | ||
| 800 | } | ||
| 801 | |||
| 802 | double sign = v_to > v_from ? 1.0 : -1.0; | ||
| 803 | double dv_full = limits->amax*limits->amax/limits->jmax; | ||
| 804 | |||
| 805 | if (dv <= dv_full) { | ||
| 806 | double tj = sqrt (dv/limits->jmax); | ||
| 807 | double ap = limits->jmax*tj; | ||
| 808 | |||
| 809 | int rc = mp_append_phase (p, tj, 0.0, sign*limits->jmax, | ||
| 810 | s_cursor, t_cursor, v_cursor, a_cursor); | ||
| 811 | if (rc != MP_OK) return rc; | ||
| 812 | |||
| 813 | return mp_append_phase (p, tj, sign*ap, -sign*limits->jmax, | ||
| 814 | s_cursor, t_cursor, v_cursor, a_cursor); | ||
| 815 | } | ||
| 816 | |||
| 817 | double tj = limits->amax/limits->jmax; | ||
| 818 | double tc = dv/limits->amax - tj; | ||
| 819 | |||
| 820 | int rc = mp_append_phase (p, tj, 0.0, sign*limits->jmax, | ||
| 821 | s_cursor, t_cursor, v_cursor, a_cursor); | ||
| 822 | if (rc != MP_OK) return rc; | ||
| 823 | |||
| 824 | rc = mp_append_phase (p, tc, sign*limits->amax, 0.0, | ||
| 825 | s_cursor, t_cursor, v_cursor, a_cursor); | ||
| 826 | if (rc != MP_OK) return rc; | ||
| 827 | |||
| 828 | return mp_append_phase (p, tj, sign*limits->amax, -sign*limits->jmax, | ||
| 829 | s_cursor, t_cursor, v_cursor, a_cursor); | ||
| 830 | } | ||
| 831 | |||
| 832 | static int | ||
| 833 | mp_build_interval_scurve (struct motion_plan *p, | ||
| 834 | double length, | ||
| 835 | double v0, | ||
| 836 | double v1, | ||
| 837 | double vcap, | ||
| 838 | const struct mp_limits *limits, | ||
| 839 | double s_target, | ||
| 840 | double *s_cursor, | ||
| 841 | double *t_cursor, | ||
| 842 | double *v_cursor, | ||
| 843 | double *a_cursor) | ||
| 844 | { | ||
| 845 | if (!(length > 1e-12)) { | ||
| 846 | *s_cursor = s_target; | ||
| 847 | *v_cursor = v1; | ||
| 848 | *a_cursor = 0.0; | ||
| 849 | return MP_OK; | ||
| 850 | } | ||
| 851 | |||
| 852 | if (!(vcap > 0.0) || !isfinite (vcap)) | ||
| 853 | return MP_ERR_PROFILE; | ||
| 854 | |||
| 855 | if (v0 > vcap + 1e-7 || v1 > vcap + 1e-7) | ||
| 856 | return MP_ERR_PROFILE; | ||
| 857 | |||
| 858 | double vlo = fmax (v0, v1); | ||
| 859 | double vhi = fmax (vcap, vlo); | ||
| 860 | |||
| 861 | double s_at_vhi = | ||
| 862 | mp_speed_change_distance_scurve (v0, vhi, limits->amax, limits->jmax) + | ||
| 863 | mp_speed_change_distance_scurve (v1, vhi, limits->amax, limits->jmax); | ||
| 864 | |||
| 865 | double vp; | ||
| 866 | double cruise_len; | ||
| 867 | |||
| 868 | if (s_at_vhi <= length + 1e-8) { | ||
| 869 | vp = vhi; | ||
| 870 | cruise_len = fmax (0.0, length - s_at_vhi); | ||
| 871 | } else { | ||
| 872 | double s_at_vlo = | ||
| 873 | mp_speed_change_distance_scurve (v0, vlo, limits->amax, limits->jmax) + | ||
| 874 | mp_speed_change_distance_scurve (v1, vlo, limits->amax, limits->jmax); | ||
| 875 | |||
| 876 | if (s_at_vlo > length + 1e-6) | ||
| 877 | return MP_ERR_PROFILE; | ||
| 878 | |||
| 879 | double lo = vlo; | ||
| 880 | double hi = vhi; | ||
| 881 | |||
| 882 | for (int k = 0; k < 80; ++k) { | ||
| 883 | double mid = 0.5*(lo + hi); | ||
| 884 | double sm = | ||
| 885 | mp_speed_change_distance_scurve (v0, mid, limits->amax, limits->jmax) + | ||
| 886 | mp_speed_change_distance_scurve (v1, mid, limits->amax, limits->jmax); | ||
| 887 | |||
| 888 | if (sm <= length) | ||
| 889 | lo = mid; | ||
| 890 | else | ||
| 891 | hi = mid; | ||
| 892 | } | ||
| 893 | |||
| 894 | vp = lo; | ||
| 895 | cruise_len = 0.0; | ||
| 896 | } | ||
| 897 | |||
| 898 | *v_cursor = v0; | ||
| 899 | *a_cursor = 0.0; | ||
| 900 | |||
| 901 | int rc = mp_append_speed_change_scurve (p, v0, vp, limits, | ||
| 902 | s_cursor, t_cursor, v_cursor, a_cursor); | ||
| 903 | if (rc != MP_OK) | ||
| 904 | return rc; | ||
| 905 | |||
| 906 | if (cruise_len > 1e-10) { | ||
| 907 | if (!(vp > 1e-12)) | ||
| 908 | return MP_ERR_PROFILE; | ||
| 909 | |||
| 910 | rc = mp_append_phase (p, cruise_len/vp, 0.0, 0.0, | ||
| 911 | s_cursor, t_cursor, v_cursor, a_cursor); | ||
| 912 | if (rc != MP_OK) | ||
| 913 | return rc; | ||
| 914 | } | ||
| 915 | |||
| 916 | rc = mp_append_speed_change_scurve (p, vp, v1, limits, | ||
| 917 | s_cursor, t_cursor, v_cursor, a_cursor); | ||
| 918 | if (rc != MP_OK) | ||
| 919 | return rc; | ||
| 920 | |||
| 921 | *s_cursor = s_target; | ||
| 922 | *v_cursor = v1; | ||
| 923 | *a_cursor = 0.0; | ||
| 924 | |||
| 925 | if (p->phase_count > 0u) | ||
| 926 | p->phases[p->phase_count - 1u].s_end = s_target; | ||
| 927 | |||
| 928 | return MP_OK; | ||
| 929 | } | ||
| 930 | |||
| 931 | static int | ||
| 932 | mp_build_global_phases (struct motion_plan *p, | ||
| 933 | const struct mp_limits *limits) | ||
| 934 | { | ||
| 935 | p->phase_count = 0u; | ||
| 936 | p->total_time = 0.0; | ||
| 937 | |||
| 938 | p->v_min = DBL_MAX; | ||
| 939 | p->v_max = -DBL_MAX; | ||
| 940 | p->a_min = DBL_MAX; | ||
| 941 | p->a_max = -DBL_MAX; | ||
| 942 | p->j_min = DBL_MAX; | ||
| 943 | p->j_max = -DBL_MAX; | ||
| 944 | |||
| 945 | double s_cursor = 0.0; | ||
| 946 | double t_cursor = 0.0; | ||
| 947 | double v_cursor = 0.0; | ||
| 948 | double a_cursor = 0.0; | ||
| 949 | |||
| 950 | uint32_t knot_begin = 0u; | ||
| 951 | |||
| 952 | while (knot_begin + 1u < p->node_count) { | ||
| 953 | uint32_t knot_end = knot_begin + 1u; | ||
| 954 | |||
| 955 | while (knot_end + 1u < p->node_count && | ||
| 956 | !(p->nodes[knot_end].flags & MP_NODE_KNOT)) | ||
| 957 | ++knot_end; | ||
| 958 | |||
| 959 | double length = p->nodes[knot_end].s - p->nodes[knot_begin].s; | ||
| 960 | double v0 = p->nodes[knot_begin].v; | ||
| 961 | double v1 = p->nodes[knot_end].v; | ||
| 962 | double vcap = mp_interval_vcap (p, knot_begin, knot_end); | ||
| 963 | double s_target = p->nodes[knot_end].s; | ||
| 964 | |||
| 965 | int rc = mp_build_interval_scurve (p, length, v0, v1, vcap, limits, | ||
| 966 | s_target, | ||
| 967 | &s_cursor, &t_cursor, &v_cursor, &a_cursor); | ||
| 968 | if (rc != MP_OK) | ||
| 969 | return rc; | ||
| 970 | |||
| 971 | knot_begin = knot_end; | ||
| 972 | } | ||
| 973 | |||
| 974 | p->total_time = t_cursor; | ||
| 975 | |||
| 976 | if (p->phase_count == 0u) { | ||
| 977 | p->v_min = p->v_max = 0.0; | ||
| 978 | p->a_min = p->a_max = 0.0; | ||
| 979 | p->j_min = p->j_max = 0.0; | ||
| 980 | } | ||
| 981 | |||
| 982 | return MP_OK; | ||
| 983 | } | ||
| 984 | |||
| 985 | int | ||
| 986 | mp_plan_build (const struct path *path, | ||
| 987 | double initial_x, | ||
| 988 | double initial_y, | ||
| 989 | const struct mp_limits *limits, | ||
| 990 | struct motion_plan *out) | ||
| 991 | { | ||
| 992 | if (!path || !out || !mp_limits_valid (limits)) | ||
| 993 | return MP_ERR_INVALID_ARG; | ||
| 994 | |||
| 995 | mp_plan_free (out); | ||
| 996 | |||
| 997 | out->curvature_blend_len = limits->curvature_blend_len > 0.0 | ||
| 998 | ? limits->curvature_blend_len | ||
| 999 | : 0.0; | ||
| 1000 | |||
| 1001 | if (path->count == 0u) | ||
| 1002 | return MP_OK; | ||
| 1003 | |||
| 1004 | int rc = mp_build_segments (path, initial_x, initial_y, limits, out); | ||
| 1005 | if (rc != MP_OK) { | ||
| 1006 | mp_plan_free (out); | ||
| 1007 | return rc; | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | if (out->segment_count == 0u) | ||
| 1011 | return MP_OK; | ||
| 1012 | |||
| 1013 | rc = mp_build_nodes (out, limits); | ||
| 1014 | if (rc != MP_OK) { | ||
| 1015 | mp_plan_free (out); | ||
| 1016 | return rc; | ||
| 1017 | } | ||
| 1018 | |||
| 1019 | mp_apply_speed_lookahead (out, limits); | ||
| 1020 | mp_mark_knots (out, limits); | ||
| 1021 | |||
| 1022 | rc = mp_build_global_phases (out, limits); | ||
| 1023 | if (rc != MP_OK) { | ||
| 1024 | mp_plan_free (out); | ||
| 1025 | return rc; | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | return MP_OK; | ||
| 1029 | } | ||
| 1030 | |||
| 1031 | void | ||
| 1032 | mp_plan_eval_scalar (const struct motion_plan *plan, double t, | ||
| 1033 | double *s_out, double *v_out, | ||
| 1034 | double *a_out, double *j_out) | ||
| 1035 | { | ||
| 1036 | if (!plan || plan->phase_count == 0u) { | ||
| 1037 | if (s_out) *s_out = 0.0; | ||
| 1038 | if (v_out) *v_out = 0.0; | ||
| 1039 | if (a_out) *a_out = 0.0; | ||
| 1040 | if (j_out) *j_out = 0.0; | ||
| 1041 | return; | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | if (t <= 0.0) { | ||
| 1045 | const struct mp_phase *ph = &plan->phases[0]; | ||
| 1046 | if (s_out) *s_out = ph->s_begin; | ||
| 1047 | if (v_out) *v_out = ph->v0; | ||
| 1048 | if (a_out) *a_out = ph->a0; | ||
| 1049 | if (j_out) *j_out = ph->j; | ||
| 1050 | return; | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | if (t >= plan->total_time) { | ||
| 1054 | if (s_out) *s_out = plan->total_length; | ||
| 1055 | if (v_out) *v_out = 0.0; | ||
| 1056 | if (a_out) *a_out = 0.0; | ||
| 1057 | if (j_out) *j_out = 0.0; | ||
| 1058 | return; | ||
| 1059 | } | ||
| 1060 | |||
| 1061 | uint32_t lo = 0u; | ||
| 1062 | uint32_t hi = plan->phase_count; | ||
| 1063 | |||
| 1064 | while (lo + 1u < hi) { | ||
| 1065 | uint32_t mid = lo + (hi - lo)/2u; | ||
| 1066 | const struct mp_phase *ph = &plan->phases[mid]; | ||
| 1067 | |||
| 1068 | if (t < ph->t_begin + ph->dt) | ||
| 1069 | hi = mid; | ||
| 1070 | else | ||
| 1071 | lo = mid; | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | uint32_t idx = lo; | ||
| 1075 | while (idx + 1u < plan->phase_count && | ||
| 1076 | t >= plan->phases[idx].t_begin + plan->phases[idx].dt) | ||
| 1077 | ++idx; | ||
| 1078 | |||
| 1079 | const struct mp_phase *ph = &plan->phases[idx]; | ||
| 1080 | double tau = t - ph->t_begin; | ||
| 1081 | tau = mp_clamp (tau, 0.0, ph->dt); | ||
| 1082 | |||
| 1083 | double s = ph->s_begin + ph->v0*tau + 0.5*ph->a0*tau*tau + (ph->j*tau*tau*tau)/6.0; | ||
| 1084 | double v = ph->v0 + ph->a0*tau + 0.5*ph->j*tau*tau; | ||
| 1085 | double a = ph->a0 + ph->j*tau; | ||
| 1086 | |||
| 1087 | if (s_out) *s_out = mp_clamp (s, 0.0, plan->total_length); | ||
| 1088 | if (v_out) *v_out = fmax (0.0, v); | ||
| 1089 | if (a_out) *a_out = a; | ||
| 1090 | if (j_out) *j_out = ph->j; | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | static uint32_t | ||
| 1094 | mp_find_segment_by_s (const struct motion_plan *plan, double s) | ||
| 1095 | { | ||
| 1096 | if (plan->segment_count == 0u) | ||
| 1097 | return 0u; | ||
| 1098 | |||
| 1099 | if (s <= 0.0) | ||
| 1100 | return 0u; | ||
| 1101 | |||
| 1102 | if (s >= plan->total_length) | ||
| 1103 | return plan->segment_count - 1u; | ||
| 1104 | |||
| 1105 | uint32_t lo = 0u; | ||
| 1106 | uint32_t hi = plan->segment_count; | ||
| 1107 | |||
| 1108 | while (lo + 1u < hi) { | ||
| 1109 | uint32_t mid = lo + (hi - lo)/2u; | ||
| 1110 | |||
| 1111 | if (s < plan->segments[mid].s_end) | ||
| 1112 | hi = mid; | ||
| 1113 | else | ||
| 1114 | lo = mid; | ||
| 1115 | } | ||
| 1116 | |||
| 1117 | while (lo + 1u < plan->segment_count && s >= plan->segments[lo].s_end) | ||
| 1118 | ++lo; | ||
| 1119 | |||
| 1120 | return lo; | ||
| 1121 | } | ||
| 1122 | |||
| 1123 | void | ||
| 1124 | mp_plan_sample (const struct motion_plan *plan, double t, | ||
| 1125 | struct mp_sample *out) | ||
| 1126 | { | ||
| 1127 | if (!out) | ||
| 1128 | return; | ||
| 1129 | |||
| 1130 | memset (out, 0, sizeof *out); | ||
| 1131 | |||
| 1132 | if (!plan || plan->segment_count == 0u) { | ||
| 1133 | out->t = t; | ||
| 1134 | return; | ||
| 1135 | } | ||
| 1136 | |||
| 1137 | double s = 0.0; | ||
| 1138 | double v = 0.0; | ||
| 1139 | double a_t = 0.0; | ||
| 1140 | double j_t = 0.0; | ||
| 1141 | |||
| 1142 | mp_plan_eval_scalar (plan, t, &s, &v, &a_t, &j_t); | ||
| 1143 | |||
| 1144 | uint32_t si = mp_find_segment_by_s (plan, s); | ||
| 1145 | const struct mp_segment *g = &plan->segments[si]; | ||
| 1146 | double local_s = mp_clamp (s - g->s_begin, 0.0, g->length); | ||
| 1147 | |||
| 1148 | struct mp_vec2 p; | ||
| 1149 | struct mp_vec2 tangent; | ||
| 1150 | struct mp_vec2 normal; | ||
| 1151 | double kappa_geom; | ||
| 1152 | |||
| 1153 | mp_geom_eval_frame (g, local_s, &p, &tangent, &normal, &kappa_geom); | ||
| 1154 | |||
| 1155 | double kappa = mp_effective_kappa (plan, si, local_s, kappa_geom); | ||
| 1156 | double a_n = v*v*kappa; | ||
| 1157 | double a_abs = hypot (a_t, a_n); | ||
| 1158 | |||
| 1159 | struct mp_vec2 vel = mp_vec2_mul (tangent, v); | ||
| 1160 | struct mp_vec2 acc = mp_vec2_add (mp_vec2_mul (tangent, a_t), | ||
| 1161 | mp_vec2_mul (normal, a_n)); | ||
| 1162 | |||
| 1163 | double j_vec_t = j_t - v*v*v*kappa*kappa; | ||
| 1164 | double j_vec_n = 3.0*v*a_t*kappa; | ||
| 1165 | struct mp_vec2 jerk = mp_vec2_add (mp_vec2_mul (tangent, j_vec_t), | ||
| 1166 | mp_vec2_mul (normal, j_vec_n)); | ||
| 1167 | double j_abs = copysign (mp_vec2_len (jerk), j_t); | ||
| 1168 | |||
| 1169 | out->t = mp_clamp (t, 0.0, plan->total_time); | ||
| 1170 | out->s = s; | ||
| 1171 | out->p = p; | ||
| 1172 | out->tangent = tangent; | ||
| 1173 | out->normal = normal; | ||
| 1174 | out->kappa = kappa; | ||
| 1175 | out->kappa_geom = kappa_geom; | ||
| 1176 | out->v = v; | ||
| 1177 | out->a_t = a_t; | ||
| 1178 | out->j_t = j_t; | ||
| 1179 | out->a_n = a_n; | ||
| 1180 | out->a_abs = a_abs; | ||
| 1181 | out->j_abs = j_abs; | ||
| 1182 | out->vel = vel; | ||
| 1183 | out->acc = acc; | ||
| 1184 | out->jerk = jerk; | ||
| 1185 | out->segment_index = si; | ||
| 1186 | out->source_index = g->source_index; | ||
| 1187 | out->source_flags = g->flags; | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | void | ||
| 1191 | mp_plan_write_csv (const struct motion_plan *plan, | ||
| 1192 | double dt, | ||
| 1193 | FILE *out, | ||
| 1194 | int write_header) | ||
| 1195 | { | ||
| 1196 | if (!plan || !out || !(dt > 0.0) || !isfinite (dt)) | ||
| 1197 | return; | ||
| 1198 | |||
| 1199 | if (write_header) { | ||
| 1200 | fputs ("t,s,x,y,v,a_t,j_t,kappa,a_n,a_abs,j_abs," | ||
| 1201 | "vx,vy,ax,ay,jx,jy,tx,ty,nx,ny,segment_index,source_index,source_flags\n", | ||
| 1202 | out); | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | if (plan->segment_count == 0u) | ||
| 1206 | return; | ||
| 1207 | |||
| 1208 | double t = 0.0; | ||
| 1209 | |||
| 1210 | while (t < plan->total_time) { | ||
| 1211 | struct mp_sample smp; | ||
| 1212 | mp_plan_sample (plan, t, &smp); | ||
| 1213 | |||
| 1214 | fprintf (out, | ||
| 1215 | "%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.12f,%.9f,%.9f,%.9f," | ||
| 1216 | "%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%u,%u,%d\n", | ||
| 1217 | smp.t, smp.s, smp.p.x, smp.p.y, | ||
| 1218 | smp.v, smp.a_t, smp.j_t, smp.kappa, | ||
| 1219 | smp.a_n, smp.a_abs, smp.j_abs, | ||
| 1220 | smp.vel.x, smp.vel.y, | ||
| 1221 | smp.acc.x, smp.acc.y, | ||
| 1222 | smp.jerk.x, smp.jerk.y, | ||
| 1223 | smp.tangent.x, smp.tangent.y, | ||
| 1224 | smp.normal.x, smp.normal.y, | ||
| 1225 | smp.segment_index, smp.source_index, smp.source_flags); | ||
| 1226 | |||
| 1227 | t += dt; | ||
| 1228 | } | ||
| 1229 | |||
| 1230 | struct mp_sample last; | ||
| 1231 | mp_plan_sample (plan, plan->total_time, &last); | ||
| 1232 | |||
| 1233 | fprintf (out, | ||
| 1234 | "%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.12f,%.9f,%.9f,%.9f," | ||
| 1235 | "%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%.9f,%u,%u,%d\n", | ||
| 1236 | last.t, last.s, last.p.x, last.p.y, | ||
| 1237 | last.v, last.a_t, last.j_t, last.kappa, | ||
| 1238 | last.a_n, last.a_abs, last.j_abs, | ||
| 1239 | last.vel.x, last.vel.y, | ||
| 1240 | last.acc.x, last.acc.y, | ||
| 1241 | last.jerk.x, last.jerk.y, | ||
| 1242 | last.tangent.x, last.tangent.y, | ||
| 1243 | last.normal.x, last.normal.y, | ||
| 1244 | last.segment_index, last.source_index, last.source_flags); | ||
| 1245 | } | ||
