diff options
| author | 2026-11-11 22:28:13 +0000 | |
|---|---|---|
| committer | 2026-11-11 22:47:34 +0000 | |
| commit | cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch) | |
| tree | c7d90c0abe9533c87d51b5196871846855efa1ba /src/simulate.c | |
| download | thesis-cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f.tar.gz | |
Diffstat (limited to 'src/simulate.c')
| -rw-r--r-- | src/simulate.c | 646 |
1 files changed, 646 insertions, 0 deletions
diff --git a/src/simulate.c b/src/simulate.c new file mode 100644 index 0000000..e053599 --- /dev/null +++ b/src/simulate.c | |||
| @@ -0,0 +1,646 @@ | |||
| 1 | /// src/simulate.h | ||
| 2 | // | ||
| 3 | // Copyright (C) 2026 by Martin Michalec <martin@michalec.dev> | ||
| 4 | // | ||
| 5 | // SPDX-License-Identifier: MIT | ||
| 6 | // | ||
| 7 | /// Commentary: | ||
| 8 | // | ||
| 9 | /// Code: | ||
| 10 | |||
| 11 | #include <stdlib.h> | ||
| 12 | #include <stdio.h> | ||
| 13 | #include <fcntl.h> | ||
| 14 | #include <unistd.h> | ||
| 15 | |||
| 16 | #include <raylib.h> | ||
| 17 | #include <raymath.h> | ||
| 18 | #include <rlgl.h> | ||
| 19 | |||
| 20 | #include "utils.h" | ||
| 21 | #include "da.h" | ||
| 22 | #include "rs274ngc_lexer.h" | ||
| 23 | #include "rs274ngc_cst.h" | ||
| 24 | #include "path.h" | ||
| 25 | #include "motion_planner.h" | ||
| 26 | #include "critical_points.h" | ||
| 27 | #include "compensations.h" | ||
| 28 | #include "rate.h" | ||
| 29 | |||
| 30 | #define LINE_THICKNESS 4 | ||
| 31 | #define TITLE "RS274NGC Simulator" | ||
| 32 | |||
| 33 | #define DPU 8 // dots per unit | ||
| 34 | |||
| 35 | struct point { | ||
| 36 | Vector2 c; | ||
| 37 | Vector2 n; | ||
| 38 | }; | ||
| 39 | |||
| 40 | struct points { | ||
| 41 | struct point *items; | ||
| 42 | uint32_t count; | ||
| 43 | uint32_t capacity; | ||
| 44 | } points = {0}; | ||
| 45 | |||
| 46 | struct rgb32 { | ||
| 47 | uint8_t r, g, b, a; | ||
| 48 | }; | ||
| 49 | |||
| 50 | struct overlay { | ||
| 51 | const char *name; | ||
| 52 | struct rgb32 *items; | ||
| 53 | uint32_t count; | ||
| 54 | uint32_t capacity; | ||
| 55 | }; | ||
| 56 | |||
| 57 | struct overlays { | ||
| 58 | struct overlay *items; | ||
| 59 | uint32_t count; | ||
| 60 | uint32_t capacity; | ||
| 61 | } overlays = {0}; | ||
| 62 | |||
| 63 | struct path path = {0}; | ||
| 64 | struct critical_points cps = {0}; | ||
| 65 | struct compensations comps = {0}; | ||
| 66 | |||
| 67 | struct rgb32 | ||
| 68 | lerp_rgb32 (struct rgb32 a, struct rgb32 b, float t) | ||
| 69 | { | ||
| 70 | struct rgb32 c; | ||
| 71 | c.r = (uint8_t) (a.r + (b.r - a.r)*t); | ||
| 72 | c.g = (uint8_t) (a.g + (b.g - a.g)*t); | ||
| 73 | c.b = (uint8_t) (a.b + (b.b - a.b)*t); | ||
| 74 | c.a = (uint8_t) (a.a + (b.a - a.a)*t); | ||
| 75 | return c; | ||
| 76 | } | ||
| 77 | |||
| 78 | void | ||
| 79 | print_arena (const struct arena *a) | ||
| 80 | { | ||
| 81 | for (struct arena_chunk *ch = a->head; ch; ch = ch->next) | ||
| 82 | printf ("%p %lu\n", ch, (void *) ch - ch->begin); | ||
| 83 | } | ||
| 84 | |||
| 85 | void | ||
| 86 | print_path (struct path path) | ||
| 87 | { | ||
| 88 | DA_FOR_EACH (pe, path) { | ||
| 89 | printf ("%04b | %10.4lf | %10.4lf | %10.4lf | %10.4lf | %10.4lf | %.*s\n", | ||
| 90 | pe->flags, pe->x, pe->y, pe->f, pe->i, pe->j, SV_FMTA (pe->line)); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | void | ||
| 95 | draw_path (struct path path) | ||
| 96 | { | ||
| 97 | SetConfigFlags (FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT); | ||
| 98 | InitWindow (700, 700, TITLE); | ||
| 99 | Camera2D camera = { | ||
| 100 | .zoom = 8, | ||
| 101 | }; | ||
| 102 | |||
| 103 | int first = 1; | ||
| 104 | while (!WindowShouldClose ()) { | ||
| 105 | float line_thickness = LINE_THICKNESS/camera.zoom; | ||
| 106 | |||
| 107 | Vector2 cursor = Vector2Zero (); | ||
| 108 | if (IsMouseButtonDown (MOUSE_BUTTON_LEFT) || | ||
| 109 | IsMouseButtonDown (MOUSE_BUTTON_RIGHT)) { | ||
| 110 | camera.target = Vector2Add (camera.target, Vector2Scale (GetMouseDelta (), -1/camera.zoom)); | ||
| 111 | } | ||
| 112 | |||
| 113 | float wheel_move = -GetMouseWheelMove (); | ||
| 114 | if (wheel_move) { | ||
| 115 | Vector2 offset = GetMousePosition (); | ||
| 116 | camera.target = GetScreenToWorld2D (offset, camera); | ||
| 117 | camera.offset = offset; | ||
| 118 | camera.zoom = Clamp (expf (logf (camera.zoom) + .2*wheel_move), 4, 128); | ||
| 119 | } | ||
| 120 | |||
| 121 | BeginDrawing (); | ||
| 122 | ClearBackground (WHITE); | ||
| 123 | BeginMode2D (camera); | ||
| 124 | |||
| 125 | // DrawLine (-1000, 0, +1000, 0, GRAY); | ||
| 126 | // DrawLine (0, -1000, 0, +1000, GRAY); | ||
| 127 | rlPushMatrix (); | ||
| 128 | rlTranslatef (100, 0, 0); | ||
| 129 | rlRotatef (90, 1, 0, 0); | ||
| 130 | DrawGrid (200, 1); | ||
| 131 | rlPopMatrix(); | ||
| 132 | |||
| 133 | DA_FOR_EACH (pe, path) { | ||
| 134 | Vector2 from = cursor; | ||
| 135 | cursor = (Vector2) { pe->x, -pe->y }; | ||
| 136 | if (pe->flags & PEF_ARC) { | ||
| 137 | Vector2 center = { pe->i, -pe->j }; | ||
| 138 | float r = Vector2Distance (from, center); | ||
| 139 | |||
| 140 | Vector2 v0 = Vector2Subtract (from, center); | ||
| 141 | Vector2 v1 = Vector2Subtract (cursor, center); | ||
| 142 | float angle_beg = Vector2Angle ((Vector2) { 1, 0 }, v0)/PI*180; | ||
| 143 | float angle_end = Vector2Angle ((Vector2) { 1, 0 }, v1)/PI*180; | ||
| 144 | if (first) printf ("< %f %f | " SV_FMT "\n", angle_beg, angle_end, SV_FMTA (pe->line)); | ||
| 145 | if (pe->flags & PEF_CCW) { | ||
| 146 | float tmp = angle_beg; | ||
| 147 | angle_beg = angle_end; | ||
| 148 | angle_end = tmp; | ||
| 149 | } | ||
| 150 | if (angle_beg > angle_end) | ||
| 151 | angle_end += 360; | ||
| 152 | if (Vector2Equals (from, cursor)) { | ||
| 153 | angle_beg = 0; | ||
| 154 | angle_end = 360; | ||
| 155 | } | ||
| 156 | if (first) printf ("< %f %f\n\n", angle_beg, angle_end); | ||
| 157 | |||
| 158 | DrawCircleV (center, line_thickness/2, GREEN); | ||
| 159 | DrawRing (center, r - line_thickness/2, r + line_thickness/2, | ||
| 160 | angle_beg, angle_end, 32, BLUE); | ||
| 161 | } else { | ||
| 162 | if (pe->flags & PEF_TRAVERSING) { | ||
| 163 | // DrawLineV (from, cursor, ORANGE); | ||
| 164 | DrawCircleV (from, line_thickness/4, ORANGE); | ||
| 165 | DrawLineEx (from, cursor, line_thickness/2, ORANGE); | ||
| 166 | DrawCircleV (cursor, line_thickness/4, ORANGE); | ||
| 167 | } else { | ||
| 168 | // DrawLineV (from, cursor, BLUE); | ||
| 169 | DrawCircleV (from, line_thickness/2, BLUE); | ||
| 170 | DrawLineEx (from, cursor, line_thickness, BLUE); | ||
| 171 | DrawCircleV (cursor, line_thickness/2, BLUE); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | } | ||
| 175 | EndMode2D (); | ||
| 176 | EndDrawing (); | ||
| 177 | first = 0; | ||
| 178 | } | ||
| 179 | |||
| 180 | CloseWindow (); | ||
| 181 | } | ||
| 182 | |||
| 183 | void | ||
| 184 | draw_grid (Rectangle bb, float step) | ||
| 185 | { | ||
| 186 | rlBegin (RL_LINES); | ||
| 187 | int i = 0; | ||
| 188 | for (float y = bb.y; y <= bb.y + bb.height; y += step) { | ||
| 189 | if (i % 10 == 0) { | ||
| 190 | rlColor3f (.25, .25, .25); | ||
| 191 | rlColor3f (.25, .25, .25); | ||
| 192 | } else { | ||
| 193 | rlColor3f (.5, .5, .5); | ||
| 194 | rlColor3f (.5, .5, .5); | ||
| 195 | } i += 1; | ||
| 196 | |||
| 197 | rlVertex3f (bb.x, -y, 0); | ||
| 198 | rlVertex3f (bb.x + bb.width, -y, 0); | ||
| 199 | } | ||
| 200 | i = 0; | ||
| 201 | for (float x = bb.x; x <= bb.x + bb.width; x += step) { | ||
| 202 | if (i % 10 == 0) { | ||
| 203 | rlColor3f (.25, .25, .25); | ||
| 204 | rlColor3f (.25, .25, .25); | ||
| 205 | } else { | ||
| 206 | rlColor3f (.5, .5, .5); | ||
| 207 | rlColor3f (.5, .5, .5); | ||
| 208 | } i += 1; | ||
| 209 | |||
| 210 | rlVertex3f (x, -bb.y, 0); | ||
| 211 | rlVertex3f (x, -bb.y - bb.height, 0); | ||
| 212 | } | ||
| 213 | rlEnd (); | ||
| 214 | } | ||
| 215 | |||
| 216 | #define ARROW_LENGTH .5 | ||
| 217 | #define ARROW_ANGLE 10 | ||
| 218 | |||
| 219 | void | ||
| 220 | draw_arrow (Vector2 beg, Vector2 end, float thick, Color c) | ||
| 221 | { | ||
| 222 | Vector2 off = Vector2Scale (Vector2Normalize (Vector2Subtract (end, beg)), | ||
| 223 | ARROW_LENGTH); | ||
| 224 | Vector2 ahl = Vector2Add (end, Vector2Rotate (off, +ARROW_ANGLE)); | ||
| 225 | Vector2 ahr = Vector2Add (end, Vector2Rotate (off, -ARROW_ANGLE)); | ||
| 226 | |||
| 227 | DrawLineEx (beg, end, thick, c); | ||
| 228 | DrawLineEx (end, ahl, thick, c); | ||
| 229 | DrawLineEx (end, ahr, thick, c); | ||
| 230 | |||
| 231 | DrawCircleV (beg, thick/2., c); | ||
| 232 | DrawCircleV (end, thick/2., c); | ||
| 233 | DrawCircleV (ahl, thick/2., c); | ||
| 234 | DrawCircleV (ahr, thick/2., c); | ||
| 235 | } | ||
| 236 | |||
| 237 | void | ||
| 238 | visualize (void) | ||
| 239 | { | ||
| 240 | uint32_t active_overlay = 0; | ||
| 241 | |||
| 242 | SetConfigFlags (FLAG_MSAA_4X_HINT | | ||
| 243 | FLAG_VSYNC_HINT | | ||
| 244 | FLAG_WINDOW_UNDECORATED | | ||
| 245 | FLAG_BORDERLESS_WINDOWED_MODE | | ||
| 246 | FLAG_WINDOW_RESIZABLE | | ||
| 247 | 0); | ||
| 248 | float s = 1.25; | ||
| 249 | // InitWindow (700*s, 700, TextFormat (TITLE " - %s", overlays.items[active_overlay].name)); | ||
| 250 | InitWindow (750, 650, TextFormat (TITLE " - %s", overlays.items[active_overlay].name)); | ||
| 251 | SetTargetFPS (60); | ||
| 252 | rlDisableBackfaceCulling (); | ||
| 253 | |||
| 254 | Camera2D camera = { | ||
| 255 | // .zoom = 16, | ||
| 256 | .zoom = 7*s, | ||
| 257 | .offset = { | ||
| 258 | .x = 0, | ||
| 259 | .y = GetScreenHeight (), | ||
| 260 | }, | ||
| 261 | .target = { | ||
| 262 | .x = 25.85, | ||
| 263 | .y = -33.6, | ||
| 264 | }, | ||
| 265 | }; | ||
| 266 | |||
| 267 | int draw_grid_flag = 1; | ||
| 268 | int draw_fps_flag = 0; | ||
| 269 | int draw_cps_flag = 0; | ||
| 270 | int draw_comps_flag = 0; | ||
| 271 | // uint32_t from = 37120; | ||
| 272 | // uint32_t upto = 48080; | ||
| 273 | // uint32_t from = 17800; | ||
| 274 | // uint32_t upto = 24000; | ||
| 275 | uint32_t from = 0; | ||
| 276 | uint32_t upto = points.count; | ||
| 277 | while (!WindowShouldClose ()) { | ||
| 278 | if (IsKeyPressed (KEY_UP)) { | ||
| 279 | active_overlay = (active_overlay + 1) % overlays.count; | ||
| 280 | SetWindowTitle (TextFormat (TITLE " - %s", overlays.items[active_overlay].name)); | ||
| 281 | } | ||
| 282 | if (IsKeyPressed (KEY_DOWN)) { | ||
| 283 | active_overlay = overlays.count - 1 - (overlays.count - active_overlay) % overlays.count; | ||
| 284 | SetWindowTitle (TextFormat (TITLE " - %s", overlays.items[active_overlay].name)); | ||
| 285 | } | ||
| 286 | |||
| 287 | uint32_t speed = (300 + 700*IsKeyDown (KEY_LEFT_SHIFT))*GetFrameTime (); | ||
| 288 | // speed = 1; | ||
| 289 | if (IsKeyUp (KEY_LEFT_CONTROL)) { | ||
| 290 | if (IsKeyDown (KEY_RIGHT)) | ||
| 291 | upto = (upto + speed) % (points.count + 1); | ||
| 292 | if (IsKeyDown (KEY_LEFT)) | ||
| 293 | upto = upto <= speed ? from : upto - speed; | ||
| 294 | } else { | ||
| 295 | if (IsKeyDown (KEY_RIGHT)) | ||
| 296 | from = (from + speed) % (upto + 1); | ||
| 297 | if (IsKeyDown (KEY_LEFT)) | ||
| 298 | from = from <= speed ? points.count : from - speed; | ||
| 299 | } | ||
| 300 | |||
| 301 | if (IsKeyPressed (KEY_G)) | ||
| 302 | draw_grid_flag = !draw_grid_flag; | ||
| 303 | if (IsKeyPressed (KEY_F)) | ||
| 304 | draw_fps_flag = !draw_fps_flag; | ||
| 305 | if (IsKeyPressed (KEY_P)) | ||
| 306 | draw_cps_flag = !draw_cps_flag; | ||
| 307 | if (IsKeyPressed (KEY_C)) | ||
| 308 | draw_comps_flag = !draw_comps_flag; | ||
| 309 | |||
| 310 | float line_thickness = LINE_THICKNESS/camera.zoom; | ||
| 311 | |||
| 312 | Vector2 cursor = Vector2Zero (); | ||
| 313 | if (IsMouseButtonDown (MOUSE_BUTTON_LEFT) || | ||
| 314 | IsMouseButtonDown (MOUSE_BUTTON_RIGHT)) { | ||
| 315 | camera.target = | ||
| 316 | Vector2Add (camera.target, | ||
| 317 | Vector2Scale (GetMouseDelta (), -1/camera.zoom)); | ||
| 318 | } | ||
| 319 | |||
| 320 | float wheel_move = -GetMouseWheelMove (); | ||
| 321 | if (wheel_move) { | ||
| 322 | Vector2 offset = GetMousePosition (); | ||
| 323 | camera.target = GetScreenToWorld2D (offset, camera); | ||
| 324 | camera.offset = offset; | ||
| 325 | camera.zoom = Clamp (expf (logf (camera.zoom) + .2*wheel_move), 4, 128); | ||
| 326 | } | ||
| 327 | |||
| 328 | Rectangle screen = { | ||
| 329 | .width = GetScreenWidth (), | ||
| 330 | .height = GetScreenHeight (), | ||
| 331 | }; | ||
| 332 | |||
| 333 | BeginDrawing (); | ||
| 334 | ClearBackground (WHITE); | ||
| 335 | BeginMode2D (camera); | ||
| 336 | |||
| 337 | float grid_size = 200; | ||
| 338 | if (draw_grid_flag) { | ||
| 339 | // rlPushMatrix (); | ||
| 340 | // rlTranslatef (grid_size/2, 0, 0); | ||
| 341 | // rlRotatef (90, 1, 0, 0); | ||
| 342 | // DrawGrid (grid_size, 1); | ||
| 343 | // rlPopMatrix(); | ||
| 344 | draw_grid ((Rectangle) { 0, 0, 100, 80 }, 10); | ||
| 345 | } | ||
| 346 | |||
| 347 | struct overlay *overlay = &overlays.items[active_overlay]; | ||
| 348 | |||
| 349 | uint32_t i = from; | ||
| 350 | struct rgb32 pc = overlay->items[i]; | ||
| 351 | |||
| 352 | Vector2 c = points.items[i].c; | ||
| 353 | Vector2 n = Vector2Scale (points.items[i].n, LINE_THICKNESS/2/camera.zoom); | ||
| 354 | Vector2 pl = Vector2Add (c, n); | ||
| 355 | Vector2 pr = Vector2Subtract (c, n); | ||
| 356 | |||
| 357 | rlBegin (RL_TRIANGLES); | ||
| 358 | for (i = from + 1; i < upto; i += logf (128)/logf (camera.zoom)) { | ||
| 359 | Vector2 c = points.items[i].c; | ||
| 360 | Vector2 n = Vector2Scale (points.items[i].n, LINE_THICKNESS/2/camera.zoom); | ||
| 361 | Vector2 cl = Vector2Add (c, n); | ||
| 362 | Vector2 cr = Vector2Subtract (c, n); | ||
| 363 | |||
| 364 | struct rgb32 cc = overlay->items[i]; | ||
| 365 | |||
| 366 | rlColor4ub (pc.r, pc.g, pc.b, pc.a); rlVertex2f (pl.x, pl.y); | ||
| 367 | rlColor4ub (cc.r, cc.g, cc.b, cc.a); rlVertex2f (cl.x, cl.y); | ||
| 368 | rlColor4ub (pc.r, pc.g, pc.b, pc.a); rlVertex2f (pr.x, pr.y); | ||
| 369 | |||
| 370 | rlColor4ub (pc.r, pc.g, pc.b, pc.a); rlVertex2f (pr.x, pr.y); | ||
| 371 | rlColor4ub (cc.r, cc.g, cc.b, cc.a); rlVertex2f (cl.x, cl.y); | ||
| 372 | rlColor4ub (cc.r, cc.g, cc.b, cc.a); rlVertex2f (cr.x, cr.y); | ||
| 373 | |||
| 374 | pl = cl; | ||
| 375 | pr = cr; | ||
| 376 | pc = cc; | ||
| 377 | } | ||
| 378 | rlEnd (); | ||
| 379 | |||
| 380 | if (draw_comps_flag) { | ||
| 381 | for (uint32_t i = 0; i < comps.count; ++i) { | ||
| 382 | const struct compensation *c = &comps.items[i]; | ||
| 383 | const struct polyline *pl = &c->polyline; | ||
| 384 | Vector2 from = { c->initial_position.x, -c->initial_position.y }; | ||
| 385 | for (uint32_t j = 0; j < pl->count; ++j) { | ||
| 386 | struct vec2 tov = pl->items[j]; | ||
| 387 | Vector2 to = { tov.x, -tov.y }; | ||
| 388 | DrawLineEx (from, to, line_thickness/2, GREEN); | ||
| 389 | from = to; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | if (draw_cps_flag) { | ||
| 395 | for (uint32_t i = 0; i < cps.count; ++i) { | ||
| 396 | const struct critical_point *cp = &cps.items[i]; | ||
| 397 | Vector2 c = { cp->r.x, -cp->r.y }; | ||
| 398 | Vector2 in = { 1.5*cp->t_left .x, -1.5*cp->t_left .y }; | ||
| 399 | Vector2 out = { 1.5*cp->t_right.x, -1.5*cp->t_right.y }; | ||
| 400 | |||
| 401 | struct Color colors[] = { | ||
| 402 | [CPT_IN ] = RED, | ||
| 403 | [CPT_OUT ] = GREEN, | ||
| 404 | [CPT_LOOP] = BLACK, | ||
| 405 | [CPT_BEG ] = RED, | ||
| 406 | [CPT_END ] = GREEN, | ||
| 407 | }; | ||
| 408 | DrawCircleV (c, line_thickness, colors[cp->type]); | ||
| 409 | // DrawCircleV (c, 2*line_thickness, BLACK); | ||
| 410 | |||
| 411 | draw_arrow (c, Vector2Add (c, in ), line_thickness, GREEN); | ||
| 412 | draw_arrow (c, Vector2Add (c, out), line_thickness, RED); | ||
| 413 | } | ||
| 414 | } | ||
| 415 | |||
| 416 | EndMode2D (); | ||
| 417 | if (draw_fps_flag) | ||
| 418 | DrawFPS (2, 0); | ||
| 419 | // DrawText (TextFormat ("FROM: %u", from), 2, 0, 20, BLACK); | ||
| 420 | // DrawText (TextFormat ("UPTO: %u", upto), 2, 20, 20, BLACK); | ||
| 421 | // DrawText (TextFormat ("WIDTH: %d", GetScreenWidth ()), 2, 0*20, 20, BLACK); | ||
| 422 | // DrawText (TextFormat ("HEIGHT: %d", GetScreenHeight ()), 2, 1*20, 20, BLACK); | ||
| 423 | // DrawText (TextFormat ("ZOOM: %f", camera.zoom ), 2, 2*20, 20, BLACK); | ||
| 424 | // DrawText (TextFormat ("XOFF: %f", camera.offset.x ), 2, 3*20, 20, BLACK); | ||
| 425 | // DrawText (TextFormat ("YOFF: %f", camera.offset.y ), 2, 4*20, 20, BLACK); | ||
| 426 | // DrawText (TextFormat ("YTAR: %f", camera.target.x ), 2, 5*20, 20, BLACK); | ||
| 427 | // DrawText (TextFormat ("YTAR: %f", camera.target.y ), 2, 6*20, 20, BLACK); | ||
| 428 | EndDrawing (); | ||
| 429 | } | ||
| 430 | |||
| 431 | CloseWindow (); | ||
| 432 | } | ||
| 433 | |||
| 434 | struct overlay line_overlay = { .name = "line" }; | ||
| 435 | struct overlay type_overlay = { .name = "type" }; | ||
| 436 | struct overlay velocity_overlay = { .name = "velocity" }; | ||
| 437 | struct overlay velocity_error_overlay = { .name = "velocity error" }; | ||
| 438 | struct overlay velocity_error_alpha_overlay = { .name = "velocity error (alpha)" }; | ||
| 439 | struct overlay acceleration_overlay = { .name = "acceleration" }; | ||
| 440 | struct overlay acceleration_t_overlay = { .name = "tangential acceleration" }; | ||
| 441 | struct overlay acceleration_n_overlay = { .name = "normal acceleration" }; | ||
| 442 | struct overlay jerk_overlay = { .name = "jerk" }; | ||
| 443 | struct overlay jerk_alpha_overlay = { .name = "jerk (alpha)" }; | ||
| 444 | |||
| 445 | void | ||
| 446 | add_sample (const struct motion_plan *plan, | ||
| 447 | const struct mp_sample *sample) | ||
| 448 | { | ||
| 449 | struct point point = { | ||
| 450 | .c = { sample->p.x, -sample->p.y }, | ||
| 451 | .n = { sample->normal.x, -sample->normal.y }, | ||
| 452 | }; | ||
| 453 | if (sample->source_flags & PEF_TRAVERSING) { | ||
| 454 | point.n.x /= 2; | ||
| 455 | point.n.y /= 2; | ||
| 456 | } | ||
| 457 | DA_APPEND (points, point); | ||
| 458 | struct rgb32 red = { 0xff, 0x00, 0x00, 0xff }; | ||
| 459 | struct rgb32 blue = { 0x00, 0x00, 0xff, 0xff }; | ||
| 460 | struct rgb32 green = { 0x00, 0xff, 0x00, 0xff }; | ||
| 461 | struct rgb32 orange = { 0xff, 0x7f, 0x00, 0xff }; | ||
| 462 | struct rgb32 blank = { 0x00, 0x00, 0x00, 0x00 }; | ||
| 463 | if (sample->source_flags & PEF_TRAVERSING) { | ||
| 464 | DA_APPEND ( velocity_overlay, orange); | ||
| 465 | DA_APPEND (velocity_error_overlay, orange); | ||
| 466 | DA_APPEND (velocity_error_alpha_overlay, blank); | ||
| 467 | DA_APPEND (jerk_alpha_overlay, blank); | ||
| 468 | DA_APPEND ( acceleration_t_overlay, orange); | ||
| 469 | DA_APPEND ( acceleration_n_overlay, orange); | ||
| 470 | DA_APPEND ( jerk_overlay, orange); | ||
| 471 | } | ||
| 472 | else { | ||
| 473 | struct rgb32 black_red = { 0xff, 0x00, 0x00, 0x00 }; | ||
| 474 | double v_nominal = plan->segments[sample->segment_index].v_nominal; | ||
| 475 | double error = fabs (sample->v - v_nominal); | ||
| 476 | float t = Normalize (error, plan->v_min, v_nominal); | ||
| 477 | DA_APPEND (velocity_error_overlay, lerp_rgb32 (green, red, t)); | ||
| 478 | DA_APPEND (velocity_error_alpha_overlay, lerp_rgb32 (black_red, red, t)); | ||
| 479 | struct rgb32 jc = lerp_rgb32 (red, blue, Normalize (sample->j_abs, plan->j_min, plan->j_max)); | ||
| 480 | jc.a = 0xff*Clamp (Normalize (sample->j_abs, 0, sample->j_abs > 0 ? plan->j_max : plan->j_min), 0, 1); | ||
| 481 | DA_APPEND (jerk_alpha_overlay, jc); | ||
| 482 | } | ||
| 483 | |||
| 484 | DA_APPEND ( velocity_overlay, lerp_rgb32 (red, blue, Normalize (sample->v, plan->v_min, plan->v_max))); | ||
| 485 | DA_APPEND (acceleration_t_overlay, lerp_rgb32 (red, blue, Normalize (sample->a_t, plan->a_min, plan->a_max))); | ||
| 486 | DA_APPEND (acceleration_n_overlay, lerp_rgb32 (red, blue, Normalize (sample->a_n, plan->a_min, plan->a_max))); | ||
| 487 | DA_APPEND ( jerk_overlay, lerp_rgb32 (red, blue, Normalize (sample->j_abs, plan->j_min, plan->j_max))); | ||
| 488 | |||
| 489 | struct rgb32 colors[] = { | ||
| 490 | { 0xff, 0x00, 0x00, 0xff }, | ||
| 491 | { 0x00, 0xff, 0xff, 0xff }, | ||
| 492 | { 0x00, 0xff, 0x00, 0xff }, | ||
| 493 | { 0xff, 0x00, 0xff, 0xff }, | ||
| 494 | { 0x00, 0x00, 0xff, 0xff }, | ||
| 495 | { 0xff, 0xff, 0x00, 0xff }, | ||
| 496 | }; | ||
| 497 | DA_APPEND (line_overlay, colors[sample->source_index % ARRAY_LENGTH (colors)]); | ||
| 498 | |||
| 499 | if (sample->source_flags & PEF_ARC) { | ||
| 500 | if (sample->source_flags & PEF_CCW) | ||
| 501 | DA_APPEND (type_overlay, red); | ||
| 502 | else | ||
| 503 | DA_APPEND (type_overlay, green); | ||
| 504 | } else { | ||
| 505 | if (sample->source_flags & PEF_TRAVERSING) | ||
| 506 | DA_APPEND (type_overlay, orange); | ||
| 507 | else | ||
| 508 | DA_APPEND (type_overlay, blue); | ||
| 509 | } | ||
| 510 | } | ||
| 511 | |||
| 512 | void | ||
| 513 | build_overlays (const struct motion_plan *plan) | ||
| 514 | { | ||
| 515 | double dt = .001; | ||
| 516 | |||
| 517 | struct mp_sample smp; | ||
| 518 | for (double t = 0; t < plan->total_time; t += dt) { | ||
| 519 | mp_plan_sample (plan, t, &smp); | ||
| 520 | add_sample (plan, &smp); | ||
| 521 | } mp_plan_sample (plan, plan->total_time, &smp); | ||
| 522 | add_sample (plan, &smp); | ||
| 523 | |||
| 524 | DA_APPEND (overlays, line_overlay); | ||
| 525 | DA_APPEND (overlays, type_overlay); | ||
| 526 | DA_APPEND (overlays, velocity_overlay); | ||
| 527 | DA_APPEND (overlays, velocity_error_overlay); | ||
| 528 | DA_APPEND (overlays, velocity_error_alpha_overlay); | ||
| 529 | DA_APPEND (overlays, acceleration_t_overlay); | ||
| 530 | DA_APPEND (overlays, acceleration_n_overlay); | ||
| 531 | DA_APPEND (overlays, jerk_overlay); | ||
| 532 | DA_APPEND (overlays, jerk_alpha_overlay); | ||
| 533 | } | ||
| 534 | |||
| 535 | int | ||
| 536 | main (int argc, char *argv[]) | ||
| 537 | { | ||
| 538 | progname = argv[0]; | ||
| 539 | |||
| 540 | const char *filename; | ||
| 541 | int fd = 0; | ||
| 542 | if (argc == 1) { | ||
| 543 | filename = "-"; | ||
| 544 | } else if (argc == 2) { | ||
| 545 | filename = argv[1]; | ||
| 546 | fd = open (filename, O_RDONLY); | ||
| 547 | } else { | ||
| 548 | DIE ("too many arguments provided"); | ||
| 549 | } | ||
| 550 | |||
| 551 | struct arena a = {0}; | ||
| 552 | struct arena t = {0}; | ||
| 553 | |||
| 554 | struct sv rs274ngc = read_until_eof (&a, fd); | ||
| 555 | if (fd) close (fd); | ||
| 556 | |||
| 557 | rs274ngc_lexer_compile_regexes (); | ||
| 558 | struct rs274ngc_lexer lexer = rs274ngc_lexer_from_sv (rs274ngc, filename); | ||
| 559 | |||
| 560 | struct rs274ngc_cst_parser parser = { | ||
| 561 | .lexer = &lexer, | ||
| 562 | .output = &a, | ||
| 563 | .temporary = &t, | ||
| 564 | }; | ||
| 565 | |||
| 566 | path = build_path (parser); | ||
| 567 | ASSERT (path.count > 0); | ||
| 568 | // print_path (path); | ||
| 569 | // draw_path (path); | ||
| 570 | // return 0; | ||
| 571 | |||
| 572 | double initial_x = 0; | ||
| 573 | double initial_y = 0; | ||
| 574 | |||
| 575 | struct mp_limits limits = { | ||
| 576 | .vmax = 80, | ||
| 577 | .rapid_vmax = 100, | ||
| 578 | .amax = 800, | ||
| 579 | .jmax = 16000, | ||
| 580 | .corner_tol = .05, | ||
| 581 | // .corner_tol = 1, | ||
| 582 | .min_segment_len = 1e-6, | ||
| 583 | .default_feed = 50, | ||
| 584 | |||
| 585 | .straight_angle_tol = 0.02, | ||
| 586 | .stop_angle = M_PI/8, | ||
| 587 | .corner_speed_deadband = .002, | ||
| 588 | |||
| 589 | .curvature_blend_len = 0.05, | ||
| 590 | }; | ||
| 591 | |||
| 592 | cps = find_critical_points (&path); | ||
| 593 | uint32_t cpcs[5] = {0}; | ||
| 594 | for (uint32_t i = 0; i < cps.count; ++i) { | ||
| 595 | const struct critical_point *cp = &cps.items[i]; | ||
| 596 | cpcs[cp->type] += 1; | ||
| 597 | } | ||
| 598 | printf ("CPT_IN: %u\n", cpcs[CPT_IN]), | ||
| 599 | printf ("CPT_OUT: %u\n", cpcs[CPT_OUT]), | ||
| 600 | printf ("CPT_LOOP: %u\n", cpcs[CPT_LOOP]), | ||
| 601 | printf ("CPT_BEG: %u\n", cpcs[CPT_BEG]), | ||
| 602 | printf ("CPT_END: %u\n", cpcs[CPT_END]), | ||
| 603 | printf ("CPT_TOTAL: %u\n", cpcs[0] + cpcs[1] + cpcs[2] + cpcs[3] + cpcs[4]), | ||
| 604 | |||
| 605 | comps = synthesize_compensations (&cps); | ||
| 606 | |||
| 607 | struct motion_plan plan = {0}; | ||
| 608 | |||
| 609 | ASSERT (path.count > 0); | ||
| 610 | int rc = mp_plan_build (&path, initial_x, initial_y, &limits, &plan); | ||
| 611 | if (rc) { | ||
| 612 | printf ("%d\n", rc); | ||
| 613 | return 1; | ||
| 614 | } | ||
| 615 | // FILE *csv = fopen ("motion-plan.csv", "w"); | ||
| 616 | // mp_plan_write_csv (&plan, 0.001, csv, 1); | ||
| 617 | // fclose (csv); | ||
| 618 | // mp_plan_free(&plan); | ||
| 619 | |||
| 620 | printf ("total_length: %lg\n", plan.total_length); | ||
| 621 | printf ("total_time: %lg\n", plan.total_time); | ||
| 622 | printf ("v_min: %lg\n", plan.v_min); | ||
| 623 | printf ("v_max: %lg\n", plan.v_max); | ||
| 624 | printf ("a_min: %lg\n", plan.a_min); | ||
| 625 | printf ("a_max: %lg\n", plan.a_max); | ||
| 626 | printf ("j_min: %lg\n", plan.j_min); | ||
| 627 | printf ("j_max: %lg\n", plan.j_max); | ||
| 628 | |||
| 629 | struct work_motion_stats stats = measure_work_motion_stats (&path, &plan, 0.001); | ||
| 630 | |||
| 631 | printf ("work_active_time: %lg s\n", stats.active_time); | ||
| 632 | printf ("work_active_length: %lg mm\n", stats.active_length); | ||
| 633 | printf ("work_speed_error_max_abs: %lg mm/s\n", stats.speed_error_max_abs); | ||
| 634 | printf ("work_speed_error_mean_abs: %lg mm/s\n", stats.speed_error_mean_abs); | ||
| 635 | printf ("work_speed_error_rms: %lg mm/s\n", stats.speed_error_rms); | ||
| 636 | printf ("work_jerk_abs_integral: %lg mm/s^2\n", stats.jerk_abs_integral); | ||
| 637 | printf ("work_jerk_abs_mean: %lg mm/s^3\n", stats.jerk_abs_mean); | ||
| 638 | printf ("work_jerk_abs_max: %lg mm/s^3\n", stats.jerk_abs_max); | ||
| 639 | |||
| 640 | build_overlays (&plan); | ||
| 641 | visualize (); | ||
| 642 | |||
| 643 | return EXIT_SUCCESS; | ||
| 644 | } | ||
| 645 | |||
| 646 | /// src/simulate.h ends here | ||
