temperaturecontrol: allow setting background tool without activating
[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 "modules/robot/ActuatorCoordinates.h"
13 #include "Endstops.h"
14 #include "libs/nuts_bolts.h"
15 #include "libs/Pin.h"
16 #include "libs/StepperMotor.h"
17 #include "wait_api.h" // mbed.h lib
18 #include "Robot.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 "StepTicker.h"
30 #include "BaseSolution.h"
31 #include "SerialMessage.h"
32
33 #include <ctype.h>
34 #include <algorithm>
35
36 // OLD deprecated syntax
37 #define endstops_module_enable_checksum CHECKSUM("endstops_enable")
38
39 #define ENDSTOP_CHECKSUMS(X) { \
40 CHECKSUM(X "_min_endstop"), \
41 CHECKSUM(X "_max_endstop"), \
42 CHECKSUM(X "_max_travel"), \
43 CHECKSUM(X "_fast_homing_rate_mm_s"), \
44 CHECKSUM(X "_slow_homing_rate_mm_s"), \
45 CHECKSUM(X "_homing_retract_mm"), \
46 CHECKSUM(X "_homing_direction"), \
47 CHECKSUM(X "_min"), \
48 CHECKSUM(X "_max"), \
49 CHECKSUM(X "_limit_enable"), \
50 }
51
52 // checksum defns
53 enum DEFNS {MIN_PIN, MAX_PIN, MAX_TRAVEL, FAST_RATE, SLOW_RATE, RETRACT, DIRECTION, MIN, MAX, LIMIT, NDEFNS};
54
55 // global config settings
56 #define corexy_homing_checksum CHECKSUM("corexy_homing")
57 #define delta_homing_checksum CHECKSUM("delta_homing")
58 #define rdelta_homing_checksum CHECKSUM("rdelta_homing")
59 #define scara_homing_checksum CHECKSUM("scara_homing")
60
61 #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
62 #define endstop_debounce_ms_checksum CHECKSUM("endstop_debounce_ms")
63
64 #define home_z_first_checksum CHECKSUM("home_z_first")
65 #define homing_order_checksum CHECKSUM("homing_order")
66 #define move_to_origin_checksum CHECKSUM("move_to_origin_after_home")
67
68 #define alpha_trim_checksum CHECKSUM("alpha_trim_mm")
69 #define beta_trim_checksum CHECKSUM("beta_trim_mm")
70 #define gamma_trim_checksum CHECKSUM("gamma_trim_mm")
71
72 // new config syntax
73 // endstop.xmin.enable true
74 // endstop.xmin.pin 1.29
75 // endstop.xmin.axis X
76 // endstop.xmin.homing_direction home_to_min
77
78 #define endstop_checksum CHECKSUM("endstop")
79 #define enable_checksum CHECKSUM("enable")
80 #define pin_checksum CHECKSUM("pin")
81 #define axis_checksum CHECKSUM("axis")
82 #define direction_checksum CHECKSUM("homing_direction")
83 #define position_checksum CHECKSUM("homing_position")
84 #define fast_rate_checksum CHECKSUM("fast_rate")
85 #define slow_rate_checksum CHECKSUM("slow_rate")
86 #define max_travel_checksum CHECKSUM("max_travel")
87 #define retract_checksum CHECKSUM("retract")
88 #define limit_checksum CHECKSUM("limit_enable")
89
90 #define STEPPER THEROBOT->actuators
91 #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
92
93
94
95 // Homing States
96 enum STATES {
97 MOVING_TO_ENDSTOP_FAST, // homing move
98 MOVING_TO_ENDSTOP_SLOW, // homing move
99 MOVING_BACK, // homing move
100 NOT_HOMING,
101 BACK_OFF_HOME,
102 MOVE_TO_ORIGIN,
103 LIMIT_TRIGGERED
104 };
105
106 Endstops::Endstops()
107 {
108 this->status = NOT_HOMING;
109 }
110
111 void Endstops::on_module_loaded()
112 {
113 // Do not do anything if not enabled or if no pins are defined
114 if (THEKERNEL->config->value( endstops_module_enable_checksum )->by_default(false)->as_bool()) {
115 if(!load_old_config()) {
116 delete this;
117 return;
118 }
119
120 }else{
121 // check for new config syntax
122 if(!load_config()) {
123 delete this;
124 return;
125 }
126 }
127
128 register_for_event(ON_GCODE_RECEIVED);
129 register_for_event(ON_GET_PUBLIC_DATA);
130 register_for_event(ON_SET_PUBLIC_DATA);
131
132
133 THEKERNEL->slow_ticker->attach(1000, this, &Endstops::read_endstops);
134 }
135
136 // Get config using old deprecated syntax Does not support ABC
137 bool Endstops::load_old_config()
138 {
139 uint16_t const checksums[][NDEFNS] = {
140 ENDSTOP_CHECKSUMS("alpha"), // X
141 ENDSTOP_CHECKSUMS("beta"), // Y
142 ENDSTOP_CHECKSUMS("gamma") // Z
143 };
144
145 bool limit_enabled= false;
146 for (int i = X_AXIS; i <= Z_AXIS; ++i) { // X_AXIS to Z_AXIS
147 homing_info_t hinfo;
148
149 // init homing struct
150 hinfo.home_offset= 0;
151 hinfo.homed= false;
152 hinfo.axis= 'X'+i;
153 hinfo.axis_index= i;
154 hinfo.pin_info= nullptr;
155
156 // rates in mm/sec
157 hinfo.fast_rate= THEKERNEL->config->value(checksums[i][FAST_RATE])->by_default(100)->as_number();
158 hinfo.slow_rate= THEKERNEL->config->value(checksums[i][SLOW_RATE])->by_default(10)->as_number();
159
160 // retract in mm
161 hinfo.retract= THEKERNEL->config->value(checksums[i][RETRACT])->by_default(5)->as_number();
162
163 // get homing direction and convert to boolean where true is home to min, and false is home to max
164 hinfo.home_direction= THEKERNEL->config->value(checksums[i][DIRECTION])->by_default("home_to_min")->as_string() != "home_to_max";
165
166 // homing cartesian position
167 hinfo.homing_position= hinfo.home_direction ? THEKERNEL->config->value(checksums[i][MIN])->by_default(0)->as_number() : THEKERNEL->config->value(checksums[i][MAX])->by_default(200)->as_number();
168
169 // used to set maximum movement on homing, set by alpha_max_travel if defined
170 hinfo.max_travel= THEKERNEL->config->value(checksums[i][MAX_TRAVEL])->by_default(500)->as_number();
171
172
173 // pin definitions for endstop pins
174 for (int j = MIN_PIN; j <= MAX_PIN; ++j) {
175 endstop_info_t *info= new endstop_info_t;
176 info->pin.from_string(THEKERNEL->config->value(checksums[i][j])->by_default("nc" )->as_string())->as_input();
177 if(!info->pin.connected()){
178 // no pin defined try next
179 delete info;
180 continue;
181 }
182
183 // enter into endstop array
184 endstops.push_back(info);
185
186 // add index to the homing struct if this is the one used for homing
187 if((hinfo.home_direction && j == MIN_PIN) || (!hinfo.home_direction && j == MAX_PIN)) hinfo.pin_info= info;
188
189 // init struct
190 info->debounce= 0;
191 info->axis= 'X'+i;
192 info->axis_index= i;
193
194 // limits enabled
195 info->limit_enable= THEKERNEL->config->value(checksums[i][LIMIT])->by_default(false)->as_bool();
196 limit_enabled |= info->limit_enable;
197 }
198
199 homing_axis.push_back(hinfo);
200 }
201
202 // if no pins defined then disable the module
203 if(endstops.empty()) return false;
204
205 homing_axis.shrink_to_fit();
206 endstops.shrink_to_fit();
207
208 get_global_configs();
209
210 if(limit_enabled) {
211 register_for_event(ON_IDLE);
212 }
213
214 // sanity check for deltas
215 /*
216 if(this->is_delta || this->is_rdelta) {
217 // some things must be the same or they will die, so force it here to avoid config errors
218 this->fast_rates[1] = this->fast_rates[2] = this->fast_rates[0];
219 this->slow_rates[1] = this->slow_rates[2] = this->slow_rates[0];
220 this->retract_mm[1] = this->retract_mm[2] = this->retract_mm[0];
221 this->home_direction[1] = this->home_direction[2] = this->home_direction[0];
222 // NOTE homing_position for rdelta is the angle of the actuator not the cartesian position
223 if(!this->is_rdelta) this->homing_position[0] = this->homing_position[1] = 0;
224 }
225 */
226
227 return true;
228 }
229
230 // Get config using new syntax supports ABC
231 bool Endstops::load_config()
232 {
233 bool limit_enabled= false;
234 size_t max_index= 0;
235
236 std::array<homing_info_t, k_max_actuators> temp_axis_array; // needs to be at least XYZ, but allow for ABC
237 {
238 homing_info_t t;
239 t.axis= 0;
240 t.axis_index= 0;
241 t.pin_info= nullptr;
242
243 temp_axis_array.fill(t);
244 }
245
246 // iterate over all endstop.*.*
247 std::vector<uint16_t> modules;
248 THEKERNEL->config->get_module_list(&modules, endstop_checksum);
249 for(auto cs : modules ) {
250 if(!THEKERNEL->config->value(endstop_checksum, cs, enable_checksum )->as_bool()) continue;
251
252 endstop_info_t *pin_info= new endstop_info_t;
253 pin_info->pin.from_string(THEKERNEL->config->value(endstop_checksum, cs, pin_checksum)->by_default("nc" )->as_string())->as_input();
254 if(!pin_info->pin.connected()){
255 // no pin defined try next
256 delete pin_info;
257 continue;
258 }
259
260 string axis= THEKERNEL->config->value(endstop_checksum, cs, axis_checksum)->by_default("")->as_string();
261 if(axis.empty()){
262 // axis is required
263 delete pin_info;
264 continue;
265 }
266
267 size_t i;
268 switch(toupper(axis[0])) {
269 case 'X': i= X_AXIS; break;
270 case 'Y': i= Y_AXIS; break;
271 case 'Z': i= Z_AXIS; break;
272 case 'A': i= A_AXIS; break;
273 case 'B': i= B_AXIS; break;
274 case 'C': i= C_AXIS; break;
275 default: // not a recognized axis
276 delete pin_info;
277 continue;
278 }
279
280 // check we are not going above the number of defined actuators/axis
281 if(i >= THEROBOT->get_number_registered_motors()) {
282 // too many axis we only have configured n_motors
283 THEKERNEL->streams->printf("ERROR: endstop %d is greater than number of defined motors. Endstops disabled\n", i);
284 delete pin_info;
285 return false;
286 }
287
288 // keep track of the maximum index that has been defined
289 if(i > max_index) max_index= i;
290
291 // init pin struct
292 pin_info->debounce= 0;
293 pin_info->axis= toupper(axis[0]);
294 pin_info->axis_index= i;
295
296 // are limits enabled
297 pin_info->limit_enable= THEKERNEL->config->value(endstop_checksum, cs, limit_checksum)->by_default(false)->as_bool();
298 limit_enabled |= pin_info->limit_enable;
299
300 // enter into endstop array
301 endstops.push_back(pin_info);
302
303 // if set to none it means not used for homing (maybe limit only) so do not add to the homing array
304 string direction= THEKERNEL->config->value(endstop_checksum, cs, direction_checksum)->by_default("none")->as_string();
305 if(direction == "none") {
306 continue;
307 }
308
309 // setup the homing array
310 homing_info_t hinfo;
311
312 // init homing struct
313 hinfo.home_offset= 0;
314 hinfo.homed= false;
315 hinfo.axis= toupper(axis[0]);
316 hinfo.axis_index= i;
317 hinfo.pin_info= pin_info;
318
319 // rates in mm/sec
320 hinfo.fast_rate= THEKERNEL->config->value(endstop_checksum, cs, fast_rate_checksum)->by_default(100)->as_number();
321 hinfo.slow_rate= THEKERNEL->config->value(endstop_checksum, cs, slow_rate_checksum)->by_default(10)->as_number();
322
323 // retract in mm
324 hinfo.retract= THEKERNEL->config->value(endstop_checksum, cs, retract_checksum)->by_default(5)->as_number();
325
326 // homing direction and convert to boolean where true is home to min, and false is home to max
327 hinfo.home_direction= direction == "home_to_min";
328
329 // homing cartesian position
330 hinfo.homing_position= THEKERNEL->config->value(endstop_checksum, cs, position_checksum)->by_default(hinfo.home_direction ? 0 : 200)->as_number();
331
332 // used to set maximum movement on homing, set by max_travel if defined
333 hinfo.max_travel= THEKERNEL->config->value(endstop_checksum, cs, max_travel_checksum)->by_default(500)->as_number();
334
335 // stick into array in correct place
336 temp_axis_array[hinfo.axis_index]= hinfo;
337 }
338
339 // if no pins defined then disable the module
340 if(endstops.empty()) return false;
341
342 // copy to the homing_axis array, make sure that undefined entries are filled in as well
343 // as the order is important and all slots must be filled upto the max_index
344 for (size_t i = 0; i < temp_axis_array.size(); ++i) {
345 if(temp_axis_array[i].axis == 0) {
346 // was not configured above, if it is XYZ then we need to force a dummy entry
347 if(i <= Z_AXIS) {
348 homing_info_t t;
349 t.axis= 'X' + i;
350 t.axis_index= i;
351 t.pin_info= nullptr; // this tells it that it cannot be used for homing
352 homing_axis.push_back(t);
353
354 }else if(i <= max_index) {
355 // for instance case where we defined C without A or B
356 homing_info_t t;
357 t.axis= 'A' + i;
358 t.axis_index= i;
359 t.pin_info= nullptr; // this tells it that it cannot be used for homing
360 homing_axis.push_back(t);
361 }
362
363 }else{
364 homing_axis.push_back(temp_axis_array[i]);
365 }
366 }
367
368 // saves some memory
369 homing_axis.shrink_to_fit();
370 endstops.shrink_to_fit();
371
372 // sets some endstop global configs applicable to all endstops
373 get_global_configs();
374
375 if(limit_enabled) {
376 register_for_event(ON_IDLE);
377 }
378
379 return true;
380 }
381
382 void Endstops::get_global_configs()
383 {
384 // NOTE the debounce count is in milliseconds so probably does not need to beset anymore
385 this->debounce_ms= THEKERNEL->config->value(endstop_debounce_ms_checksum)->by_default(0)->as_number();
386 this->debounce_count= THEKERNEL->config->value(endstop_debounce_count_checksum)->by_default(100)->as_number();
387
388 this->is_corexy= THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool();
389 this->is_delta= THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool();
390 this->is_rdelta= THEKERNEL->config->value(rdelta_homing_checksum)->by_default(false)->as_bool();
391 this->is_scara= THEKERNEL->config->value(scara_homing_checksum)->by_default(false)->as_bool();
392
393 this->home_z_first= THEKERNEL->config->value(home_z_first_checksum)->by_default(false)->as_bool();
394
395 this->trim_mm[0] = THEKERNEL->config->value(alpha_trim_checksum)->by_default(0)->as_number();
396 this->trim_mm[1] = THEKERNEL->config->value(beta_trim_checksum)->by_default(0)->as_number();
397 this->trim_mm[2] = THEKERNEL->config->value(gamma_trim_checksum)->by_default(0)->as_number();
398
399 // see if an order has been specified, must be three or more characters, XYZABC or ABYXZ etc
400 string order = THEKERNEL->config->value(homing_order_checksum)->by_default("")->as_string();
401 this->homing_order = 0;
402 if(order.size() >= 3 && order.size() <= homing_axis.size() && !(this->is_delta || this->is_rdelta)) {
403 int shift = 0;
404 for(auto c : order) {
405 char n= toupper(c);
406 uint32_t i = n >= 'X' ? n - 'X' : n - 'A' + 3;
407 i += 1; // So X is 1
408 if(i > 6) { // bad value
409 this->homing_order = 0;
410 break;
411 }
412 homing_order |= (i << shift);
413 shift += 3;
414 }
415 }
416
417 // set to true by default for deltas due to trim, false on cartesians
418 this->move_to_origin_after_home = THEKERNEL->config->value(move_to_origin_checksum)->by_default(is_delta)->as_bool();
419 }
420
421 bool Endstops::debounced_get(Pin *pin)
422 {
423 if(pin == nullptr) return false;
424 uint8_t debounce = 0;
425 while(pin->get()) {
426 if ( ++debounce >= this->debounce_count ) {
427 // pin triggered
428 return true;
429 }
430 }
431 return false;
432 }
433
434 // only called if limits are enabled
435 void Endstops::on_idle(void *argument)
436 {
437 if(this->status == LIMIT_TRIGGERED) {
438 // if we were in limit triggered see if it has been cleared
439 for(auto& i : endstops) {
440 if(i->limit_enable) {
441 if(i->pin.get()) {
442 // still triggered, so exit
443 i->debounce = 0;
444 return;
445 }
446
447 if(i->debounce++ > debounce_count) { // can use less as it calls on_idle in between
448 // clear the state
449 this->status = NOT_HOMING;
450 }
451 }
452 }
453 return;
454
455 } else if(this->status != NOT_HOMING) {
456 // don't check while homing
457 return;
458 }
459
460 for(auto& i : endstops) {
461 if(i->limit_enable && STEPPER[i->axis_index]->is_moving()) {
462 // check min and max endstops
463 if(debounced_get(&i->pin)) {
464 // endstop triggered
465 if(!THEKERNEL->is_grbl_mode()) {
466 THEKERNEL->streams->printf("Limit switch %c%c was hit - reset or M999 required\n", STEPPER[i->axis_index]->which_direction() ? '-' : '+', i->axis);
467 }else{
468 THEKERNEL->streams->printf("ALARM: Hard limit %c%c\n", STEPPER[i->axis_index]->which_direction() ? '-' : '+', i->axis);
469 }
470 this->status = LIMIT_TRIGGERED;
471 i->debounce= 0;
472 // disables heaters and motors, ignores incoming Gcode and flushes block queue
473 THEKERNEL->call_event(ON_HALT, nullptr);
474 return;
475 }
476 }
477 }
478 }
479
480 // if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move
481 // checks if triggered and only backs off if triggered
482 void Endstops::back_off_home(axis_bitmap_t axis)
483 {
484 std::vector<std::pair<char, float>> params;
485 this->status = BACK_OFF_HOME;
486
487 float slow_rate= NAN; // default mm/sec
488
489 // these are handled differently
490 if(is_delta) {
491 // Move off of the endstop using a regular relative move in Z only
492 params.push_back({'Z', THEROBOT->from_millimeters(homing_axis[Z_AXIS].retract * (homing_axis[Z_AXIS].home_direction ? 1 : -1))});
493 slow_rate= homing_axis[Z_AXIS].slow_rate;
494
495 } else {
496 // cartesians concatenate all the moves we need to do into one gcode
497 for( auto& e : homing_axis) {
498 if(!axis[e.axis_index]) continue; // only for axes we asked to move
499
500 // if not triggered no need to move off
501 if(e.pin_info != nullptr && e.pin_info->limit_enable && debounced_get(&e.pin_info->pin)) {
502 char ax= e.axis;
503 params.push_back({ax, THEROBOT->from_millimeters(e.retract * (e.home_direction ? 1 : -1))});
504 // select slowest of them all
505 slow_rate= isnan(slow_rate) ? e.slow_rate : std::min(slow_rate, e.slow_rate);
506 }
507 }
508 }
509
510 if(!params.empty()) {
511 // Move off of the endstop using a regular relative move
512 params.insert(params.begin(), {'G', 0});
513 // use X slow rate to move, Z should have a max speed set anyway
514 params.push_back({'F', THEROBOT->from_millimeters(slow_rate * 60.0F)});
515 char gcode_buf[64];
516 append_parameters(gcode_buf, params, sizeof(gcode_buf));
517 Gcode gc(gcode_buf, &(StreamOutput::NullStream));
518 THEROBOT->push_state();
519 THEROBOT->absolute_mode = false; // needs to be relative mode
520 THEROBOT->on_gcode_received(&gc); // send to robot directly
521 // Wait for above to finish
522 THECONVEYOR->wait_for_idle();
523 THEROBOT->pop_state();
524 }
525
526 this->status = NOT_HOMING;
527 }
528
529 // If enabled will move the head to 0,0 after homing, but only if X and Y were set to home
530 void Endstops::move_to_origin(axis_bitmap_t axis)
531 {
532 if(!is_delta && (!axis[X_AXIS] || !axis[Y_AXIS])) return; // ignore if X and Y not homing, unless delta
533
534 // Do we need to check if we are already at 0,0? probably not as the G0 will not do anything if we are
535 // float pos[3]; THEROBOT->get_axis_position(pos); if(pos[0] == 0 && pos[1] == 0) return;
536
537 this->status = MOVE_TO_ORIGIN;
538 // Move to center using a regular move, use slower of X and Y fast rate in mm/sec
539 float rate = std::min(homing_axis[X_AXIS].fast_rate, homing_axis[Y_AXIS].fast_rate) * 60.0F;
540 char buf[32];
541 THEROBOT->push_state();
542 THEROBOT->absolute_mode = true;
543 snprintf(buf, sizeof(buf), "G53 G0 X0 Y0 F%1.4f", THEROBOT->from_millimeters(rate)); // must use machine coordinates in case G92 or WCS is in effect
544 struct SerialMessage message;
545 message.message = buf;
546 message.stream = &(StreamOutput::NullStream);
547 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); // as it is a multi G code command
548 // Wait for above to finish
549 THECONVEYOR->wait_for_idle();
550 THEROBOT->pop_state();
551 this->status = NOT_HOMING;
552 }
553
554 // Called every millisecond in an ISR
555 uint32_t Endstops::read_endstops(uint32_t dummy)
556 {
557 if(this->status != MOVING_TO_ENDSTOP_SLOW && this->status != MOVING_TO_ENDSTOP_FAST) return 0; // not doing anything we need to monitor for
558
559 // check each homing endstop
560 for(auto& e : homing_axis) { // check all axis homing endstops
561 if(e.pin_info == nullptr) continue; // ignore if not a homing endstop
562 int m= e.axis_index;
563
564 // for corexy homing in X or Y we must only check the associated endstop, works as we only home one axis at a time for corexy
565 if(is_corexy && (m == X_AXIS || m == Y_AXIS) && !axis_to_home[m]) continue;
566
567 if(STEPPER[m]->is_moving()) {
568 // if it is moving then we check the associated endstop, and debounce it
569 if(e.pin_info->pin.get()) {
570 if(e.pin_info->debounce < debounce_ms) {
571 e.pin_info->debounce++;
572
573 } else {
574 if(is_corexy && (m == X_AXIS || m == Y_AXIS)) {
575 // corexy when moving in X or Y we need to stop both the X and Y motors
576 STEPPER[X_AXIS]->stop_moving();
577 STEPPER[Y_AXIS]->stop_moving();
578
579 }else{
580 // we signal the motor to stop, which will preempt any moves on that axis
581 STEPPER[m]->stop_moving();
582 }
583 e.pin_info->triggered= true;
584 }
585
586 } else {
587 // The endstop was not hit yet
588 e.pin_info->debounce= 0;
589 }
590 }
591 }
592
593 return 0;
594 }
595
596 void Endstops::home_xy()
597 {
598 if(axis_to_home[X_AXIS] && axis_to_home[Y_AXIS]) {
599 // Home XY first so as not to slow them down by homing Z at the same time
600 float delta[3] {homing_axis[X_AXIS].max_travel, homing_axis[Y_AXIS].max_travel, 0};
601 if(homing_axis[X_AXIS].home_direction) delta[X_AXIS]= -delta[X_AXIS];
602 if(homing_axis[Y_AXIS].home_direction) delta[Y_AXIS]= -delta[Y_AXIS];
603 float feed_rate = std::min(homing_axis[X_AXIS].fast_rate, homing_axis[Y_AXIS].fast_rate);
604 THEROBOT->delta_move(delta, feed_rate, 3);
605
606 } else if(axis_to_home[X_AXIS]) {
607 // now home X only
608 float delta[3] {homing_axis[X_AXIS].max_travel, 0, 0};
609 if(homing_axis[X_AXIS].home_direction) delta[X_AXIS]= -delta[X_AXIS];
610 THEROBOT->delta_move(delta, homing_axis[X_AXIS].fast_rate, 3);
611
612 } else if(axis_to_home[Y_AXIS]) {
613 // now home Y only
614 float delta[3] {0, homing_axis[Y_AXIS].max_travel, 0};
615 if(homing_axis[Y_AXIS].home_direction) delta[Y_AXIS]= -delta[Y_AXIS];
616 THEROBOT->delta_move(delta, homing_axis[Y_AXIS].fast_rate, 3);
617 }
618
619 // Wait for axis to have homed
620 THECONVEYOR->wait_for_idle();
621 }
622
623 void Endstops::home(axis_bitmap_t a)
624 {
625 // reset debounce counts for all endstops
626 for(auto& e : endstops) {
627 e->debounce= 0;
628 e->triggered= false;
629 }
630
631 if (is_scara) {
632 THEROBOT->disable_arm_solution = true; // Polar bots has to home in the actuator space. Arm solution disabled.
633 }
634
635 this->axis_to_home= a;
636
637 // Start moving the axes to the origin
638 this->status = MOVING_TO_ENDSTOP_FAST;
639
640 THEROBOT->disable_segmentation= true; // we must disable segmentation as this won't work with it enabled
641
642 if(!home_z_first) home_xy();
643
644 if(axis_to_home[Z_AXIS]) {
645 // now home z
646 float delta[3] {0, 0, homing_axis[Z_AXIS].max_travel}; // we go the max z
647 if(homing_axis[Z_AXIS].home_direction) delta[Z_AXIS]= -delta[Z_AXIS];
648 THEROBOT->delta_move(delta, homing_axis[Z_AXIS].fast_rate, 3);
649 // wait for Z
650 THECONVEYOR->wait_for_idle();
651 }
652
653 if(home_z_first) home_xy();
654
655 // potentially home A B and C individually
656 if(homing_axis.size() > 3){
657 for (size_t i = A_AXIS; i < homing_axis.size(); ++i) {
658 if(axis_to_home[i]) {
659 // now home A B or C
660 float delta[i+1];
661 for (size_t j = 0; j <= i; ++j) delta[j]= 0;
662 delta[i]= homing_axis[i].max_travel; // we go the max
663 if(homing_axis[i].home_direction) delta[i]= -delta[i];
664 THEROBOT->delta_move(delta, homing_axis[i].fast_rate, i+1);
665 // wait for it
666 THECONVEYOR->wait_for_idle();
667 }
668 }
669 }
670
671 // check that the endstops were hit and it did not stop short for some reason
672 // if the endstop is not triggered then enter ALARM state
673 // with deltas we check all three axis were triggered, but at least one of XYZ must be set to home
674 if(axis_to_home[X_AXIS] || axis_to_home[Y_AXIS] || axis_to_home[Z_AXIS]) {
675 for (size_t i = X_AXIS; i <= Z_AXIS; ++i) {
676 if((axis_to_home[i] || this->is_delta || this->is_rdelta) && !homing_axis[i].pin_info->triggered) {
677 this->status = NOT_HOMING;
678 THEKERNEL->call_event(ON_HALT, nullptr);
679 return;
680 }
681 }
682 }
683
684 // also check ABC
685 if(homing_axis.size() > 3){
686 for (size_t i = A_AXIS; i < homing_axis.size(); ++i) {
687 if(axis_to_home[i] && !homing_axis[i].pin_info->triggered) {
688 this->status = NOT_HOMING;
689 THEKERNEL->call_event(ON_HALT, nullptr);
690 return;
691 }
692 }
693 }
694
695 if (!is_scara) {
696 // Only for non polar bots
697 // we did not complete movement the full distance if we hit the endstops
698 // TODO Maybe only reset axis involved in the homing cycle
699 THEROBOT->reset_position_from_current_actuator_position();
700 }
701
702 // Move back a small distance for all homing axis
703 this->status = MOVING_BACK;
704 float delta[homing_axis.size()];
705 for (size_t i = 0; i < homing_axis.size(); ++i) delta[i]= 0;
706
707 // use minimum feed rate of all axes that are being homed (sub optimal, but necessary)
708 float feed_rate= homing_axis[X_AXIS].slow_rate;
709 for (auto& i : homing_axis) {
710 int c= i.axis_index;
711 if(axis_to_home[c]) {
712 delta[c]= i.retract;
713 if(!i.home_direction) delta[c]= -delta[c];
714 feed_rate= std::min(i.slow_rate, feed_rate);
715 }
716 }
717
718 THEROBOT->delta_move(delta, feed_rate, homing_axis.size());
719 // wait until finished
720 THECONVEYOR->wait_for_idle();
721
722 // Start moving the axes towards the endstops slowly
723 this->status = MOVING_TO_ENDSTOP_SLOW;
724 for (auto& i : homing_axis) {
725 int c= i.axis_index;
726 if(axis_to_home[c]) {
727 delta[c]= i.retract*2; // move further than we moved off to make sure we hit it cleanly
728 if(i.home_direction) delta[c]= -delta[c];
729 }else{
730 delta[c]= 0;
731 }
732 }
733 THEROBOT->delta_move(delta, feed_rate, homing_axis.size());
734 // wait until finished
735 THECONVEYOR->wait_for_idle();
736
737 // we did not complete movement the full distance if we hit the endstops
738 // TODO Maybe only reset axis involved in the homing cycle
739 THEROBOT->reset_position_from_current_actuator_position();
740
741 THEROBOT->disable_segmentation= false;
742 if (is_scara) {
743 THEROBOT->disable_arm_solution = false; // Arm solution enabled again.
744 }
745
746 this->status = NOT_HOMING;
747 }
748
749 void Endstops::process_home_command(Gcode* gcode)
750 {
751 // First wait for the queue to be empty
752 THECONVEYOR->wait_for_idle();
753
754 // turn off any compensation transform so Z does not move as XY home
755 auto savect= THEROBOT->compensationTransform;
756 THEROBOT->compensationTransform= nullptr;
757
758 // deltas always home Z axis only, which moves all three actuators
759 bool home_in_z_only = this->is_delta || this->is_rdelta;
760
761 // figure out which axis to home
762 axis_bitmap_t haxis;
763 haxis.reset();
764
765 bool axis_speced = (gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') ||
766 gcode->has_letter('A') || gcode->has_letter('B') || gcode->has_letter('C'));
767
768 if(!home_in_z_only) { // ie not a delta
769 for (auto &p : homing_axis) {
770 // only enable homing if the endstop is defined,
771 if(p.pin_info == nullptr) continue;
772 if(!axis_speced || gcode->has_letter(p.axis)) {
773 haxis.set(p.axis_index);
774 // now reset axis to 0 as we do not know what state we are in
775 if (!is_scara) {
776 THEROBOT->reset_axis_position(0, p.axis_index);
777 } else {
778 // SCARA resets arms to plausable minimum angles
779 THEROBOT->reset_axis_position(-30,30,0); // angles set into axis space for homing.
780 }
781 }
782 }
783
784 } else {
785 bool home_z= !axis_speced || gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z');
786
787 // if we specified an axis we check ABC
788 for (size_t i = A_AXIS; i < homing_axis.size(); ++i) {
789 auto &p= homing_axis[i];
790 if(p.pin_info == nullptr) continue;
791 if(!axis_speced || gcode->has_letter(p.axis)) haxis.set(p.axis_index);
792 }
793
794 if(home_z){
795 // Only Z axis homes (even though all actuators move this is handled by arm solution)
796 haxis.set(Z_AXIS);
797 // we also set the kinematics to a known good position, this is necessary for a rotary delta, but doesn't hurt for linear delta
798 THEROBOT->reset_axis_position(0, 0, 0);
799 }
800 }
801
802 if(haxis.none()) {
803 THEKERNEL->streams->printf("WARNING: Nothing to home\n");
804 return;
805 }
806
807 // do the actual homing
808 if(homing_order != 0 && !is_scara) {
809 // if an order has been specified do it in the specified order
810 // homing order is 0bfffeeedddcccbbbaaa where aaa is 1,2,3,4,5,6 to specify the first axis (XYZABC), bbb is the second and ccc is the third etc
811 // eg 0b0101011001010 would be Y X Z A, 011 010 001 100 101 would be B A X Y Z
812 for (uint32_t m = homing_order; m != 0; m >>= 3) {
813 uint32_t a= (m & 0x07)-1; // axis to home
814 if(a < homing_axis.size() && haxis[a]) { // if axis is selected to home
815 axis_bitmap_t bs;
816 bs.set(a);
817 home(bs);
818 }
819 // check if on_halt (eg kill)
820 if(THEKERNEL->is_halted()) break;
821 }
822
823 } else if(is_corexy) {
824 // corexy must home each axis individually
825 for (auto &p : homing_axis) {
826 if(haxis[p.axis_index]) {
827 axis_bitmap_t bs;
828 bs.set(p.axis_index);
829 home(bs);
830 }
831 // check if on_halt (eg kill)
832 if(THEKERNEL->is_halted()) break;
833 }
834
835 } else {
836 // they could all home at the same time
837 home(haxis);
838 }
839
840 // restore compensationTransform
841 THEROBOT->compensationTransform= savect;
842
843 // check if on_halt (eg kill or fail)
844 if(THEKERNEL->is_halted()) {
845 if(!THEKERNEL->is_grbl_mode()) {
846 THEKERNEL->streams->printf("ERROR: Homing cycle failed - check the max_travel settings\n");
847 }else{
848 THEKERNEL->streams->printf("ALARM: Homing fail\n");
849 }
850 // clear all the homed flags
851 for (auto &p : homing_axis) p.homed= false;
852 return;
853 }
854
855 if(home_in_z_only || is_scara) { // deltas and scaras only
856 // Here's where we would have been if the endstops were perfectly trimmed
857 // NOTE on a rotary delta home_offset is actuator position in degrees when homed and
858 // home_offset is the theta offset for each actuator, so M206 is used to set theta offset for each actuator in degrees
859 // FIXME not sure this will work with compensation transforms on.
860 float ideal_position[3] = {
861 homing_axis[X_AXIS].homing_position + homing_axis[X_AXIS].home_offset,
862 homing_axis[Y_AXIS].homing_position + homing_axis[Y_AXIS].home_offset,
863 homing_axis[Z_AXIS].homing_position + homing_axis[Z_AXIS].home_offset
864 };
865
866 bool has_endstop_trim = this->is_delta || is_scara;
867 if (has_endstop_trim) {
868 ActuatorCoordinates ideal_actuator_position;
869 THEROBOT->arm_solution->cartesian_to_actuator(ideal_position, ideal_actuator_position);
870
871 // We are actually not at the ideal position, but a trim away
872 ActuatorCoordinates real_actuator_position = {
873 ideal_actuator_position[X_AXIS] - this->trim_mm[X_AXIS],
874 ideal_actuator_position[Y_AXIS] - this->trim_mm[Y_AXIS],
875 ideal_actuator_position[Z_AXIS] - this->trim_mm[Z_AXIS]
876 };
877
878 float real_position[3];
879 THEROBOT->arm_solution->actuator_to_cartesian(real_actuator_position, real_position);
880 // Reset the actuator positions to correspond to our real position
881 THEROBOT->reset_axis_position(real_position[0], real_position[1], real_position[2]);
882
883 } else {
884 // without endstop trim, real_position == ideal_position
885 if(is_rdelta) {
886 // with a rotary delta we set the actuators angle then use the FK to calculate the resulting cartesian coordinates
887 ActuatorCoordinates real_actuator_position = {ideal_position[0], ideal_position[1], ideal_position[2]};
888 THEROBOT->reset_actuator_position(real_actuator_position);
889
890 } else {
891 // Reset the actuator positions to correspond to our real position
892 THEROBOT->reset_axis_position(ideal_position[0], ideal_position[1], ideal_position[2]);
893 }
894 }
895
896 // for deltas we say all 3 axis are homed even though it was only Z
897 homing_axis[X_AXIS].homed= true;
898 homing_axis[Y_AXIS].homed= true;
899 homing_axis[Z_AXIS].homed= true;
900
901 // if we also homed ABC then we need to reset them
902 for (size_t i = A_AXIS; i < homing_axis.size(); ++i) {
903 auto &p= homing_axis[i];
904 if (haxis[p.axis_index]) { // if we requested this axis to home
905 THEROBOT->reset_axis_position(p.homing_position + p.home_offset, p.axis_index);
906 // set flag indicating axis was homed, it stays set once set until H/W reset or unhomed
907 p.homed= true;
908 }
909 }
910
911 } else {
912 // Zero the ax(i/e)s position, add in the home offset
913 // NOTE that if compensation is active the Z will be set based on where XY are, so make sure XY are homed first then Z
914 // so XY are at a known consistent position. (especially true if using a proximity probe)
915 for (auto &p : homing_axis) {
916 if (haxis[p.axis_index]) { // if we requested this axis to home
917 THEROBOT->reset_axis_position(p.homing_position + p.home_offset, p.axis_index);
918 // set flag indicating axis was homed, it stays set once set until H/W reset or unhomed
919 p.homed= true;
920 }
921 }
922 }
923
924 // on some systems where 0,0 is bed center it is nice to have home goto 0,0 after homing
925 // default is off for cartesian on for deltas
926 if(!is_delta) {
927 // NOTE a rotary delta usually has optical or hall-effect endstops so it is safe to go past them a little bit
928 if(this->move_to_origin_after_home) move_to_origin(haxis);
929 // if limit switches are enabled we must back off endstop after setting home
930 back_off_home(haxis);
931
932 } else if(haxis[Z_AXIS] && (this->move_to_origin_after_home || homing_axis[X_AXIS].pin_info->limit_enable)) {
933 // deltas are not left at 0,0 because of the trim settings, so move to 0,0 if requested, but we need to back off endstops first
934 // also need to back off endstops if limits are enabled
935 back_off_home(haxis);
936 if(this->move_to_origin_after_home) move_to_origin(haxis);
937 }
938 }
939
940 void Endstops::set_homing_offset(Gcode *gcode)
941 {
942 // M306 Similar to M206 but sets Homing offsets based on current MCS position
943 // Basically it finds the delta between the current MCS position and the requested position and adds it to the homing offset
944 // then will not let it be set again until that axis is homed.
945 float pos[3];
946 THEROBOT->get_axis_position(pos);
947
948 if (gcode->has_letter('X')) {
949 if(!homing_axis[X_AXIS].homed) {
950 gcode->stream->printf("error: Axis X must be homed before setting Homing offset\n");
951 return;
952 }
953 homing_axis[X_AXIS].home_offset += (THEROBOT->to_millimeters(gcode->get_value('X')) - pos[X_AXIS]);
954 homing_axis[X_AXIS].homed= false; // force it to be homed
955 }
956 if (gcode->has_letter('Y')) {
957 if(!homing_axis[Y_AXIS].homed) {
958 gcode->stream->printf("error: Axis Y must be homed before setting Homing offset\n");
959 return;
960 }
961 homing_axis[Y_AXIS].home_offset += (THEROBOT->to_millimeters(gcode->get_value('Y')) - pos[Y_AXIS]);
962 homing_axis[Y_AXIS].homed= false; // force it to be homed
963 }
964 if (gcode->has_letter('Z')) {
965 if(!homing_axis[Z_AXIS].homed) {
966 gcode->stream->printf("error: Axis Z must be homed before setting Homing offset\n");
967 return;
968 }
969 homing_axis[Z_AXIS].home_offset += (THEROBOT->to_millimeters(gcode->get_value('Z')) - pos[Z_AXIS]);
970 homing_axis[Z_AXIS].homed= false; // force it to be homed
971 }
972
973 gcode->stream->printf("Homing Offset: X %5.3f Y %5.3f Z %5.3f will take effect next home\n", homing_axis[X_AXIS].home_offset, homing_axis[Y_AXIS].home_offset, homing_axis[Z_AXIS].home_offset);
974 }
975
976 void Endstops::handle_park(Gcode * gcode)
977 {
978 // TODO: spec says if XYZ specified move to them first then move to MCS of specifed axis
979 THEROBOT->push_state();
980 THEROBOT->absolute_mode = true;
981 char buf[32];
982 snprintf(buf, sizeof(buf), "G53 G0 X%f Y%f", THEROBOT->from_millimeters(saved_position[X_AXIS]), THEROBOT->from_millimeters(saved_position[Y_AXIS])); // must use machine coordinates in case G92 or WCS is in effect
983 struct SerialMessage message;
984 message.message = buf;
985 message.stream = &(StreamOutput::NullStream);
986 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); // as it is a multi G code command
987 // Wait for above to finish
988 THECONVEYOR->wait_for_idle();
989 THEROBOT->pop_state();
990 }
991
992 // parse gcodes
993 void Endstops::on_gcode_received(void *argument)
994 {
995 Gcode *gcode = static_cast<Gcode *>(argument);
996
997 if ( gcode->has_g && gcode->g == 28) {
998 switch(gcode->subcode) {
999 case 0: // G28 in grbl mode will do a rapid to the predefined position otherwise it is home command
1000 if(THEKERNEL->is_grbl_mode()){
1001 handle_park(gcode);
1002 }else{
1003 process_home_command(gcode);
1004 }
1005 break;
1006
1007 case 1: // G28.1 set pre defined park position
1008 // saves current position in absolute machine coordinates
1009 THEROBOT->get_axis_position(saved_position); // Only XY are used
1010 // Note the following is only meant to be used for recovering a saved position from config-override
1011 // Not a standard Gcode and not to be relied on
1012 if (gcode->has_letter('X')) saved_position[X_AXIS] = gcode->get_value('X');
1013 if (gcode->has_letter('Y')) saved_position[Y_AXIS] = gcode->get_value('Y');
1014 break;
1015
1016 case 2: // G28.2 in grbl mode does homing (triggered by $H), otherwise it moves to the park position
1017 if(THEKERNEL->is_grbl_mode()) {
1018 process_home_command(gcode);
1019 }else{
1020 handle_park(gcode);
1021 }
1022 break;
1023
1024 case 3: // G28.3 is a smoothie special it sets manual homing
1025 if(gcode->get_num_args() == 0) {
1026 for (auto &p : homing_axis) {
1027 if(p.pin_info == nullptr) continue; // ignore if not a homing endstop
1028 p.homed= true;
1029 THEROBOT->reset_axis_position(0, p.axis_index);
1030 }
1031 } else {
1032 // do a manual homing based on given coordinates, no endstops required
1033 if(gcode->has_letter('X')){ THEROBOT->reset_axis_position(gcode->get_value('X'), X_AXIS); homing_axis[X_AXIS].homed= true; }
1034 if(gcode->has_letter('Y')){ THEROBOT->reset_axis_position(gcode->get_value('Y'), Y_AXIS); homing_axis[Y_AXIS].homed= true; }
1035 if(gcode->has_letter('Z')){ THEROBOT->reset_axis_position(gcode->get_value('Z'), Z_AXIS); homing_axis[Z_AXIS].homed= true; }
1036 if(homing_axis.size() > A_AXIS && homing_axis[A_AXIS].pin_info != nullptr && gcode->has_letter('A')){ THEROBOT->reset_axis_position(gcode->get_value('A'), A_AXIS); homing_axis[A_AXIS].homed= true; }
1037 if(homing_axis.size() > B_AXIS && homing_axis[B_AXIS].pin_info != nullptr && gcode->has_letter('B')){ THEROBOT->reset_axis_position(gcode->get_value('B'), B_AXIS); homing_axis[B_AXIS].homed= true; }
1038 if(homing_axis.size() > C_AXIS && homing_axis[C_AXIS].pin_info != nullptr && gcode->has_letter('C')){ THEROBOT->reset_axis_position(gcode->get_value('C'), C_AXIS); homing_axis[C_AXIS].homed= true; }
1039 }
1040 break;
1041
1042 case 4: { // G28.4 is a smoothie special it sets manual homing based on the actuator position (used for rotary delta)
1043 // do a manual homing based on given coordinates, no endstops required
1044 ActuatorCoordinates ac{NAN, NAN, NAN};
1045 if(gcode->has_letter('X')){ ac[0] = gcode->get_value('X'); homing_axis[X_AXIS].homed= true; }
1046 if(gcode->has_letter('Y')){ ac[1] = gcode->get_value('Y'); homing_axis[Y_AXIS].homed= true; }
1047 if(gcode->has_letter('Z')){ ac[2] = gcode->get_value('Z'); homing_axis[Z_AXIS].homed= true; }
1048 THEROBOT->reset_actuator_position(ac);
1049 }
1050 break;
1051
1052 case 5: // G28.5 is a smoothie special it clears the homed flag for the specified axis, or all if not specifed
1053 if(gcode->get_num_args() == 0) {
1054 for (auto &p : homing_axis) p.homed= false;
1055 } else {
1056 if(gcode->has_letter('X')) homing_axis[X_AXIS].homed= false;
1057 if(gcode->has_letter('Y')) homing_axis[Y_AXIS].homed= false;
1058 if(gcode->has_letter('Z')) homing_axis[Z_AXIS].homed= false;
1059 if(homing_axis.size() > A_AXIS && gcode->has_letter('A')) homing_axis[A_AXIS].homed= false;
1060 if(homing_axis.size() > B_AXIS && gcode->has_letter('B')) homing_axis[B_AXIS].homed= false;
1061 if(homing_axis.size() > C_AXIS && gcode->has_letter('C')) homing_axis[C_AXIS].homed= false;
1062 }
1063 break;
1064
1065 case 6: // G28.6 is a smoothie special it shows the homing status of each axis
1066 for (auto &p : homing_axis) {
1067 if(p.pin_info == nullptr) continue; // ignore if not a homing endstop
1068 gcode->stream->printf("%c:%d ", p.axis, p.homed);
1069 }
1070 gcode->add_nl= true;
1071 break;
1072
1073 default:
1074 if(THEKERNEL->is_grbl_mode()) {
1075 gcode->stream->printf("error:Unsupported command\n");
1076 }
1077 break;
1078 }
1079
1080 } else if (gcode->has_m) {
1081
1082 switch (gcode->m) {
1083 case 119: {
1084 for(auto& h : homing_axis) {
1085 if(h.pin_info == nullptr) continue; // ignore if not a homing endstop
1086 string name;
1087 name.append(1, h.axis).append(h.home_direction ? "_min" : "_max");
1088 gcode->stream->printf("%s:%d ", name.c_str(), h.pin_info->pin.get());
1089 }
1090 gcode->stream->printf("pins- ");
1091 for(auto& p : endstops) {
1092 string str(1, p->axis);
1093 if(p->limit_enable) str.append("L");
1094 gcode->stream->printf("(%s)P%d.%d:%d ", str.c_str(), p->pin.port_number, p->pin.pin, p->pin.get());
1095 }
1096 gcode->add_nl = true;
1097 }
1098 break;
1099
1100 case 206: // M206 - set homing offset
1101 if(is_rdelta) return; // RotaryDeltaCalibration module will handle this
1102 for (auto &p : homing_axis) {
1103 if(p.pin_info == nullptr) continue; // ignore if not a homing endstop
1104 if (gcode->has_letter(p.axis)) p.home_offset= gcode->get_value(p.axis);
1105 }
1106
1107 for (auto &p : homing_axis) {
1108 if(p.pin_info == nullptr) continue; // ignore if not a homing endstop
1109 gcode->stream->printf("%c: %5.3f ", p.axis, p.home_offset);
1110 }
1111
1112 gcode->stream->printf(" will take effect next home\n");
1113 break;
1114
1115 case 306: // set homing offset based on current position
1116 if(is_rdelta) return; // RotaryDeltaCalibration module will handle this
1117
1118 set_homing_offset(gcode);
1119 break;
1120
1121 case 500: // save settings
1122 case 503: // print settings
1123 if(!is_rdelta) {
1124 gcode->stream->printf(";Home offset (mm):\nM206 ");
1125 for (auto &p : homing_axis) {
1126 if(p.pin_info == nullptr) continue; // ignore if not a homing endstop
1127 gcode->stream->printf("%c%1.2f ", p.axis, p.home_offset);
1128 }
1129 gcode->stream->printf("\n");
1130
1131 }else{
1132 gcode->stream->printf(";Theta offset (degrees):\nM206 A%1.5f B%1.5f C%1.5f\n",
1133 homing_axis[X_AXIS].home_offset, homing_axis[Y_AXIS].home_offset, homing_axis[Z_AXIS].home_offset);
1134 }
1135
1136 if (this->is_delta || this->is_scara) {
1137 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]);
1138 gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", homing_axis[Z_AXIS].homing_position);
1139 }
1140 if(saved_position[X_AXIS] != 0 || saved_position[Y_AXIS] != 0) {
1141 gcode->stream->printf(";predefined position:\nG28.1 X%1.4f Y%1.4f\n", saved_position[X_AXIS], saved_position[Y_AXIS]);
1142 }
1143 break;
1144
1145 case 665:
1146 if (this->is_delta || this->is_scara) { // M665 - set max gamma/z height
1147 float gamma_max = homing_axis[Z_AXIS].homing_position;
1148 if (gcode->has_letter('Z')) {
1149 homing_axis[Z_AXIS].homing_position= gamma_max = gcode->get_value('Z');
1150 }
1151 gcode->stream->printf("Max Z %8.3f ", gamma_max);
1152 gcode->add_nl = true;
1153 }
1154 break;
1155
1156 case 666:
1157 if(this->is_delta || this->is_scara) { // M666 - set trim for each axis in mm, NB negative mm trim is down
1158 if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X');
1159 if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y');
1160 if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z');
1161
1162 // print the current trim values in mm
1163 gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
1164
1165 }
1166 break;
1167
1168 }
1169 }
1170 }
1171
1172 void Endstops::on_get_public_data(void* argument)
1173 {
1174 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
1175
1176 if(!pdr->starts_with(endstops_checksum)) return;
1177
1178 if(pdr->second_element_is(trim_checksum)) {
1179 pdr->set_data_ptr(&this->trim_mm);
1180 pdr->set_taken();
1181
1182 } else if(pdr->second_element_is(home_offset_checksum)) {
1183 // provided by caller
1184 float *data = static_cast<float *>(pdr->get_data_ptr());
1185 for (int i = 0; i < 3; ++i) {
1186 data[i]= homing_axis[i].home_offset;
1187 }
1188 pdr->set_taken();
1189
1190 } else if(pdr->second_element_is(saved_position_checksum)) {
1191 pdr->set_data_ptr(&this->saved_position);
1192 pdr->set_taken();
1193
1194 } else if(pdr->second_element_is(get_homing_status_checksum)) {
1195 bool *homing = static_cast<bool *>(pdr->get_data_ptr());
1196 *homing = this->status != NOT_HOMING;
1197 pdr->set_taken();
1198
1199 } else if(pdr->second_element_is(get_homed_status_checksum)) {
1200 bool *homed = static_cast<bool *>(pdr->get_data_ptr());
1201 for (int i = 0; i < 3; ++i) {
1202 homed[i]= homing_axis[i].homed;
1203 }
1204 pdr->set_taken();
1205 }
1206 }
1207
1208 void Endstops::on_set_public_data(void* argument)
1209 {
1210 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
1211
1212 if(!pdr->starts_with(endstops_checksum)) return;
1213
1214 if(pdr->second_element_is(trim_checksum)) {
1215 float *t = static_cast<float*>(pdr->get_data_ptr());
1216 this->trim_mm[0] = t[0];
1217 this->trim_mm[1] = t[1];
1218 this->trim_mm[2] = t[2];
1219 pdr->set_taken();
1220
1221 } else if(pdr->second_element_is(home_offset_checksum)) {
1222 float *t = static_cast<float*>(pdr->get_data_ptr());
1223 if(!isnan(t[0])) homing_axis[0].home_offset= t[0];
1224 if(!isnan(t[1])) homing_axis[1].home_offset= t[1];
1225 if(!isnan(t[2])) homing_axis[2].home_offset= t[2];
1226 }
1227 }