2e319156b291f506037e913fe236038a5de9cffd
[clinton/Smoothieware.git] / src / modules / tools / endstops / Endstops.cpp
1 /*
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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8 #include "libs/Module.h"
9 #include "libs/Kernel.h"
10 #include "modules/communication/utils/Gcode.h"
11 #include "modules/robot/Conveyor.h"
12 #include "Endstops.h"
13 #include "libs/nuts_bolts.h"
14 #include "libs/Pin.h"
15 #include "libs/StepperMotor.h"
16 #include "wait_api.h" // mbed.h lib
17 #include "Robot.h"
18 #include "Stepper.h"
19 #include "Config.h"
20 #include "SlowTicker.h"
21 #include "Planner.h"
22 #include "checksumm.h"
23 #include "utils.h"
24 #include "ConfigValue.h"
25 #include "libs/StreamOutput.h"
26 #include "PublicDataRequest.h"
27 #include "EndstopsPublicAccess.h"
28 #include "StreamOutputPool.h"
29 #include "Pauser.h"
30
31 #include <ctype.h>
32
33 #define ALPHA_AXIS 0
34 #define BETA_AXIS 1
35 #define GAMMA_AXIS 2
36 #define X_AXIS 0
37 #define Y_AXIS 1
38 #define Z_AXIS 2
39
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")
43 #define scara_homing_checksum CHECKSUM("scara_homing")
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
70 // same as above but in user friendly mm/s and mm
71 #define alpha_fast_homing_rate_mm_checksum CHECKSUM("alpha_fast_homing_rate_mm_s")
72 #define beta_fast_homing_rate_mm_checksum CHECKSUM("beta_fast_homing_rate_mm_s")
73 #define gamma_fast_homing_rate_mm_checksum CHECKSUM("gamma_fast_homing_rate_mm_s")
74
75 #define alpha_slow_homing_rate_mm_checksum CHECKSUM("alpha_slow_homing_rate_mm_s")
76 #define beta_slow_homing_rate_mm_checksum CHECKSUM("beta_slow_homing_rate_mm_s")
77 #define gamma_slow_homing_rate_mm_checksum CHECKSUM("gamma_slow_homing_rate_mm_s")
78
79 #define alpha_homing_retract_mm_checksum CHECKSUM("alpha_homing_retract_mm")
80 #define beta_homing_retract_mm_checksum CHECKSUM("beta_homing_retract_mm")
81 #define gamma_homing_retract_mm_checksum CHECKSUM("gamma_homing_retract_mm")
82
83 #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
84
85 #define alpha_homing_direction_checksum CHECKSUM("alpha_homing_direction")
86 #define beta_homing_direction_checksum CHECKSUM("beta_homing_direction")
87 #define gamma_homing_direction_checksum CHECKSUM("gamma_homing_direction")
88 #define home_to_max_checksum CHECKSUM("home_to_max")
89 #define home_to_min_checksum CHECKSUM("home_to_min")
90 #define alpha_min_checksum CHECKSUM("alpha_min")
91 #define beta_min_checksum CHECKSUM("beta_min")
92 #define gamma_min_checksum CHECKSUM("gamma_min")
93
94 #define alpha_max_checksum CHECKSUM("alpha_max")
95 #define beta_max_checksum CHECKSUM("beta_max")
96 #define gamma_max_checksum CHECKSUM("gamma_max")
97
98 #define alpha_limit_enable_checksum CHECKSUM("alpha_limit_enable")
99 #define beta_limit_enable_checksum CHECKSUM("beta_limit_enable")
100 #define gamma_limit_enable_checksum CHECKSUM("gamma_limit_enable")
101
102 #define homing_order_checksum CHECKSUM("homing_order")
103 #define move_to_origin_checksum CHECKSUM("move_to_origin_after_home")
104
105 #define STEPPER THEKERNEL->robot->actuators
106 #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
107
108 #define max(a,b) (((a) > (b)) ? (a) : (b))
109 #define min(a,b) (((a) <= (b)) ? (a) : (b))
110
111 // Homing States
112 enum{
113 MOVING_TO_ENDSTOP_FAST, // homing move
114 MOVING_BACK, // homing move
115 MOVING_TO_ENDSTOP_SLOW, // homing move
116 NOT_HOMING,
117 BACK_OFF_HOME,
118 MOVE_TO_ORIGIN,
119 LIMIT_TRIGGERED
120 };
121
122 Endstops::Endstops()
123 {
124 this->status = NOT_HOMING;
125 home_offset[0] = home_offset[1] = home_offset[2] = 0.0F;
126 }
127
128 void Endstops::on_module_loaded()
129 {
130 // Do not do anything if not enabled
131 if ( THEKERNEL->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) {
132 delete this;
133 return;
134 }
135
136 register_for_event(ON_GCODE_RECEIVED);
137 register_for_event(ON_GET_PUBLIC_DATA);
138 register_for_event(ON_SET_PUBLIC_DATA);
139
140 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->get_acceleration_ticks_per_second() , this, &Endstops::acceleration_tick );
141
142 // Settings
143 this->on_config_reload(this);
144 }
145
146 // Get config
147 void Endstops::on_config_reload(void *argument)
148 {
149 this->pins[0].from_string( THEKERNEL->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
150 this->pins[1].from_string( THEKERNEL->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
151 this->pins[2].from_string( THEKERNEL->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
152 this->pins[3].from_string( THEKERNEL->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
153 this->pins[4].from_string( THEKERNEL->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
154 this->pins[5].from_string( THEKERNEL->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
155
156 // These are the old ones in steps still here for backwards compatibility
157 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(0);
158 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(1);
159 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number() / STEPS_PER_MM(2);
160 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(0);
161 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(1);
162 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number() / STEPS_PER_MM(2);
163 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(0);
164 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(1);
165 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number() / STEPS_PER_MM(2);
166
167 // 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
168 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0])->as_number();
169 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[1])->as_number();
170 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2])->as_number();
171 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0])->as_number();
172 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[1])->as_number();
173 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2])->as_number();
174 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum )->by_default(this->retract_mm[0])->as_number();
175 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum )->by_default(this->retract_mm[1])->as_number();
176 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum )->by_default(this->retract_mm[2])->as_number();
177
178 this->debounce_count = THEKERNEL->config->value(endstop_debounce_count_checksum )->by_default(100)->as_number();
179
180
181 // get homing direction and convert to boolean where true is home to min, and false is home to max
182 int home_dir = get_checksum(THEKERNEL->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string());
183 this->home_direction[0] = home_dir != home_to_max_checksum;
184
185 home_dir = get_checksum(THEKERNEL->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string());
186 this->home_direction[1] = home_dir != home_to_max_checksum;
187
188 home_dir = get_checksum(THEKERNEL->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string());
189 this->home_direction[2] = home_dir != home_to_max_checksum;
190
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();
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();
194
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();
197 this->is_scara = THEKERNEL->config->value(scara_homing_checksum)->by_default(false)->as_bool();
198
199 // see if an order has been specified, must be three characters, XYZ or YXZ etc
200 string order= THEKERNEL->config->value(homing_order_checksum)->by_default("")->as_string();
201 this->homing_order= 0;
202 if(order.size() == 3 && !this->is_delta) {
203 int shift= 0;
204 for(auto c : order) {
205 uint8_t i= toupper(c) - 'X';
206 if(i > 2) { // bad value
207 this->homing_order= 0;
208 break;
209 }
210 homing_order |= (i << shift);
211 shift += 2;
212 }
213 }
214
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();
220
221 // limits enabled
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();
225
226 this->move_to_origin_after_home= THEKERNEL->config->value(move_to_origin_checksum)->by_default(false)->as_bool();
227
228 if(this->limit_enable[X_AXIS] || this->limit_enable[Y_AXIS] || this->limit_enable[Z_AXIS]){
229 register_for_event(ON_IDLE);
230 }
231 }
232
233 static const char *endstop_names[]= {"min_x", "min_y", "min_z", "max_x", "max_y", "max_z"};
234
235 void Endstops::on_idle(void *argument)
236 {
237 if(this->status == LIMIT_TRIGGERED) {
238 // if we were in limit triggered see if it has been cleared
239 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
240 if(this->limit_enable[c]) {
241 std::array<int, 2> minmax{{0, 3}};
242 // check min and max endstops
243 for (int i : minmax) {
244 int n= c+i;
245 if(this->pins[n].get()) {
246 // still triggered, so exit
247 bounce_cnt= 0;
248 return;
249 }
250 }
251 }
252 }
253 if(++bounce_cnt > 10) { // can use less as it calls on_idle in between
254 // clear the state
255 this->status= NOT_HOMING;
256 }
257 return;
258
259 }else if(this->status != NOT_HOMING) {
260 // don't check while homing
261 return;
262 }
263
264 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
265 if(this->limit_enable[c] && STEPPER[c]->is_moving()) {
266 std::array<int, 2> minmax{{0, 3}};
267 // check min and max endstops
268 for (int i : minmax) {
269 int n= c+i;
270 uint8_t debounce= 0;
271 while(this->pins[n].get()) {
272 if ( ++debounce >= debounce_count ) {
273 // endstop triggered
274 THEKERNEL->streams->printf("Limit switch %s was hit - reset or M999 required\n", endstop_names[n]);
275 this->status= LIMIT_TRIGGERED;
276 // disables heaters and motors, ignores incoming Gcode and flushes block queue
277 THEKERNEL->call_event(ON_HALT, nullptr);
278 return;
279 }
280 }
281 }
282 }
283 }
284 }
285
286 // if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move
287 // checks if triggered and only backs off if triggered
288 void Endstops::back_off_home(char axes_to_move)
289 {
290 this->status = BACK_OFF_HOME;
291 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
292 if( ((axes_to_move >> c ) & 1) == 0) continue; // only for axes we asked to move
293 if(this->limit_enable[c]) {
294 if( !this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) continue; // if not triggered no need to move off
295
296 // Move off of the endstop using a regular relative move
297 char buf[32];
298 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);
299 Gcode gc(buf, &(StreamOutput::NullStream));
300 bool oldmode= THEKERNEL->robot->absolute_mode;
301 THEKERNEL->robot->absolute_mode= false; // needs to be relative mode
302 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
303 THEKERNEL->robot->absolute_mode= oldmode; // restore mode
304 }
305 }
306 // Wait for above to finish
307 THEKERNEL->conveyor->wait_for_empty_queue();
308 this->status = NOT_HOMING;
309 }
310
311 // If enabled will move the head to 0,0 after homing, but only if X and Y were set to home
312 void Endstops::move_to_origin(char axes_to_move)
313 {
314 if( (axes_to_move&0x03) != 3 ) return; // ignore if X and Y not homing
315
316 // Do we need to check if we are already at 0,0? probably not as the G0 will not do anything if we are
317 // float pos[3]; THEKERNEL->robot->get_axis_position(pos); if(pos[0] == 0 && pos[1] == 0) return;
318
319 this->status = MOVE_TO_ORIGIN;
320 // Move to center using a regular move, use slower of X and Y fast rate
321 float rate= min(this->fast_rates[0], this->fast_rates[1])*60.0F;
322 char buf[32];
323 snprintf(buf, sizeof(buf), "G0 X0 Y0 F%1.4f", rate);
324 Gcode gc(buf, &(StreamOutput::NullStream));
325 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
326
327 // Wait for above to finish
328 THEKERNEL->conveyor->wait_for_empty_queue();
329 this->status = NOT_HOMING;
330 }
331
332 void Endstops::wait_for_homed(char axes_to_move)
333 {
334 bool running = true;
335 unsigned int debounce[3] = {0, 0, 0};
336 while (running) {
337 running = false;
338 THEKERNEL->call_event(ON_IDLE);
339 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
340 if ( ( axes_to_move >> c ) & 1 ) {
341 if ( this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) {
342 if ( debounce[c] < debounce_count ) {
343 debounce[c]++;
344 running = true;
345 } else if ( STEPPER[c]->is_moving() ) {
346 STEPPER[c]->move(0, 0);
347 axes_to_move &= ~(1<<c); // no need to check it again
348 }
349 } else {
350 // The endstop was not hit yet
351 running = true;
352 debounce[c] = 0;
353 }
354 }
355 }
356 }
357 }
358
359 void Endstops::do_homing_cartesian(char axes_to_move)
360 {
361 // this homing works for cartesian and delta printers
362 // Start moving the axes to the origin
363 this->status = MOVING_TO_ENDSTOP_FAST;
364 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
365 if ( ( axes_to_move >> c) & 1 ) {
366 this->feed_rate[c]= this->fast_rates[c];
367 STEPPER[c]->move(this->home_direction[c], 10000000, 0);
368 }
369 }
370
371 // Wait for all axes to have homed
372 this->wait_for_homed(axes_to_move);
373
374 // Move back a small distance
375 this->status = MOVING_BACK;
376 bool inverted_dir;
377 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
378 if ( ( axes_to_move >> c ) & 1 ) {
379 inverted_dir = !this->home_direction[c];
380 this->feed_rate[c]= this->slow_rates[c];
381 STEPPER[c]->move(inverted_dir, this->retract_mm[c]*STEPS_PER_MM(c), 0);
382 }
383 }
384
385 // Wait for moves to be done
386 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
387 if ( ( axes_to_move >> c ) & 1 ) {
388 while ( STEPPER[c]->is_moving() ) {
389 THEKERNEL->call_event(ON_IDLE);
390 }
391 }
392 }
393
394 // Start moving the axes to the origin slowly
395 this->status = MOVING_TO_ENDSTOP_SLOW;
396 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
397 if ( ( axes_to_move >> c ) & 1 ) {
398 this->feed_rate[c]= this->slow_rates[c];
399 STEPPER[c]->move(this->home_direction[c], 10000000, 0);
400 }
401 }
402
403 // Wait for all axes to have homed
404 this->wait_for_homed(axes_to_move);
405
406 if (this->is_delta || this->is_scara) {
407 // move for soft trim
408 this->status = MOVING_BACK;
409 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
410 if ( this->trim_mm[c] != 0.0F && ( axes_to_move >> c ) & 1 ) {
411 inverted_dir = this->home_direction[c];
412 // move up or down depending on sign of trim, -ive is down away from home
413 if (this->trim_mm[c] < 0) inverted_dir = !inverted_dir;
414 this->feed_rate[c]= this->slow_rates[c];
415 STEPPER[c]->move(inverted_dir, abs(round(this->trim_mm[c]*STEPS_PER_MM(c))), 0);
416 }
417 }
418
419 // Wait for moves to be done
420 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
421 if ( ( axes_to_move >> c ) & 1 ) {
422 //THEKERNEL->streams->printf("axis %c \r\n", c );
423 while ( STEPPER[c]->is_moving() ) {
424 THEKERNEL->call_event(ON_IDLE);
425 }
426 }
427 }
428 }
429
430 // Homing is done
431 this->status = NOT_HOMING;
432 }
433
434 void Endstops::wait_for_homed_corexy(int axis)
435 {
436 bool running = true;
437 unsigned int debounce[3] = {0, 0, 0};
438 while (running) {
439 running = false;
440 THEKERNEL->call_event(ON_IDLE);
441 if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) {
442 if ( debounce[axis] < debounce_count ) {
443 debounce[axis] ++;
444 running = true;
445 } else {
446 // turn both off if running
447 if (STEPPER[X_AXIS]->is_moving()) STEPPER[X_AXIS]->move(0, 0);
448 if (STEPPER[Y_AXIS]->is_moving()) STEPPER[Y_AXIS]->move(0, 0);
449 }
450 } else {
451 // The endstop was not hit yet
452 running = true;
453 debounce[axis] = 0;
454 }
455 }
456 }
457
458 void Endstops::corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps)
459 {
460 this->status = MOVING_TO_ENDSTOP_FAST;
461 this->feed_rate[X_AXIS]= fast_rate;
462 STEPPER[X_AXIS]->move(dirx, 10000000, 0);
463 this->feed_rate[Y_AXIS]= fast_rate;
464 STEPPER[Y_AXIS]->move(diry, 10000000, 0);
465
466 // wait for primary axis
467 this->wait_for_homed_corexy(home_axis);
468
469 // Move back a small distance
470 this->status = MOVING_BACK;
471 this->feed_rate[X_AXIS]= slow_rate;
472 STEPPER[X_AXIS]->move(!dirx, retract_steps, 0);
473 this->feed_rate[Y_AXIS]= slow_rate;
474 STEPPER[Y_AXIS]->move(!diry, retract_steps, 0);
475
476 // wait until done
477 while ( STEPPER[X_AXIS]->is_moving() || STEPPER[Y_AXIS]->is_moving()) {
478 THEKERNEL->call_event(ON_IDLE);
479 }
480
481 // Start moving the axes to the origin slowly
482 this->status = MOVING_TO_ENDSTOP_SLOW;
483 this->feed_rate[X_AXIS]= slow_rate;
484 STEPPER[X_AXIS]->move(dirx, 10000000, 0);
485 this->feed_rate[Y_AXIS]= slow_rate;
486 STEPPER[Y_AXIS]->move(diry, 10000000, 0);
487
488 // wait for primary axis
489 this->wait_for_homed_corexy(home_axis);
490 }
491
492 // this homing works for HBots/CoreXY
493 void Endstops::do_homing_corexy(char axes_to_move)
494 {
495 // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally
496 // 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
497 // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed
498 // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit
499
500 if((axes_to_move & 0x03) == 0x03) { // both X and Y need Homing
501 // determine which motor to turn and which way
502 bool dirx= this->home_direction[X_AXIS];
503 bool diry= this->home_direction[Y_AXIS];
504 int motor;
505 bool dir;
506 if(dirx && diry) { // min/min
507 motor= X_AXIS;
508 dir= true;
509 }else if(dirx && !diry) { // min/max
510 motor= Y_AXIS;
511 dir= true;
512 }else if(!dirx && diry) { // max/min
513 motor= Y_AXIS;
514 dir= false;
515 }else if(!dirx && !diry) { // max/max
516 motor= X_AXIS;
517 dir= false;
518 }
519
520 // then move both X and Y until one hits the endstop
521 this->status = MOVING_TO_ENDSTOP_FAST;
522 // need to allow for more ground covered when moving diagonally
523 this->feed_rate[motor]= this->fast_rates[motor]*1.4142;
524 STEPPER[motor]->move(dir, 10000000, 0);
525 // wait until either X or Y hits the endstop
526 bool running= true;
527 while (running) {
528 THEKERNEL->call_event(ON_IDLE);
529 for(int m=X_AXIS;m<=Y_AXIS;m++) {
530 if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) {
531 // turn off motor
532 if(STEPPER[motor]->is_moving()) STEPPER[motor]->move(0, 0);
533 running= false;
534 break;
535 }
536 }
537 }
538 }
539
540 // move individual axis
541 if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
542 bool dir= this->home_direction[X_AXIS];
543 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));
544 }
545
546 if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
547 bool dir= this->home_direction[Y_AXIS];
548 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));
549 }
550
551 if (axes_to_move & 0x04) { // move Z
552 do_homing_cartesian(0x04); // just home normally for Z
553 }
554
555 // Homing is done
556 this->status = NOT_HOMING;
557 }
558
559 void Endstops::home(char axes_to_move)
560 {
561 if (is_corexy){
562 // corexy/HBot homing
563 do_homing_corexy(axes_to_move);
564 }else{
565 // cartesian/delta homing
566 do_homing_cartesian(axes_to_move);
567 }
568 }
569
570 // Start homing sequences by response to GCode commands
571 void Endstops::on_gcode_received(void *argument)
572 {
573 Gcode *gcode = static_cast<Gcode *>(argument);
574 if ( gcode->has_g) {
575 if ( gcode->g == 28 ) {
576 gcode->mark_as_taken();
577 // G28 is received, we have homing to do
578
579 // First wait for the queue to be empty
580 THEKERNEL->conveyor->wait_for_empty_queue();
581
582 // Do we move select axes or all of them
583 char axes_to_move = 0;
584 // only enable homing if the endstop is defined, deltas, scaras always home all axis
585 bool home_all = this->is_delta || this->is_scara || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') );
586
587 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
588 if ( (home_all || gcode->has_letter(c+'X')) && this->pins[c + (this->home_direction[c] ? 0 : 3)].connected() ) {
589 axes_to_move += ( 1 << c );
590 }
591 }
592
593 // Enable the motors
594 THEKERNEL->stepper->turn_enable_pins_on();
595
596 // do the actual homing
597 if(homing_order != 0){
598 // if an order has been specified do it in the specified order
599 // homing order is 0b00ccbbaa where aa is 0,1,2 to specify the first axis, bb is the second and cc is the third
600 // eg 0b00100001 would be Y X Z, 0b00100100 would be X Y Z
601 for (uint8_t m = homing_order; m != 0; m >>= 2) {
602 int a= (1 << (m & 0x03)); // axis to move
603 if((a & axes_to_move) != 0)
604 home(a);
605 }
606 }else {
607 // they all home at the same time
608 home(axes_to_move);
609 }
610
611 if(home_all) {
612 // for deltas this may be important rather than setting each individually
613 THEKERNEL->robot->reset_axis_position(
614 this->homing_position[X_AXIS] + this->home_offset[X_AXIS],
615 this->homing_position[Y_AXIS] + this->home_offset[Y_AXIS],
616 this->homing_position[Z_AXIS] + this->home_offset[Z_AXIS]);
617 }else{
618 // Zero the ax(i/e)s position, add in the home offset
619 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
620 if ( (axes_to_move >> c) & 1 ) {
621 THEKERNEL->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c);
622 }
623 }
624 }
625
626 // on some systems where 0,0 is bed center it is noce to have home goto 0,0 after homing
627 // default is off
628 if(this->move_to_origin_after_home)
629 move_to_origin(axes_to_move);
630
631 // if limit switches are enabled we must back off endstop after setting home
632 back_off_home(axes_to_move);
633 }
634
635 } else if (gcode->has_m) {
636 switch (gcode->m) {
637 case 119: {
638 for (int i = 0; i < 6; ++i) {
639 if(this->pins[i].connected())
640 gcode->stream->printf("%s:%d ", endstop_names[i], this->pins[i].get());
641 }
642 gcode->add_nl= true;
643 gcode->mark_as_taken();
644 }
645 break;
646
647 case 206: // M206 - set homing offset
648 if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X');
649 if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y');
650 if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z');
651 gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
652 gcode->mark_as_taken();
653 break;
654
655 case 306: // Similar to M206 and G92 but sets Homing offsets based on current position, Would be M207 but that is taken
656 {
657 float cartesian[3];
658 THEKERNEL->robot->get_axis_position(cartesian); // get actual position from robot
659 if (gcode->has_letter('X')){
660 home_offset[0] -= (cartesian[X_AXIS] - gcode->get_value('X'));
661 THEKERNEL->robot->reset_axis_position(gcode->get_value('X'), X_AXIS);
662 }
663 if (gcode->has_letter('Y')) {
664 home_offset[1] -= (cartesian[Y_AXIS] - gcode->get_value('Y'));
665 THEKERNEL->robot->reset_axis_position(gcode->get_value('Y'), Y_AXIS);
666 }
667 if (gcode->has_letter('Z')) {
668 home_offset[2] -= (cartesian[Z_AXIS] - gcode->get_value('Z'));
669 THEKERNEL->robot->reset_axis_position(gcode->get_value('Z'), Z_AXIS);
670 }
671
672 gcode->stream->printf("Homing Offset: X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
673 gcode->mark_as_taken();
674 }
675 break;
676
677 case 500: // save settings
678 case 503: // print settings
679 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]);
680 if (is_delta) {
681 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]);
682 gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position[2]);
683 }
684 gcode->mark_as_taken();
685 break;
686
687 case 665: { // M665 - set max gamma/z height
688 gcode->mark_as_taken();
689 float gamma_max = this->homing_position[2];
690 if (gcode->has_letter('Z')) {
691 this->homing_position[2] = gamma_max = gcode->get_value('Z');
692 }
693 gcode->stream->printf("Max Z %8.3f ", gamma_max);
694 gcode->add_nl = true;
695 }
696 break;
697
698
699 case 666:
700 if(this->is_delta || this->is_scara) { // M666 - set trim for each axis in mm, NB negative mm trim is down
701 if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X');
702 if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y');
703 if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z');
704
705 // print the current trim values in mm
706 gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
707 gcode->mark_as_taken();
708 }
709 break;
710
711 // NOTE this is to test accuracy of lead screws etc.
712 case 910: { // M910 - move specific number of raw steps
713 // Enable the motors
714 THEKERNEL->stepper->turn_enable_pins_on();
715
716 int x= 0, y=0 , z= 0, f= 200*16;
717 if (gcode->has_letter('F')) f = gcode->get_value('F');
718 if (gcode->has_letter('X')) {
719 x = gcode->get_value('X');
720 STEPPER[X_AXIS]->move(x<0, abs(x), f);
721 }
722 if (gcode->has_letter('Y')) {
723 y = gcode->get_value('Y');
724 STEPPER[Y_AXIS]->move(y<0, abs(y), f);
725 }
726 if (gcode->has_letter('Z')) {
727 z = gcode->get_value('Z');
728 STEPPER[Z_AXIS]->move(z<0, abs(z), f);
729 }
730 gcode->stream->printf("Moved X %d Y %d Z %d F %d steps\n", x, y, z, f);
731 gcode->mark_as_taken();
732 break;
733 }
734 }
735 }
736 }
737
738 // Called periodically to change the speed to match acceleration
739 uint32_t Endstops::acceleration_tick(uint32_t dummy)
740 {
741 if(this->status >= NOT_HOMING) return(0); // nothing to do, only do this when moving for homing sequence
742
743 // foreach stepper that is moving
744 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
745 if( !STEPPER[c]->is_moving() ) continue;
746
747 uint32_t current_rate = STEPPER[c]->get_steps_per_second();
748 uint32_t target_rate = int(floor(this->feed_rate[c]*STEPS_PER_MM(c)));
749 float acc= (c==Z_AXIS) ? THEKERNEL->planner->get_z_acceleration() : THEKERNEL->planner->get_acceleration();
750 if( current_rate < target_rate ){
751 uint32_t rate_increase = floor((acc/THEKERNEL->stepper->get_acceleration_ticks_per_second())*STEPS_PER_MM(c));
752 current_rate = min( target_rate, current_rate + rate_increase );
753 }
754 if( current_rate > target_rate ){ current_rate = target_rate; }
755
756 // steps per second
757 STEPPER[c]->set_speed(current_rate);
758 }
759
760 return 0;
761 }
762
763 void Endstops::on_get_public_data(void* argument){
764 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
765
766 if(!pdr->starts_with(endstops_checksum)) return;
767
768 if(pdr->second_element_is(trim_checksum)) {
769 pdr->set_data_ptr(&this->trim_mm);
770 pdr->set_taken();
771
772 }else if(pdr->second_element_is(home_offset_checksum)) {
773 pdr->set_data_ptr(&this->home_offset);
774 pdr->set_taken();
775 }
776 }
777
778 void Endstops::on_set_public_data(void* argument){
779 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
780
781 if(!pdr->starts_with(endstops_checksum)) return;
782
783 if(pdr->second_element_is(trim_checksum)) {
784 float *t= static_cast<float*>(pdr->get_data_ptr());
785 this->trim_mm[0]= t[0];
786 this->trim_mm[1]= t[1];
787 this->trim_mm[2]= t[2];
788 pdr->set_taken();
789
790 }else if(pdr->second_element_is(home_offset_checksum)) {
791 float *t= static_cast<float*>(pdr->get_data_ptr());
792 if(!isnan(t[0])) this->home_offset[0]= t[0];
793 if(!isnan(t[1])) this->home_offset[1]= t[1];
794 if(!isnan(t[2])) this->home_offset[2]= t[2];
795 }
796 }