fix config options for rotatable delta arm solution
[clinton/Smoothieware.git] / src / modules / tools / endstops / Endstops.cpp
CommitLineData
df27a6a3 1/*
201bcb94
AW
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
201bcb94
AW
6*/
7
8#include "libs/Module.h"
9#include "libs/Kernel.h"
10#include "modules/communication/utils/Gcode.h"
3fceb8eb 11#include "modules/robot/Conveyor.h"
807b9b57 12#include "modules/robot/ActuatorCoordinates.h"
201bcb94
AW
13#include "Endstops.h"
14#include "libs/nuts_bolts.h"
750277f8 15#include "libs/Pin.h"
670fa10b 16#include "libs/StepperMotor.h"
201bcb94 17#include "wait_api.h" // mbed.h lib
61134a65
JM
18#include "Robot.h"
19#include "Stepper.h"
20#include "Config.h"
21#include "SlowTicker.h"
22#include "Planner.h"
7af0714f
JM
23#include "checksumm.h"
24#include "utils.h"
8d54c34c 25#include "ConfigValue.h"
fc7b9a7b 26#include "libs/StreamOutput.h"
9f6f04a5
JM
27#include "PublicDataRequest.h"
28#include "EndstopsPublicAccess.h"
3c947f85 29#include "StreamOutputPool.h"
a157d099 30#include "StepTicker.h"
7552475b 31#include "BaseSolution.h"
e551657a 32#include "SerialMessage.h"
201bcb94 33
80605954
JM
34#include <ctype.h>
35
33e4cc02
JM
36#define ALPHA_AXIS 0
37#define BETA_AXIS 1
38#define GAMMA_AXIS 2
9a993543
JM
39#define X_AXIS 0
40#define Y_AXIS 1
41#define Z_AXIS 2
33e4cc02 42
33e4cc02
JM
43#define endstops_module_enable_checksum CHECKSUM("endstops_enable")
44#define corexy_homing_checksum CHECKSUM("corexy_homing")
45#define delta_homing_checksum CHECKSUM("delta_homing")
3e1f5b74 46#define scara_homing_checksum CHECKSUM("scara_homing")
33e4cc02
JM
47
48#define alpha_min_endstop_checksum CHECKSUM("alpha_min_endstop")
49#define beta_min_endstop_checksum CHECKSUM("beta_min_endstop")
50#define gamma_min_endstop_checksum CHECKSUM("gamma_min_endstop")
51
52#define alpha_max_endstop_checksum CHECKSUM("alpha_max_endstop")
53#define beta_max_endstop_checksum CHECKSUM("beta_max_endstop")
54#define gamma_max_endstop_checksum CHECKSUM("gamma_max_endstop")
55
56#define alpha_trim_checksum CHECKSUM("alpha_trim")
57#define beta_trim_checksum CHECKSUM("beta_trim")
58#define gamma_trim_checksum CHECKSUM("gamma_trim")
59
60// these values are in steps and should be deprecated
61#define alpha_fast_homing_rate_checksum CHECKSUM("alpha_fast_homing_rate")
62#define beta_fast_homing_rate_checksum CHECKSUM("beta_fast_homing_rate")
63#define gamma_fast_homing_rate_checksum CHECKSUM("gamma_fast_homing_rate")
64
65#define alpha_slow_homing_rate_checksum CHECKSUM("alpha_slow_homing_rate")
66#define beta_slow_homing_rate_checksum CHECKSUM("beta_slow_homing_rate")
67#define gamma_slow_homing_rate_checksum CHECKSUM("gamma_slow_homing_rate")
68
69#define alpha_homing_retract_checksum CHECKSUM("alpha_homing_retract")
70#define beta_homing_retract_checksum CHECKSUM("beta_homing_retract")
71#define gamma_homing_retract_checksum CHECKSUM("gamma_homing_retract")
33e4cc02
JM
72
73// same as above but in user friendly mm/s and mm
74#define alpha_fast_homing_rate_mm_checksum CHECKSUM("alpha_fast_homing_rate_mm_s")
75#define beta_fast_homing_rate_mm_checksum CHECKSUM("beta_fast_homing_rate_mm_s")
76#define gamma_fast_homing_rate_mm_checksum CHECKSUM("gamma_fast_homing_rate_mm_s")
77
78#define alpha_slow_homing_rate_mm_checksum CHECKSUM("alpha_slow_homing_rate_mm_s")
79#define beta_slow_homing_rate_mm_checksum CHECKSUM("beta_slow_homing_rate_mm_s")
80#define gamma_slow_homing_rate_mm_checksum CHECKSUM("gamma_slow_homing_rate_mm_s")
81
82#define alpha_homing_retract_mm_checksum CHECKSUM("alpha_homing_retract_mm")
83#define beta_homing_retract_mm_checksum CHECKSUM("beta_homing_retract_mm")
84#define gamma_homing_retract_mm_checksum CHECKSUM("gamma_homing_retract_mm")
85
86#define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
87
88#define alpha_homing_direction_checksum CHECKSUM("alpha_homing_direction")
89#define beta_homing_direction_checksum CHECKSUM("beta_homing_direction")
90#define gamma_homing_direction_checksum CHECKSUM("gamma_homing_direction")
91#define home_to_max_checksum CHECKSUM("home_to_max")
92#define home_to_min_checksum CHECKSUM("home_to_min")
93#define alpha_min_checksum CHECKSUM("alpha_min")
94#define beta_min_checksum CHECKSUM("beta_min")
95#define gamma_min_checksum CHECKSUM("gamma_min")
96
97#define alpha_max_checksum CHECKSUM("alpha_max")
98#define beta_max_checksum CHECKSUM("beta_max")
99#define gamma_max_checksum CHECKSUM("gamma_max")
100
3c947f85
JM
101#define alpha_limit_enable_checksum CHECKSUM("alpha_limit_enable")
102#define beta_limit_enable_checksum CHECKSUM("beta_limit_enable")
103#define gamma_limit_enable_checksum CHECKSUM("gamma_limit_enable")
104
80605954 105#define homing_order_checksum CHECKSUM("homing_order")
2ddf75fd 106#define move_to_origin_checksum CHECKSUM("move_to_origin_after_home")
80605954 107
dd0a7cfa
JM
108#define STEPPER THEKERNEL->robot->actuators
109#define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
56ce2b5a 110
2b3cedc7 111
3c947f85 112// Homing States
e714bd32 113enum {
2ddf75fd
JM
114 MOVING_TO_ENDSTOP_FAST, // homing move
115 MOVING_BACK, // homing move
116 MOVING_TO_ENDSTOP_SLOW, // homing move
fafb45df 117 NOT_HOMING,
3c947f85 118 BACK_OFF_HOME,
2ddf75fd 119 MOVE_TO_ORIGIN,
3c947f85
JM
120 LIMIT_TRIGGERED
121};
122
33e4cc02
JM
123Endstops::Endstops()
124{
201bcb94 125 this->status = NOT_HOMING;
33e4cc02 126 home_offset[0] = home_offset[1] = home_offset[2] = 0.0F;
201bcb94
AW
127}
128
33e4cc02
JM
129void Endstops::on_module_loaded()
130{
7dee696d 131 // Do not do anything if not enabled
314ab8f7 132 if ( THEKERNEL->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) {
4e722c00 133 delete this;
33e4cc02
JM
134 return;
135 }
7d62445b 136
9f6f04a5
JM
137 register_for_event(ON_GCODE_RECEIVED);
138 register_for_event(ON_GET_PUBLIC_DATA);
7d6fe308 139 register_for_event(ON_SET_PUBLIC_DATA);
201bcb94 140
e714bd32 141 THEKERNEL->step_ticker->register_acceleration_tick_handler([this]() {acceleration_tick(); });
201bcb94 142
750277f8
AW
143 // Settings
144 this->on_config_reload(this);
201bcb94
AW
145}
146
750277f8 147// Get config
33e4cc02
JM
148void Endstops::on_config_reload(void *argument)
149{
56ce2b5a
JM
150 this->pins[0].from_string( THEKERNEL->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
151 this->pins[1].from_string( THEKERNEL->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
152 this->pins[2].from_string( THEKERNEL->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
153 this->pins[3].from_string( THEKERNEL->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
154 this->pins[4].from_string( THEKERNEL->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
155 this->pins[5].from_string( THEKERNEL->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
156
157 // These are the old ones in steps still here for backwards compatibility
158 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(0);
159 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(1);
160 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number() / STEPS_PER_MM(2);
161 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(0);
162 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(1);
163 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number() / STEPS_PER_MM(2);
164 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(0);
165 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(1);
166 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number() / STEPS_PER_MM(2);
5de98d7c
JM
167
168 // newer mm based config values override the old ones, convert to steps/mm and steps, defaults to what was set in the older config settings above
56ce2b5a
JM
169 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0])->as_number();
170 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[1])->as_number();
171 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2])->as_number();
172 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0])->as_number();
173 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[1])->as_number();
174 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2])->as_number();
175 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum )->by_default(this->retract_mm[0])->as_number();
176 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum )->by_default(this->retract_mm[1])->as_number();
177 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum )->by_default(this->retract_mm[2])->as_number();
5de98d7c 178
06c48ee8 179 this->debounce_count = THEKERNEL->config->value(endstop_debounce_count_checksum )->by_default(100)->as_number();
5de98d7c 180
409ff5b3 181 // get homing direction and convert to boolean where true is home to min, and false is home to max
314ab8f7 182 int home_dir = get_checksum(THEKERNEL->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 183 this->home_direction[0] = home_dir != home_to_max_checksum;
47bbe224 184
314ab8f7 185 home_dir = get_checksum(THEKERNEL->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 186 this->home_direction[1] = home_dir != home_to_max_checksum;
47bbe224 187
314ab8f7 188 home_dir = get_checksum(THEKERNEL->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 189 this->home_direction[2] = home_dir != home_to_max_checksum;
47bbe224 190
314ab8f7 191 this->homing_position[0] = this->home_direction[0] ? THEKERNEL->config->value(alpha_min_checksum)->by_default(0)->as_number() : THEKERNEL->config->value(alpha_max_checksum)->by_default(200)->as_number();
f3b66360
JM
192 this->homing_position[1] = this->home_direction[1] ? THEKERNEL->config->value(beta_min_checksum )->by_default(0)->as_number() : THEKERNEL->config->value(beta_max_checksum )->by_default(200)->as_number();
193 this->homing_position[2] = this->home_direction[2] ? THEKERNEL->config->value(gamma_min_checksum)->by_default(0)->as_number() : THEKERNEL->config->value(gamma_max_checksum)->by_default(200)->as_number();
f29b0272 194
314ab8f7
MM
195 this->is_corexy = THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool();
196 this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool();
504f0e3e
JM
197 this->is_scara = THEKERNEL->config->value(scara_homing_checksum)->by_default(false)->as_bool();
198
80605954 199 // see if an order has been specified, must be three characters, XYZ or YXZ etc
e714bd32
JM
200 string order = THEKERNEL->config->value(homing_order_checksum)->by_default("")->as_string();
201 this->homing_order = 0;
80605954 202 if(order.size() == 3 && !this->is_delta) {
e714bd32 203 int shift = 0;
80605954 204 for(auto c : order) {
e714bd32 205 uint8_t i = toupper(c) - 'X';
80605954 206 if(i > 2) { // bad value
e714bd32 207 this->homing_order = 0;
80605954
JM
208 break;
209 }
210 homing_order |= (i << shift);
211 shift += 2;
212 }
213 }
214
192bca32
JM
215 // endstop trim used by deltas to do soft adjusting
216 // on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
217 this->trim_mm[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0 )->as_number();
218 this->trim_mm[1] = THEKERNEL->config->value(beta_trim_checksum )->by_default(0 )->as_number();
219 this->trim_mm[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0 )->as_number();
3c947f85
JM
220
221 // limits enabled
e714bd32
JM
222 this->limit_enable[X_AXIS] = THEKERNEL->config->value(alpha_limit_enable_checksum)->by_default(false)->as_bool();
223 this->limit_enable[Y_AXIS] = THEKERNEL->config->value(beta_limit_enable_checksum)->by_default(false)->as_bool();
224 this->limit_enable[Z_AXIS] = THEKERNEL->config->value(gamma_limit_enable_checksum)->by_default(false)->as_bool();
3c947f85 225
b62eb905 226 //s et to true by default for deltas duwe to trim, false on cartesians
e714bd32 227 this->move_to_origin_after_home = THEKERNEL->config->value(move_to_origin_checksum)->by_default(is_delta)->as_bool();
2b3cedc7 228
e714bd32 229 if(this->limit_enable[X_AXIS] || this->limit_enable[Y_AXIS] || this->limit_enable[Z_AXIS]) {
3c947f85 230 register_for_event(ON_IDLE);
7f3e6350
JM
231 if(this->is_delta) {
232 // we must enable all the limits not just one
e714bd32
JM
233 this->limit_enable[X_AXIS] = true;
234 this->limit_enable[Y_AXIS] = true;
235 this->limit_enable[Z_AXIS] = true;
7f3e6350 236 }
3c947f85 237 }
44127aca
JM
238
239 // NOTE this may also be true of scara. TBD
240 if(this->is_delta) {
241 // some things must be the same or they will die, so force it here to avoid config errors
e714bd32
JM
242 this->fast_rates[1] = this->fast_rates[2] = this->fast_rates[0];
243 this->slow_rates[1] = this->slow_rates[2] = this->slow_rates[0];
244 this->retract_mm[1] = this->retract_mm[2] = this->retract_mm[0];
245 this->home_direction[1] = this->home_direction[2] = this->home_direction[0];
246 this->homing_position[0] = this->homing_position[1] = 0;
44127aca 247 }
3c947f85
JM
248}
249
28166daf
JM
250bool Endstops::debounced_get(int pin)
251{
e714bd32 252 uint8_t debounce = 0;
28166daf
JM
253 while(this->pins[pin].get()) {
254 if ( ++debounce >= this->debounce_count ) {
255 // pin triggered
256 return true;
257 }
258 }
259 return false;
260}
261
e714bd32 262static const char *endstop_names[] = {"min_x", "min_y", "min_z", "max_x", "max_y", "max_z"};
3c947f85
JM
263
264void Endstops::on_idle(void *argument)
265{
c3d7feb6
JM
266 if(this->status == LIMIT_TRIGGERED) {
267 // if we were in limit triggered see if it has been cleared
268 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
269 if(this->limit_enable[c]) {
270 std::array<int, 2> minmax{{0, 3}};
271 // check min and max endstops
272 for (int i : minmax) {
e714bd32 273 int n = c + i;
c3d7feb6
JM
274 if(this->pins[n].get()) {
275 // still triggered, so exit
e714bd32 276 bounce_cnt = 0;
c3d7feb6
JM
277 return;
278 }
279 }
280 }
281 }
ee1c3181
JM
282 if(++bounce_cnt > 10) { // can use less as it calls on_idle in between
283 // clear the state
e714bd32 284 this->status = NOT_HOMING;
ee1c3181 285 }
4befe777 286 return;
c3d7feb6 287
e714bd32 288 } else if(this->status != NOT_HOMING) {
c3d7feb6
JM
289 // don't check while homing
290 return;
291 }
3c947f85
JM
292
293 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
294 if(this->limit_enable[c] && STEPPER[c]->is_moving()) {
295 std::array<int, 2> minmax{{0, 3}};
296 // check min and max endstops
297 for (int i : minmax) {
e714bd32 298 int n = c + i;
28166daf
JM
299 if(debounced_get(n)) {
300 // endstop triggered
301 THEKERNEL->streams->printf("Limit switch %s was hit - reset or M999 required\n", endstop_names[n]);
e714bd32 302 this->status = LIMIT_TRIGGERED;
28166daf
JM
303 // disables heaters and motors, ignores incoming Gcode and flushes block queue
304 THEKERNEL->call_event(ON_HALT, nullptr);
305 return;
3c947f85
JM
306 }
307 }
308 }
309 }
310}
311
312// if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move
5bfcd44a
JM
313// checks if triggered and only backs off if triggered
314void Endstops::back_off_home(char axes_to_move)
3c947f85 315{
e714bd32 316 std::vector<std::pair<char, float>> params;
3c947f85 317 this->status = BACK_OFF_HOME;
7f3e6350
JM
318
319 // these are handled differently
1a6870b3 320 if(is_delta) {
7f3e6350 321 // Move off of the endstop using a regular relative move in Z only
e714bd32 322 params.push_back({'Z', this->retract_mm[Z_AXIS] * (this->home_direction[Z_AXIS] ? 1 : -1)});
44127aca 323
e714bd32 324 } else {
7f3e6350 325 // cartesians, concatenate all the moves we need to do into one gcode
44127aca
JM
326 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
327 if( ((axes_to_move >> c ) & 1) == 0) continue; // only for axes we asked to move
7f3e6350
JM
328
329 // if not triggered no need to move off
28166daf 330 if(this->limit_enable[c] && debounced_get(c + (this->home_direction[c] ? 0 : 3)) ) {
e714bd32 331 params.push_back({c + 'X', this->retract_mm[c] * (this->home_direction[c] ? 1 : -1)});
44127aca 332 }
3c947f85
JM
333 }
334 }
7f3e6350
JM
335
336 if(!params.empty()) {
337 // Move off of the endstop using a regular relative move
7c801094 338 params.insert(params.begin(), {'G', 0});
7f3e6350 339 // use X slow rate to move, Z should have a max speed set anyway
e714bd32 340 params.push_back({'F', this->slow_rates[X_AXIS] * 60.0F});
7c801094
JM
341 char gcode_buf[64];
342 append_parameters(gcode_buf, params, sizeof(gcode_buf));
7f3e6350 343 Gcode gc(gcode_buf, &(StreamOutput::NullStream));
e714bd32
JM
344 bool oldmode = THEKERNEL->robot->absolute_mode;
345 THEKERNEL->robot->absolute_mode = false; // needs to be relative mode
7f3e6350 346 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
e714bd32 347 THEKERNEL->robot->absolute_mode = oldmode; // restore mode
7f3e6350
JM
348 // Wait for above to finish
349 THEKERNEL->conveyor->wait_for_empty_queue();
350 }
351
3c947f85 352 this->status = NOT_HOMING;
a0e0d592
BG
353}
354
2b3cedc7 355// If enabled will move the head to 0,0 after homing, but only if X and Y were set to home
2ddf75fd 356void Endstops::move_to_origin(char axes_to_move)
2b3cedc7 357{
e714bd32 358 if( (axes_to_move & 0x03) != 3 ) return; // ignore if X and Y not homing
2b3cedc7 359
fafb45df
JM
360 // Do we need to check if we are already at 0,0? probably not as the G0 will not do anything if we are
361 // float pos[3]; THEKERNEL->robot->get_axis_position(pos); if(pos[0] == 0 && pos[1] == 0) return;
362
2ddf75fd 363 this->status = MOVE_TO_ORIGIN;
2b3cedc7 364 // Move to center using a regular move, use slower of X and Y fast rate
e714bd32 365 float rate = std::min(this->fast_rates[0], this->fast_rates[1]) * 60.0F;
2b3cedc7 366 char buf[32];
e551657a
JM
367 snprintf(buf, sizeof(buf), "G53 G0 X0 Y0 F%1.4f", rate); // must use machine coordinates in case G92 or WCS is in effect
368 struct SerialMessage message;
369 message.message = buf;
370 message.stream = &(StreamOutput::NullStream);
371 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); // as it is a multi G code command
2b3cedc7
JM
372 // Wait for above to finish
373 THEKERNEL->conveyor->wait_for_empty_queue();
374 this->status = NOT_HOMING;
375}
376
798295c1 377bool Endstops::wait_for_homed(char axes_to_move)
33e4cc02 378{
a0e0d592 379 bool running = true;
33e4cc02
JM
380 unsigned int debounce[3] = {0, 0, 0};
381 while (running) {
a0e0d592 382 running = false;
314ab8f7 383 THEKERNEL->call_event(ON_IDLE);
798295c1
JM
384
385 // check if on_halt (eg kill)
73706276 386 if(THEKERNEL->is_halted()) return false;
798295c1 387
56ce2b5a
JM
388 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
389 if ( ( axes_to_move >> c ) & 1 ) {
390 if ( this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) {
391 if ( debounce[c] < debounce_count ) {
392 debounce[c]++;
a0e0d592 393 running = true;
dd0a7cfa
JM
394 } else if ( STEPPER[c]->is_moving() ) {
395 STEPPER[c]->move(0, 0);
e714bd32 396 axes_to_move &= ~(1 << c); // no need to check it again
a0e0d592 397 }
33e4cc02 398 } else {
a0e0d592
BG
399 // The endstop was not hit yet
400 running = true;
56ce2b5a 401 debounce[c] = 0;
323cca60 402 }
a0e0d592
BG
403 }
404 }
405 }
798295c1 406 return true;
750277f8 407}
201bcb94 408
81f02e89 409void Endstops::do_homing_cartesian(char axes_to_move)
33e4cc02 410{
798295c1 411 // check if on_halt (eg kill)
73706276 412 if(THEKERNEL->is_halted()) return;
798295c1 413
80605954 414 // this homing works for cartesian and delta printers
f29b0272 415 // Start moving the axes to the origin
2ddf75fd 416 this->status = MOVING_TO_ENDSTOP_FAST;
56ce2b5a
JM
417 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
418 if ( ( axes_to_move >> c) & 1 ) {
e714bd32 419 this->feed_rate[c] = this->fast_rates[c];
6f6677fc 420 STEPPER[c]->move(this->home_direction[c], 10000000, 0);
f29b0272
JM
421 }
422 }
423
424 // Wait for all axes to have homed
798295c1 425 if(!this->wait_for_homed(axes_to_move)) return;
f29b0272
JM
426
427 // Move back a small distance
428 this->status = MOVING_BACK;
429 bool inverted_dir;
56ce2b5a
JM
430 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
431 if ( ( axes_to_move >> c ) & 1 ) {
432 inverted_dir = !this->home_direction[c];
e714bd32 433 this->feed_rate[c] = this->slow_rates[c];
6f6677fc 434 STEPPER[c]->move(inverted_dir, this->retract_mm[c]*STEPS_PER_MM(c), 0);
f29b0272
JM
435 }
436 }
437
33e4cc02 438 // Wait for moves to be done
56ce2b5a
JM
439 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
440 if ( ( axes_to_move >> c ) & 1 ) {
dd0a7cfa 441 while ( STEPPER[c]->is_moving() ) {
314ab8f7 442 THEKERNEL->call_event(ON_IDLE);
ca287785 443 if(THEKERNEL->is_halted()) return;
f29b0272
JM
444 }
445 }
446 }
447
448 // Start moving the axes to the origin slowly
2ddf75fd 449 this->status = MOVING_TO_ENDSTOP_SLOW;
56ce2b5a
JM
450 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
451 if ( ( axes_to_move >> c ) & 1 ) {
e714bd32 452 this->feed_rate[c] = this->slow_rates[c];
6f6677fc 453 STEPPER[c]->move(this->home_direction[c], 10000000, 0);
f29b0272
JM
454 }
455 }
456
457 // Wait for all axes to have homed
798295c1 458 if(!this->wait_for_homed(axes_to_move)) return;
f29b0272
JM
459}
460
798295c1 461bool Endstops::wait_for_homed_corexy(int axis)
33e4cc02 462{
3db88866 463 bool running = true;
33e4cc02
JM
464 unsigned int debounce[3] = {0, 0, 0};
465 while (running) {
3db88866 466 running = false;
314ab8f7 467 THEKERNEL->call_event(ON_IDLE);
798295c1
JM
468
469 // check if on_halt (eg kill)
73706276 470 if(THEKERNEL->is_halted()) return false;
798295c1 471
33e4cc02
JM
472 if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) {
473 if ( debounce[axis] < debounce_count ) {
3db88866
JM
474 debounce[axis] ++;
475 running = true;
476 } else {
477 // turn both off if running
dd0a7cfa
JM
478 if (STEPPER[X_AXIS]->is_moving()) STEPPER[X_AXIS]->move(0, 0);
479 if (STEPPER[Y_AXIS]->is_moving()) STEPPER[Y_AXIS]->move(0, 0);
3db88866 480 }
33e4cc02 481 } else {
3db88866
JM
482 // The endstop was not hit yet
483 running = true;
484 debounce[axis] = 0;
485 }
486 }
798295c1 487 return true;
3db88866
JM
488}
489
1ad23cd3 490void Endstops::corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps)
33e4cc02 491{
798295c1 492 // check if on_halt (eg kill)
73706276 493 if(THEKERNEL->is_halted()) return;
798295c1 494
2ddf75fd 495 this->status = MOVING_TO_ENDSTOP_FAST;
e714bd32 496 this->feed_rate[X_AXIS] = fast_rate;
6f6677fc 497 STEPPER[X_AXIS]->move(dirx, 10000000, 0);
e714bd32 498 this->feed_rate[Y_AXIS] = fast_rate;
6f6677fc 499 STEPPER[Y_AXIS]->move(diry, 10000000, 0);
f29b0272 500
33e4cc02 501 // wait for primary axis
798295c1 502 if(!this->wait_for_homed_corexy(home_axis)) return;
f29b0272 503
33e4cc02
JM
504 // Move back a small distance
505 this->status = MOVING_BACK;
e714bd32 506 this->feed_rate[X_AXIS] = slow_rate;
6f6677fc 507 STEPPER[X_AXIS]->move(!dirx, retract_steps, 0);
e714bd32 508 this->feed_rate[Y_AXIS] = slow_rate;
6f6677fc 509 STEPPER[Y_AXIS]->move(!diry, retract_steps, 0);
33e4cc02
JM
510
511 // wait until done
dd0a7cfa 512 while ( STEPPER[X_AXIS]->is_moving() || STEPPER[Y_AXIS]->is_moving()) {
314ab8f7 513 THEKERNEL->call_event(ON_IDLE);
ca287785 514 if(THEKERNEL->is_halted()) return;
f29b0272
JM
515 }
516
33e4cc02 517 // Start moving the axes to the origin slowly
2ddf75fd 518 this->status = MOVING_TO_ENDSTOP_SLOW;
e714bd32 519 this->feed_rate[X_AXIS] = slow_rate;
6f6677fc 520 STEPPER[X_AXIS]->move(dirx, 10000000, 0);
e714bd32 521 this->feed_rate[Y_AXIS] = slow_rate;
6f6677fc 522 STEPPER[Y_AXIS]->move(diry, 10000000, 0);
f29b0272 523
33e4cc02 524 // wait for primary axis
798295c1 525 if(!this->wait_for_homed_corexy(home_axis)) return;
33e4cc02 526}
f29b0272 527
33e4cc02
JM
528// this homing works for HBots/CoreXY
529void Endstops::do_homing_corexy(char axes_to_move)
530{
03dffc07 531 // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally
174d9961 532 // To move XY at the same time only one motor needs to turn, determine which motor and which direction based on min or max directions
03dffc07 533 // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed
174d9961 534 // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit
174d9961
JM
535
536 if((axes_to_move & 0x03) == 0x03) { // both X and Y need Homing
537 // determine which motor to turn and which way
e714bd32
JM
538 bool dirx = this->home_direction[X_AXIS];
539 bool diry = this->home_direction[Y_AXIS];
174d9961
JM
540 int motor;
541 bool dir;
542 if(dirx && diry) { // min/min
e714bd32
JM
543 motor = X_AXIS;
544 dir = true;
545 } else if(dirx && !diry) { // min/max
546 motor = Y_AXIS;
547 dir = true;
548 } else if(!dirx && diry) { // max/min
549 motor = Y_AXIS;
550 dir = false;
551 } else if(!dirx && !diry) { // max/max
552 motor = X_AXIS;
553 dir = false;
174d9961
JM
554 }
555
556 // then move both X and Y until one hits the endstop
2ddf75fd 557 this->status = MOVING_TO_ENDSTOP_FAST;
e714bd32
JM
558 // need to allow for more ground covered when moving diagonally
559 this->feed_rate[motor] = this->fast_rates[motor] * 1.4142;
6f6677fc 560 STEPPER[motor]->move(dir, 10000000, 0);
174d9961 561 // wait until either X or Y hits the endstop
e714bd32 562 bool running = true;
174d9961 563 while (running) {
314ab8f7 564 THEKERNEL->call_event(ON_IDLE);
ca287785 565 if(THEKERNEL->is_halted()) return;
e714bd32 566 for(int m = X_AXIS; m <= Y_AXIS; m++) {
174d9961
JM
567 if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) {
568 // turn off motor
dd0a7cfa 569 if(STEPPER[motor]->is_moving()) STEPPER[motor]->move(0, 0);
e714bd32 570 running = false;
174d9961
JM
571 break;
572 }
573 }
574 }
575 }
33e4cc02 576
174d9961 577 // move individual axis
33e4cc02 578 if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
e714bd32 579 bool dir = this->home_direction[X_AXIS];
56ce2b5a 580 corexy_home(X_AXIS, dir, dir, this->fast_rates[X_AXIS], this->slow_rates[X_AXIS], this->retract_mm[X_AXIS]*STEPS_PER_MM(X_AXIS));
f29b0272
JM
581 }
582
65191d91 583 if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
e714bd32 584 bool dir = this->home_direction[Y_AXIS];
56ce2b5a 585 corexy_home(Y_AXIS, dir, !dir, this->fast_rates[Y_AXIS], this->slow_rates[Y_AXIS], this->retract_mm[Y_AXIS]*STEPS_PER_MM(Y_AXIS));
65191d91
JM
586 }
587
33e4cc02 588 if (axes_to_move & 0x04) { // move Z
81f02e89 589 do_homing_cartesian(0x04); // just home normally for Z
f29b0272 590 }
f29b0272
JM
591}
592
81f02e89
JM
593void Endstops::home(char axes_to_move)
594{
72833629
JM
595 // not a block move so disable the last tick setting
596 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
597 STEPPER[c]->set_moved_last_block(false);
598 }
599
e714bd32 600 if (is_corexy) {
81f02e89
JM
601 // corexy/HBot homing
602 do_homing_corexy(axes_to_move);
e714bd32 603 } else {
81f02e89
JM
604 // cartesian/delta homing
605 do_homing_cartesian(axes_to_move);
606 }
0058d8d4
JM
607
608 // make sure all steppers are off (especially if aborted)
609 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
610 STEPPER[c]->move(0, 0);
611 }
ca287785 612 this->status = NOT_HOMING;
81f02e89
JM
613}
614
201bcb94 615// Start homing sequences by response to GCode commands
33e4cc02 616void Endstops::on_gcode_received(void *argument)
c339d634 617{
33e4cc02 618 Gcode *gcode = static_cast<Gcode *>(argument);
e714bd32
JM
619 if ( gcode->has_g && gcode->g == 28) {
620 if( (gcode->subcode == 0 && THEKERNEL->is_grbl_mode()) || (gcode->subcode == 2 && !THEKERNEL->is_grbl_mode()) ) {
621 // G28 in grbl mode or G28.2 in normal mode will do a rapid to the predefined position
622 // TODO spec says if XYZ specified move to them first then move to MCS of specifed axis
623 char buf[32];
624 snprintf(buf, sizeof(buf), "G53 G0 X%f Y%f", saved_position[X_AXIS], saved_position[Y_AXIS]); // must use machine coordinates in case G92 or WCS is in effect
625 struct SerialMessage message;
626 message.message = buf;
627 message.stream = &(StreamOutput::NullStream);
628 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); // as it is a multi G code command
629 return;
630
631 } else if(THEKERNEL->is_grbl_mode() && gcode->subcode == 2) { // G28.2 in grbl mode forces homing (triggered by $H)
632 // fall through so it does homing cycle
633
634 } else if(gcode->subcode == 1) { // G28.1 set pre defined position
635 // saves current position in absolute machine coordinates
636 THEKERNEL->robot->get_axis_position(saved_position);
637 return;
638
639 } else if(gcode->subcode == 3) { // G28.3 is a smoothie special it sets manual homing
640 if(gcode->get_num_args() == 0) {
641 THEKERNEL->robot->reset_axis_position(0, 0, 0);
642 } else {
643 // do a manual homing based on current position, no endstops required
644 if(gcode->has_letter('X')) THEKERNEL->robot->reset_axis_position(gcode->get_value('X'), X_AXIS);
645 if(gcode->has_letter('Y')) THEKERNEL->robot->reset_axis_position(gcode->get_value('Y'), Y_AXIS);
646 if(gcode->has_letter('Z')) THEKERNEL->robot->reset_axis_position(gcode->get_value('Z'), Z_AXIS);
cbf7b053 647 }
e714bd32 648 return;
6e92ab91 649
e714bd32
JM
650 } else if(THEKERNEL->is_grbl_mode()) {
651 gcode->stream->printf("error:Unsupported command\n");
652 return;
653 }
201bcb94 654
e714bd32 655 // G28 is received, we have homing to do
201bcb94 656
e714bd32
JM
657 // First wait for the queue to be empty
658 THEKERNEL->conveyor->wait_for_empty_queue();
47bbe224 659
e714bd32
JM
660 // Do we move select axes or all of them
661 char axes_to_move = 0;
662 // only enable homing if the endstop is defined, deltas, scaras always home all axis
663 bool home_all = this->is_delta || this->is_scara || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') );
664
665 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
666 if ( (home_all || gcode->has_letter(c + 'X')) && this->pins[c + (this->home_direction[c] ? 0 : 3)].connected() ) {
667 axes_to_move += ( 1 << c );
df27a6a3 668 }
e714bd32 669 }
3b948656 670
e714bd32
JM
671 // Enable the motors
672 THEKERNEL->stepper->turn_enable_pins_on();
673
674 // do the actual homing
675 if(homing_order != 0) {
676 // if an order has been specified do it in the specified order
677 // homing order is 0b00ccbbaa where aa is 0,1,2 to specify the first axis, bb is the second and cc is the third
678 // eg 0b00100001 would be Y X Z, 0b00100100 would be X Y Z
679 for (uint8_t m = homing_order; m != 0; m >>= 2) {
680 int a = (1 << (m & 0x03)); // axis to move
681 if((a & axes_to_move) != 0) {
682 home(a);
80605954 683 }
e714bd32
JM
684 // check if on_halt (eg kill)
685 if(THEKERNEL->is_halted()) break;
80605954 686 }
3ffe27fb 687
e714bd32
JM
688 } else {
689 // they all home at the same time
690 home(axes_to_move);
691 }
692
693 // check if on_halt (eg kill)
694 if(THEKERNEL->is_halted()) {
07186543
JM
695 if(!THEKERNEL->is_grbl_mode()) {
696 THEKERNEL->streams->printf("Homing cycle aborted by kill\n");
697 }
e714bd32
JM
698 return;
699 }
798295c1 700
e714bd32
JM
701 if(home_all) {
702 // Here's where we would have been if the endstops were perfectly trimmed
703 float ideal_position[3] = {
704 this->homing_position[X_AXIS] + this->home_offset[X_AXIS],
705 this->homing_position[Y_AXIS] + this->home_offset[Y_AXIS],
706 this->homing_position[Z_AXIS] + this->home_offset[Z_AXIS]
707 };
708
709 bool has_endstop_trim = this->is_delta || this->is_scara;
710 if (has_endstop_trim) {
711 ActuatorCoordinates ideal_actuator_position;
712 THEKERNEL->robot->arm_solution->cartesian_to_actuator(ideal_position, ideal_actuator_position);
713
714 // We are actually not at the ideal position, but a trim away
715 ActuatorCoordinates real_actuator_position = {
716 ideal_actuator_position[X_AXIS] - this->trim_mm[X_AXIS],
717 ideal_actuator_position[Y_AXIS] - this->trim_mm[Y_AXIS],
718 ideal_actuator_position[Z_AXIS] - this->trim_mm[Z_AXIS]
7552475b
OL
719 };
720
e714bd32
JM
721 float real_position[3];
722 THEKERNEL->robot->arm_solution->actuator_to_cartesian(real_actuator_position, real_position);
723 // Reset the actuator positions to correspond our real position
724 THEKERNEL->robot->reset_axis_position(real_position[0], real_position[1], real_position[2]);
7552475b 725 } else {
e714bd32
JM
726 // without endstop trim, real_position == ideal_position
727 // Reset the actuator positions to correspond our real position
728 THEKERNEL->robot->reset_axis_position(ideal_position[0], ideal_position[1], ideal_position[2]);
729 }
730 } else {
731 // Zero the ax(i/e)s position, add in the home offset
732 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
733 if ( (axes_to_move >> c) & 1 ) {
734 THEKERNEL->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c);
3ffe27fb
AV
735 }
736 }
e714bd32 737 }
3c947f85 738
e714bd32
JM
739 // on some systems where 0,0 is bed center it is nice to have home goto 0,0 after homing
740 // default is off for cartesian on for deltas
741 if(!is_delta) {
742 if(this->move_to_origin_after_home) move_to_origin(axes_to_move);
743 // if limit switches are enabled we must back off endstop after setting home
744 back_off_home(axes_to_move);
745
746 } else if(this->move_to_origin_after_home || this->limit_enable[X_AXIS]) {
747 // deltas are not left at 0,0 because of the trim settings, so move to 0,0 if requested, but we need to back off endstops first
748 // also need to back off endstops if limits are enabled
749 back_off_home(axes_to_move);
750 if(this->move_to_origin_after_home) move_to_origin(axes_to_move);
c339d634 751 }
e714bd32 752 }
81f02e89 753
e714bd32 754 if (gcode->has_m) {
33e4cc02
JM
755 switch (gcode->m) {
756 case 119: {
ef7bd372
JM
757 for (int i = 0; i < 6; ++i) {
758 if(this->pins[i].connected())
759 gcode->stream->printf("%s:%d ", endstop_names[i], this->pins[i].get());
760 }
e714bd32 761 gcode->add_nl = true;
6e92ab91 762
33e4cc02
JM
763 }
764 break;
765
766 case 206: // M206 - set homing offset
504f0e3e
JM
767 if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X');
768 if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y');
769 if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z');
770 gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
6e92ab91 771
504f0e3e 772 break;
0e4bf280 773
e714bd32
JM
774 case 306: { // Similar to M206 and G92 but sets Homing offsets based on current position
775 float cartesian[3];
776 THEKERNEL->robot->get_axis_position(cartesian); // get actual position from robot
777 if (gcode->has_letter('X')) {
778 home_offset[0] -= (cartesian[X_AXIS] - gcode->get_value('X'));
779 THEKERNEL->robot->reset_axis_position(gcode->get_value('X'), X_AXIS);
780 }
781 if (gcode->has_letter('Y')) {
782 home_offset[1] -= (cartesian[Y_AXIS] - gcode->get_value('Y'));
783 THEKERNEL->robot->reset_axis_position(gcode->get_value('Y'), Y_AXIS);
784 }
785 if (gcode->has_letter('Z')) {
786 home_offset[2] -= (cartesian[Z_AXIS] - gcode->get_value('Z'));
787 THEKERNEL->robot->reset_axis_position(gcode->get_value('Z'), Z_AXIS);
788 }
9ae8c7e4 789
e714bd32 790 gcode->stream->printf("Homing Offset: X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
6e92ab91 791
e714bd32
JM
792 }
793 break;
33e4cc02
JM
794
795 case 500: // save settings
796 case 503: // print settings
797 gcode->stream->printf(";Home offset (mm):\nM206 X%1.2f Y%1.2f Z%1.2f\n", home_offset[0], home_offset[1], home_offset[2]);
d0280b9d 798 if (this->is_delta || this->is_scara) {
56ce2b5a
JM
799 gcode->stream->printf(";Trim (mm):\nM666 X%1.3f Y%1.3f Z%1.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
800 gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position[2]);
7a8fe6e0 801 }
e714bd32
JM
802 if(saved_position[X_AXIS] != 0 || saved_position[Y_AXIS] != 0) {
803 gcode->stream->printf(";predefined position:\nG28.1 X%1.4f Y%1.4f Z%1.4f\n", saved_position[X_AXIS], saved_position[Y_AXIS], saved_position[Z_AXIS]);
804 }
c339d634 805 break;
47bbe224 806
ec4773e5 807 case 665: { // M665 - set max gamma/z height
6e92ab91 808
1ad23cd3 809 float gamma_max = this->homing_position[2];
33e4cc02
JM
810 if (gcode->has_letter('Z')) {
811 this->homing_position[2] = gamma_max = gcode->get_value('Z');
ec4773e5 812 }
33e4cc02
JM
813 gcode->stream->printf("Max Z %8.3f ", gamma_max);
814 gcode->add_nl = true;
815 }
816 break;
ec4773e5 817
47bbe224 818
56ce2b5a 819 case 666:
3e1f5b74 820 if(this->is_delta || this->is_scara) { // M666 - set trim for each axis in mm, NB negative mm trim is down
56ce2b5a
JM
821 if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X');
822 if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y');
823 if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z');
47bbe224 824
56ce2b5a
JM
825 // print the current trim values in mm
826 gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
6e92ab91 827
56ce2b5a 828 }
e714bd32 829 break;
47bbe224 830
d4ee6ee2 831 // NOTE this is to test accuracy of lead screws etc.
fc320ac5 832 case 1910: { // M1910 - move specific number of raw steps
b023850b
JM
833 if(gcode->subcode == 0) {
834 // Enable the motors
835 THEKERNEL->stepper->turn_enable_pins_on();
836
e714bd32 837 int x = 0, y = 0 , z = 0, f = 200 * 16;
b023850b
JM
838 if (gcode->has_letter('F')) f = gcode->get_value('F');
839 if (gcode->has_letter('X')) {
840 x = gcode->get_value('X');
e714bd32 841 STEPPER[X_AXIS]->move(x < 0, abs(x), f);
b023850b
JM
842 }
843 if (gcode->has_letter('Y')) {
844 y = gcode->get_value('Y');
e714bd32 845 STEPPER[Y_AXIS]->move(y < 0, abs(y), f);
b023850b
JM
846 }
847 if (gcode->has_letter('Z')) {
848 z = gcode->get_value('Z');
e714bd32 849 STEPPER[Z_AXIS]->move(z < 0, abs(z), f);
b023850b
JM
850 }
851 gcode->stream->printf("Moving X %d Y %d Z %d steps at F %d steps/sec\n", x, y, z, f);
852
e714bd32 853 } else if(gcode->subcode == 1) {
b023850b
JM
854 // stop any that are moving
855 for (int i = 0; i < 3; ++i) {
e714bd32
JM
856 if(STEPPER[i]->is_moving()) STEPPER[i]->move(0, 0);
857 }
d4ee6ee2 858 }
d4ee6ee2
JM
859 break;
860 }
201bcb94
AW
861 }
862 }
863}
864
64eaf21e 865// Called periodically to change the speed to match acceleration
a157d099 866void Endstops::acceleration_tick(void)
64eaf21e 867{
a157d099 868 if(this->status >= NOT_HOMING) return; // nothing to do, only do this when moving for homing sequence
64eaf21e
JM
869
870 // foreach stepper that is moving
871 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
dd0a7cfa 872 if( !STEPPER[c]->is_moving() ) continue;
64eaf21e 873
dd0a7cfa 874 uint32_t current_rate = STEPPER[c]->get_steps_per_second();
e714bd32
JM
875 uint32_t target_rate = floorf(this->feed_rate[c] * STEPS_PER_MM(c));
876 float acc = (c == Z_AXIS) ? THEKERNEL->planner->get_z_acceleration() : THEKERNEL->planner->get_acceleration();
877 if( current_rate < target_rate ) {
878 uint32_t rate_increase = floorf((acc / THEKERNEL->acceleration_ticks_per_second) * STEPS_PER_MM(c));
64eaf21e
JM
879 current_rate = min( target_rate, current_rate + rate_increase );
880 }
e714bd32 881 if( current_rate > target_rate ) { current_rate = target_rate; }
64eaf21e
JM
882
883 // steps per second
6f6677fc 884 STEPPER[c]->set_speed(current_rate);
64eaf21e
JM
885 }
886
a157d099 887 return;
64eaf21e 888}
9f6f04a5 889
e714bd32
JM
890void Endstops::on_get_public_data(void* argument)
891{
9f6f04a5
JM
892 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
893
894 if(!pdr->starts_with(endstops_checksum)) return;
895
896 if(pdr->second_element_is(trim_checksum)) {
86fa0b93 897 pdr->set_data_ptr(&this->trim_mm);
ea5c6d92
JM
898 pdr->set_taken();
899
e714bd32 900 } else if(pdr->second_element_is(home_offset_checksum)) {
86fa0b93 901 pdr->set_data_ptr(&this->home_offset);
9f6f04a5 902 pdr->set_taken();
e714bd32
JM
903
904 } else if(pdr->second_element_is(saved_position_checksum)) {
905 pdr->set_data_ptr(&this->saved_position);
906 pdr->set_taken();
07186543
JM
907
908 } else if(pdr->second_element_is(get_homing_status_checksum)) {
909 bool *homing= static_cast<bool *>(pdr->get_data_ptr());
910 *homing= this->status != NOT_HOMING;
911 pdr->set_taken();
9f6f04a5
JM
912 }
913}
7d6fe308 914
e714bd32
JM
915void Endstops::on_set_public_data(void* argument)
916{
7d6fe308
JM
917 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
918
919 if(!pdr->starts_with(endstops_checksum)) return;
920
921 if(pdr->second_element_is(trim_checksum)) {
e714bd32
JM
922 float *t = static_cast<float*>(pdr->get_data_ptr());
923 this->trim_mm[0] = t[0];
924 this->trim_mm[1] = t[1];
925 this->trim_mm[2] = t[2];
7d6fe308 926 pdr->set_taken();
ea5c6d92 927
e714bd32
JM
928 } else if(pdr->second_element_is(home_offset_checksum)) {
929 float *t = static_cast<float*>(pdr->get_data_ptr());
930 if(!isnan(t[0])) this->home_offset[0] = t[0];
931 if(!isnan(t[1])) this->home_offset[1] = t[1];
932 if(!isnan(t[2])) this->home_offset[2] = t[2];
7d6fe308
JM
933 }
934}