1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
#include "rate.h"
#include <math.h>
#include <float.h>
#include <string.h>
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;
}
|