From cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Thu, 12 Nov 2026 01:28:13 +0300 Subject: publish --- src/rate.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/rate.c (limited to 'src/rate.c') diff --git a/src/rate.c b/src/rate.c new file mode 100644 index 0000000..33def16 --- /dev/null +++ b/src/rate.c @@ -0,0 +1,134 @@ +#include "rate.h" +#include +#include +#include + +static int +work_motion_is_working_pe (const struct path_element *pe) +{ + if (!pe) + return 0; + + return ~pe->flags & PEF_TRAVERSING; +} + +static double +work_motion_clamp (double x, double lo, double hi) +{ + return x < lo ? lo : (x > hi ? hi : x); +} + +/* + * Находит время t, в которое глобальная координата траектории равна s. + * Нужно, чтобы интегрировать строго по рабочим segment.s_begin..segment.s_end, + * а не смешивать рабочие и холостые участки одним временным шагом. + */ +static double +work_motion_time_at_s (const struct motion_plan *plan, double s) +{ + if (!plan || !(plan->total_time > 0.0)) + return 0.0; + + if (s <= 0.0) + return 0.0; + + if (s >= plan->total_length) + return plan->total_time; + + double lo = 0.0; + double hi = plan->total_time; + + for (int i = 0; i < 80; ++i) { + double mid = 0.5*(lo + hi); + double smid = 0.0; + + mp_plan_eval_scalar (plan, mid, &smid, NULL, NULL, NULL); + + if (smid < s) + lo = mid; + else + hi = mid; + } + + return 0.5*(lo + hi); +} + +/* + * dt -- шаг численного интегрирования в секундах. + * Для текущей визуализации можно использовать 0.001. + */ +struct work_motion_stats +measure_work_motion_stats (const struct path *path, + const struct motion_plan *plan, + double dt) +{ + ASSERT (path); + ASSERT (plan); + ASSERT (plan->segment_count > 0); + ASSERT (plan->total_time > 0); + ASSERT (dt > 0); + ASSERT (isfinite (dt)); + + struct work_motion_stats out = {0}; + out.dt = dt; + + for (uint32_t si = 0; si < plan->segment_count; ++si) { + const struct mp_segment *seg = &plan->segments[si]; + + ASSERT (seg->source_index < path->count); + + const struct path_element *pe = &path->items[seg->source_index]; + + if (!work_motion_is_working_pe (pe)) + continue; + + double t0 = work_motion_time_at_s (plan, seg->s_begin); + double t1 = work_motion_time_at_s (plan, seg->s_end); + + t0 = work_motion_clamp (t0, 0, plan->total_time); + t1 = work_motion_clamp (t1, 0, plan->total_time); + + if (!(t1 > t0)) + continue; + + out.active_segment_count += 1; + out.active_length += seg->length; + + for (double t = t0; t < t1;) { + double h = fmin (dt, t1 - t); + double tm = t + h/2; + + struct mp_sample sample; + mp_plan_sample (plan, tm, &sample); + + /* + * Берём v_nominal именно у текущего рабочего сегмента. + * Это устойчивее, чем брать sample.segment_index на границе сегментов. + */ + double v_ref = seg->v_nominal; + double e = sample.v - v_ref; + // double e = (sample.v - v_ref)/v_ref; + double e_abs = fabs (e); + + out.speed_error_abs_integral += e_abs*h; + out.speed_error_sq_integral += e*e*h; + out.speed_error_max_abs = fmax (out.speed_error_max_abs, e_abs); + + out.jerk_abs_integral += fabs (sample.j_abs)*h; + out.jerk_abs_max = fmax (out.jerk_abs_max, fabs (sample.j_abs)); + + out.active_time += h; + out.sample_count += 1; + + t += h; + } + } + + if (out.active_time > 0) { + out.speed_error_mean_abs = out.speed_error_abs_integral/out.active_time; + out.speed_error_rms = sqrt (out.speed_error_sq_integral/out.active_time); + out.jerk_abs_mean = out.jerk_abs_integral/out.active_time; + } + + return out; +} -- cgit v1.3.1