aboutsummaryrefslogtreecommitdiff
path: root/src/compensations.c
blob: 5d87c22a530acb4e5ed0fcceeb591448b152de63 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "compensations.h"
#include <math.h>
#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;
}