From cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Thu, 12 Nov 2026 01:28:13 +0300 Subject: publish --- src/motion_planner.h | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 src/motion_planner.h (limited to 'src/motion_planner.h') diff --git a/src/motion_planner.h b/src/motion_planner.h new file mode 100644 index 0000000..0073f79 --- /dev/null +++ b/src/motion_planner.h @@ -0,0 +1,159 @@ +#ifndef INCLUDED_MOTION_PLANNER_H +#define INCLUDED_MOTION_PLANNER_H + +#include +#include +#include "path.h" + +#define MP_EPS 1e-9 + +enum mp_status { + MP_OK = 0, + MP_ERR_INVALID_ARG = -1, + MP_ERR_LIMITS = -2, + MP_ERR_ALLOC = -3, + MP_ERR_GEOMETRY = -4, + MP_ERR_PROFILE = -5 +}; + +enum mp_geom_type { + MP_GEOM_LINE = 1, + MP_GEOM_ARC = 2 +}; + +enum mp_node_flags { + MP_NODE_START = 1u << 0, + MP_NODE_END = 1u << 1, + MP_NODE_GEOM_GAP = 1u << 2, + MP_NODE_STRAIGHT = 1u << 3, + MP_NODE_CORNER_LIMIT = 1u << 4, + MP_NODE_HARD_STOP = 1u << 5, + MP_NODE_SPEED_CHANGE = 1u << 6, + MP_NODE_KNOT = 1u << 7 +}; + +struct mp_vec2 { + double x; + double y; +}; + +struct mp_limits { + double vmax; // mm/s: предел для рабочих G1-перемещений + double rapid_vmax; // mm/s: предел для G0; если <= 0, используется vmax + double amax; // mm/s^2: скалярное тангенциальное ускорение + double jmax; // mm/s^3: скалярный jerk; должен быть > 0 + double corner_tol; // mm: виртуальный допуск скругления углов + double min_segment_len; // mm: сегменты короче порога игнорируются + double default_feed; // mm/s: feed для G1, если path_element.f <= 0 + + double straight_angle_tol; // rad + double stop_angle; // rad + double corner_speed_deadband; // относительный deadband, например 0.02 + + double curvature_blend_len; // mm +}; + +struct mp_segment { + enum mp_geom_type type; + uint32_t source_index; + int flags; + + struct mp_vec2 p0; + struct mp_vec2 p1; + + struct mp_vec2 c; // центр дуги + double r; // радиус дуги + double a0; // начальный угол дуги + double sweep; // signed sweep, rad + + double length; // mm + double s_begin; // global mm + double s_end; // global mm + + double feed; // mm/s: командная скорость до ограничений + double v_nominal; // mm/s: скорость участка после ограничений +}; + +struct mp_node { + double s; // global mm + double v_cap; // mm/s: геометрический/командный предел узла + double v; // mm/s: скорость после lookahead + double theta; // rad: угол между касательными + uint32_t flags; // enum mp_node_flags +}; + +struct mp_phase { + double t_begin; // s + double dt; // s + + double s_begin; // mm + double s_end; // mm + + double v0; // mm/s + double a0; // mm/s^2 + double j; // mm/s^3 +}; + +struct motion_plan { + struct mp_segment *segments; + uint32_t segment_count; + uint32_t segment_capacity; + + struct mp_node *nodes; + uint32_t node_count; + uint32_t node_capacity; + + struct mp_phase *phases; + uint32_t phase_count; + uint32_t phase_capacity; + + double total_length; + double total_time; + + double curvature_blend_len; // mm + + double v_min; + double v_max; + double a_min; + double a_max; + double j_min; + double j_max; +}; + +struct mp_sample { + double t; + double s; + + struct mp_vec2 p; + struct mp_vec2 tangent; + struct mp_vec2 normal; + + double kappa; + double kappa_geom; + + double v; + double a_t; + double j_t; + + double a_n; + double a_abs; + double j_abs; + + struct mp_vec2 vel; + struct mp_vec2 acc; + struct mp_vec2 jerk; + + uint32_t segment_index; + uint32_t source_index; + int source_flags; +}; + +void mp_plan_init (struct motion_plan *plan); +void mp_plan_free (struct motion_plan *plan); + +int mp_plan_build (const struct path *path, double initial_x, double initial_y, const struct mp_limits *limits, struct motion_plan *out); +void mp_plan_eval_scalar (const struct motion_plan *plan, double t, double *s_out, double *v_out, double *a_out, double *j_out); +void mp_plan_sample (const struct motion_plan *plan, double t, struct mp_sample *out); +void mp_plan_write_csv (const struct motion_plan *plan, double dt, FILE *out, int write_header); + +#endif // INCLUDED_MOTION_PLANNER_H -- cgit v1.3.1