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/>.
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"
14 #include "libs/nuts_bolts.h"
16 #include "libs/StepperMotor.h"
17 #include "wait_api.h" // mbed.h lib
20 #include "SlowTicker.h"
22 #include "checksumm.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"
36 // OLD deprecated syntax
37 #define endstops_module_enable_checksum CHECKSUM("endstops_enable")
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"), \
49 CHECKSUM(X "_limit_enable"), \
53 enum DEFNS
{MIN_PIN
, MAX_PIN
, MAX_TRAVEL
, FAST_RATE
, SLOW_RATE
, RETRACT
, DIRECTION
, MIN
, MAX
, LIMIT
, NDEFNS
};
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")
61 #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
62 #define endstop_debounce_ms_checksum CHECKSUM("endstop_debounce_ms")
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")
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")
73 // endstop.xmin.enable true
74 // endstop.xmin.pin 1.29
75 // endstop.xmin.axis X
76 // endstop.xmin.homing_direction home_to_min
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")
90 #define STEPPER THEROBOT->actuators
91 #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
97 MOVING_TO_ENDSTOP_FAST
, // homing move
98 MOVING_TO_ENDSTOP_SLOW
, // homing move
99 MOVING_BACK
, // homing move
108 this->status
= NOT_HOMING
;
111 void Endstops::on_module_loaded()
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()) {
121 // check for new config syntax
128 register_for_event(ON_GCODE_RECEIVED
);
129 register_for_event(ON_GET_PUBLIC_DATA
);
130 register_for_event(ON_SET_PUBLIC_DATA
);
133 THEKERNEL
->slow_ticker
->attach(1000, this, &Endstops::read_endstops
);
136 // Get config using old deprecated syntax Does not support ABC
137 bool Endstops::load_old_config()
139 uint16_t const checksums
[][NDEFNS
] = {
140 ENDSTOP_CHECKSUMS("alpha"), // X
141 ENDSTOP_CHECKSUMS("beta"), // Y
142 ENDSTOP_CHECKSUMS("gamma") // Z
145 bool limit_enabled
= false;
146 for (int i
= X_AXIS
; i
<= Z_AXIS
; ++i
) { // X_AXIS to Z_AXIS
149 // init homing struct
150 hinfo
.home_offset
= 0;
154 hinfo
.pin_info
= nullptr;
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();
161 hinfo
.retract
= THEKERNEL
->config
->value(checksums
[i
][RETRACT
])->by_default(5)->as_number();
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";
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();
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();
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
183 // enter into endstop array
184 endstops
.push_back(info
);
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
;
195 info
->limit_enable
= THEKERNEL
->config
->value(checksums
[i
][LIMIT
])->by_default(false)->as_bool();
196 limit_enabled
|= info
->limit_enable
;
199 homing_axis
.push_back(hinfo
);
202 // if no pins defined then disable the module
203 if(endstops
.empty()) return false;
205 get_global_configs();
208 register_for_event(ON_IDLE
);
211 // sanity check for deltas
213 if(this->is_delta || this->is_rdelta) {
214 // some things must be the same or they will die, so force it here to avoid config errors
215 this->fast_rates[1] = this->fast_rates[2] = this->fast_rates[0];
216 this->slow_rates[1] = this->slow_rates[2] = this->slow_rates[0];
217 this->retract_mm[1] = this->retract_mm[2] = this->retract_mm[0];
218 this->home_direction[1] = this->home_direction[2] = this->home_direction[0];
219 // NOTE homing_position for rdelta is the angle of the actuator not the cartesian position
220 if(!this->is_rdelta) this->homing_position[0] = this->homing_position[1] = 0;
227 // Get config using new syntax supports ABC
228 bool Endstops::load_config()
230 bool limit_enabled
= false;
232 std::array
<homing_info_t
, k_max_actuators
> temp_axis_array
; // needs to be at least XYZ, but allow for ABC
239 temp_axis_array
.fill(t
);
242 // iterate over all endstop.*.*
243 std::vector
<uint16_t> modules
;
244 THEKERNEL
->config
->get_module_list(&modules
, endstop_checksum
);
245 for(auto cs
: modules
) {
246 if(!THEKERNEL
->config
->value(endstop_checksum
, cs
, enable_checksum
)->as_bool()) continue;
248 endstop_info_t
*pin_info
= new endstop_info_t
;
249 pin_info
->pin
.from_string(THEKERNEL
->config
->value(endstop_checksum
, cs
, pin_checksum
)->by_default("nc" )->as_string())->as_input();
250 if(!pin_info
->pin
.connected()){
251 // no pin defined try next
256 string axis
= THEKERNEL
->config
->value(endstop_checksum
, cs
, axis_checksum
)->by_default("")->as_string();
264 switch(toupper(axis
[0])) {
265 case 'X': i
= X_AXIS
; break;
266 case 'Y': i
= Y_AXIS
; break;
267 case 'Z': i
= Z_AXIS
; break;
268 case 'A': i
= A_AXIS
; break;
269 case 'B': i
= B_AXIS
; break;
270 case 'C': i
= C_AXIS
; break;
271 default: // not a recognized axis
277 pin_info
->debounce
= 0;
278 pin_info
->axis
= toupper(axis
[0]);
279 pin_info
->axis_index
= i
;
281 // are limits enabled
282 pin_info
->limit_enable
= THEKERNEL
->config
->value(endstop_checksum
, cs
, limit_checksum
)->by_default(false)->as_bool();
283 limit_enabled
|= pin_info
->limit_enable
;
285 // enter into endstop array
286 endstops
.push_back(pin_info
);
288 // check we are not going above the number of defined actuators/axis
289 if(i
>= k_max_actuators
) {
290 // too many axis we only have configured k_max_actuators
294 // if set to none it means not used for homing (maybe limit only) so do not add to the homing array
295 string direction
= THEKERNEL
->config
->value(endstop_checksum
, cs
, direction_checksum
)->by_default("none")->as_string();
296 if(direction
== "none") {
300 // setup the homing array
303 // init homing struct
304 hinfo
.home_offset
= 0;
306 hinfo
.axis
= toupper(axis
[0]);
308 hinfo
.pin_info
= pin_info
;
311 hinfo
.fast_rate
= THEKERNEL
->config
->value(endstop_checksum
, cs
, fast_rate_checksum
)->by_default(100)->as_number();
312 hinfo
.slow_rate
= THEKERNEL
->config
->value(endstop_checksum
, cs
, slow_rate_checksum
)->by_default(10)->as_number();
315 hinfo
.retract
= THEKERNEL
->config
->value(endstop_checksum
, cs
, retract_checksum
)->by_default(5)->as_number();
317 // homing direction and convert to boolean where true is home to min, and false is home to max
318 hinfo
.home_direction
= direction
== "home_to_min";
320 // homing cartesian position
321 hinfo
.homing_position
= THEKERNEL
->config
->value(endstop_checksum
, cs
, position_checksum
)->by_default(hinfo
.home_direction
? 0 : 200)->as_number();
323 // used to set maximum movement on homing, set by max_travel if defined
324 hinfo
.max_travel
= THEKERNEL
->config
->value(endstop_checksum
, cs
, max_travel_checksum
)->by_default(500)->as_number();
326 // stick into array in correct place
327 temp_axis_array
[hinfo
.axis_index
]= hinfo
;
330 // if no pins defined then disable the module
331 if(endstops
.empty()) return false;
333 // copy to the homing_axis array
334 for (size_t i
= 0; i
< temp_axis_array
.size(); ++i
) {
335 if(temp_axis_array
[i
].axis
== 0) {
336 // was not configured above, if it is XYZ then we need to force a dummy entry
341 t
.pin_info
= nullptr; // this tells it that it cannot be used for homing
342 homing_axis
.push_back(t
);
346 homing_axis
.push_back(temp_axis_array
[i
]);
350 // sets some endstop global configs applicable to all endstops
351 get_global_configs();
354 register_for_event(ON_IDLE
);
360 void Endstops::get_global_configs()
362 // NOTE the debounce count is in milliseconds so probably does not need to beset anymore
363 this->debounce_ms
= THEKERNEL
->config
->value(endstop_debounce_ms_checksum
)->by_default(0)->as_number();
364 this->debounce_count
= THEKERNEL
->config
->value(endstop_debounce_count_checksum
)->by_default(100)->as_number();
366 this->is_corexy
= THEKERNEL
->config
->value(corexy_homing_checksum
)->by_default(false)->as_bool();
367 this->is_delta
= THEKERNEL
->config
->value(delta_homing_checksum
)->by_default(false)->as_bool();
368 this->is_rdelta
= THEKERNEL
->config
->value(rdelta_homing_checksum
)->by_default(false)->as_bool();
369 this->is_scara
= THEKERNEL
->config
->value(scara_homing_checksum
)->by_default(false)->as_bool();
371 this->home_z_first
= THEKERNEL
->config
->value(home_z_first_checksum
)->by_default(false)->as_bool();
373 this->trim_mm
[0] = THEKERNEL
->config
->value(alpha_trim_checksum
)->by_default(0)->as_number();
374 this->trim_mm
[1] = THEKERNEL
->config
->value(beta_trim_checksum
)->by_default(0)->as_number();
375 this->trim_mm
[2] = THEKERNEL
->config
->value(gamma_trim_checksum
)->by_default(0)->as_number();
377 // see if an order has been specified, must be three or more characters, XYZABC or ABYXZ etc
378 string order
= THEKERNEL
->config
->value(homing_order_checksum
)->by_default("")->as_string();
379 this->homing_order
= 0;
380 if(order
.size() >= 3 && order
.size() <= homing_axis
.size() && !(this->is_delta
|| this->is_rdelta
)) {
382 for(auto c
: order
) {
383 uint8_t n
= toupper(c
);
384 uint8_t i
= n
>= 'X' ? n
- 'X' : n
- 'A' + 3;
385 if(i
> 6) { // bad value
386 this->homing_order
= 0;
389 homing_order
|= (i
<< shift
);
394 // set to true by default for deltas due to trim, false on cartesians
395 this->move_to_origin_after_home
= THEKERNEL
->config
->value(move_to_origin_checksum
)->by_default(is_delta
)->as_bool();
398 bool Endstops::debounced_get(Pin
*pin
)
400 if(pin
== nullptr) return false;
401 uint8_t debounce
= 0;
403 if ( ++debounce
>= this->debounce_count
) {
411 // only called if limits are enabled
412 void Endstops::on_idle(void *argument
)
414 if(this->status
== LIMIT_TRIGGERED
) {
415 // if we were in limit triggered see if it has been cleared
416 for(auto& i
: endstops
) {
417 if(i
->limit_enable
) {
419 // still triggered, so exit
424 if(i
->debounce
++ > debounce_count
) { // can use less as it calls on_idle in between
426 this->status
= NOT_HOMING
;
432 } else if(this->status
!= NOT_HOMING
) {
433 // don't check while homing
437 for(auto& i
: endstops
) {
438 if(i
->limit_enable
&& STEPPER
[i
->axis_index
]->is_moving()) {
439 // check min and max endstops
440 if(debounced_get(&i
->pin
)) {
442 THEKERNEL
->streams
->printf("Limit switch %c was hit - reset or M999 required\n", i
->axis
);
443 this->status
= LIMIT_TRIGGERED
;
445 // disables heaters and motors, ignores incoming Gcode and flushes block queue
446 THEKERNEL
->call_event(ON_HALT
, nullptr);
453 // if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move
454 // checks if triggered and only backs off if triggered
455 void Endstops::back_off_home(axis_bitmap_t axis
)
457 std::vector
<std::pair
<char, float>> params
;
458 this->status
= BACK_OFF_HOME
;
460 float slow_rate
= NAN
; // default mm/sec
462 // these are handled differently
464 // Move off of the endstop using a regular relative move in Z only
465 params
.push_back({'Z', homing_axis
[Z_AXIS
].retract
* (homing_axis
[Z_AXIS
].home_direction
? 1 : -1)});
466 slow_rate
= homing_axis
[Z_AXIS
].slow_rate
;
469 // cartesians, concatenate all the moves we need to do into one gcode
470 for( auto& e
: homing_axis
) {
471 if(!axis
[e
.axis_index
]) continue; // only for axes we asked to move
473 // if not triggered no need to move off
474 if(e
.pin_info
!= nullptr && e
.pin_info
->limit_enable
&& debounced_get(&e
.pin_info
->pin
)) {
476 params
.push_back({ax
, e
.retract
* (e
.home_direction
? 1 : -1)});
477 // select slowest of them all
478 slow_rate
= isnan(slow_rate
) ? e
.slow_rate
: std::min(slow_rate
, e
.slow_rate
);
483 if(!params
.empty()) {
484 // Move off of the endstop using a regular relative move
485 params
.insert(params
.begin(), {'G', 0});
486 // use X slow rate to move, Z should have a max speed set anyway
487 params
.push_back({'F', slow_rate
* 60.0F
});
489 append_parameters(gcode_buf
, params
, sizeof(gcode_buf
));
490 Gcode
gc(gcode_buf
, &(StreamOutput::NullStream
));
491 THEROBOT
->push_state();
492 THEROBOT
->inch_mode
= false; // needs to be in mm
493 THEROBOT
->absolute_mode
= false; // needs to be relative mode
494 THEROBOT
->on_gcode_received(&gc
); // send to robot directly
495 // Wait for above to finish
496 THECONVEYOR
->wait_for_idle();
497 THEROBOT
->pop_state();
500 this->status
= NOT_HOMING
;
503 // If enabled will move the head to 0,0 after homing, but only if X and Y were set to home
504 void Endstops::move_to_origin(axis_bitmap_t axis
)
506 if(!is_delta
&& (!axis
[X_AXIS
] || !axis
[Y_AXIS
])) return; // ignore if X and Y not homing, unless delta
508 // Do we need to check if we are already at 0,0? probably not as the G0 will not do anything if we are
509 // float pos[3]; THEROBOT->get_axis_position(pos); if(pos[0] == 0 && pos[1] == 0) return;
511 this->status
= MOVE_TO_ORIGIN
;
512 // Move to center using a regular move, use slower of X and Y fast rate
513 //float rate = std::min(this->fast_rates[0], this->fast_rates[1]) * 60.0F;
515 THEROBOT
->push_state();
516 THEROBOT
->inch_mode
= false; // needs to be in mm
517 THEROBOT
->absolute_mode
= true;
518 //snprintf(buf, sizeof(buf), "G53 G0 X0 Y0 F%1.4f", rate); // must use machine coordinates in case G92 or WCS is in effect
519 snprintf(buf
, sizeof(buf
), "G53 G0 X0 Y0"); // must use machine coordinates in case G92 or WCS is in effect
520 struct SerialMessage message
;
521 message
.message
= buf
;
522 message
.stream
= &(StreamOutput::NullStream
);
523 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
); // as it is a multi G code command
524 // Wait for above to finish
525 THECONVEYOR
->wait_for_idle();
526 THEROBOT
->pop_state();
527 this->status
= NOT_HOMING
;
530 // Called every millisecond in an ISR
531 uint32_t Endstops::read_endstops(uint32_t dummy
)
533 if(this->status
!= MOVING_TO_ENDSTOP_SLOW
&& this->status
!= MOVING_TO_ENDSTOP_FAST
) return 0; // not doing anything we need to monitor for
535 // check each homing endstop
536 for(auto& e
: homing_axis
) { // check all axis homing endstops
537 if(e
.pin_info
== nullptr) continue; // ignore if not a homing endstop
540 // 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
541 if(is_corexy
&& (m
== X_AXIS
|| m
== Y_AXIS
) && !axis_to_home
[m
]) continue;
543 if(STEPPER
[m
]->is_moving()) {
544 // if it is moving then we check the associated endstop, and debounce it
545 if(e
.pin_info
->pin
.get()) {
546 if(e
.pin_info
->debounce
< debounce_ms
) {
547 e
.pin_info
->debounce
++;
550 if(is_corexy
&& (m
== X_AXIS
|| m
== Y_AXIS
)) {
551 // corexy when moving in X or Y we need to stop both the X and Y motors
552 STEPPER
[X_AXIS
]->stop_moving();
553 STEPPER
[Y_AXIS
]->stop_moving();
556 // we signal the motor to stop, which will preempt any moves on that axis
557 STEPPER
[m
]->stop_moving();
562 // The endstop was not hit yet
563 e
.pin_info
->debounce
= 0;
571 void Endstops::home_xy()
573 if(axis_to_home
[X_AXIS
] && axis_to_home
[Y_AXIS
]) {
574 // Home XY first so as not to slow them down by homing Z at the same time
575 float delta
[3] {homing_axis
[X_AXIS
].max_travel
, homing_axis
[Y_AXIS
].max_travel
, 0};
576 if(homing_axis
[X_AXIS
].home_direction
) delta
[X_AXIS
]= -delta
[X_AXIS
];
577 if(homing_axis
[Y_AXIS
].home_direction
) delta
[Y_AXIS
]= -delta
[Y_AXIS
];
578 float feed_rate
= std::min(homing_axis
[X_AXIS
].fast_rate
, homing_axis
[Y_AXIS
].fast_rate
);
579 THEROBOT
->delta_move(delta
, feed_rate
, 3);
581 } else if(axis_to_home
[X_AXIS
]) {
583 float delta
[3] {homing_axis
[X_AXIS
].max_travel
, 0, 0};
584 if(homing_axis
[X_AXIS
].home_direction
) delta
[X_AXIS
]= -delta
[X_AXIS
];
585 THEROBOT
->delta_move(delta
, homing_axis
[X_AXIS
].fast_rate
, 3);
587 } else if(axis_to_home
[Y_AXIS
]) {
589 float delta
[3] {0, homing_axis
[Y_AXIS
].max_travel
, 0};
590 if(homing_axis
[Y_AXIS
].home_direction
) delta
[Y_AXIS
]= -delta
[Y_AXIS
];
591 THEROBOT
->delta_move(delta
, homing_axis
[Y_AXIS
].fast_rate
, 3);
594 // Wait for axis to have homed
595 THECONVEYOR
->wait_for_idle();
598 void Endstops::home(axis_bitmap_t a
)
600 // reset debounce counts for all endstops
601 for(auto& e
: endstops
) {
605 this->axis_to_home
= a
;
607 // Start moving the axes to the origin
608 this->status
= MOVING_TO_ENDSTOP_FAST
;
610 THEROBOT
->disable_segmentation
= true; // we must disable segmentation as this won't work with it enabled
612 if(!home_z_first
) home_xy();
614 if(axis_to_home
[Z_AXIS
]) {
616 float delta
[3] {0, 0, homing_axis
[Z_AXIS
].max_travel
}; // we go the max z
617 if(homing_axis
[Z_AXIS
].home_direction
) delta
[Z_AXIS
]= -delta
[Z_AXIS
];
618 THEROBOT
->delta_move(delta
, homing_axis
[Z_AXIS
].fast_rate
, 3);
620 THECONVEYOR
->wait_for_idle();
623 if(home_z_first
) home_xy();
625 // potentially home A B and C individually
626 if(homing_axis
.size() > 3){
627 for (size_t i
= A_AXIS
; i
< homing_axis
.size(); ++i
) {
628 if(axis_to_home
[i
]) {
631 for (size_t j
= 0; j
<= i
; ++j
) delta
[j
]= 0;
632 delta
[i
]= homing_axis
[i
].max_travel
; // we go the max
633 if(homing_axis
[i
].home_direction
) delta
[i
]= -delta
[i
];
634 THEROBOT
->delta_move(delta
, homing_axis
[i
].fast_rate
, i
+1);
636 THECONVEYOR
->wait_for_idle();
642 // TODO: should check that the endstops were hit and it did not stop short for some reason
643 // we did not complete movement the full distance if we hit the endstops
644 // TODO Maybe only reset axis involved in the homing cycle
645 THEROBOT
->reset_position_from_current_actuator_position();
647 // Move back a small distance for all homing axis
648 this->status
= MOVING_BACK
;
649 float delta
[homing_axis
.size()];
650 for (size_t i
= 0; i
< homing_axis
.size(); ++i
) delta
[i
]= 0;
652 // use minimum feed rate of all axes that are being homed (sub optimal, but necessary)
653 float feed_rate
= homing_axis
[X_AXIS
].slow_rate
;
654 for (auto& i
: homing_axis
) {
656 if(axis_to_home
[c
]) {
658 if(!i
.home_direction
) delta
[c
]= -delta
[c
];
659 feed_rate
= std::min(i
.slow_rate
, feed_rate
);
663 THEROBOT
->delta_move(delta
, feed_rate
, homing_axis
.size());
664 // wait until finished
665 THECONVEYOR
->wait_for_idle();
667 // Start moving the axes towards the endstops slowly
668 this->status
= MOVING_TO_ENDSTOP_SLOW
;
669 for (auto& i
: homing_axis
) {
671 if(axis_to_home
[c
]) {
672 delta
[c
]= i
.retract
*2; // move further than we moved off to make sure we hit it cleanly
673 if(i
.home_direction
) delta
[c
]= -delta
[c
];
678 THEROBOT
->delta_move(delta
, feed_rate
, homing_axis
.size());
679 // wait until finished
680 THECONVEYOR
->wait_for_idle();
682 // TODO: should check that the endstops were hit and it did not stop short for some reason
683 // we did not complete movement the full distance if we hit the endstops
684 // TODO Maybe only reset axis involved in the homing cycle
685 THEROBOT
->reset_position_from_current_actuator_position();
687 THEROBOT
->disable_segmentation
= false;
689 this->status
= NOT_HOMING
;
692 void Endstops::process_home_command(Gcode
* gcode
)
694 // First wait for the queue to be empty
695 THECONVEYOR
->wait_for_idle();
697 // turn off any compensation transform so Z does not move as XY home
698 auto savect
= THEROBOT
->compensationTransform
;
699 THEROBOT
->compensationTransform
= nullptr;
701 // deltas always home Z axis only, which moves all three actuators
702 bool home_in_z
= this->is_delta
|| this->is_rdelta
;
704 // figure out which axis to home
708 if(!home_in_z
) { // ie not a delta
709 bool axis_speced
= (gcode
->has_letter('X') || gcode
->has_letter('Y') || gcode
->has_letter('Z') ||
710 gcode
->has_letter('A') || gcode
->has_letter('B') || gcode
->has_letter('C'));
712 for (auto &p
: homing_axis
) {
713 // only enable homing if the endstop is defined,
714 if(p
.pin_info
== nullptr) continue;
715 if(!axis_speced
|| gcode
->has_letter(p
.axis
)) {
716 haxis
.set(p
.axis_index
);
717 // now reset axis to 0 as we do not know what state we are in
718 THEROBOT
->reset_axis_position(0, p
.axis_index
);
723 // Only Z axis homes (even though all actuators move this is handled by arm solution)
725 // we also set the kinematics to a known good position, this is necessary for a rotary delta, but doesn't hurt for linear delta
726 THEROBOT
->reset_axis_position(0, 0, 0);
729 // do the actual homing
730 if(homing_order
!= 0) {
731 // if an order has been specified do it in the specified order
732 // homing order is 0bfffeeedddcccbbbaaa where aaa is 0,1,2,3,4,5 to specify the first axis (XYZABC), bb is the second and cc is the third etc
733 // eg 0b011010000001 would be Y X Z A, 010 001 000 011 100 would be B A X Y Z
734 for (uint8_t m
= homing_order
; m
!= 0; m
>>= 3) {
735 uint8_t a
= (m
& 0x07); // axis to home
736 if(a
<= homing_axis
.size() && haxis
[a
]) { // if axis is selected to home
741 // check if on_halt (eg kill)
742 if(THEKERNEL
->is_halted()) break;
745 } else if(is_corexy
) {
746 // corexy must home each axis individually
747 for (auto &p
: homing_axis
) {
748 if(haxis
[p
.axis_index
]) {
750 bs
.set(p
.axis_index
);
753 // check if on_halt (eg kill)
754 if(THEKERNEL
->is_halted()) break;
758 // they could all home at the same time
762 // restore compensationTransform
763 THEROBOT
->compensationTransform
= savect
;
765 // check if on_halt (eg kill)
766 if(THEKERNEL
->is_halted()) {
767 if(!THEKERNEL
->is_grbl_mode()) {
768 THEKERNEL
->streams
->printf("Homing cycle aborted by kill\n");
770 // clear all the homed flags
771 for (auto &p
: homing_axis
) p
.homed
= false;
775 if(home_in_z
) { // deltas only
776 // Here's where we would have been if the endstops were perfectly trimmed
777 // NOTE on a rotary delta home_offset is actuator position in degrees when homed and
778 // home_offset is the theta offset for each actuator, so M206 is used to set theta offset for each actuator in degrees
779 // FIXME not sure this will work with compensation transforms on.
780 float ideal_position
[3] = {
781 homing_axis
[X_AXIS
].homing_position
+ homing_axis
[X_AXIS
].home_offset
,
782 homing_axis
[Y_AXIS
].homing_position
+ homing_axis
[Y_AXIS
].home_offset
,
783 homing_axis
[Z_AXIS
].homing_position
+ homing_axis
[Z_AXIS
].home_offset
786 bool has_endstop_trim
= this->is_delta
;
787 if (has_endstop_trim
) {
788 ActuatorCoordinates ideal_actuator_position
;
789 THEROBOT
->arm_solution
->cartesian_to_actuator(ideal_position
, ideal_actuator_position
);
791 // We are actually not at the ideal position, but a trim away
792 ActuatorCoordinates real_actuator_position
= {
793 ideal_actuator_position
[X_AXIS
] - this->trim_mm
[X_AXIS
],
794 ideal_actuator_position
[Y_AXIS
] - this->trim_mm
[Y_AXIS
],
795 ideal_actuator_position
[Z_AXIS
] - this->trim_mm
[Z_AXIS
]
798 float real_position
[3];
799 THEROBOT
->arm_solution
->actuator_to_cartesian(real_actuator_position
, real_position
);
800 // Reset the actuator positions to correspond to our real position
801 THEROBOT
->reset_axis_position(real_position
[0], real_position
[1], real_position
[2]);
804 // without endstop trim, real_position == ideal_position
806 // with a rotary delta we set the actuators angle then use the FK to calculate the resulting cartesian coordinates
807 ActuatorCoordinates real_actuator_position
= {ideal_position
[0], ideal_position
[1], ideal_position
[2]};
808 THEROBOT
->reset_actuator_position(real_actuator_position
);
811 // Reset the actuator positions to correspond to our real position
812 THEROBOT
->reset_axis_position(ideal_position
[0], ideal_position
[1], ideal_position
[2]);
816 // for deltas we say all axis are homed even though it was only Z
817 for (auto &p
: homing_axis
) p
.homed
= true;
820 // Zero the ax(i/e)s position, add in the home offset
821 // 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
822 // so XY are at a known consistent position. (especially true if using a proximity probe)
823 for (auto &p
: homing_axis
) {
824 if (haxis
[p
.axis_index
]) { // if we requested this axis to home
825 THEROBOT
->reset_axis_position(p
.homing_position
+ p
.home_offset
, p
.axis_index
);
826 // set flag indicating axis was homed, it stays set once set until H/W reset or unhomed
832 // on some systems where 0,0 is bed center it is nice to have home goto 0,0 after homing
833 // default is off for cartesian on for deltas
835 // NOTE a rotary delta usually has optical or hall-effect endstops so it is safe to go past them a little bit
836 if(this->move_to_origin_after_home
) move_to_origin(haxis
);
837 // if limit switches are enabled we must back off endstop after setting home
838 back_off_home(haxis
);
840 } else if(this->move_to_origin_after_home
|| homing_axis
[X_AXIS
].pin_info
->limit_enable
) {
841 // 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
842 // also need to back off endstops if limits are enabled
843 back_off_home(haxis
);
844 if(this->move_to_origin_after_home
) move_to_origin(haxis
);
848 void Endstops::set_homing_offset(Gcode
*gcode
)
850 // M306 Similar to M206 but sets Homing offsets based on current MCS position
851 // Basically it finds the delta between the current MCS position and the requested position and adds it to the homing offset
852 // then will not let it be set again until that axis is homed.
854 THEROBOT
->get_axis_position(pos
);
856 if (gcode
->has_letter('X')) {
857 if(!homing_axis
[X_AXIS
].homed
) {
858 gcode
->stream
->printf("error: Axis X must be homed before setting Homing offset\n");
861 homing_axis
[X_AXIS
].home_offset
+= (THEROBOT
->to_millimeters(gcode
->get_value('X')) - pos
[X_AXIS
]);
862 homing_axis
[X_AXIS
].homed
= false; // force it to be homed
864 if (gcode
->has_letter('Y')) {
865 if(!homing_axis
[Y_AXIS
].homed
) {
866 gcode
->stream
->printf("error: Axis Y must be homed before setting Homing offset\n");
869 homing_axis
[Y_AXIS
].home_offset
+= (THEROBOT
->to_millimeters(gcode
->get_value('Y')) - pos
[Y_AXIS
]);
870 homing_axis
[Y_AXIS
].homed
= false; // force it to be homed
872 if (gcode
->has_letter('Z')) {
873 if(!homing_axis
[Z_AXIS
].homed
) {
874 gcode
->stream
->printf("error: Axis Z must be homed before setting Homing offset\n");
877 homing_axis
[Z_AXIS
].home_offset
+= (THEROBOT
->to_millimeters(gcode
->get_value('Z')) - pos
[Z_AXIS
]);
878 homing_axis
[Z_AXIS
].homed
= false; // force it to be homed
881 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
);
884 void Endstops::handle_park(Gcode
* gcode
)
886 // TODO: spec says if XYZ specified move to them first then move to MCS of specifed axis
887 THEROBOT
->push_state();
888 THEROBOT
->inch_mode
= false; // needs to be in mm
889 THEROBOT
->absolute_mode
= true;
891 snprintf(buf
, sizeof(buf
), "G53 G0 X%f Y%f", saved_position
[X_AXIS
], saved_position
[Y_AXIS
]); // must use machine coordinates in case G92 or WCS is in effect
892 struct SerialMessage message
;
893 message
.message
= buf
;
894 message
.stream
= &(StreamOutput::NullStream
);
895 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
); // as it is a multi G code command
896 // Wait for above to finish
897 THECONVEYOR
->wait_for_idle();
898 THEROBOT
->pop_state();
902 void Endstops::on_gcode_received(void *argument
)
904 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
906 if ( gcode
->has_g
&& gcode
->g
== 28) {
907 switch(gcode
->subcode
) {
908 case 0: // G28 in grbl mode will do a rapid to the predefined position otherwise it is home command
909 if(THEKERNEL
->is_grbl_mode()){
912 process_home_command(gcode
);
916 case 1: // G28.1 set pre defined park position
917 // saves current position in absolute machine coordinates
918 THEROBOT
->get_axis_position(saved_position
); // Only XY are used
919 // Note the following is only meant to be used for recovering a saved position from config-override
920 // Not a standard Gcode and not to be relied on
921 if (gcode
->has_letter('X')) saved_position
[X_AXIS
] = gcode
->get_value('X');
922 if (gcode
->has_letter('Y')) saved_position
[Y_AXIS
] = gcode
->get_value('Y');
925 case 2: // G28.2 in grbl mode does homing (triggered by $H), otherwise it moves to the park position
926 if(THEKERNEL
->is_grbl_mode()) {
927 process_home_command(gcode
);
933 case 3: // G28.3 is a smoothie special it sets manual homing
934 if(gcode
->get_num_args() == 0) {
935 for (auto &p
: homing_axis
) {
937 THEROBOT
->reset_axis_position(0, p
.axis_index
);
940 // do a manual homing based on given coordinates, no endstops required
941 if(gcode
->has_letter('X')){ THEROBOT
->reset_axis_position(gcode
->get_value('X'), X_AXIS
); homing_axis
[X_AXIS
].homed
= true; }
942 if(gcode
->has_letter('Y')){ THEROBOT
->reset_axis_position(gcode
->get_value('Y'), Y_AXIS
); homing_axis
[Y_AXIS
].homed
= true; }
943 if(gcode
->has_letter('Z')){ THEROBOT
->reset_axis_position(gcode
->get_value('Z'), Z_AXIS
); homing_axis
[Z_AXIS
].homed
= true; }
944 if(homing_axis
.size() > A_AXIS
&& gcode
->has_letter('A')){ THEROBOT
->reset_axis_position(gcode
->get_value('A'), A_AXIS
); homing_axis
[A_AXIS
].homed
= true; }
945 if(homing_axis
.size() > B_AXIS
&& gcode
->has_letter('B')){ THEROBOT
->reset_axis_position(gcode
->get_value('B'), B_AXIS
); homing_axis
[B_AXIS
].homed
= true; }
946 if(homing_axis
.size() > C_AXIS
&& gcode
->has_letter('C')){ THEROBOT
->reset_axis_position(gcode
->get_value('C'), C_AXIS
); homing_axis
[C_AXIS
].homed
= true; }
950 case 4: { // G28.4 is a smoothie special it sets manual homing based on the actuator position (used for rotary delta)
951 // do a manual homing based on given coordinates, no endstops required
952 ActuatorCoordinates ac
{NAN
, NAN
, NAN
};
953 if(gcode
->has_letter('X')){ ac
[0] = gcode
->get_value('X'); homing_axis
[X_AXIS
].homed
= true; }
954 if(gcode
->has_letter('Y')){ ac
[1] = gcode
->get_value('Y'); homing_axis
[Y_AXIS
].homed
= true; }
955 if(gcode
->has_letter('Z')){ ac
[2] = gcode
->get_value('Z'); homing_axis
[Z_AXIS
].homed
= true; }
956 THEROBOT
->reset_actuator_position(ac
);
960 case 5: // G28.5 is a smoothie special it clears the homed flag for the specified axis, or all if not specifed
961 if(gcode
->get_num_args() == 0) {
962 for (auto &p
: homing_axis
) p
.homed
= false;
964 if(gcode
->has_letter('X')) homing_axis
[X_AXIS
].homed
= false;
965 if(gcode
->has_letter('Y')) homing_axis
[Y_AXIS
].homed
= false;
966 if(gcode
->has_letter('Z')) homing_axis
[Z_AXIS
].homed
= false;
967 if(homing_axis
.size() > A_AXIS
&& gcode
->has_letter('A')) homing_axis
[A_AXIS
].homed
= false;
968 if(homing_axis
.size() > B_AXIS
&& gcode
->has_letter('B')) homing_axis
[B_AXIS
].homed
= false;
969 if(homing_axis
.size() > C_AXIS
&& gcode
->has_letter('C')) homing_axis
[C_AXIS
].homed
= false;
973 case 6: // G28.6 is a smoothie special it shows the homing status of each axis
974 for (auto &p
: homing_axis
) {
975 gcode
->stream
->printf("%c:%d ", p
.axis
, p
.homed
);
981 if(THEKERNEL
->is_grbl_mode()) {
982 gcode
->stream
->printf("error:Unsupported command\n");
987 } else if (gcode
->has_m
) {
991 for(auto& h
: homing_axis
) {
993 name
.append(1, h
.axis
).append(h
.home_direction
? "_min" : "_max");
994 gcode
->stream
->printf("%s:%d ", name
.c_str(), h
.pin_info
->pin
.get());
996 gcode
->stream
->printf("pins- ");
997 for(auto& p
: endstops
) {
998 string
str(1, p
->axis
);
999 if(p
->limit_enable
) str
.append("L");
1000 gcode
->stream
->printf("(%s)P%d.%d:%d ", str
.c_str(), p
->pin
.port_number
, p
->pin
.pin
, p
->pin
.get());
1002 gcode
->add_nl
= true;
1006 case 206: // M206 - set homing offset
1007 if(is_rdelta
) return; // RotaryDeltaCalibration module will handle this
1008 for (auto &p
: homing_axis
) {
1009 if (gcode
->has_letter(p
.axis
)) p
.home_offset
= gcode
->get_value(p
.axis
);
1012 for (auto &p
: homing_axis
) {
1013 gcode
->stream
->printf("%c: %5.3f ", p
.axis
, p
.home_offset
);
1016 gcode
->stream
->printf(" will take effect next home\n");
1019 case 306: // set homing offset based on current position
1020 if(is_rdelta
) return; // RotaryDeltaCalibration module will handle this
1022 set_homing_offset(gcode
);
1025 case 500: // save settings
1026 case 503: // print settings
1028 gcode
->stream
->printf(";Home offset (mm):\nM206 ");
1029 for (auto &p
: homing_axis
) {
1030 gcode
->stream
->printf("%c%1.2f ", p
.axis
, p
.home_offset
);
1032 gcode
->stream
->printf("\n");
1035 gcode
->stream
->printf(";Theta offset (degrees):\nM206 A%1.5f B%1.5f C%1.5f\n",
1036 homing_axis
[X_AXIS
].home_offset
, homing_axis
[Y_AXIS
].home_offset
, homing_axis
[Z_AXIS
].home_offset
);
1039 if (this->is_delta
|| this->is_scara
) {
1040 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]);
1041 gcode
->stream
->printf(";Max Z\nM665 Z%1.3f\n", homing_axis
[Z_AXIS
].homing_position
);
1043 if(saved_position
[X_AXIS
] != 0 || saved_position
[Y_AXIS
] != 0) {
1044 gcode
->stream
->printf(";predefined position:\nG28.1 X%1.4f Y%1.4f\n", saved_position
[X_AXIS
], saved_position
[Y_AXIS
]);
1049 if (this->is_delta
|| this->is_scara
) { // M665 - set max gamma/z height
1050 float gamma_max
= homing_axis
[Z_AXIS
].homing_position
;
1051 if (gcode
->has_letter('Z')) {
1052 homing_axis
[Z_AXIS
].homing_position
= gamma_max
= gcode
->get_value('Z');
1054 gcode
->stream
->printf("Max Z %8.3f ", gamma_max
);
1055 gcode
->add_nl
= true;
1060 if(this->is_delta
|| this->is_scara
) { // M666 - set trim for each axis in mm, NB negative mm trim is down
1061 if (gcode
->has_letter('X')) trim_mm
[0] = gcode
->get_value('X');
1062 if (gcode
->has_letter('Y')) trim_mm
[1] = gcode
->get_value('Y');
1063 if (gcode
->has_letter('Z')) trim_mm
[2] = gcode
->get_value('Z');
1065 // print the current trim values in mm
1066 gcode
->stream
->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm
[0], trim_mm
[1], trim_mm
[2]);
1075 void Endstops::on_get_public_data(void* argument
)
1077 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
1079 if(!pdr
->starts_with(endstops_checksum
)) return;
1081 if(pdr
->second_element_is(trim_checksum
)) {
1082 pdr
->set_data_ptr(&this->trim_mm
);
1085 } else if(pdr
->second_element_is(home_offset_checksum
)) {
1086 // provided by caller
1087 float *data
= static_cast<float *>(pdr
->get_data_ptr());
1088 for (int i
= 0; i
< 3; ++i
) {
1089 data
[i
]= homing_axis
[i
].home_offset
;
1093 } else if(pdr
->second_element_is(saved_position_checksum
)) {
1094 pdr
->set_data_ptr(&this->saved_position
);
1097 } else if(pdr
->second_element_is(get_homing_status_checksum
)) {
1098 bool *homing
= static_cast<bool *>(pdr
->get_data_ptr());
1099 *homing
= this->status
!= NOT_HOMING
;
1104 void Endstops::on_set_public_data(void* argument
)
1106 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
1108 if(!pdr
->starts_with(endstops_checksum
)) return;
1110 if(pdr
->second_element_is(trim_checksum
)) {
1111 float *t
= static_cast<float*>(pdr
->get_data_ptr());
1112 this->trim_mm
[0] = t
[0];
1113 this->trim_mm
[1] = t
[1];
1114 this->trim_mm
[2] = t
[2];
1117 } else if(pdr
->second_element_is(home_offset_checksum
)) {
1118 float *t
= static_cast<float*>(pdr
->get_data_ptr());
1119 if(!isnan(t
[0])) homing_axis
[0].home_offset
= t
[0];
1120 if(!isnan(t
[1])) homing_axis
[1].home_offset
= t
[1];
1121 if(!isnan(t
[2])) homing_axis
[2].home_offset
= t
[2];