aboutsummaryrefslogtreecommitdiff
path: root/src/rate.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 22:28:13 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 22:47:34 +0000
commitcb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch)
treec7d90c0abe9533c87d51b5196871846855efa1ba /src/rate.c
downloadthesis-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/rate.c')
-rw-r--r--src/rate.c134
1 files changed, 134 insertions, 0 deletions
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 @@
1#include "rate.h"
2#include <math.h>
3#include <float.h>
4#include <string.h>
5
6static int
7work_motion_is_working_pe (const struct path_element *pe)
8{
9 if (!pe)
10 return 0;
11
12 return ~pe->flags & PEF_TRAVERSING;
13}
14
15static double
16work_motion_clamp (double x, double lo, double hi)
17{
18 return x < lo ? lo : (x > hi ? hi : x);
19}
20
21/*
22 * Находит время t, в которое глобальная координата траектории равна s.
23 * Нужно, чтобы интегрировать строго по рабочим segment.s_begin..segment.s_end,
24 * а не смешивать рабочие и холостые участки одним временным шагом.
25 */
26static double
27work_motion_time_at_s (const struct motion_plan *plan, double s)
28{
29 if (!plan || !(plan->total_time > 0.0))
30 return 0.0;
31
32 if (s <= 0.0)
33 return 0.0;
34
35 if (s >= plan->total_length)
36 return plan->total_time;
37
38 double lo = 0.0;
39 double hi = plan->total_time;
40
41 for (int i = 0; i < 80; ++i) {
42 double mid = 0.5*(lo + hi);
43 double smid = 0.0;
44
45 mp_plan_eval_scalar (plan, mid, &smid, NULL, NULL, NULL);
46
47 if (smid < s)
48 lo = mid;
49 else
50 hi = mid;
51 }
52
53 return 0.5*(lo + hi);
54}
55
56/*
57 * dt -- шаг численного интегрирования в секундах.
58 * Для текущей визуализации можно использовать 0.001.
59 */
60struct work_motion_stats
61measure_work_motion_stats (const struct path *path,
62 const struct motion_plan *plan,
63 double dt)
64{
65 ASSERT (path);
66 ASSERT (plan);
67 ASSERT (plan->segment_count > 0);
68 ASSERT (plan->total_time > 0);
69 ASSERT (dt > 0);
70 ASSERT (isfinite (dt));
71
72 struct work_motion_stats out = {0};
73 out.dt = dt;
74
75 for (uint32_t si = 0; si < plan->segment_count; ++si) {
76 const struct mp_segment *seg = &plan->segments[si];
77
78 ASSERT (seg->source_index < path->count);
79
80 const struct path_element *pe = &path->items[seg->source_index];
81
82 if (!work_motion_is_working_pe (pe))
83 continue;
84
85 double t0 = work_motion_time_at_s (plan, seg->s_begin);
86 double t1 = work_motion_time_at_s (plan, seg->s_end);
87
88 t0 = work_motion_clamp (t0, 0, plan->total_time);
89 t1 = work_motion_clamp (t1, 0, plan->total_time);
90
91 if (!(t1 > t0))
92 continue;
93
94 out.active_segment_count += 1;
95 out.active_length += seg->length;
96
97 for (double t = t0; t < t1;) {
98 double h = fmin (dt, t1 - t);
99 double tm = t + h/2;
100
101 struct mp_sample sample;
102 mp_plan_sample (plan, tm, &sample);
103
104 /*
105 * Берём v_nominal именно у текущего рабочего сегмента.
106 * Это устойчивее, чем брать sample.segment_index на границе сегментов.
107 */
108 double v_ref = seg->v_nominal;
109 double e = sample.v - v_ref;
110 // double e = (sample.v - v_ref)/v_ref;
111 double e_abs = fabs (e);
112
113 out.speed_error_abs_integral += e_abs*h;
114 out.speed_error_sq_integral += e*e*h;
115 out.speed_error_max_abs = fmax (out.speed_error_max_abs, e_abs);
116
117 out.jerk_abs_integral += fabs (sample.j_abs)*h;
118 out.jerk_abs_max = fmax (out.jerk_abs_max, fabs (sample.j_abs));
119
120 out.active_time += h;
121 out.sample_count += 1;
122
123 t += h;
124 }
125 }
126
127 if (out.active_time > 0) {
128 out.speed_error_mean_abs = out.speed_error_abs_integral/out.active_time;
129 out.speed_error_rms = sqrt (out.speed_error_sq_integral/out.active_time);
130 out.jerk_abs_mean = out.jerk_abs_integral/out.active_time;
131 }
132
133 return out;
134}