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"
13 #include "libs/nuts_bolts.h"
15 #include "libs/StepperMotor.h"
16 #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"
30 #include "StepTicker.h"
31 #include "BaseSolution.h"
42 #define endstops_module_enable_checksum CHECKSUM("endstops_enable")
43 #define corexy_homing_checksum CHECKSUM("corexy_homing")
44 #define delta_homing_checksum CHECKSUM("delta_homing")
45 #define scara_homing_checksum CHECKSUM("scara_homing")
47 #define alpha_min_endstop_checksum CHECKSUM("alpha_min_endstop")
48 #define beta_min_endstop_checksum CHECKSUM("beta_min_endstop")
49 #define gamma_min_endstop_checksum CHECKSUM("gamma_min_endstop")
51 #define alpha_max_endstop_checksum CHECKSUM("alpha_max_endstop")
52 #define beta_max_endstop_checksum CHECKSUM("beta_max_endstop")
53 #define gamma_max_endstop_checksum CHECKSUM("gamma_max_endstop")
55 #define alpha_trim_checksum CHECKSUM("alpha_trim")
56 #define beta_trim_checksum CHECKSUM("beta_trim")
57 #define gamma_trim_checksum CHECKSUM("gamma_trim")
59 // these values are in steps and should be deprecated
60 #define alpha_fast_homing_rate_checksum CHECKSUM("alpha_fast_homing_rate")
61 #define beta_fast_homing_rate_checksum CHECKSUM("beta_fast_homing_rate")
62 #define gamma_fast_homing_rate_checksum CHECKSUM("gamma_fast_homing_rate")
64 #define alpha_slow_homing_rate_checksum CHECKSUM("alpha_slow_homing_rate")
65 #define beta_slow_homing_rate_checksum CHECKSUM("beta_slow_homing_rate")
66 #define gamma_slow_homing_rate_checksum CHECKSUM("gamma_slow_homing_rate")
68 #define alpha_homing_retract_checksum CHECKSUM("alpha_homing_retract")
69 #define beta_homing_retract_checksum CHECKSUM("beta_homing_retract")
70 #define gamma_homing_retract_checksum CHECKSUM("gamma_homing_retract")
72 // same as above but in user friendly mm/s and mm
73 #define alpha_fast_homing_rate_mm_checksum CHECKSUM("alpha_fast_homing_rate_mm_s")
74 #define beta_fast_homing_rate_mm_checksum CHECKSUM("beta_fast_homing_rate_mm_s")
75 #define gamma_fast_homing_rate_mm_checksum CHECKSUM("gamma_fast_homing_rate_mm_s")
77 #define alpha_slow_homing_rate_mm_checksum CHECKSUM("alpha_slow_homing_rate_mm_s")
78 #define beta_slow_homing_rate_mm_checksum CHECKSUM("beta_slow_homing_rate_mm_s")
79 #define gamma_slow_homing_rate_mm_checksum CHECKSUM("gamma_slow_homing_rate_mm_s")
81 #define alpha_homing_retract_mm_checksum CHECKSUM("alpha_homing_retract_mm")
82 #define beta_homing_retract_mm_checksum CHECKSUM("beta_homing_retract_mm")
83 #define gamma_homing_retract_mm_checksum CHECKSUM("gamma_homing_retract_mm")
85 #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
87 #define alpha_homing_direction_checksum CHECKSUM("alpha_homing_direction")
88 #define beta_homing_direction_checksum CHECKSUM("beta_homing_direction")
89 #define gamma_homing_direction_checksum CHECKSUM("gamma_homing_direction")
90 #define home_to_max_checksum CHECKSUM("home_to_max")
91 #define home_to_min_checksum CHECKSUM("home_to_min")
92 #define alpha_min_checksum CHECKSUM("alpha_min")
93 #define beta_min_checksum CHECKSUM("beta_min")
94 #define gamma_min_checksum CHECKSUM("gamma_min")
96 #define alpha_max_checksum CHECKSUM("alpha_max")
97 #define beta_max_checksum CHECKSUM("beta_max")
98 #define gamma_max_checksum CHECKSUM("gamma_max")
100 #define alpha_limit_enable_checksum CHECKSUM("alpha_limit_enable")
101 #define beta_limit_enable_checksum CHECKSUM("beta_limit_enable")
102 #define gamma_limit_enable_checksum CHECKSUM("gamma_limit_enable")
104 #define homing_order_checksum CHECKSUM("homing_order")
105 #define move_to_origin_checksum CHECKSUM("move_to_origin_after_home")
107 #define STEPPER THEKERNEL->robot->actuators
108 #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
113 MOVING_TO_ENDSTOP_FAST
, // homing move
114 MOVING_BACK
, // homing move
115 MOVING_TO_ENDSTOP_SLOW
, // homing move
124 this->status
= NOT_HOMING
;
125 home_offset
[0] = home_offset
[1] = home_offset
[2] = 0.0F
;
128 void Endstops::on_module_loaded()
130 // Do not do anything if not enabled
131 if ( THEKERNEL
->config
->value( endstops_module_enable_checksum
)->by_default(true)->as_bool() == false ) {
136 register_for_event(ON_GCODE_RECEIVED
);
137 register_for_event(ON_GET_PUBLIC_DATA
);
138 register_for_event(ON_SET_PUBLIC_DATA
);
140 THEKERNEL
->step_ticker
->register_acceleration_tick_handler([this](){acceleration_tick(); });
143 this->on_config_reload(this);
147 void Endstops::on_config_reload(void *argument
)
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();
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);
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();
178 this->debounce_count
= THEKERNEL
->config
->value(endstop_debounce_count_checksum
)->by_default(100)->as_number();
180 // get homing direction and convert to boolean where true is home to min, and false is home to max
181 int home_dir
= get_checksum(THEKERNEL
->config
->value(alpha_homing_direction_checksum
)->by_default("home_to_min")->as_string());
182 this->home_direction
[0] = home_dir
!= home_to_max_checksum
;
184 home_dir
= get_checksum(THEKERNEL
->config
->value(beta_homing_direction_checksum
)->by_default("home_to_min")->as_string());
185 this->home_direction
[1] = home_dir
!= home_to_max_checksum
;
187 home_dir
= get_checksum(THEKERNEL
->config
->value(gamma_homing_direction_checksum
)->by_default("home_to_min")->as_string());
188 this->home_direction
[2] = home_dir
!= home_to_max_checksum
;
190 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();
191 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();
192 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 this->is_corexy
= THEKERNEL
->config
->value(corexy_homing_checksum
)->by_default(false)->as_bool();
195 this->is_delta
= THEKERNEL
->config
->value(delta_homing_checksum
)->by_default(false)->as_bool();
196 this->is_scara
= THEKERNEL
->config
->value(scara_homing_checksum
)->by_default(false)->as_bool();
198 // see if an order has been specified, must be three characters, XYZ or YXZ etc
199 string order
= THEKERNEL
->config
->value(homing_order_checksum
)->by_default("")->as_string();
200 this->homing_order
= 0;
201 if(order
.size() == 3 && !this->is_delta
) {
203 for(auto c
: order
) {
204 uint8_t i
= toupper(c
) - 'X';
205 if(i
> 2) { // bad value
206 this->homing_order
= 0;
209 homing_order
|= (i
<< shift
);
214 // endstop trim used by deltas to do soft adjusting
215 // on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
216 this->trim_mm
[0] = THEKERNEL
->config
->value(alpha_trim_checksum
)->by_default(0 )->as_number();
217 this->trim_mm
[1] = THEKERNEL
->config
->value(beta_trim_checksum
)->by_default(0 )->as_number();
218 this->trim_mm
[2] = THEKERNEL
->config
->value(gamma_trim_checksum
)->by_default(0 )->as_number();
221 this->limit_enable
[X_AXIS
]= THEKERNEL
->config
->value(alpha_limit_enable_checksum
)->by_default(false)->as_bool();
222 this->limit_enable
[Y_AXIS
]= THEKERNEL
->config
->value(beta_limit_enable_checksum
)->by_default(false)->as_bool();
223 this->limit_enable
[Z_AXIS
]= THEKERNEL
->config
->value(gamma_limit_enable_checksum
)->by_default(false)->as_bool();
225 this->move_to_origin_after_home
= THEKERNEL
->config
->value(move_to_origin_checksum
)->by_default(false)->as_bool();
227 if(this->limit_enable
[X_AXIS
] || this->limit_enable
[Y_AXIS
] || this->limit_enable
[Z_AXIS
]){
228 register_for_event(ON_IDLE
);
230 // we must enable all the limits not just one
231 this->limit_enable
[X_AXIS
]= true;
232 this->limit_enable
[Y_AXIS
]= true;
233 this->limit_enable
[Z_AXIS
]= true;
237 // NOTE this may also be true of scara. TBD
239 // some things must be the same or they will die, so force it here to avoid config errors
240 this->fast_rates
[1]= this->fast_rates
[2]= this->fast_rates
[0];
241 this->slow_rates
[1]= this->slow_rates
[2]= this->slow_rates
[0];
242 this->retract_mm
[1]= this->retract_mm
[2]= this->retract_mm
[0];
243 this->home_direction
[1]= this->home_direction
[2]= this->home_direction
[0];
244 this->homing_position
[0]= this->homing_position
[1]= 0;
248 bool Endstops::debounced_get(int pin
)
251 while(this->pins
[pin
].get()) {
252 if ( ++debounce
>= this->debounce_count
) {
260 static const char *endstop_names
[]= {"min_x", "min_y", "min_z", "max_x", "max_y", "max_z"};
262 void Endstops::on_idle(void *argument
)
264 if(this->status
== LIMIT_TRIGGERED
) {
265 // if we were in limit triggered see if it has been cleared
266 for( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
267 if(this->limit_enable
[c
]) {
268 std::array
<int, 2> minmax
{{0, 3}};
269 // check min and max endstops
270 for (int i
: minmax
) {
272 if(this->pins
[n
].get()) {
273 // still triggered, so exit
280 if(++bounce_cnt
> 10) { // can use less as it calls on_idle in between
282 this->status
= NOT_HOMING
;
286 }else if(this->status
!= NOT_HOMING
) {
287 // don't check while homing
291 for( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
292 if(this->limit_enable
[c
] && STEPPER
[c
]->is_moving()) {
293 std::array
<int, 2> minmax
{{0, 3}};
294 // check min and max endstops
295 for (int i
: minmax
) {
297 if(debounced_get(n
)) {
299 THEKERNEL
->streams
->printf("Limit switch %s was hit - reset or M999 required\n", endstop_names
[n
]);
300 this->status
= LIMIT_TRIGGERED
;
301 // disables heaters and motors, ignores incoming Gcode and flushes block queue
302 THEKERNEL
->call_event(ON_HALT
, nullptr);
310 // if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move
311 // checks if triggered and only backs off if triggered
312 void Endstops::back_off_home(char axes_to_move
)
314 std::vector
<std::pair
<char,float>> params
;
315 this->status
= BACK_OFF_HOME
;
317 // these are handled differently
318 if((is_delta
|| is_scara
) && this->limit_enable
[X_AXIS
]) {
319 // Move off of the endstop using a regular relative move in Z only
320 params
.push_back({'Z', this->retract_mm
[Z_AXIS
]*(this->home_direction
[Z_AXIS
]?1:-1)});
323 // cartesians, concatenate all the moves we need to do into one gcode
324 for( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
325 if( ((axes_to_move
>> c
) & 1) == 0) continue; // only for axes we asked to move
327 // if not triggered no need to move off
328 if(this->limit_enable
[c
] && debounced_get(c
+ (this->home_direction
[c
] ? 0 : 3)) ) {
329 params
.push_back({c
+'X', this->retract_mm
[c
]*(this->home_direction
[c
]?1:-1)});
334 if(!params
.empty()) {
335 // Move off of the endstop using a regular relative move
336 params
.insert(params
.begin(), {'G', 0});
337 // use X slow rate to move, Z should have a max speed set anyway
338 params
.push_back({'F', this->slow_rates
[X_AXIS
]*60.0F
});
340 append_parameters(gcode_buf
, params
, sizeof(gcode_buf
));
341 Gcode
gc(gcode_buf
, &(StreamOutput::NullStream
));
342 bool oldmode
= THEKERNEL
->robot
->absolute_mode
;
343 THEKERNEL
->robot
->absolute_mode
= false; // needs to be relative mode
344 THEKERNEL
->robot
->on_gcode_received(&gc
); // send to robot directly
345 THEKERNEL
->robot
->absolute_mode
= oldmode
; // restore mode
346 // Wait for above to finish
347 THEKERNEL
->conveyor
->wait_for_empty_queue();
350 this->status
= NOT_HOMING
;
353 // If enabled will move the head to 0,0 after homing, but only if X and Y were set to home
354 void Endstops::move_to_origin(char axes_to_move
)
356 if( (axes_to_move
&0x03) != 3 ) return; // ignore if X and Y not homing
358 // Do we need to check if we are already at 0,0? probably not as the G0 will not do anything if we are
359 // float pos[3]; THEKERNEL->robot->get_axis_position(pos); if(pos[0] == 0 && pos[1] == 0) return;
361 this->status
= MOVE_TO_ORIGIN
;
362 // Move to center using a regular move, use slower of X and Y fast rate
363 float rate
= std::min(this->fast_rates
[0], this->fast_rates
[1])*60.0F
;
365 snprintf(buf
, sizeof(buf
), "G0 X0 Y0 F%1.4f", rate
);
366 Gcode
gc(buf
, &(StreamOutput::NullStream
));
367 THEKERNEL
->robot
->on_gcode_received(&gc
); // send to robot directly
369 // Wait for above to finish
370 THEKERNEL
->conveyor
->wait_for_empty_queue();
371 this->status
= NOT_HOMING
;
374 bool Endstops::wait_for_homed(char axes_to_move
)
377 unsigned int debounce
[3] = {0, 0, 0};
380 THEKERNEL
->call_event(ON_IDLE
);
382 // check if on_halt (eg kill)
383 if(THEKERNEL
->is_halted()) return false;
385 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
386 if ( ( axes_to_move
>> c
) & 1 ) {
387 if ( this->pins
[c
+ (this->home_direction
[c
] ? 0 : 3)].get() ) {
388 if ( debounce
[c
] < debounce_count
) {
391 } else if ( STEPPER
[c
]->is_moving() ) {
392 STEPPER
[c
]->move(0, 0);
393 axes_to_move
&= ~(1<<c
); // no need to check it again
396 // The endstop was not hit yet
406 void Endstops::do_homing_cartesian(char axes_to_move
)
408 // check if on_halt (eg kill)
409 if(THEKERNEL
->is_halted()) return;
411 // this homing works for cartesian and delta printers
412 // Start moving the axes to the origin
413 this->status
= MOVING_TO_ENDSTOP_FAST
;
414 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
415 if ( ( axes_to_move
>> c
) & 1 ) {
416 this->feed_rate
[c
]= this->fast_rates
[c
];
417 STEPPER
[c
]->move(this->home_direction
[c
], 10000000, 0);
421 // Wait for all axes to have homed
422 if(!this->wait_for_homed(axes_to_move
)) return;
424 // Move back a small distance
425 this->status
= MOVING_BACK
;
427 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
428 if ( ( axes_to_move
>> c
) & 1 ) {
429 inverted_dir
= !this->home_direction
[c
];
430 this->feed_rate
[c
]= this->slow_rates
[c
];
431 STEPPER
[c
]->move(inverted_dir
, this->retract_mm
[c
]*STEPS_PER_MM(c
), 0);
435 // Wait for moves to be done
436 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
437 if ( ( axes_to_move
>> c
) & 1 ) {
438 while ( STEPPER
[c
]->is_moving() ) {
439 THEKERNEL
->call_event(ON_IDLE
);
444 // Start moving the axes to the origin slowly
445 this->status
= MOVING_TO_ENDSTOP_SLOW
;
446 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
447 if ( ( axes_to_move
>> c
) & 1 ) {
448 this->feed_rate
[c
]= this->slow_rates
[c
];
449 STEPPER
[c
]->move(this->home_direction
[c
], 10000000, 0);
453 // Wait for all axes to have homed
454 if(!this->wait_for_homed(axes_to_move
)) return;
457 this->status
= NOT_HOMING
;
460 bool Endstops::wait_for_homed_corexy(int axis
)
463 unsigned int debounce
[3] = {0, 0, 0};
466 THEKERNEL
->call_event(ON_IDLE
);
468 // check if on_halt (eg kill)
469 if(THEKERNEL
->is_halted()) return false;
471 if ( this->pins
[axis
+ (this->home_direction
[axis
] ? 0 : 3)].get() ) {
472 if ( debounce
[axis
] < debounce_count
) {
476 // turn both off if running
477 if (STEPPER
[X_AXIS
]->is_moving()) STEPPER
[X_AXIS
]->move(0, 0);
478 if (STEPPER
[Y_AXIS
]->is_moving()) STEPPER
[Y_AXIS
]->move(0, 0);
481 // The endstop was not hit yet
489 void Endstops::corexy_home(int home_axis
, bool dirx
, bool diry
, float fast_rate
, float slow_rate
, unsigned int retract_steps
)
491 // check if on_halt (eg kill)
492 if(THEKERNEL
->is_halted()) return;
494 this->status
= MOVING_TO_ENDSTOP_FAST
;
495 this->feed_rate
[X_AXIS
]= fast_rate
;
496 STEPPER
[X_AXIS
]->move(dirx
, 10000000, 0);
497 this->feed_rate
[Y_AXIS
]= fast_rate
;
498 STEPPER
[Y_AXIS
]->move(diry
, 10000000, 0);
500 // wait for primary axis
501 if(!this->wait_for_homed_corexy(home_axis
)) return;
503 // Move back a small distance
504 this->status
= MOVING_BACK
;
505 this->feed_rate
[X_AXIS
]= slow_rate
;
506 STEPPER
[X_AXIS
]->move(!dirx
, retract_steps
, 0);
507 this->feed_rate
[Y_AXIS
]= slow_rate
;
508 STEPPER
[Y_AXIS
]->move(!diry
, retract_steps
, 0);
511 while ( STEPPER
[X_AXIS
]->is_moving() || STEPPER
[Y_AXIS
]->is_moving()) {
512 THEKERNEL
->call_event(ON_IDLE
);
515 // Start moving the axes to the origin slowly
516 this->status
= MOVING_TO_ENDSTOP_SLOW
;
517 this->feed_rate
[X_AXIS
]= slow_rate
;
518 STEPPER
[X_AXIS
]->move(dirx
, 10000000, 0);
519 this->feed_rate
[Y_AXIS
]= slow_rate
;
520 STEPPER
[Y_AXIS
]->move(diry
, 10000000, 0);
522 // wait for primary axis
523 if(!this->wait_for_homed_corexy(home_axis
)) return;
526 // this homing works for HBots/CoreXY
527 void Endstops::do_homing_corexy(char axes_to_move
)
529 // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally
530 // 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
531 // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed
532 // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit
534 if((axes_to_move
& 0x03) == 0x03) { // both X and Y need Homing
535 // determine which motor to turn and which way
536 bool dirx
= this->home_direction
[X_AXIS
];
537 bool diry
= this->home_direction
[Y_AXIS
];
540 if(dirx
&& diry
) { // min/min
543 }else if(dirx
&& !diry
) { // min/max
546 }else if(!dirx
&& diry
) { // max/min
549 }else if(!dirx
&& !diry
) { // max/max
554 // then move both X and Y until one hits the endstop
555 this->status
= MOVING_TO_ENDSTOP_FAST
;
556 // need to allow for more ground covered when moving diagonally
557 this->feed_rate
[motor
]= this->fast_rates
[motor
]*1.4142;
558 STEPPER
[motor
]->move(dir
, 10000000, 0);
559 // wait until either X or Y hits the endstop
562 THEKERNEL
->call_event(ON_IDLE
);
563 for(int m
=X_AXIS
;m
<=Y_AXIS
;m
++) {
564 if(this->pins
[m
+ (this->home_direction
[m
] ? 0 : 3)].get()) {
566 if(STEPPER
[motor
]->is_moving()) STEPPER
[motor
]->move(0, 0);
574 // move individual axis
575 if (axes_to_move
& 0x01) { // Home X, which means both X and Y in same direction
576 bool dir
= this->home_direction
[X_AXIS
];
577 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
));
580 if (axes_to_move
& 0x02) { // Home Y, which means both X and Y in different directions
581 bool dir
= this->home_direction
[Y_AXIS
];
582 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
));
585 if (axes_to_move
& 0x04) { // move Z
586 do_homing_cartesian(0x04); // just home normally for Z
590 this->status
= NOT_HOMING
;
593 void Endstops::home(char axes_to_move
)
595 // not a block move so disable the last tick setting
596 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
597 STEPPER
[c
]->set_moved_last_block(false);
601 // corexy/HBot homing
602 do_homing_corexy(axes_to_move
);
604 // cartesian/delta homing
605 do_homing_cartesian(axes_to_move
);
609 // Start homing sequences by response to GCode commands
610 void Endstops::on_gcode_received(void *argument
)
612 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
614 if ( gcode
->g
== 28 ) {
616 // G28 is received, we have homing to do
618 // First wait for the queue to be empty
619 THEKERNEL
->conveyor
->wait_for_empty_queue();
621 // Do we move select axes or all of them
622 char axes_to_move
= 0;
623 // only enable homing if the endstop is defined, deltas, scaras always home all axis
624 bool home_all
= this->is_delta
|| this->is_scara
|| !( gcode
->has_letter('X') || gcode
->has_letter('Y') || gcode
->has_letter('Z') );
626 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
627 if ( (home_all
|| gcode
->has_letter(c
+'X')) && this->pins
[c
+ (this->home_direction
[c
] ? 0 : 3)].connected() ) {
628 axes_to_move
+= ( 1 << c
);
633 THEKERNEL
->stepper
->turn_enable_pins_on();
635 // do the actual homing
636 if(homing_order
!= 0){
637 // if an order has been specified do it in the specified order
638 // homing order is 0b00ccbbaa where aa is 0,1,2 to specify the first axis, bb is the second and cc is the third
639 // eg 0b00100001 would be Y X Z, 0b00100100 would be X Y Z
640 for (uint8_t m
= homing_order
; m
!= 0; m
>>= 2) {
641 int a
= (1 << (m
& 0x03)); // axis to move
642 if((a
& axes_to_move
) != 0){
645 // check if on_halt (eg kill)
646 if(THEKERNEL
->is_halted()) return;
650 // they all home at the same time
654 // check if on_halt (eg kill)
655 if(THEKERNEL
->is_halted()) return;
658 // for deltas this may be important rather than setting each individually
660 // Here's where we would have been if the endstops were perfectly trimmed
661 float ideal_position
[3] = {
662 this->homing_position
[X_AXIS
] + this->home_offset
[X_AXIS
],
663 this->homing_position
[Y_AXIS
] + this->home_offset
[Y_AXIS
],
664 this->homing_position
[Z_AXIS
] + this->home_offset
[Z_AXIS
]
667 bool has_endstop_trim
= this->is_delta
|| this->is_scara
;
668 if (has_endstop_trim
) {
669 float ideal_actuator_position
[3];
670 THEKERNEL
->robot
->arm_solution
->cartesian_to_actuator(ideal_position
, ideal_actuator_position
);
672 // We are actually not at the ideal position, but a trim away
673 float real_actuator_position
[3] = {
674 ideal_actuator_position
[X_AXIS
] - this->trim_mm
[X_AXIS
],
675 ideal_actuator_position
[Y_AXIS
] - this->trim_mm
[Y_AXIS
],
676 ideal_actuator_position
[Z_AXIS
] - this->trim_mm
[Z_AXIS
]
679 float real_position
[3];
680 THEKERNEL
->robot
->arm_solution
->actuator_to_cartesian(real_actuator_position
, real_position
);
681 // Reset the actuator positions to correspond our real position
682 THEKERNEL
->robot
->reset_axis_position(real_position
[0], real_position
[1], real_position
[2]);
684 // without endstop trim, real_position == ideal_position
685 // Reset the actuator positions to correspond our real position
686 THEKERNEL
->robot
->reset_axis_position(ideal_position
[0], ideal_position
[1], ideal_position
[2]);
689 // Zero the ax(i/e)s position, add in the home offset
690 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
691 if ( (axes_to_move
>> c
) & 1 ) {
692 THEKERNEL
->robot
->reset_axis_position(this->homing_position
[c
] + this->home_offset
[c
], c
);
697 // on some systems where 0,0 is bed center it is noce to have home goto 0,0 after homing
699 if(this->move_to_origin_after_home
)
700 move_to_origin(axes_to_move
);
702 // if limit switches are enabled we must back off endstop after setting home
703 back_off_home(axes_to_move
);
706 } else if (gcode
->has_m
) {
709 for (int i
= 0; i
< 6; ++i
) {
710 if(this->pins
[i
].connected())
711 gcode
->stream
->printf("%s:%d ", endstop_names
[i
], this->pins
[i
].get());
718 case 206: // M206 - set homing offset
719 if (gcode
->has_letter('X')) home_offset
[0] = gcode
->get_value('X');
720 if (gcode
->has_letter('Y')) home_offset
[1] = gcode
->get_value('Y');
721 if (gcode
->has_letter('Z')) home_offset
[2] = gcode
->get_value('Z');
722 gcode
->stream
->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset
[0], home_offset
[1], home_offset
[2]);
726 case 306: // Similar to M206 and G92 but sets Homing offsets based on current position, Would be M207 but that is taken
729 THEKERNEL
->robot
->get_axis_position(cartesian
); // get actual position from robot
730 if (gcode
->has_letter('X')){
731 home_offset
[0] -= (cartesian
[X_AXIS
] - gcode
->get_value('X'));
732 THEKERNEL
->robot
->reset_axis_position(gcode
->get_value('X'), X_AXIS
);
734 if (gcode
->has_letter('Y')) {
735 home_offset
[1] -= (cartesian
[Y_AXIS
] - gcode
->get_value('Y'));
736 THEKERNEL
->robot
->reset_axis_position(gcode
->get_value('Y'), Y_AXIS
);
738 if (gcode
->has_letter('Z')) {
739 home_offset
[2] -= (cartesian
[Z_AXIS
] - gcode
->get_value('Z'));
740 THEKERNEL
->robot
->reset_axis_position(gcode
->get_value('Z'), Z_AXIS
);
743 gcode
->stream
->printf("Homing Offset: X %5.3f Y %5.3f Z %5.3f\n", home_offset
[0], home_offset
[1], home_offset
[2]);
748 case 500: // save settings
749 case 503: // print settings
750 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]);
751 if (this->is_delta
|| this->is_scara
) {
752 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]);
753 gcode
->stream
->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position
[2]);
757 case 665: { // M665 - set max gamma/z height
759 float gamma_max
= this->homing_position
[2];
760 if (gcode
->has_letter('Z')) {
761 this->homing_position
[2] = gamma_max
= gcode
->get_value('Z');
763 gcode
->stream
->printf("Max Z %8.3f ", gamma_max
);
764 gcode
->add_nl
= true;
770 if(this->is_delta
|| this->is_scara
) { // M666 - set trim for each axis in mm, NB negative mm trim is down
771 if (gcode
->has_letter('X')) trim_mm
[0] = gcode
->get_value('X');
772 if (gcode
->has_letter('Y')) trim_mm
[1] = gcode
->get_value('Y');
773 if (gcode
->has_letter('Z')) trim_mm
[2] = gcode
->get_value('Z');
775 // print the current trim values in mm
776 gcode
->stream
->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm
[0], trim_mm
[1], trim_mm
[2]);
781 // NOTE this is to test accuracy of lead screws etc.
782 case 910: { // M910 - move specific number of raw steps
784 THEKERNEL
->stepper
->turn_enable_pins_on();
786 int x
= 0, y
=0 , z
= 0, f
= 200*16;
787 if (gcode
->has_letter('F')) f
= gcode
->get_value('F');
788 if (gcode
->has_letter('X')) {
789 x
= gcode
->get_value('X');
790 STEPPER
[X_AXIS
]->move(x
<0, abs(x
), f
);
792 if (gcode
->has_letter('Y')) {
793 y
= gcode
->get_value('Y');
794 STEPPER
[Y_AXIS
]->move(y
<0, abs(y
), f
);
796 if (gcode
->has_letter('Z')) {
797 z
= gcode
->get_value('Z');
798 STEPPER
[Z_AXIS
]->move(z
<0, abs(z
), f
);
800 gcode
->stream
->printf("Moved X %d Y %d Z %d F %d steps\n", x
, y
, z
, f
);
807 // Called periodically to change the speed to match acceleration
808 void Endstops::acceleration_tick(void)
810 if(this->status
>= NOT_HOMING
) return; // nothing to do, only do this when moving for homing sequence
812 // foreach stepper that is moving
813 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
814 if( !STEPPER
[c
]->is_moving() ) continue;
816 uint32_t current_rate
= STEPPER
[c
]->get_steps_per_second();
817 uint32_t target_rate
= floorf(this->feed_rate
[c
]*STEPS_PER_MM(c
));
818 float acc
= (c
==Z_AXIS
) ? THEKERNEL
->planner
->get_z_acceleration() : THEKERNEL
->planner
->get_acceleration();
819 if( current_rate
< target_rate
){
820 uint32_t rate_increase
= floorf((acc
/THEKERNEL
->acceleration_ticks_per_second
)*STEPS_PER_MM(c
));
821 current_rate
= min( target_rate
, current_rate
+ rate_increase
);
823 if( current_rate
> target_rate
){ current_rate
= target_rate
; }
826 STEPPER
[c
]->set_speed(current_rate
);
832 void Endstops::on_get_public_data(void* argument
){
833 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
835 if(!pdr
->starts_with(endstops_checksum
)) return;
837 if(pdr
->second_element_is(trim_checksum
)) {
838 pdr
->set_data_ptr(&this->trim_mm
);
841 }else if(pdr
->second_element_is(home_offset_checksum
)) {
842 pdr
->set_data_ptr(&this->home_offset
);
847 void Endstops::on_set_public_data(void* argument
){
848 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
850 if(!pdr
->starts_with(endstops_checksum
)) return;
852 if(pdr
->second_element_is(trim_checksum
)) {
853 float *t
= static_cast<float*>(pdr
->get_data_ptr());
854 this->trim_mm
[0]= t
[0];
855 this->trim_mm
[1]= t
[1];
856 this->trim_mm
[2]= t
[2];
859 }else if(pdr
->second_element_is(home_offset_checksum
)) {
860 float *t
= static_cast<float*>(pdr
->get_data_ptr());
861 if(!isnan(t
[0])) this->home_offset
[0]= t
[0];
862 if(!isnan(t
[1])) this->home_offset
[1]= t
[1];
863 if(!isnan(t
[2])) this->home_offset
[2]= t
[2];