aboutsummaryrefslogtreecommitdiff
path: root/src/transition.h
blob: 70b3ae9f858b4b960f45c7cfacae6411d53e7dd0 (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
#ifndef INCLUDED_TRANSITION_V3_SPARAM_H
#define INCLUDED_TRANSITION_V3_SPARAM_H

#include <stdint.h>
#include "vec2.h"

/*
 * Time-optimal jerk-limited transition for independent X/Y axes.
 *
 * The planner treats X and Y as independent 1D axes with identical symmetric
 * limits for both directions:
 *   |v_axis| <= vmax, |a_axis| <= amax, |j_axis| <= jmax.
 *
 * Boundary acceleration is fixed to zero on both axes.  Jerk is a bounded
 * control, not a state; tr_sample() reports j=0 outside the motion interval,
 * but the time-optimal profile may jump from 0 to +/-jmax at t=0 and jump back
 * to 0 at t=T.  If continuous jerk is required, the problem is snap-limited,
 * not jerk-limited.
 *
 * The transition can be sampled either by time t or by travelled arclength s.
 * Arclength is computed as
 *   s(t) = integral_0^t hypot(vx(tau), vy(tau)) d tau.
 *
 * For fast repeated sampling by s, tr_transition stores an internal monotone
 * lookup table parameterized by arclength.  The dynamic profile is still stored
 * as time-polynomial axis phases; the s-table only maps s -> t.
 */

#define TR_S_TABLE_CAP 2049u

#define TR_S_PARAM_TABLE_UNIFORM_BY_LENGTH 1

enum tr_status {
  TR_OK = 0,
  TR_ERR_INVALID_ARG = -1,
  TR_ERR_LIMITS      = -2,
  TR_ERR_INFEASIBLE  = -3,
  TR_ERR_NUMERIC     = -4,
  TR_ERR_NO_PROFILE  = -5
};

struct tr_axis_phase {
  double t_begin;
  double dt;
  double q0;
  double v0;
  double a0;
  double j;
};

struct tr_axis_plan {
  struct tr_axis_phase phases[16];
  uint32_t count;
  double duration;
  double q0, q1;
  double v0, v1;
};

struct tr_s_node {
  double s;
  double t;
};

struct tr_transition {
  struct tr_axis_plan x;
  struct tr_axis_plan y;
  double duration;
  double length;

  /* Uniform arclength parameterization: s_nodes[i].s is approximately
   * i*length/(s_node_count-1), and s_nodes[i].t is the corresponding time.
   */
  uint32_t s_node_count;
  double s_step;
  struct tr_s_node s_nodes[TR_S_TABLE_CAP];

  double vmax, amax, jmax;
};

struct tr_sample {
  double t;
  double s;
  struct vec2 r;
  struct vec2 v;
  struct vec2 a;
  struct vec2 j;
};

int tr_build (struct tr_transition *out,
              struct vec2 r0,
              struct vec2 r1,
              struct vec2 v0,
              struct vec2 v1,
              double vmax,
              double amax,
              double jmax);

void tr_sample (const struct tr_transition *tr, double t, struct tr_sample *out);
void tr_sample_at_s (const struct tr_transition *tr, double s, struct tr_sample *out);

double tr_length_at_time (const struct tr_transition *tr, double t);
double tr_time_at_length (const struct tr_transition *tr, double s);

const char *tr_status_string (int status);

#endif /* INCLUDED_TRANSITION_V3_SPARAM_H */