Skip to content

Commit

Permalink
trapq: Add code to calculate definitive integral
Browse files Browse the repository at this point in the history
Support calculating the definitive integral of a cartesian axis
position over a time range of the movement queue.

Signed-off-by: Kevin O'Connor <[email protected]>
  • Loading branch information
KevinOConnor committed Nov 21, 2019
1 parent d00023f commit a28b299
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
44 changes: 44 additions & 0 deletions klippy/chelper/trapq.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,50 @@ move_get_coord(struct move *m, double move_time)
.z = m->start_pos.z + m->axes_r.z * move_dist };
}

// Helper code for integrating acceleration
static double
integrate_accel(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);
double ei = end * end * (half_v + sixth_a * end);
return ei - si;
}

// Calculate the definitive integral on part of a move
static double
move_integrate(struct move *m, int axis, 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'];
}

// Calculate the definitive integral for a cartesian axis
double
trapq_integrate(struct move *m, int axis, double start, double end)
{
double res = move_integrate(m, axis, 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);
}
// 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);
}
return res;
}

#define NEVER_TIME 9999999999999999.9

// Allocate a new 'trapq' object
Expand Down
8 changes: 7 additions & 1 deletion klippy/chelper/trapq.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
#include "list.h" // list_node

struct coord {
double x, y, z;
union {
struct {
double x, y, z;
};
double axis[3];
};
};

struct move {
Expand All @@ -27,6 +32,7 @@ void trapq_append(struct trapq *tq, double print_time
, double start_v, double cruise_v, double accel);
double move_get_distance(struct move *m, double move_time);
struct coord move_get_coord(struct move *m, double move_time);
double trapq_integrate(struct move *m, int axis, double start, double end);
struct trapq *trapq_alloc(void);
void trapq_free(struct trapq *tq);
void trapq_check_sentinels(struct trapq *tq);
Expand Down

0 comments on commit a28b299

Please sign in to comment.