Skip to content

Commit 88ea9f9

Browse files
committed
Better toolhead posistion detection and even triggering
The algorithm for detecting of the toolhead is at its origin point was flawed in that assume that the axis positions will reach precisely 0.0. However, this may not always be the case. The ending axis position could be a fraction of a millimeter away from 0.0, in which case the toolhead will never reach the origin point. Instead of assuming that the axes will reach 0.0, compute the axes positions up to a certain precision and use that value to determine the toolhead position. In addition, ensure that the TOOLHEAD_ORIGIN event is triggered only once per reaching the origin point.
1 parent 6c1462c commit 88ea9f9

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

src/core/objects/toolhead.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <cache.h>
2222
#include <errno.h>
2323
#include <utils.h>
24+
#include <math.h>
2425
#include <kinematics.h>
2526
#include <common_defs.h>
2627
#include <debug.h>
@@ -29,6 +30,9 @@
2930
#include "toolhead.h"
3031
#include "axis.h"
3132

33+
#define PRECISION 2
34+
static float precision_factor;
35+
3236
typedef struct {
3337
const char axes[AXIS_TYPE_MAX];
3438
const char attachment[AXIS_TYPE_MAX];
@@ -44,6 +48,7 @@ typedef struct {
4448
axis_type_t *axes;
4549
toolhead_axis_t *attachment;
4650
coordinates_t position;
51+
bool single_event_guard;
4752
size_t n_axes;
4853
size_t n_attached;
4954
} toolhead_t;
@@ -170,18 +175,31 @@ static void toolhead_update(core_object_t *object, uint64_t ticks,
170175
toolhead->position.a, toolhead->position.b, toolhead->position.c,
171176
toolhead->position.e);
172177
for (i = 0; i < toolhead->n_axes; i++) {
173-
if (get_axis_position(&toolhead->position, toolhead->axes[i]) != 0.0) {
178+
double axis_position = get_axis_position(&toolhead->position, toolhead->axes[i]);
179+
180+
axis_position = round(axis_position * precision_factor);
181+
axis_position = (double)((long)axis_position) / precision_factor;
182+
log_debug(toolhead, " position %c: %f", kinematics_axis_type_to_char(toolhead->axes[i]),
183+
axis_position);
184+
if (axis_position > (1 / precision_factor)) {
174185
at_origin = false;
175186
break;
176187
}
177188
}
178189

179-
if (at_origin) {
190+
if (!at_origin && toolhead->single_event_guard)
191+
toolhead->single_event_guard = false;
192+
193+
log_debug(toolhead, "at_origin: %u, single_event_guard: %u", at_origin,
194+
toolhead->single_event_guard);
195+
if (at_origin && !toolhead->single_event_guard) {
180196
event = object_cache_alloc(toolhead_event_cache);
181197
if (event) {
182198
memcpy(event->position, &toolhead->position,
183199
sizeof(event->position));
200+
log_debug(toolhead, "TOOLHEAD_ORIGIN triggered");
184201
CORE_EVENT_SUBMIT(toolhead, OBJECT_EVENT_TOOLHEAD_ORIGIN, event);
202+
toolhead->single_event_guard = true;
185203
}
186204
}
187205
}
@@ -262,5 +280,6 @@ toolhead_t *object_create(const char *name, void *config_ptr) {
262280
kinematics_axis_type_from_char(config->attachment[n_axis]);
263281
}
264282

283+
precision_factor = pow(10, PRECISION);
265284
return toolhead;
266285
}

0 commit comments

Comments
 (0)