2018-06-09 01:55:24 +02:00
|
|
|
// Iterative solver for kinematic moves
|
|
|
|
//
|
2020-02-25 03:59:39 +01:00
|
|
|
// Copyright (C) 2018-2020 Kevin O'Connor <kevin@koconnor.net>
|
2018-06-09 01:55:24 +02:00
|
|
|
//
|
|
|
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
2019-10-27 18:23:03 +01:00
|
|
|
#include <math.h> // fabs
|
2019-10-28 02:05:57 +01:00
|
|
|
#include <stddef.h> // offsetof
|
2018-06-09 01:55:24 +02:00
|
|
|
#include <string.h> // memset
|
|
|
|
#include "compiler.h" // __visible
|
2019-10-29 17:23:19 +01:00
|
|
|
#include "itersolve.h" // itersolve_generate_steps
|
2018-06-09 01:55:24 +02:00
|
|
|
#include "pyhelper.h" // errorf
|
|
|
|
#include "stepcompress.h" // queue_append_start
|
2019-10-27 18:23:03 +01:00
|
|
|
#include "trapq.h" // struct move
|
2018-06-09 01:55:24 +02:00
|
|
|
|
2020-02-25 18:54:55 +01:00
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* Main iterative solver
|
|
|
|
****************************************************************/
|
|
|
|
|
2018-06-09 01:55:24 +02:00
|
|
|
struct timepos {
|
|
|
|
double time, position;
|
|
|
|
};
|
|
|
|
|
2020-08-08 01:36:55 +02:00
|
|
|
// Find step using "false position" method (with "Illinois algorithm")
|
2018-06-09 01:55:24 +02:00
|
|
|
static struct timepos
|
|
|
|
itersolve_find_step(struct stepper_kinematics *sk, struct move *m
|
|
|
|
, struct timepos low, struct timepos high
|
|
|
|
, double target)
|
|
|
|
{
|
2019-10-27 16:06:46 +01:00
|
|
|
sk_calc_callback calc_position_cb = sk->calc_position_cb;
|
2018-06-09 01:55:24 +02:00
|
|
|
struct timepos best_guess = high;
|
|
|
|
low.position -= target;
|
|
|
|
high.position -= target;
|
|
|
|
if (!high.position)
|
|
|
|
// The high range was a perfect guess for the next step
|
|
|
|
return best_guess;
|
|
|
|
int high_sign = signbit(high.position);
|
|
|
|
if (high_sign == signbit(low.position))
|
|
|
|
// The target is not in the low/high range - return low range
|
|
|
|
return (struct timepos){ low.time, target };
|
2020-08-08 01:36:55 +02:00
|
|
|
int prev_choice = 0;
|
2018-06-09 01:55:24 +02:00
|
|
|
for (;;) {
|
|
|
|
double guess_time = ((low.time*high.position - high.time*low.position)
|
|
|
|
/ (high.position - low.position));
|
|
|
|
best_guess.time = guess_time;
|
2019-10-27 16:06:46 +01:00
|
|
|
best_guess.position = calc_position_cb(sk, m, guess_time);
|
2018-06-09 01:55:24 +02:00
|
|
|
double guess_position = best_guess.position - target;
|
2020-08-08 17:39:31 +02:00
|
|
|
if (fabs(guess_position) <= .000000001)
|
|
|
|
break;
|
2018-06-09 01:55:24 +02:00
|
|
|
int guess_sign = signbit(guess_position);
|
|
|
|
if (guess_sign == high_sign) {
|
|
|
|
high.time = guess_time;
|
|
|
|
high.position = guess_position;
|
2020-08-08 01:36:55 +02:00
|
|
|
if (prev_choice > 0)
|
|
|
|
low.position *= .5;
|
|
|
|
prev_choice = 1;
|
2018-06-09 01:55:24 +02:00
|
|
|
} else {
|
|
|
|
low.time = guess_time;
|
|
|
|
low.position = guess_position;
|
2020-08-08 01:36:55 +02:00
|
|
|
if (prev_choice < 0)
|
|
|
|
high.position *= .5;
|
|
|
|
prev_choice = -1;
|
2018-06-09 01:55:24 +02:00
|
|
|
}
|
2020-08-08 17:39:31 +02:00
|
|
|
if (high.time - low.time <= .000000001)
|
|
|
|
break;
|
2018-06-09 01:55:24 +02:00
|
|
|
}
|
|
|
|
return best_guess;
|
|
|
|
}
|
|
|
|
|
2020-02-26 17:28:36 +01:00
|
|
|
#define SEEK_TIME_RESET 0.000100
|
|
|
|
|
2019-10-28 02:05:57 +01:00
|
|
|
// Generate step times for a portion of a move
|
|
|
|
static int32_t
|
|
|
|
itersolve_gen_steps_range(struct stepper_kinematics *sk, struct move *m
|
2020-05-04 16:24:46 +02:00
|
|
|
, double abs_start, double abs_end)
|
2018-06-09 01:55:24 +02:00
|
|
|
{
|
2019-10-27 16:06:46 +01:00
|
|
|
sk_calc_callback calc_position_cb = sk->calc_position_cb;
|
2018-06-09 01:55:24 +02:00
|
|
|
double half_step = .5 * sk->step_dist;
|
2020-05-04 16:24:46 +02:00
|
|
|
double start = abs_start - m->print_time, end = abs_end - m->print_time;
|
|
|
|
if (start < 0.)
|
|
|
|
start = 0.;
|
|
|
|
if (end > m->move_t)
|
|
|
|
end = m->move_t;
|
2019-10-28 02:05:57 +01:00
|
|
|
struct timepos last = { start, sk->commanded_pos }, low = last, high = last;
|
2020-02-26 17:28:36 +01:00
|
|
|
double seek_time_delta = SEEK_TIME_RESET;
|
2020-02-25 18:54:55 +01:00
|
|
|
int sdir = stepcompress_get_step_dir(sk->sc), is_dir_change = 0;
|
2018-06-09 01:55:24 +02:00
|
|
|
for (;;) {
|
2020-02-26 04:50:33 +01:00
|
|
|
double diff = high.position - last.position, dist = sdir ? diff : -diff;
|
|
|
|
if (dist >= half_step) {
|
|
|
|
// Have valid upper bound - now find step
|
|
|
|
double target = last.position + (sdir ? half_step : -half_step);
|
|
|
|
struct timepos next = itersolve_find_step(sk, m, low, high, target);
|
|
|
|
// Add step at given time
|
2020-03-06 00:37:02 +01:00
|
|
|
int ret = stepcompress_append(sk->sc, sdir
|
|
|
|
, m->print_time, next.time);
|
2020-02-26 04:50:33 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
seek_time_delta = next.time - last.time;
|
|
|
|
if (seek_time_delta < .000000001)
|
|
|
|
seek_time_delta = .000000001;
|
2020-02-26 17:28:36 +01:00
|
|
|
if (is_dir_change && seek_time_delta > SEEK_TIME_RESET)
|
|
|
|
seek_time_delta = SEEK_TIME_RESET;
|
|
|
|
is_dir_change = 0;
|
2020-02-26 04:50:33 +01:00
|
|
|
last.position = target + (sdir ? half_step : -half_step);
|
|
|
|
last.time = next.time;
|
|
|
|
low = next;
|
|
|
|
if (low.time < high.time)
|
|
|
|
// The existing search range is still valid
|
|
|
|
continue;
|
2020-02-25 18:54:55 +01:00
|
|
|
} else if (dist > 0.) {
|
|
|
|
// Avoid rollback if stepper fully reaches target position
|
|
|
|
stepcompress_commit(sk->sc);
|
2020-02-26 04:50:33 +01:00
|
|
|
} else if (unlikely(dist < -(half_step + .000000001))) {
|
|
|
|
// Found direction change
|
2020-02-26 17:28:36 +01:00
|
|
|
is_dir_change = 1;
|
|
|
|
if (seek_time_delta > SEEK_TIME_RESET)
|
|
|
|
seek_time_delta = SEEK_TIME_RESET;
|
2020-02-26 04:50:33 +01:00
|
|
|
if (low.time > last.time) {
|
|
|
|
// Update direction and retry
|
|
|
|
sdir = !sdir;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Must update range to avoid re-finding previous time
|
|
|
|
if (high.time > last.time + .000000001) {
|
|
|
|
// Reduce the high bound - it will become a better low bound
|
2018-06-09 01:55:24 +02:00
|
|
|
high.time = (last.time + high.time) * .5;
|
2019-10-27 16:06:46 +01:00
|
|
|
high.position = calc_position_cb(sk, m, high.time);
|
2018-06-09 01:55:24 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2020-02-26 04:50:33 +01:00
|
|
|
// Need to increase the search range to find an upper bound
|
|
|
|
if (high.time >= end)
|
|
|
|
// At end of move
|
|
|
|
break;
|
|
|
|
low = high;
|
|
|
|
do {
|
|
|
|
high.time = last.time + seek_time_delta;
|
|
|
|
seek_time_delta += seek_time_delta;
|
|
|
|
} while (unlikely(high.time <= low.time));
|
|
|
|
if (high.time > end)
|
|
|
|
high.time = end;
|
|
|
|
high.position = calc_position_cb(sk, m, high.time);
|
2018-06-09 01:55:24 +02:00
|
|
|
}
|
|
|
|
sk->commanded_pos = last.position;
|
2019-10-27 16:14:40 +01:00
|
|
|
if (sk->post_cb)
|
|
|
|
sk->post_cb(sk);
|
2018-06-14 22:33:57 +02:00
|
|
|
return 0;
|
2018-06-09 01:55:24 +02:00
|
|
|
}
|
|
|
|
|
2020-02-25 18:54:55 +01:00
|
|
|
|
|
|
|
/****************************************************************
|
|
|
|
* Interface functions
|
|
|
|
****************************************************************/
|
|
|
|
|
2019-10-28 02:05:57 +01:00
|
|
|
// Check if a move is likely to cause movement on a stepper
|
|
|
|
static inline int
|
|
|
|
check_active(struct stepper_kinematics *sk, struct move *m)
|
|
|
|
{
|
|
|
|
int af = sk->active_flags;
|
|
|
|
return ((af & AF_X && m->axes_r.x != 0.)
|
|
|
|
|| (af & AF_Y && m->axes_r.y != 0.)
|
|
|
|
|| (af & AF_Z && m->axes_r.z != 0.));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate step times for a range of moves on the trapq
|
|
|
|
int32_t __visible
|
|
|
|
itersolve_generate_steps(struct stepper_kinematics *sk, double flush_time)
|
|
|
|
{
|
|
|
|
double last_flush_time = sk->last_flush_time;
|
|
|
|
sk->last_flush_time = flush_time;
|
2019-11-01 23:32:06 +01:00
|
|
|
if (!sk->tq)
|
2019-10-28 02:05:57 +01:00
|
|
|
return 0;
|
2019-11-01 23:32:06 +01:00
|
|
|
trapq_check_sentinels(sk->tq);
|
2019-10-28 02:05:57 +01:00
|
|
|
struct move *m = list_first_entry(&sk->tq->moves, struct move, node);
|
2019-11-01 23:32:06 +01:00
|
|
|
while (last_flush_time >= m->print_time + m->move_t)
|
2019-11-08 19:31:47 +01:00
|
|
|
m = list_next_entry(m, node);
|
2020-03-06 00:37:02 +01:00
|
|
|
double force_steps_time = sk->last_move_time + sk->gen_steps_post_active;
|
2020-05-04 20:00:53 +02:00
|
|
|
int skip_count = 0;
|
2019-10-28 02:05:57 +01:00
|
|
|
for (;;) {
|
2020-05-04 20:00:53 +02:00
|
|
|
double move_start = m->print_time, move_end = move_start + m->move_t;
|
2019-10-28 02:05:57 +01:00
|
|
|
if (check_active(sk, m)) {
|
2020-05-04 20:00:53 +02:00
|
|
|
if (skip_count && sk->gen_steps_pre_active) {
|
2019-11-02 03:21:30 +01:00
|
|
|
// Must generate steps leading up to stepper activity
|
2020-05-04 20:00:53 +02:00
|
|
|
double abs_start = move_start - sk->gen_steps_pre_active;
|
|
|
|
if (abs_start < last_flush_time)
|
|
|
|
abs_start = last_flush_time;
|
|
|
|
if (abs_start < force_steps_time)
|
|
|
|
abs_start = force_steps_time;
|
|
|
|
struct move *pm = list_prev_entry(m, node);
|
|
|
|
while (--skip_count && pm->print_time > abs_start)
|
|
|
|
pm = list_prev_entry(pm, node);
|
|
|
|
do {
|
|
|
|
int32_t ret = itersolve_gen_steps_range(sk, pm, abs_start
|
|
|
|
, flush_time);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
pm = list_next_entry(pm, node);
|
|
|
|
} while (pm != m);
|
2019-11-02 03:21:30 +01:00
|
|
|
}
|
|
|
|
// Generate steps for this move
|
2020-05-04 20:00:53 +02:00
|
|
|
int32_t ret = itersolve_gen_steps_range(sk, m, last_flush_time
|
|
|
|
, flush_time);
|
2019-11-02 03:21:30 +01:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2020-05-04 20:00:53 +02:00
|
|
|
if (move_end >= flush_time) {
|
|
|
|
sk->last_move_time = flush_time;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
skip_count = 0;
|
|
|
|
sk->last_move_time = move_end;
|
|
|
|
force_steps_time = sk->last_move_time + sk->gen_steps_post_active;
|
|
|
|
} else {
|
|
|
|
if (move_start < force_steps_time) {
|
|
|
|
// Must generates steps just past stepper activity
|
|
|
|
double abs_end = force_steps_time;
|
|
|
|
if (abs_end > flush_time)
|
|
|
|
abs_end = flush_time;
|
|
|
|
int32_t ret = itersolve_gen_steps_range(sk, m, last_flush_time
|
|
|
|
, abs_end);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
skip_count = 1;
|
|
|
|
} else {
|
|
|
|
// This move doesn't impact this stepper - skip it
|
|
|
|
skip_count++;
|
|
|
|
}
|
|
|
|
if (flush_time + sk->gen_steps_pre_active <= move_end)
|
|
|
|
return 0;
|
2019-10-28 02:05:57 +01:00
|
|
|
}
|
2019-11-08 19:31:47 +01:00
|
|
|
m = list_next_entry(m, node);
|
2019-10-28 02:05:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the given stepper is likely to be active in the given time range
|
|
|
|
double __visible
|
|
|
|
itersolve_check_active(struct stepper_kinematics *sk, double flush_time)
|
|
|
|
{
|
2019-11-01 23:32:06 +01:00
|
|
|
if (!sk->tq)
|
2019-10-28 02:05:57 +01:00
|
|
|
return 0.;
|
2019-11-01 23:32:06 +01:00
|
|
|
trapq_check_sentinels(sk->tq);
|
2019-10-28 02:05:57 +01:00
|
|
|
struct move *m = list_first_entry(&sk->tq->moves, struct move, node);
|
2019-11-01 23:32:06 +01:00
|
|
|
while (sk->last_flush_time >= m->print_time + m->move_t)
|
2019-10-28 02:05:57 +01:00
|
|
|
m = list_next_entry(m, node);
|
2019-11-01 23:32:06 +01:00
|
|
|
for (;;) {
|
2019-10-28 02:05:57 +01:00
|
|
|
if (check_active(sk, m))
|
|
|
|
return m->print_time;
|
2019-11-01 23:32:06 +01:00
|
|
|
if (flush_time <= m->print_time + m->move_t)
|
2019-10-28 02:05:57 +01:00
|
|
|
return 0.;
|
|
|
|
m = list_next_entry(m, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 03:30:32 +01:00
|
|
|
// Report if the given stepper is registered for the given axis
|
|
|
|
int32_t __visible
|
|
|
|
itersolve_is_active_axis(struct stepper_kinematics *sk, char axis)
|
|
|
|
{
|
|
|
|
if (axis < 'x' || axis > 'z')
|
|
|
|
return 0;
|
|
|
|
return (sk->active_flags & (AF_X << (axis - 'x'))) != 0;
|
|
|
|
}
|
|
|
|
|
2019-10-28 02:05:57 +01:00
|
|
|
void __visible
|
|
|
|
itersolve_set_trapq(struct stepper_kinematics *sk, struct trapq *tq)
|
|
|
|
{
|
|
|
|
sk->tq = tq;
|
|
|
|
}
|
|
|
|
|
2018-06-09 01:55:24 +02:00
|
|
|
void __visible
|
|
|
|
itersolve_set_stepcompress(struct stepper_kinematics *sk
|
|
|
|
, struct stepcompress *sc, double step_dist)
|
|
|
|
{
|
|
|
|
sk->sc = sc;
|
|
|
|
sk->step_dist = step_dist;
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:27:33 +01:00
|
|
|
double __visible
|
2018-07-18 17:51:28 +02:00
|
|
|
itersolve_calc_position_from_coord(struct stepper_kinematics *sk
|
|
|
|
, double x, double y, double z)
|
2018-06-22 19:03:07 +02:00
|
|
|
{
|
|
|
|
struct move m;
|
|
|
|
memset(&m, 0, sizeof(m));
|
2019-10-29 17:44:39 +01:00
|
|
|
m.start_pos.x = x;
|
|
|
|
m.start_pos.y = y;
|
|
|
|
m.start_pos.z = z;
|
2019-11-02 03:21:30 +01:00
|
|
|
m.move_t = 1000.;
|
|
|
|
return sk->calc_position_cb(sk, &m, 500.);
|
2018-06-22 19:03:07 +02:00
|
|
|
}
|
|
|
|
|
2018-06-09 01:55:24 +02:00
|
|
|
void __visible
|
2019-11-14 00:58:51 +01:00
|
|
|
itersolve_set_position(struct stepper_kinematics *sk
|
|
|
|
, double x, double y, double z)
|
2018-06-09 01:55:24 +02:00
|
|
|
{
|
2019-11-14 00:58:51 +01:00
|
|
|
sk->commanded_pos = itersolve_calc_position_from_coord(sk, x, y, z);
|
2018-06-09 01:55:24 +02:00
|
|
|
}
|
2018-06-14 22:33:57 +02:00
|
|
|
|
|
|
|
double __visible
|
|
|
|
itersolve_get_commanded_pos(struct stepper_kinematics *sk)
|
|
|
|
{
|
|
|
|
return sk->commanded_pos;
|
|
|
|
}
|