kin_extruder: Apply pressure advance in kin_extruder.c

Implement the pressure advance calculations while performing the
definitive integral calculations.  This simplifies both the
extruder.py and kin_extruder.c code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-12-08 19:54:36 -05:00
parent 730a6d868b
commit 080ee0b512
3 changed files with 37 additions and 75 deletions

View file

@ -91,8 +91,8 @@ defs_kin_winch = """
defs_kin_extruder = """
struct stepper_kinematics *extruder_stepper_alloc(void);
void extruder_set_pressure(struct stepper_kinematics *sk
, double pressure_advance, double half_smooth_time);
void extruder_set_smooth_time(struct stepper_kinematics *sk
, double smooth_time);
"""
defs_serialqueue = """

View file

@ -12,9 +12,9 @@
#include "pyhelper.h" // errorf
#include "trapq.h" // move_get_distance
// Helper code for integrating acceleration
// Calculate the definitive integral of the move distance
static double
integrate_accel(struct move *m, double start, double end)
move_integrate_distance(struct move *m, double start, double end)
{
double half_v = .5 * m->start_v, sixth_a = (1. / 3.) * m->half_accel;
double si = start * start * (half_v + sixth_a * start);
@ -22,64 +22,45 @@ integrate_accel(struct move *m, double start, double end)
return ei - si;
}
// Calculate the definitive integral on part of a move
// Calculate the definitive integral of extruder with pressure advance
static double
move_integrate(struct move *m, int axis, double start, double end)
pa_move_integrate(struct move *m, double start, double end)
{
if (start < 0.)
start = 0.;
if (end > m->move_t)
end = m->move_t;
double base = m->start_pos.axis[axis - 'x'] * (end - start);
double integral = integrate_accel(m, start, end);
return base + integral * m->axes_r.axis[axis - 'x'];
double pressure_advance = m->axes_r.y;
double avg_v = m->start_v + (start + end) * m->half_accel;
double pa_add = pressure_advance * avg_v;
double base = (m->start_pos.x + pa_add) * (end - start);
return base + move_integrate_distance(m, start, end);
}
// Calculate the definitive integral for a cartesian axis
// Calculate the definitive integral of the extruder over a range of moves
static double
trapq_integrate(struct move *m, int axis, double start, double end)
pa_range_integrate(struct move *m, double start, double end)
{
double res = move_integrate(m, axis, start, end);
double res = pa_move_integrate(m, start, end);
// Integrate over previous moves
struct move *prev = m;
while (unlikely(start < 0.)) {
prev = list_prev_entry(prev, node);
start += prev->move_t;
res += move_integrate(prev, axis, start, prev->move_t);
res += pa_move_integrate(prev, start, prev->move_t);
}
// Integrate over future moves
while (unlikely(end > m->move_t)) {
end -= m->move_t;
m = list_next_entry(m, node);
res += move_integrate(m, axis, 0., end);
res += pa_move_integrate(m, 0., end);
}
return res;
}
// Find a move associated with a given time
static struct move *
trapq_find_move(struct move *m, double *ptime)
{
double move_time = *ptime;
for (;;) {
if (unlikely(move_time < 0.)) {
// Check previous move in list
m = list_prev_entry(m, node);
move_time += m->move_t;
} else if (unlikely(move_time > m->move_t)) {
// Check next move in list
move_time -= m->move_t;
m = list_next_entry(m, node);
} else {
*ptime = move_time;
return m;
}
}
}
struct extruder_stepper {
struct stepper_kinematics sk;
double pressure_advance_factor, half_smooth_time, inv_smooth_time;
double half_smooth_time, inv_smooth_time;
};
static double
@ -91,36 +72,20 @@ extruder_calc_position(struct stepper_kinematics *sk, struct move *m
if (!hst)
// Pressure advance not enabled
return m->start_pos.x + move_get_distance(m, move_time);
// Calculate average position over smooth_time
double area = trapq_integrate(m, 'x', move_time - hst, move_time + hst);
double base_pos = area * es->inv_smooth_time;
// Calculate position 'half_smooth_time' in the past
double start_time = move_time - hst;
struct move *sm = trapq_find_move(m, &start_time);
double start_dist = move_get_distance(sm, start_time);
double pa_start_pos = sm->start_pos.y + (sm->axes_r.y ? start_dist : 0.);
// Calculate position 'half_smooth_time' in the future
double end_time = move_time + hst;
struct move *em = trapq_find_move(m, &end_time);
double end_dist = move_get_distance(em, end_time);
double pa_end_pos = em->start_pos.y + (em->axes_r.y ? end_dist : 0.);
// Calculate position with pressure advance
return base_pos + (pa_end_pos - pa_start_pos) * es->pressure_advance_factor;
// Apply pressure advance and average over smooth_time
double area = pa_range_integrate(m, move_time - hst, move_time + hst);
return area * es->inv_smooth_time;
}
void __visible
extruder_set_pressure(struct stepper_kinematics *sk
, double pressure_advance, double half_smooth_time)
extruder_set_smooth_time(struct stepper_kinematics *sk, double smooth_time)
{
struct extruder_stepper *es = container_of(sk, struct extruder_stepper, sk);
if (! half_smooth_time) {
es->pressure_advance_factor = es->half_smooth_time = 0.;
double hst = smooth_time * .5;
es->sk.scan_past = es->sk.scan_future = es->half_smooth_time = hst;
if (! hst)
return;
}
es->sk.scan_past = es->sk.scan_future = half_smooth_time;
es->half_smooth_time = half_smooth_time;
es->inv_smooth_time = .5 / half_smooth_time;
es->pressure_advance_factor = pressure_advance * es->inv_smooth_time;
es->inv_smooth_time = 1. / smooth_time;
}
struct stepper_kinematics * __visible