Repairs issues surrounding homing for scara systems
[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
80605954
JM
31#include <ctype.h>
32
33e4cc02
JM
33#define ALPHA_AXIS 0
34#define BETA_AXIS 1
35#define GAMMA_AXIS 2
9a993543
JM
36#define X_AXIS 0
37#define Y_AXIS 1
38#define Z_AXIS 2
33e4cc02 39
33e4cc02
JM
40#define endstops_module_enable_checksum CHECKSUM("endstops_enable")
41#define corexy_homing_checksum CHECKSUM("corexy_homing")
42#define delta_homing_checksum CHECKSUM("delta_homing")
3e1f5b74 43#define scara_homing_checksum CHECKSUM("scara_homing")
33e4cc02
JM
44
45#define alpha_min_endstop_checksum CHECKSUM("alpha_min_endstop")
46#define beta_min_endstop_checksum CHECKSUM("beta_min_endstop")
47#define gamma_min_endstop_checksum CHECKSUM("gamma_min_endstop")
48
49#define alpha_max_endstop_checksum CHECKSUM("alpha_max_endstop")
50#define beta_max_endstop_checksum CHECKSUM("beta_max_endstop")
51#define gamma_max_endstop_checksum CHECKSUM("gamma_max_endstop")
52
53#define alpha_trim_checksum CHECKSUM("alpha_trim")
54#define beta_trim_checksum CHECKSUM("beta_trim")
55#define gamma_trim_checksum CHECKSUM("gamma_trim")
56
57// these values are in steps and should be deprecated
58#define alpha_fast_homing_rate_checksum CHECKSUM("alpha_fast_homing_rate")
59#define beta_fast_homing_rate_checksum CHECKSUM("beta_fast_homing_rate")
60#define gamma_fast_homing_rate_checksum CHECKSUM("gamma_fast_homing_rate")
61
62#define alpha_slow_homing_rate_checksum CHECKSUM("alpha_slow_homing_rate")
63#define beta_slow_homing_rate_checksum CHECKSUM("beta_slow_homing_rate")
64#define gamma_slow_homing_rate_checksum CHECKSUM("gamma_slow_homing_rate")
65
66#define alpha_homing_retract_checksum CHECKSUM("alpha_homing_retract")
67#define beta_homing_retract_checksum CHECKSUM("beta_homing_retract")
68#define gamma_homing_retract_checksum CHECKSUM("gamma_homing_retract")
69#define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
70
71// same as above but in user friendly mm/s and mm
72#define alpha_fast_homing_rate_mm_checksum CHECKSUM("alpha_fast_homing_rate_mm_s")
73#define beta_fast_homing_rate_mm_checksum CHECKSUM("beta_fast_homing_rate_mm_s")
74#define gamma_fast_homing_rate_mm_checksum CHECKSUM("gamma_fast_homing_rate_mm_s")
75
76#define alpha_slow_homing_rate_mm_checksum CHECKSUM("alpha_slow_homing_rate_mm_s")
77#define beta_slow_homing_rate_mm_checksum CHECKSUM("beta_slow_homing_rate_mm_s")
78#define gamma_slow_homing_rate_mm_checksum CHECKSUM("gamma_slow_homing_rate_mm_s")
79
80#define alpha_homing_retract_mm_checksum CHECKSUM("alpha_homing_retract_mm")
81#define beta_homing_retract_mm_checksum CHECKSUM("beta_homing_retract_mm")
82#define gamma_homing_retract_mm_checksum CHECKSUM("gamma_homing_retract_mm")
83
84#define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
85
86#define alpha_homing_direction_checksum CHECKSUM("alpha_homing_direction")
87#define beta_homing_direction_checksum CHECKSUM("beta_homing_direction")
88#define gamma_homing_direction_checksum CHECKSUM("gamma_homing_direction")
89#define home_to_max_checksum CHECKSUM("home_to_max")
90#define home_to_min_checksum CHECKSUM("home_to_min")
91#define alpha_min_checksum CHECKSUM("alpha_min")
92#define beta_min_checksum CHECKSUM("beta_min")
93#define gamma_min_checksum CHECKSUM("gamma_min")
94
95#define alpha_max_checksum CHECKSUM("alpha_max")
96#define beta_max_checksum CHECKSUM("beta_max")
97#define gamma_max_checksum CHECKSUM("gamma_max")
98
3c947f85
JM
99#define alpha_limit_enable_checksum CHECKSUM("alpha_limit_enable")
100#define beta_limit_enable_checksum CHECKSUM("beta_limit_enable")
101#define gamma_limit_enable_checksum CHECKSUM("gamma_limit_enable")
102
80605954 103#define homing_order_checksum CHECKSUM("homing_order")
2ddf75fd 104#define move_to_origin_checksum CHECKSUM("move_to_origin_after_home")
80605954 105
dd0a7cfa
JM
106#define STEPPER THEKERNEL->robot->actuators
107#define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
56ce2b5a 108
2b3cedc7
JM
109#define max(a,b) (((a) > (b)) ? (a) : (b))
110#define min(a,b) (((a) <= (b)) ? (a) : (b))
111
3c947f85
JM
112// Homing States
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
38bf9a1c 141 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->get_acceleration_ticks_per_second() , this, &Endstops::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
JM
180
181
409ff5b3 182 // get homing direction and convert to boolean where true is home to min, and false is home to max
314ab8f7 183 int home_dir = get_checksum(THEKERNEL->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 184 this->home_direction[0] = home_dir != home_to_max_checksum;
47bbe224 185
314ab8f7 186 home_dir = get_checksum(THEKERNEL->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 187 this->home_direction[1] = home_dir != home_to_max_checksum;
47bbe224 188
314ab8f7 189 home_dir = get_checksum(THEKERNEL->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 190 this->home_direction[2] = home_dir != home_to_max_checksum;
47bbe224 191
314ab8f7 192 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
193 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();
194 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 195
314ab8f7
MM
196 this->is_corexy = THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool();
197 this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool();
3e1f5b74
QH
198 this->is_scara = THEKERNEL->config->value(scara_homing_checksum)->by_default(false)->as_bool();
199
80605954
JM
200 // see if an order has been specified, must be three characters, XYZ or YXZ etc
201 string order= THEKERNEL->config->value(homing_order_checksum)->by_default("")->as_string();
202 this->homing_order= 0;
203 if(order.size() == 3 && !this->is_delta) {
204 int shift= 0;
205 for(auto c : order) {
206 uint8_t i= toupper(c) - 'X';
207 if(i > 2) { // bad value
208 this->homing_order= 0;
209 break;
210 }
211 homing_order |= (i << shift);
212 shift += 2;
213 }
214 }
215
192bca32
JM
216 // endstop trim used by deltas to do soft adjusting
217 // on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
218 this->trim_mm[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0 )->as_number();
219 this->trim_mm[1] = THEKERNEL->config->value(beta_trim_checksum )->by_default(0 )->as_number();
220 this->trim_mm[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0 )->as_number();
3c947f85
JM
221
222 // limits enabled
223 this->limit_enable[X_AXIS]= THEKERNEL->config->value(alpha_limit_enable_checksum)->by_default(false)->as_bool();
224 this->limit_enable[Y_AXIS]= THEKERNEL->config->value(beta_limit_enable_checksum)->by_default(false)->as_bool();
225 this->limit_enable[Z_AXIS]= THEKERNEL->config->value(gamma_limit_enable_checksum)->by_default(false)->as_bool();
226
2ddf75fd 227 this->move_to_origin_after_home= THEKERNEL->config->value(move_to_origin_checksum)->by_default(false)->as_bool();
2b3cedc7 228
3c947f85
JM
229 if(this->limit_enable[X_AXIS] || this->limit_enable[Y_AXIS] || this->limit_enable[Z_AXIS]){
230 register_for_event(ON_IDLE);
231 }
232}
233
4d149ebb 234static const char *endstop_names[]= {"min_x", "min_y", "min_z", "max_x", "max_y", "max_z"};
3c947f85
JM
235
236void Endstops::on_idle(void *argument)
237{
238 if(this->status != NOT_HOMING) return; // don't check while homing or if a LIMIT was triggered
239
240 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
241 if(this->limit_enable[c] && STEPPER[c]->is_moving()) {
242 std::array<int, 2> minmax{{0, 3}};
243 // check min and max endstops
244 for (int i : minmax) {
245 int n= c+i;
246 uint8_t debounce= 0;
247 while(this->pins[n].get()) {
248 if ( ++debounce >= debounce_count ) {
249 // endstop triggered
250 THEKERNEL->pauser->take();
3d1a4519 251 THEKERNEL->streams->printf("Limit switch %s was hit - reset required\n", endstop_names[n]);
3c947f85 252 this->status= LIMIT_TRIGGERED;
3d1a4519
JM
253 // disables heaters and motors
254 THEKERNEL->call_event(ON_HALT);
3c947f85
JM
255 return;
256 }
257 }
258 }
259 }
260 }
261}
262
263// if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move
5bfcd44a
JM
264// checks if triggered and only backs off if triggered
265void Endstops::back_off_home(char axes_to_move)
3c947f85
JM
266{
267 this->status = BACK_OFF_HOME;
268 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
5bfcd44a 269 if( ((axes_to_move >> c ) & 1) == 0) continue; // only for axes we asked to move
3c947f85 270 if(this->limit_enable[c]) {
5bfcd44a
JM
271 if( !this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) continue; // if not triggered no need to move off
272
3c947f85
JM
273 // Move off of the endstop using a regular relative move
274 char buf[32];
2b3cedc7 275 snprintf(buf, sizeof(buf), "G0 %c%1.4f F%1.4f", c+'X', this->retract_mm[c]*(this->home_direction[c]?1:-1), this->fast_rates[c]*60.0F);
3c947f85
JM
276 Gcode gc(buf, &(StreamOutput::NullStream));
277 bool oldmode= THEKERNEL->robot->absolute_mode;
278 THEKERNEL->robot->absolute_mode= false; // needs to be relative mode
279 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
280 THEKERNEL->robot->absolute_mode= oldmode; // restore mode
281 }
282 }
283 // Wait for above to finish
284 THEKERNEL->conveyor->wait_for_empty_queue();
285 this->status = NOT_HOMING;
a0e0d592
BG
286}
287
2b3cedc7 288// If enabled will move the head to 0,0 after homing, but only if X and Y were set to home
2ddf75fd 289void Endstops::move_to_origin(char axes_to_move)
2b3cedc7
JM
290{
291 if( (axes_to_move&0x03) != 3 ) return; // ignore if X and Y not homing
292
fafb45df
JM
293 // Do we need to check if we are already at 0,0? probably not as the G0 will not do anything if we are
294 // float pos[3]; THEKERNEL->robot->get_axis_position(pos); if(pos[0] == 0 && pos[1] == 0) return;
295
2ddf75fd 296 this->status = MOVE_TO_ORIGIN;
2b3cedc7
JM
297 // Move to center using a regular move, use slower of X and Y fast rate
298 float rate= min(this->fast_rates[0], this->fast_rates[1])*60.0F;
299 char buf[32];
300 snprintf(buf, sizeof(buf), "G0 X0 Y0 F%1.4f", rate);
301 Gcode gc(buf, &(StreamOutput::NullStream));
302 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
303
304 // Wait for above to finish
305 THEKERNEL->conveyor->wait_for_empty_queue();
306 this->status = NOT_HOMING;
307}
308
33e4cc02
JM
309void Endstops::wait_for_homed(char axes_to_move)
310{
a0e0d592 311 bool running = true;
33e4cc02
JM
312 unsigned int debounce[3] = {0, 0, 0};
313 while (running) {
a0e0d592 314 running = false;
314ab8f7 315 THEKERNEL->call_event(ON_IDLE);
56ce2b5a
JM
316 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
317 if ( ( axes_to_move >> c ) & 1 ) {
318 if ( this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) {
319 if ( debounce[c] < debounce_count ) {
320 debounce[c]++;
a0e0d592 321 running = true;
dd0a7cfa
JM
322 } else if ( STEPPER[c]->is_moving() ) {
323 STEPPER[c]->move(0, 0);
a0e0d592 324 }
33e4cc02 325 } else {
a0e0d592
BG
326 // The endstop was not hit yet
327 running = true;
56ce2b5a 328 debounce[c] = 0;
323cca60 329 }
a0e0d592
BG
330 }
331 }
332 }
750277f8 333}
201bcb94 334
81f02e89 335void Endstops::do_homing_cartesian(char axes_to_move)
33e4cc02 336{
80605954 337 // this homing works for cartesian and delta printers
f29b0272 338 // Start moving the axes to the origin
2ddf75fd 339 this->status = MOVING_TO_ENDSTOP_FAST;
56ce2b5a
JM
340 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
341 if ( ( axes_to_move >> c) & 1 ) {
342 this->feed_rate[c]= this->fast_rates[c];
dd0a7cfa
JM
343 STEPPER[c]->set_speed(0);
344 STEPPER[c]->move(this->home_direction[c], 10000000);
f29b0272
JM
345 }
346 }
347
348 // Wait for all axes to have homed
349 this->wait_for_homed(axes_to_move);
350
351 // Move back a small distance
352 this->status = MOVING_BACK;
353 bool inverted_dir;
56ce2b5a
JM
354 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
355 if ( ( axes_to_move >> c ) & 1 ) {
356 inverted_dir = !this->home_direction[c];
357 this->feed_rate[c]= this->slow_rates[c];
dd0a7cfa
JM
358 STEPPER[c]->set_speed(0);
359 STEPPER[c]->move(inverted_dir, this->retract_mm[c]*STEPS_PER_MM(c));
f29b0272
JM
360 }
361 }
362
33e4cc02 363 // Wait for moves to be done
56ce2b5a
JM
364 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
365 if ( ( axes_to_move >> c ) & 1 ) {
dd0a7cfa 366 while ( STEPPER[c]->is_moving() ) {
314ab8f7 367 THEKERNEL->call_event(ON_IDLE);
f29b0272
JM
368 }
369 }
370 }
371
372 // Start moving the axes to the origin slowly
2ddf75fd 373 this->status = MOVING_TO_ENDSTOP_SLOW;
56ce2b5a
JM
374 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
375 if ( ( axes_to_move >> c ) & 1 ) {
376 this->feed_rate[c]= this->slow_rates[c];
dd0a7cfa
JM
377 STEPPER[c]->set_speed(0);
378 STEPPER[c]->move(this->home_direction[c], 10000000);
f29b0272
JM
379 }
380 }
381
382 // Wait for all axes to have homed
383 this->wait_for_homed(axes_to_move);
384
3e1f5b74 385 if (this->is_delta || this->is_scara) {
959ab59c
JM
386 // move for soft trim
387 this->status = MOVING_BACK;
56ce2b5a
JM
388 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
389 if ( this->trim_mm[c] != 0.0F && ( axes_to_move >> c ) & 1 ) {
390 inverted_dir = this->home_direction[c];
391 // move up or down depending on sign of trim, -ive is down away from home
392 if (this->trim_mm[c] < 0) inverted_dir = !inverted_dir;
393 this->feed_rate[c]= this->slow_rates[c];
dd0a7cfa
JM
394 STEPPER[c]->set_speed(0);
395 STEPPER[c]->move(inverted_dir, abs(round(this->trim_mm[c]*STEPS_PER_MM(c))));
959ab59c 396 }
f29b0272 397 }
f29b0272 398
959ab59c 399 // Wait for moves to be done
56ce2b5a
JM
400 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
401 if ( ( axes_to_move >> c ) & 1 ) {
314ab8f7 402 //THEKERNEL->streams->printf("axis %c \r\n", c );
dd0a7cfa 403 while ( STEPPER[c]->is_moving() ) {
314ab8f7 404 THEKERNEL->call_event(ON_IDLE);
959ab59c 405 }
f29b0272
JM
406 }
407 }
408 }
409
410 // Homing is done
411 this->status = NOT_HOMING;
412}
413
33e4cc02
JM
414void Endstops::wait_for_homed_corexy(int axis)
415{
3db88866 416 bool running = true;
33e4cc02
JM
417 unsigned int debounce[3] = {0, 0, 0};
418 while (running) {
3db88866 419 running = false;
314ab8f7 420 THEKERNEL->call_event(ON_IDLE);
33e4cc02
JM
421 if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) {
422 if ( debounce[axis] < debounce_count ) {
3db88866
JM
423 debounce[axis] ++;
424 running = true;
425 } else {
426 // turn both off if running
dd0a7cfa
JM
427 if (STEPPER[X_AXIS]->is_moving()) STEPPER[X_AXIS]->move(0, 0);
428 if (STEPPER[Y_AXIS]->is_moving()) STEPPER[Y_AXIS]->move(0, 0);
3db88866 429 }
33e4cc02 430 } else {
3db88866
JM
431 // The endstop was not hit yet
432 running = true;
433 debounce[axis] = 0;
434 }
435 }
436}
437
1ad23cd3 438void Endstops::corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps)
33e4cc02 439{
2ddf75fd 440 this->status = MOVING_TO_ENDSTOP_FAST;
64eaf21e 441 this->feed_rate[X_AXIS]= fast_rate;
dd0a7cfa
JM
442 STEPPER[X_AXIS]->set_speed(0);
443 STEPPER[X_AXIS]->move(dirx, 10000000);
64eaf21e 444 this->feed_rate[Y_AXIS]= fast_rate;
dd0a7cfa
JM
445 STEPPER[Y_AXIS]->set_speed(0);
446 STEPPER[Y_AXIS]->move(diry, 10000000);
f29b0272 447
33e4cc02
JM
448 // wait for primary axis
449 this->wait_for_homed_corexy(home_axis);
f29b0272 450
33e4cc02
JM
451 // Move back a small distance
452 this->status = MOVING_BACK;
64eaf21e 453 this->feed_rate[X_AXIS]= slow_rate;
dd0a7cfa
JM
454 STEPPER[X_AXIS]->set_speed(0);
455 STEPPER[X_AXIS]->move(!dirx, retract_steps);
64eaf21e 456 this->feed_rate[Y_AXIS]= slow_rate;
dd0a7cfa
JM
457 STEPPER[Y_AXIS]->set_speed(0);
458 STEPPER[Y_AXIS]->move(!diry, retract_steps);
33e4cc02
JM
459
460 // wait until done
dd0a7cfa 461 while ( STEPPER[X_AXIS]->is_moving() || STEPPER[Y_AXIS]->is_moving()) {
314ab8f7 462 THEKERNEL->call_event(ON_IDLE);
f29b0272
JM
463 }
464
33e4cc02 465 // Start moving the axes to the origin slowly
2ddf75fd 466 this->status = MOVING_TO_ENDSTOP_SLOW;
64eaf21e 467 this->feed_rate[X_AXIS]= slow_rate;
dd0a7cfa
JM
468 STEPPER[X_AXIS]->set_speed(0);
469 STEPPER[X_AXIS]->move(dirx, 10000000);
64eaf21e 470 this->feed_rate[Y_AXIS]= slow_rate;
dd0a7cfa
JM
471 STEPPER[Y_AXIS]->set_speed(0);
472 STEPPER[Y_AXIS]->move(diry, 10000000);
f29b0272 473
33e4cc02
JM
474 // wait for primary axis
475 this->wait_for_homed_corexy(home_axis);
476}
f29b0272 477
33e4cc02
JM
478// this homing works for HBots/CoreXY
479void Endstops::do_homing_corexy(char axes_to_move)
480{
03dffc07 481 // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally
174d9961 482 // 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 483 // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed
174d9961 484 // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit
174d9961
JM
485
486 if((axes_to_move & 0x03) == 0x03) { // both X and Y need Homing
487 // determine which motor to turn and which way
488 bool dirx= this->home_direction[X_AXIS];
489 bool diry= this->home_direction[Y_AXIS];
490 int motor;
491 bool dir;
492 if(dirx && diry) { // min/min
493 motor= X_AXIS;
494 dir= true;
495 }else if(dirx && !diry) { // min/max
496 motor= Y_AXIS;
497 dir= true;
498 }else if(!dirx && diry) { // max/min
499 motor= Y_AXIS;
500 dir= false;
501 }else if(!dirx && !diry) { // max/max
502 motor= X_AXIS;
503 dir= false;
504 }
505
506 // then move both X and Y until one hits the endstop
2ddf75fd 507 this->status = MOVING_TO_ENDSTOP_FAST;
64eaf21e 508 this->feed_rate[motor]= this->fast_rates[motor]*1.4142;
dd0a7cfa
JM
509 STEPPER[motor]->set_speed(0); // need to allow for more ground covered when moving diagonally
510 STEPPER[motor]->move(dir, 10000000);
174d9961
JM
511 // wait until either X or Y hits the endstop
512 bool running= true;
513 while (running) {
314ab8f7 514 THEKERNEL->call_event(ON_IDLE);
174d9961
JM
515 for(int m=X_AXIS;m<=Y_AXIS;m++) {
516 if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) {
517 // turn off motor
dd0a7cfa 518 if(STEPPER[motor]->is_moving()) STEPPER[motor]->move(0, 0);
174d9961
JM
519 running= false;
520 break;
521 }
522 }
523 }
524 }
33e4cc02 525
174d9961 526 // move individual axis
33e4cc02 527 if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
413dd90c 528 bool dir= this->home_direction[X_AXIS];
56ce2b5a 529 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
530 }
531
65191d91
JM
532 if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
533 bool dir= this->home_direction[Y_AXIS];
56ce2b5a 534 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
535 }
536
33e4cc02 537 if (axes_to_move & 0x04) { // move Z
81f02e89 538 do_homing_cartesian(0x04); // just home normally for Z
f29b0272
JM
539 }
540
541 // Homing is done
542 this->status = NOT_HOMING;
543}
544
81f02e89
JM
545void Endstops::home(char axes_to_move)
546{
547 if (is_corexy){
548 // corexy/HBot homing
549 do_homing_corexy(axes_to_move);
550 }else{
551 // cartesian/delta homing
552 do_homing_cartesian(axes_to_move);
553 }
554}
555
201bcb94 556// Start homing sequences by response to GCode commands
33e4cc02 557void Endstops::on_gcode_received(void *argument)
c339d634 558{
33e4cc02
JM
559 Gcode *gcode = static_cast<Gcode *>(argument);
560 if ( gcode->has_g) {
561 if ( gcode->g == 28 ) {
74b6303c 562 gcode->mark_as_taken();
df27a6a3 563 // G28 is received, we have homing to do
201bcb94
AW
564
565 // First wait for the queue to be empty
314ab8f7 566 THEKERNEL->conveyor->wait_for_empty_queue();
201bcb94
AW
567
568 // Do we move select axes or all of them
7dee696d 569 char axes_to_move = 0;
3e1f5b74
QH
570 // only enable homing if the endstop is defined, deltas, scaras always home all axis
571 bool home_all = this->is_delta || this->is_scara || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') );
47bbe224 572
56ce2b5a
JM
573 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
574 if ( (home_all || gcode->has_letter(c+'X')) && this->pins[c + (this->home_direction[c] ? 0 : 3)].connected() ) {
575 axes_to_move += ( 1 << c );
33e4cc02 576 }
df27a6a3 577 }
3b948656 578
f6c04440 579 // Enable the motors
314ab8f7 580 THEKERNEL->stepper->turn_enable_pins_on();
f6c04440 581
f29b0272 582 // do the actual homing
80605954
JM
583 if(homing_order != 0){
584 // if an order has been specified do it in the specified order
585 // homing order is 0b00ccbbaa where aa is 0,1,2 to specify the first axis, bb is the second and cc is the third
586 // eg 0b00100001 would be Y X Z, 0b00100100 would be X Y Z
587 for (uint8_t m = homing_order; m != 0; m >>= 2) {
588 int a= (1 << (m & 0x03)); // axis to move
589 if((a & axes_to_move) != 0)
81f02e89 590 home(a);
80605954
JM
591 }
592 }else {
593 // they all home at the same time
81f02e89 594 home(axes_to_move);
80605954 595 }
3ffe27fb 596
cef9acea
JM
597 if(home_all) {
598 // for deltas this may be important rather than setting each individually
599 THEKERNEL->robot->reset_axis_position(
600 this->homing_position[X_AXIS] + this->home_offset[X_AXIS],
601 this->homing_position[Y_AXIS] + this->home_offset[Y_AXIS],
602 this->homing_position[Z_AXIS] + this->home_offset[Z_AXIS]);
603 }else{
604 // Zero the ax(i/e)s position, add in the home offset
605 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
606 if ( (axes_to_move >> c) & 1 ) {
607 THEKERNEL->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c);
608 }
3ffe27fb
AV
609 }
610 }
3c947f85 611
2b3cedc7
JM
612 // on some systems where 0,0 is bed center it is noce to have home goto 0,0 after homing
613 // default is off
2ddf75fd
JM
614 if(this->move_to_origin_after_home)
615 move_to_origin(axes_to_move);
2b3cedc7 616
3c947f85 617 // if limit switches are enabled we must back off endstop after setting home
5bfcd44a 618 back_off_home(axes_to_move);
c339d634 619 }
81f02e89 620
33e4cc02
JM
621 } else if (gcode->has_m) {
622 switch (gcode->m) {
623 case 119: {
ef7bd372
JM
624 for (int i = 0; i < 6; ++i) {
625 if(this->pins[i].connected())
626 gcode->stream->printf("%s:%d ", endstop_names[i], this->pins[i].get());
627 }
bd96f4d7 628 gcode->add_nl= true;
33e4cc02
JM
629 gcode->mark_as_taken();
630 }
631 break;
632
633 case 206: // M206 - set homing offset
634 if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X');
635 if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y');
636 if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z');
637 gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
638 gcode->mark_as_taken();
639 break;
640
641 case 500: // save settings
642 case 503: // print settings
643 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]);
644 if (is_delta) {
56ce2b5a
JM
645 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]);
646 gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position[2]);
7a8fe6e0 647 }
33e4cc02 648 gcode->mark_as_taken();
c339d634 649 break;
47bbe224 650
ec4773e5 651 case 665: { // M665 - set max gamma/z height
33e4cc02 652 gcode->mark_as_taken();
1ad23cd3 653 float gamma_max = this->homing_position[2];
33e4cc02
JM
654 if (gcode->has_letter('Z')) {
655 this->homing_position[2] = gamma_max = gcode->get_value('Z');
ec4773e5 656 }
33e4cc02
JM
657 gcode->stream->printf("Max Z %8.3f ", gamma_max);
658 gcode->add_nl = true;
659 }
660 break;
ec4773e5 661
47bbe224 662
56ce2b5a 663 case 666:
3e1f5b74 664 if(this->is_delta || this->is_scara) { // M666 - set trim for each axis in mm, NB negative mm trim is down
56ce2b5a
JM
665 if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X');
666 if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y');
667 if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z');
47bbe224 668
56ce2b5a
JM
669 // print the current trim values in mm
670 gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
671 gcode->mark_as_taken();
672 }
33e4cc02 673 break;
47bbe224 674
d4ee6ee2
JM
675 // NOTE this is to test accuracy of lead screws etc.
676 case 910: { // M910 - move specific number of raw steps
a4b04adc
JM
677 // Enable the motors
678 THEKERNEL->stepper->turn_enable_pins_on();
679
d4ee6ee2
JM
680 int x= 0, y=0 , z= 0, f= 200*16;
681 if (gcode->has_letter('F')) f = gcode->get_value('F');
682 if (gcode->has_letter('X')) {
683 x = gcode->get_value('X');
dd0a7cfa
JM
684 STEPPER[X_AXIS]->set_speed(f);
685 STEPPER[X_AXIS]->move(x<0, abs(x));
d4ee6ee2
JM
686 }
687 if (gcode->has_letter('Y')) {
688 y = gcode->get_value('Y');
dd0a7cfa
JM
689 STEPPER[Y_AXIS]->set_speed(f);
690 STEPPER[Y_AXIS]->move(y<0, abs(y));
d4ee6ee2
JM
691 }
692 if (gcode->has_letter('Z')) {
693 z = gcode->get_value('Z');
dd0a7cfa
JM
694 STEPPER[Z_AXIS]->set_speed(f);
695 STEPPER[Z_AXIS]->move(z<0, abs(z));
d4ee6ee2
JM
696 }
697 gcode->stream->printf("Moved X %d Y %d Z %d F %d steps\n", x, y, z, f);
698 gcode->mark_as_taken();
699 break;
700 }
201bcb94
AW
701 }
702 }
703}
704
64eaf21e
JM
705// Called periodically to change the speed to match acceleration
706uint32_t Endstops::acceleration_tick(uint32_t dummy)
707{
fafb45df 708 if(this->status >= NOT_HOMING) return(0); // nothing to do, only do this when moving for homing sequence
64eaf21e
JM
709
710 // foreach stepper that is moving
711 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
dd0a7cfa 712 if( !STEPPER[c]->is_moving() ) continue;
64eaf21e 713
dd0a7cfa 714 uint32_t current_rate = STEPPER[c]->get_steps_per_second();
56ce2b5a 715 uint32_t target_rate = int(floor(this->feed_rate[c]*STEPS_PER_MM(c)));
c5fe1787 716 float acc= (c==Z_AXIS) ? THEKERNEL->planner->get_z_acceleration() : THEKERNEL->planner->get_acceleration();
64eaf21e 717 if( current_rate < target_rate ){
c5fe1787 718 uint32_t rate_increase = int(floor((acc/THEKERNEL->stepper->get_acceleration_ticks_per_second())*STEPS_PER_MM(c)));
64eaf21e
JM
719 current_rate = min( target_rate, current_rate + rate_increase );
720 }
721 if( current_rate > target_rate ){ current_rate = target_rate; }
722
723 // steps per second
dd0a7cfa 724 STEPPER[c]->set_speed(max(current_rate, THEKERNEL->stepper->get_minimum_steps_per_second()));
64eaf21e
JM
725 }
726
727 return 0;
728}
9f6f04a5
JM
729
730void Endstops::on_get_public_data(void* argument){
731 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
732
733 if(!pdr->starts_with(endstops_checksum)) return;
734
735 if(pdr->second_element_is(trim_checksum)) {
86fa0b93 736 pdr->set_data_ptr(&this->trim_mm);
ea5c6d92
JM
737 pdr->set_taken();
738
739 }else if(pdr->second_element_is(home_offset_checksum)) {
86fa0b93 740 pdr->set_data_ptr(&this->home_offset);
9f6f04a5
JM
741 pdr->set_taken();
742 }
743}
7d6fe308
JM
744
745void Endstops::on_set_public_data(void* argument){
746 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
747
748 if(!pdr->starts_with(endstops_checksum)) return;
749
750 if(pdr->second_element_is(trim_checksum)) {
751 float *t= static_cast<float*>(pdr->get_data_ptr());
752 this->trim_mm[0]= t[0];
753 this->trim_mm[1]= t[1];
754 this->trim_mm[2]= t[2];
755 pdr->set_taken();
ea5c6d92
JM
756
757 }else if(pdr->second_element_is(home_offset_checksum)) {
758 float *t= static_cast<float*>(pdr->get_data_ptr());
759 if(!isnan(t[0])) this->home_offset[0]= t[0];
760 if(!isnan(t[1])) this->home_offset[1]= t[1];
761 if(!isnan(t[2])) this->home_offset[2]= t[2];
7d6fe308
JM
762 }
763}