fix pause button to be able to unpause system regardless of what caused it to pause.
[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"
201bcb94
AW
12#include "Endstops.h"
13#include "libs/nuts_bolts.h"
750277f8 14#include "libs/Pin.h"
670fa10b 15#include "libs/StepperMotor.h"
201bcb94 16#include "wait_api.h" // mbed.h lib
61134a65
JM
17#include "Robot.h"
18#include "Stepper.h"
19#include "Config.h"
20#include "SlowTicker.h"
21#include "Planner.h"
7af0714f
JM
22#include "checksumm.h"
23#include "utils.h"
8d54c34c 24#include "ConfigValue.h"
fc7b9a7b 25#include "libs/StreamOutput.h"
9f6f04a5
JM
26#include "PublicDataRequest.h"
27#include "EndstopsPublicAccess.h"
3c947f85
JM
28#include "StreamOutputPool.h"
29#include "Pauser.h"
201bcb94 30
33e4cc02
JM
31#define ALPHA_AXIS 0
32#define BETA_AXIS 1
33#define GAMMA_AXIS 2
9a993543
JM
34#define X_AXIS 0
35#define Y_AXIS 1
36#define Z_AXIS 2
33e4cc02 37
33e4cc02
JM
38#define endstops_module_enable_checksum CHECKSUM("endstops_enable")
39#define corexy_homing_checksum CHECKSUM("corexy_homing")
40#define delta_homing_checksum CHECKSUM("delta_homing")
41
42#define alpha_min_endstop_checksum CHECKSUM("alpha_min_endstop")
43#define beta_min_endstop_checksum CHECKSUM("beta_min_endstop")
44#define gamma_min_endstop_checksum CHECKSUM("gamma_min_endstop")
45
46#define alpha_max_endstop_checksum CHECKSUM("alpha_max_endstop")
47#define beta_max_endstop_checksum CHECKSUM("beta_max_endstop")
48#define gamma_max_endstop_checksum CHECKSUM("gamma_max_endstop")
49
50#define alpha_trim_checksum CHECKSUM("alpha_trim")
51#define beta_trim_checksum CHECKSUM("beta_trim")
52#define gamma_trim_checksum CHECKSUM("gamma_trim")
53
54// these values are in steps and should be deprecated
55#define alpha_fast_homing_rate_checksum CHECKSUM("alpha_fast_homing_rate")
56#define beta_fast_homing_rate_checksum CHECKSUM("beta_fast_homing_rate")
57#define gamma_fast_homing_rate_checksum CHECKSUM("gamma_fast_homing_rate")
58
59#define alpha_slow_homing_rate_checksum CHECKSUM("alpha_slow_homing_rate")
60#define beta_slow_homing_rate_checksum CHECKSUM("beta_slow_homing_rate")
61#define gamma_slow_homing_rate_checksum CHECKSUM("gamma_slow_homing_rate")
62
63#define alpha_homing_retract_checksum CHECKSUM("alpha_homing_retract")
64#define beta_homing_retract_checksum CHECKSUM("beta_homing_retract")
65#define gamma_homing_retract_checksum CHECKSUM("gamma_homing_retract")
66#define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
67
68// same as above but in user friendly mm/s and mm
69#define alpha_fast_homing_rate_mm_checksum CHECKSUM("alpha_fast_homing_rate_mm_s")
70#define beta_fast_homing_rate_mm_checksum CHECKSUM("beta_fast_homing_rate_mm_s")
71#define gamma_fast_homing_rate_mm_checksum CHECKSUM("gamma_fast_homing_rate_mm_s")
72
73#define alpha_slow_homing_rate_mm_checksum CHECKSUM("alpha_slow_homing_rate_mm_s")
74#define beta_slow_homing_rate_mm_checksum CHECKSUM("beta_slow_homing_rate_mm_s")
75#define gamma_slow_homing_rate_mm_checksum CHECKSUM("gamma_slow_homing_rate_mm_s")
76
77#define alpha_homing_retract_mm_checksum CHECKSUM("alpha_homing_retract_mm")
78#define beta_homing_retract_mm_checksum CHECKSUM("beta_homing_retract_mm")
79#define gamma_homing_retract_mm_checksum CHECKSUM("gamma_homing_retract_mm")
80
81#define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
82
83#define alpha_homing_direction_checksum CHECKSUM("alpha_homing_direction")
84#define beta_homing_direction_checksum CHECKSUM("beta_homing_direction")
85#define gamma_homing_direction_checksum CHECKSUM("gamma_homing_direction")
86#define home_to_max_checksum CHECKSUM("home_to_max")
87#define home_to_min_checksum CHECKSUM("home_to_min")
88#define alpha_min_checksum CHECKSUM("alpha_min")
89#define beta_min_checksum CHECKSUM("beta_min")
90#define gamma_min_checksum CHECKSUM("gamma_min")
91
92#define alpha_max_checksum CHECKSUM("alpha_max")
93#define beta_max_checksum CHECKSUM("beta_max")
94#define gamma_max_checksum CHECKSUM("gamma_max")
95
3c947f85
JM
96#define alpha_limit_enable_checksum CHECKSUM("alpha_limit_enable")
97#define beta_limit_enable_checksum CHECKSUM("beta_limit_enable")
98#define gamma_limit_enable_checksum CHECKSUM("gamma_limit_enable")
99
dd0a7cfa
JM
100#define STEPPER THEKERNEL->robot->actuators
101#define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
56ce2b5a 102
3c947f85
JM
103// Homing States
104enum{
105 NOT_HOMING,
106 MOVING_TO_ORIGIN_FAST,
107 MOVING_BACK,
108 MOVING_TO_ORIGIN_SLOW,
109 BACK_OFF_HOME,
110 LIMIT_TRIGGERED
111};
112
33e4cc02
JM
113Endstops::Endstops()
114{
201bcb94 115 this->status = NOT_HOMING;
33e4cc02 116 home_offset[0] = home_offset[1] = home_offset[2] = 0.0F;
201bcb94
AW
117}
118
33e4cc02
JM
119void Endstops::on_module_loaded()
120{
7dee696d 121 // Do not do anything if not enabled
314ab8f7 122 if ( THEKERNEL->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) {
4e722c00 123 delete this;
33e4cc02
JM
124 return;
125 }
7d62445b 126
9f6f04a5
JM
127 register_for_event(ON_GCODE_RECEIVED);
128 register_for_event(ON_GET_PUBLIC_DATA);
7d6fe308 129 register_for_event(ON_SET_PUBLIC_DATA);
201bcb94 130
38bf9a1c 131 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->get_acceleration_ticks_per_second() , this, &Endstops::acceleration_tick );
201bcb94 132
750277f8
AW
133 // Settings
134 this->on_config_reload(this);
201bcb94
AW
135}
136
750277f8 137// Get config
33e4cc02
JM
138void Endstops::on_config_reload(void *argument)
139{
56ce2b5a
JM
140 this->pins[0].from_string( THEKERNEL->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
141 this->pins[1].from_string( THEKERNEL->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
142 this->pins[2].from_string( THEKERNEL->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
143 this->pins[3].from_string( THEKERNEL->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
144 this->pins[4].from_string( THEKERNEL->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
145 this->pins[5].from_string( THEKERNEL->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
146
147 // These are the old ones in steps still here for backwards compatibility
148 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(0);
149 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(1);
150 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number() / STEPS_PER_MM(2);
151 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(0);
152 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(1);
153 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number() / STEPS_PER_MM(2);
154 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(0);
155 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(1);
156 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number() / STEPS_PER_MM(2);
5de98d7c
JM
157
158 // 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
159 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0])->as_number();
160 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[1])->as_number();
161 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2])->as_number();
162 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0])->as_number();
163 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[1])->as_number();
164 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2])->as_number();
165 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum )->by_default(this->retract_mm[0])->as_number();
166 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum )->by_default(this->retract_mm[1])->as_number();
167 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum )->by_default(this->retract_mm[2])->as_number();
5de98d7c 168
06c48ee8 169 this->debounce_count = THEKERNEL->config->value(endstop_debounce_count_checksum )->by_default(100)->as_number();
5de98d7c
JM
170
171
409ff5b3 172 // get homing direction and convert to boolean where true is home to min, and false is home to max
314ab8f7 173 int home_dir = get_checksum(THEKERNEL->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 174 this->home_direction[0] = home_dir != home_to_max_checksum;
47bbe224 175
314ab8f7 176 home_dir = get_checksum(THEKERNEL->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 177 this->home_direction[1] = home_dir != home_to_max_checksum;
47bbe224 178
314ab8f7 179 home_dir = get_checksum(THEKERNEL->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 180 this->home_direction[2] = home_dir != home_to_max_checksum;
47bbe224 181
314ab8f7
MM
182 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();
183 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();;
184 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 185
314ab8f7
MM
186 this->is_corexy = THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool();
187 this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool();
e678365e 188
192bca32
JM
189 // endstop trim used by deltas to do soft adjusting
190 // on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
191 this->trim_mm[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0 )->as_number();
192 this->trim_mm[1] = THEKERNEL->config->value(beta_trim_checksum )->by_default(0 )->as_number();
193 this->trim_mm[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0 )->as_number();
3c947f85
JM
194
195 // limits enabled
196 this->limit_enable[X_AXIS]= THEKERNEL->config->value(alpha_limit_enable_checksum)->by_default(false)->as_bool();
197 this->limit_enable[Y_AXIS]= THEKERNEL->config->value(beta_limit_enable_checksum)->by_default(false)->as_bool();
198 this->limit_enable[Z_AXIS]= THEKERNEL->config->value(gamma_limit_enable_checksum)->by_default(false)->as_bool();
199
200 if(this->limit_enable[X_AXIS] || this->limit_enable[Y_AXIS] || this->limit_enable[Z_AXIS]){
201 register_for_event(ON_IDLE);
202 }
203}
204
205static const char *endstop_names[]= {"MIN_X", "MIN_Y", "MIN_Z", "MAX_X", "MAX_Y", "MAX_Z"};
206
207void Endstops::on_idle(void *argument)
208{
209 if(this->status != NOT_HOMING) return; // don't check while homing or if a LIMIT was triggered
210
211 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
212 if(this->limit_enable[c] && STEPPER[c]->is_moving()) {
213 std::array<int, 2> minmax{{0, 3}};
214 // check min and max endstops
215 for (int i : minmax) {
216 int n= c+i;
217 uint8_t debounce= 0;
218 while(this->pins[n].get()) {
219 if ( ++debounce >= debounce_count ) {
220 // endstop triggered
221 THEKERNEL->pauser->take();
222 THEKERNEL->streams->printf("Limit switch %s was hit - reset required\n", endstop_names[n]);
223 this->status= LIMIT_TRIGGERED;
224 return;
225 }
226 }
227 }
228 }
229 }
230}
231
232// if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move
233void Endstops::back_off_home()
234{
235 this->status = BACK_OFF_HOME;
236 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
237 if(this->limit_enable[c]) {
238 // Move off of the endstop using a regular relative move
239 char buf[32];
240 snprintf(buf, sizeof(buf), "G0 %c%1.4f F%1.4f", c+'X', this->retract_mm[c]*(this->home_direction[c]?1:-1), this->slow_rates[c]*60.0F);
241 Gcode gc(buf, &(StreamOutput::NullStream));
242 bool oldmode= THEKERNEL->robot->absolute_mode;
243 THEKERNEL->robot->absolute_mode= false; // needs to be relative mode
244 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
245 THEKERNEL->robot->absolute_mode= oldmode; // restore mode
246 }
247 }
248 // Wait for above to finish
249 THEKERNEL->conveyor->wait_for_empty_queue();
250 this->status = NOT_HOMING;
a0e0d592
BG
251}
252
33e4cc02
JM
253void Endstops::wait_for_homed(char axes_to_move)
254{
a0e0d592 255 bool running = true;
33e4cc02
JM
256 unsigned int debounce[3] = {0, 0, 0};
257 while (running) {
a0e0d592 258 running = false;
314ab8f7 259 THEKERNEL->call_event(ON_IDLE);
56ce2b5a
JM
260 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
261 if ( ( axes_to_move >> c ) & 1 ) {
262 if ( this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) {
263 if ( debounce[c] < debounce_count ) {
264 debounce[c]++;
a0e0d592 265 running = true;
dd0a7cfa
JM
266 } else if ( STEPPER[c]->is_moving() ) {
267 STEPPER[c]->move(0, 0);
a0e0d592 268 }
33e4cc02 269 } else {
a0e0d592
BG
270 // The endstop was not hit yet
271 running = true;
56ce2b5a 272 debounce[c] = 0;
323cca60 273 }
a0e0d592
BG
274 }
275 }
276 }
750277f8 277}
201bcb94 278
f29b0272 279// this homing works for cartesian and delta printers, not for HBots/CoreXY
33e4cc02
JM
280void Endstops::do_homing(char axes_to_move)
281{
f29b0272
JM
282 // Start moving the axes to the origin
283 this->status = MOVING_TO_ORIGIN_FAST;
56ce2b5a
JM
284 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
285 if ( ( axes_to_move >> c) & 1 ) {
286 this->feed_rate[c]= this->fast_rates[c];
dd0a7cfa
JM
287 STEPPER[c]->set_speed(0);
288 STEPPER[c]->move(this->home_direction[c], 10000000);
f29b0272
JM
289 }
290 }
291
292 // Wait for all axes to have homed
293 this->wait_for_homed(axes_to_move);
294
295 // Move back a small distance
296 this->status = MOVING_BACK;
297 bool inverted_dir;
56ce2b5a
JM
298 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
299 if ( ( axes_to_move >> c ) & 1 ) {
300 inverted_dir = !this->home_direction[c];
301 this->feed_rate[c]= this->slow_rates[c];
dd0a7cfa
JM
302 STEPPER[c]->set_speed(0);
303 STEPPER[c]->move(inverted_dir, this->retract_mm[c]*STEPS_PER_MM(c));
f29b0272
JM
304 }
305 }
306
33e4cc02 307 // Wait for moves to be done
56ce2b5a
JM
308 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
309 if ( ( axes_to_move >> c ) & 1 ) {
dd0a7cfa 310 while ( STEPPER[c]->is_moving() ) {
314ab8f7 311 THEKERNEL->call_event(ON_IDLE);
f29b0272
JM
312 }
313 }
314 }
315
316 // Start moving the axes to the origin slowly
317 this->status = MOVING_TO_ORIGIN_SLOW;
56ce2b5a
JM
318 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
319 if ( ( axes_to_move >> c ) & 1 ) {
320 this->feed_rate[c]= this->slow_rates[c];
dd0a7cfa
JM
321 STEPPER[c]->set_speed(0);
322 STEPPER[c]->move(this->home_direction[c], 10000000);
f29b0272
JM
323 }
324 }
325
326 // Wait for all axes to have homed
327 this->wait_for_homed(axes_to_move);
328
33e4cc02 329 if (this->is_delta) {
959ab59c
JM
330 // move for soft trim
331 this->status = MOVING_BACK;
56ce2b5a
JM
332 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
333 if ( this->trim_mm[c] != 0.0F && ( axes_to_move >> c ) & 1 ) {
334 inverted_dir = this->home_direction[c];
335 // move up or down depending on sign of trim, -ive is down away from home
336 if (this->trim_mm[c] < 0) inverted_dir = !inverted_dir;
337 this->feed_rate[c]= this->slow_rates[c];
dd0a7cfa
JM
338 STEPPER[c]->set_speed(0);
339 STEPPER[c]->move(inverted_dir, abs(round(this->trim_mm[c]*STEPS_PER_MM(c))));
959ab59c 340 }
f29b0272 341 }
f29b0272 342
959ab59c 343 // Wait for moves to be done
56ce2b5a
JM
344 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
345 if ( ( axes_to_move >> c ) & 1 ) {
314ab8f7 346 //THEKERNEL->streams->printf("axis %c \r\n", c );
dd0a7cfa 347 while ( STEPPER[c]->is_moving() ) {
314ab8f7 348 THEKERNEL->call_event(ON_IDLE);
959ab59c 349 }
f29b0272
JM
350 }
351 }
352 }
353
354 // Homing is done
355 this->status = NOT_HOMING;
356}
357
33e4cc02
JM
358void Endstops::wait_for_homed_corexy(int axis)
359{
3db88866 360 bool running = true;
33e4cc02
JM
361 unsigned int debounce[3] = {0, 0, 0};
362 while (running) {
3db88866 363 running = false;
314ab8f7 364 THEKERNEL->call_event(ON_IDLE);
33e4cc02
JM
365 if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) {
366 if ( debounce[axis] < debounce_count ) {
3db88866
JM
367 debounce[axis] ++;
368 running = true;
369 } else {
370 // turn both off if running
dd0a7cfa
JM
371 if (STEPPER[X_AXIS]->is_moving()) STEPPER[X_AXIS]->move(0, 0);
372 if (STEPPER[Y_AXIS]->is_moving()) STEPPER[Y_AXIS]->move(0, 0);
3db88866 373 }
33e4cc02 374 } else {
3db88866
JM
375 // The endstop was not hit yet
376 running = true;
377 debounce[axis] = 0;
378 }
379 }
380}
381
1ad23cd3 382void Endstops::corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps)
33e4cc02
JM
383{
384 this->status = MOVING_TO_ORIGIN_FAST;
64eaf21e 385 this->feed_rate[X_AXIS]= fast_rate;
dd0a7cfa
JM
386 STEPPER[X_AXIS]->set_speed(0);
387 STEPPER[X_AXIS]->move(dirx, 10000000);
64eaf21e 388 this->feed_rate[Y_AXIS]= fast_rate;
dd0a7cfa
JM
389 STEPPER[Y_AXIS]->set_speed(0);
390 STEPPER[Y_AXIS]->move(diry, 10000000);
f29b0272 391
33e4cc02
JM
392 // wait for primary axis
393 this->wait_for_homed_corexy(home_axis);
f29b0272 394
33e4cc02
JM
395 // Move back a small distance
396 this->status = MOVING_BACK;
64eaf21e 397 this->feed_rate[X_AXIS]= slow_rate;
dd0a7cfa
JM
398 STEPPER[X_AXIS]->set_speed(0);
399 STEPPER[X_AXIS]->move(!dirx, retract_steps);
64eaf21e 400 this->feed_rate[Y_AXIS]= slow_rate;
dd0a7cfa
JM
401 STEPPER[Y_AXIS]->set_speed(0);
402 STEPPER[Y_AXIS]->move(!diry, retract_steps);
33e4cc02
JM
403
404 // wait until done
dd0a7cfa 405 while ( STEPPER[X_AXIS]->is_moving() || STEPPER[Y_AXIS]->is_moving()) {
314ab8f7 406 THEKERNEL->call_event(ON_IDLE);
f29b0272
JM
407 }
408
33e4cc02
JM
409 // Start moving the axes to the origin slowly
410 this->status = MOVING_TO_ORIGIN_SLOW;
64eaf21e 411 this->feed_rate[X_AXIS]= slow_rate;
dd0a7cfa
JM
412 STEPPER[X_AXIS]->set_speed(0);
413 STEPPER[X_AXIS]->move(dirx, 10000000);
64eaf21e 414 this->feed_rate[Y_AXIS]= slow_rate;
dd0a7cfa
JM
415 STEPPER[Y_AXIS]->set_speed(0);
416 STEPPER[Y_AXIS]->move(diry, 10000000);
f29b0272 417
33e4cc02
JM
418 // wait for primary axis
419 this->wait_for_homed_corexy(home_axis);
420}
f29b0272 421
33e4cc02
JM
422// this homing works for HBots/CoreXY
423void Endstops::do_homing_corexy(char axes_to_move)
424{
03dffc07 425 // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally
174d9961 426 // 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 427 // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed
174d9961 428 // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit
174d9961
JM
429
430 if((axes_to_move & 0x03) == 0x03) { // both X and Y need Homing
431 // determine which motor to turn and which way
432 bool dirx= this->home_direction[X_AXIS];
433 bool diry= this->home_direction[Y_AXIS];
434 int motor;
435 bool dir;
436 if(dirx && diry) { // min/min
437 motor= X_AXIS;
438 dir= true;
439 }else if(dirx && !diry) { // min/max
440 motor= Y_AXIS;
441 dir= true;
442 }else if(!dirx && diry) { // max/min
443 motor= Y_AXIS;
444 dir= false;
445 }else if(!dirx && !diry) { // max/max
446 motor= X_AXIS;
447 dir= false;
448 }
449
450 // then move both X and Y until one hits the endstop
451 this->status = MOVING_TO_ORIGIN_FAST;
64eaf21e 452 this->feed_rate[motor]= this->fast_rates[motor]*1.4142;
dd0a7cfa
JM
453 STEPPER[motor]->set_speed(0); // need to allow for more ground covered when moving diagonally
454 STEPPER[motor]->move(dir, 10000000);
174d9961
JM
455 // wait until either X or Y hits the endstop
456 bool running= true;
457 while (running) {
314ab8f7 458 THEKERNEL->call_event(ON_IDLE);
174d9961
JM
459 for(int m=X_AXIS;m<=Y_AXIS;m++) {
460 if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) {
461 // turn off motor
dd0a7cfa 462 if(STEPPER[motor]->is_moving()) STEPPER[motor]->move(0, 0);
174d9961
JM
463 running= false;
464 break;
465 }
466 }
467 }
468 }
33e4cc02 469
174d9961 470 // move individual axis
33e4cc02 471 if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
413dd90c 472 bool dir= this->home_direction[X_AXIS];
56ce2b5a 473 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
474 }
475
65191d91
JM
476 if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
477 bool dir= this->home_direction[Y_AXIS];
56ce2b5a 478 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
479 }
480
33e4cc02 481 if (axes_to_move & 0x04) { // move Z
f29b0272
JM
482 do_homing(0x04); // just home normally for Z
483 }
484
485 // Homing is done
486 this->status = NOT_HOMING;
487}
488
201bcb94 489// Start homing sequences by response to GCode commands
33e4cc02 490void Endstops::on_gcode_received(void *argument)
c339d634 491{
33e4cc02
JM
492 Gcode *gcode = static_cast<Gcode *>(argument);
493 if ( gcode->has_g) {
494 if ( gcode->g == 28 ) {
74b6303c 495 gcode->mark_as_taken();
df27a6a3 496 // G28 is received, we have homing to do
201bcb94
AW
497
498 // First wait for the queue to be empty
314ab8f7 499 THEKERNEL->conveyor->wait_for_empty_queue();
201bcb94
AW
500
501 // Do we move select axes or all of them
7dee696d 502 char axes_to_move = 0;
959ab59c 503 // only enable homing if the endstop is defined, deltas always home all axis
33e4cc02 504 bool home_all = this->is_delta || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') );
47bbe224 505
56ce2b5a
JM
506 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
507 if ( (home_all || gcode->has_letter(c+'X')) && this->pins[c + (this->home_direction[c] ? 0 : 3)].connected() ) {
508 axes_to_move += ( 1 << c );
33e4cc02 509 }
df27a6a3 510 }
3b948656 511
f6c04440 512 // Enable the motors
314ab8f7 513 THEKERNEL->stepper->turn_enable_pins_on();
f6c04440 514
f29b0272 515 // do the actual homing
33e4cc02 516 if (is_corexy)
f29b0272
JM
517 do_homing_corexy(axes_to_move);
518 else
519 do_homing(axes_to_move);
3ffe27fb 520
33e4cc02 521 // Zero the ax(i/e)s position, add in the home offset
56ce2b5a 522 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
33e4cc02 523 if ( (axes_to_move >> c) & 1 ) {
314ab8f7 524 THEKERNEL->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c);
3ffe27fb
AV
525 }
526 }
3c947f85
JM
527
528 // if limit switches are enabled we must back off endstop after setting home
529 back_off_home();
c339d634 530 }
33e4cc02
JM
531 } else if (gcode->has_m) {
532 switch (gcode->m) {
533 case 119: {
534
535 int px = this->home_direction[0] ? 0 : 3;
536 int py = this->home_direction[1] ? 1 : 4;
537 int pz = this->home_direction[2] ? 2 : 5;
538 const char *mx = this->home_direction[0] ? "min" : "max";
539 const char *my = this->home_direction[1] ? "min" : "max";
540 const char *mz = this->home_direction[2] ? "min" : "max";
541
bd96f4d7
JM
542 gcode->stream->printf("X %s:%d Y %s:%d Z %s:%d", mx, this->pins[px].get(), my, this->pins[py].get(), mz, this->pins[pz].get());
543 gcode->add_nl= true;
33e4cc02
JM
544 gcode->mark_as_taken();
545 }
546 break;
547
548 case 206: // M206 - set homing offset
549 if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X');
550 if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y');
551 if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z');
552 gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
553 gcode->mark_as_taken();
554 break;
555
556 case 500: // save settings
557 case 503: // print settings
558 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]);
559 if (is_delta) {
56ce2b5a
JM
560 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]);
561 gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position[2]);
7a8fe6e0 562 }
33e4cc02 563 gcode->mark_as_taken();
c339d634 564 break;
47bbe224 565
ec4773e5 566 case 665: { // M665 - set max gamma/z height
33e4cc02 567 gcode->mark_as_taken();
1ad23cd3 568 float gamma_max = this->homing_position[2];
33e4cc02
JM
569 if (gcode->has_letter('Z')) {
570 this->homing_position[2] = gamma_max = gcode->get_value('Z');
ec4773e5 571 }
33e4cc02
JM
572 gcode->stream->printf("Max Z %8.3f ", gamma_max);
573 gcode->add_nl = true;
574 }
575 break;
ec4773e5 576
47bbe224 577
56ce2b5a
JM
578 case 666:
579 if(this->is_delta) { // M666 - set trim for each axis in mm, NB negative mm trim is down
580 if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X');
581 if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y');
582 if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z');
47bbe224 583
56ce2b5a
JM
584 // print the current trim values in mm
585 gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
586 gcode->mark_as_taken();
587 }
33e4cc02 588 break;
47bbe224 589
d4ee6ee2
JM
590 // NOTE this is to test accuracy of lead screws etc.
591 case 910: { // M910 - move specific number of raw steps
a4b04adc
JM
592 // Enable the motors
593 THEKERNEL->stepper->turn_enable_pins_on();
594
d4ee6ee2
JM
595 int x= 0, y=0 , z= 0, f= 200*16;
596 if (gcode->has_letter('F')) f = gcode->get_value('F');
597 if (gcode->has_letter('X')) {
598 x = gcode->get_value('X');
dd0a7cfa
JM
599 STEPPER[X_AXIS]->set_speed(f);
600 STEPPER[X_AXIS]->move(x<0, abs(x));
d4ee6ee2
JM
601 }
602 if (gcode->has_letter('Y')) {
603 y = gcode->get_value('Y');
dd0a7cfa
JM
604 STEPPER[Y_AXIS]->set_speed(f);
605 STEPPER[Y_AXIS]->move(y<0, abs(y));
d4ee6ee2
JM
606 }
607 if (gcode->has_letter('Z')) {
608 z = gcode->get_value('Z');
dd0a7cfa
JM
609 STEPPER[Z_AXIS]->set_speed(f);
610 STEPPER[Z_AXIS]->move(z<0, abs(z));
d4ee6ee2
JM
611 }
612 gcode->stream->printf("Moved X %d Y %d Z %d F %d steps\n", x, y, z, f);
613 gcode->mark_as_taken();
614 break;
615 }
201bcb94
AW
616 }
617 }
618}
619
64eaf21e
JM
620#define max(a,b) (((a) > (b)) ? (a) : (b))
621// Called periodically to change the speed to match acceleration
622uint32_t Endstops::acceleration_tick(uint32_t dummy)
623{
624 if(this->status == NOT_HOMING) return(0); // nothing to do
625
626 // foreach stepper that is moving
627 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
dd0a7cfa 628 if( !STEPPER[c]->is_moving() ) continue;
64eaf21e 629
dd0a7cfa 630 uint32_t current_rate = STEPPER[c]->get_steps_per_second();
56ce2b5a 631 uint32_t target_rate = int(floor(this->feed_rate[c]*STEPS_PER_MM(c)));
64eaf21e
JM
632
633 if( current_rate < target_rate ){
38bf9a1c 634 uint32_t rate_increase = int(floor((THEKERNEL->planner->get_acceleration()/THEKERNEL->stepper->get_acceleration_ticks_per_second())*STEPS_PER_MM(c)));
64eaf21e
JM
635 current_rate = min( target_rate, current_rate + rate_increase );
636 }
637 if( current_rate > target_rate ){ current_rate = target_rate; }
638
639 // steps per second
dd0a7cfa 640 STEPPER[c]->set_speed(max(current_rate, THEKERNEL->stepper->get_minimum_steps_per_second()));
64eaf21e
JM
641 }
642
643 return 0;
644}
9f6f04a5
JM
645
646void Endstops::on_get_public_data(void* argument){
647 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
648
649 if(!pdr->starts_with(endstops_checksum)) return;
650
651 if(pdr->second_element_is(trim_checksum)) {
86fa0b93 652 pdr->set_data_ptr(&this->trim_mm);
ea5c6d92
JM
653 pdr->set_taken();
654
655 }else if(pdr->second_element_is(home_offset_checksum)) {
86fa0b93 656 pdr->set_data_ptr(&this->home_offset);
9f6f04a5
JM
657 pdr->set_taken();
658 }
659}
7d6fe308
JM
660
661void Endstops::on_set_public_data(void* argument){
662 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
663
664 if(!pdr->starts_with(endstops_checksum)) return;
665
666 if(pdr->second_element_is(trim_checksum)) {
667 float *t= static_cast<float*>(pdr->get_data_ptr());
668 this->trim_mm[0]= t[0];
669 this->trim_mm[1]= t[1];
670 this->trim_mm[2]= t[2];
671 pdr->set_taken();
ea5c6d92
JM
672
673 }else if(pdr->second_element_is(home_offset_checksum)) {
674 float *t= static_cast<float*>(pdr->get_data_ptr());
675 if(!isnan(t[0])) this->home_offset[0]= t[0];
676 if(!isnan(t[1])) this->home_offset[1]= t[1];
677 if(!isnan(t[2])) this->home_offset[2]= t[2];
7d6fe308
JM
678 }
679}