From cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Thu, 12 Nov 2026 01:28:13 +0300 Subject: publish --- src/compensations.c | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 src/compensations.c (limited to 'src/compensations.c') diff --git a/src/compensations.c b/src/compensations.c new file mode 100644 index 0000000..5d87c22 --- /dev/null +++ b/src/compensations.c @@ -0,0 +1,181 @@ +#include "compensations.h" +#include +#include "da.h" +#include "transition.h" + +static double +distance_to_change_speed (double v0, double v1, double vmax, double amax, double jmax) +{ + double dv = fabs (v1 - v0); + if (dv <= 1e-12) + return 0; + + double dv_crit = amax*amax/jmax; + double t = dv <= dv_crit ? 2*sqrt (dv/jmax) : dv/amax + amax/jmax; + + return t*(v0 + v1)/2; +} + +double vmax = 100/1.41421356237; +double amax = 800/1.41421356237; +double jmax = 16000/1.41421356237; + +static struct compensation +synthesize_compensation_loop (const struct critical_point *cp) +{ + struct compensation comp = { + .initial_position = cp->r, + .from = cp->left, + .to = cp->left, + .replace = CR_FROM_END_TO_END, + }; + + // struct tr_transition tr; + // tr_build (&tr, cp->r, cp->r, cp->v_left, cp->v_right, vmax, amax, jmax); + // for (double s = 0.1; s < tr.length; s += 0.1) { + // struct tr_sample sample; + // tr_sample_at_s (&tr, s, &sample); + // DA_APPEND (comp.polyline, sample.r); + // } + + double s0 = distance_to_change_speed (cp->left->f, 0, vmax, amax, jmax); + double s1 = distance_to_change_speed (0, cp->right->f, vmax, amax, jmax); + + struct vec2 out = vec2_add (cp->r, vec2_scale (cp->t_left , s0)); + struct vec2 in = vec2_sub (cp->r, vec2_scale (cp->t_right, s1)); + + DA_APPEND (comp.polyline, out); + DA_APPEND (comp.polyline, in ); + DA_APPEND (comp.polyline, cp->r); + + return comp; +} + +static struct compensation +synthesize_compensation_transfer (const struct critical_point *cp_out, + const struct critical_point *cp_in) +{ + struct compensation comp = { + .initial_position = cp_out->r, + .from = cp_out->right, + .to = cp_in ->left, + .replace = CR_FROM_BEG_TO_END, + }; + + // struct tr_transition tr; + // tr_build (&tr, cp_out->r, cp_in->r, cp_out->v_left, cp_in->v_right, vmax, amax, jmax); + // for (double s = 0.1; s < tr.length; s += 0.1) { + // struct tr_sample sample; + // tr_sample_at_s (&tr, s, &sample); + // DA_APPEND (comp.polyline, sample.r); + // } + + double s0 = distance_to_change_speed (cp_out->left->f, 0, vmax, amax, jmax); + double s1 = distance_to_change_speed (0, cp_in->right->f, vmax, amax, jmax); + + struct vec2 out = vec2_add (cp_out->r, vec2_scale (cp_out->t_left , s0)); + struct vec2 in = vec2_sub (cp_in ->r, vec2_scale (cp_in ->t_right, s1)); + + DA_APPEND (comp.polyline, out); + DA_APPEND (comp.polyline, in ); + DA_APPEND (comp.polyline, cp_in->r); + + return comp; +} + +static struct compensation +synthesize_compensation_in (const struct critical_point *cp) +{ + struct compensation comp = { + .initial_position = cp->s, + .from = cp->left, + .to = cp->left, + .replace = CR_FROM_BEG_TO_END, + }; + + double s = distance_to_change_speed (0, cp->right->f, vmax, amax, jmax); + struct vec2 in = vec2_sub (cp->r, vec2_scale (cp->t_right, s)); + + DA_APPEND (comp.polyline, in); + DA_APPEND (comp.polyline, cp->r); + + return comp; +} + +static struct compensation +synthesize_compensation_beg (const struct critical_point *cp) +{ + struct compensation comp = { + .initial_position = cp->r, + .from = cp->right, + .to = cp->right, + .replace = CR_FROM_BEG_TO_BEG, + }; + + double s = distance_to_change_speed (0, cp->right->f, vmax, amax, jmax); + struct vec2 in = vec2_sub (cp->r, vec2_scale (cp->t_right, s)); + + DA_APPEND (comp.polyline, in); + DA_APPEND (comp.polyline, cp->r); + + return comp; +} + +static struct compensation +synthesize_compensation_end (const struct critical_point *cp) +{ + struct compensation comp = { + .initial_position = cp->r, + .from = cp->left, + .to = cp->left, + .replace = CR_FROM_END_TO_END, + }; + + double s = distance_to_change_speed (cp->left->f, 0, vmax, amax, jmax); + struct vec2 out = vec2_add (cp->r, vec2_scale (cp->t_left, s)); + + DA_APPEND (comp.polyline, out); + + return comp; +} + +struct compensations +synthesize_compensations (const struct critical_points *cps) +{ + struct compensations comps = {0}; + + for (uint32_t i = 0; i < cps->count; ++i) { + struct critical_point *cp = &cps->items[i]; + struct compensation comp = {0}; + switch (cp->type) { + case CPT_IN: { + comp = synthesize_compensation_in (cp); + } break; + case CPT_OUT: { // TRANSFER + i += 1; + if (i == cps->count) { + comp = synthesize_compensation_end (cp); + } else { + struct critical_point *out = cp; + struct critical_point *in = &cps->items[i]; + ASSERT (in->type == CPT_IN); + comp = synthesize_compensation_transfer (out, in); + } + } break; + case CPT_LOOP: { + comp = synthesize_compensation_loop (cp); + } break; + case CPT_BEG: { + comp = synthesize_compensation_beg (cp); + } break; + case CPT_END: { + comp = synthesize_compensation_end (cp); + } break; + default: ASSERT (!"unexpected critical point type"); + } + + DA_APPEND (comps, comp); + } + + return comps; +} -- cgit v1.3.1