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"
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 rdelta_homing_checksum CHECKSUM("rdelta_homing")
46 #define scara_homing_checksum CHECKSUM("scara_homing")
48 #define alpha_min_endstop_checksum CHECKSUM("alpha_min_endstop")
49 #define beta_min_endstop_checksum CHECKSUM("beta_min_endstop")
50 #define gamma_min_endstop_checksum CHECKSUM("gamma_min_endstop")
52 #define alpha_max_endstop_checksum CHECKSUM("alpha_max_endstop")
53 #define beta_max_endstop_checksum CHECKSUM("beta_max_endstop")
54 #define gamma_max_endstop_checksum CHECKSUM("gamma_max_endstop")
56 #define alpha_trim_checksum CHECKSUM("alpha_trim")
57 #define beta_trim_checksum CHECKSUM("beta_trim")
58 #define gamma_trim_checksum CHECKSUM("gamma_trim")
60 // these values are in steps and should be deprecated
61 #define alpha_fast_homing_rate_checksum CHECKSUM("alpha_fast_homing_rate")
62 #define beta_fast_homing_rate_checksum CHECKSUM("beta_fast_homing_rate")
63 #define gamma_fast_homing_rate_checksum CHECKSUM("gamma_fast_homing_rate")
65 #define alpha_slow_homing_rate_checksum CHECKSUM("alpha_slow_homing_rate")
66 #define beta_slow_homing_rate_checksum CHECKSUM("beta_slow_homing_rate")
67 #define gamma_slow_homing_rate_checksum CHECKSUM("gamma_slow_homing_rate")
69 #define alpha_homing_retract_checksum CHECKSUM("alpha_homing_retract")
70 #define beta_homing_retract_checksum CHECKSUM("beta_homing_retract")
71 #define gamma_homing_retract_checksum CHECKSUM("gamma_homing_retract")
73 // same as above but in user friendly mm/s and mm
74 #define alpha_fast_homing_rate_mm_checksum CHECKSUM("alpha_fast_homing_rate_mm_s")
75 #define beta_fast_homing_rate_mm_checksum CHECKSUM("beta_fast_homing_rate_mm_s")
76 #define gamma_fast_homing_rate_mm_checksum CHECKSUM("gamma_fast_homing_rate_mm_s")
78 #define alpha_slow_homing_rate_mm_checksum CHECKSUM("alpha_slow_homing_rate_mm_s")
79 #define beta_slow_homing_rate_mm_checksum CHECKSUM("beta_slow_homing_rate_mm_s")
80 #define gamma_slow_homing_rate_mm_checksum CHECKSUM("gamma_slow_homing_rate_mm_s")
82 #define alpha_homing_retract_mm_checksum CHECKSUM("alpha_homing_retract_mm")
83 #define beta_homing_retract_mm_checksum CHECKSUM("beta_homing_retract_mm")
84 #define gamma_homing_retract_mm_checksum CHECKSUM("gamma_homing_retract_mm")
86 #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
88 #define alpha_homing_direction_checksum CHECKSUM("alpha_homing_direction")
89 #define beta_homing_direction_checksum CHECKSUM("beta_homing_direction")
90 #define gamma_homing_direction_checksum CHECKSUM("gamma_homing_direction")
91 #define home_to_max_checksum CHECKSUM("home_to_max")
92 #define home_to_min_checksum CHECKSUM("home_to_min")
93 #define alpha_min_checksum CHECKSUM("alpha_min")
94 #define beta_min_checksum CHECKSUM("beta_min")
95 #define gamma_min_checksum CHECKSUM("gamma_min")
97 #define alpha_max_checksum CHECKSUM("alpha_max")
98 #define beta_max_checksum CHECKSUM("beta_max")
99 #define gamma_max_checksum CHECKSUM("gamma_max")
101 #define alpha_limit_enable_checksum CHECKSUM("alpha_limit_enable")
102 #define beta_limit_enable_checksum CHECKSUM("beta_limit_enable")
103 #define gamma_limit_enable_checksum CHECKSUM("gamma_limit_enable")
105 #define homing_order_checksum CHECKSUM("homing_order")
106 #define move_to_origin_checksum CHECKSUM("move_to_origin_after_home")
108 #define STEPPER THEROBOT->actuators
109 #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
114 MOVING_TO_ENDSTOP_FAST
, // homing move
115 MOVING_TO_ENDSTOP_SLOW
, // homing move
116 MOVING_BACK
, // homing move
125 this->status
= NOT_HOMING
;
126 home_offset
[0] = home_offset
[1] = home_offset
[2] = 0.0F
;
130 void Endstops::on_module_loaded()
132 // Do not do anything if not enabled
133 if ( THEKERNEL
->config
->value( endstops_module_enable_checksum
)->by_default(true)->as_bool() == false ) {
138 register_for_event(ON_GCODE_RECEIVED
);
139 register_for_event(ON_GET_PUBLIC_DATA
);
140 register_for_event(ON_SET_PUBLIC_DATA
);
145 THEKERNEL
->slow_ticker
->attach(1000, this, &Endstops::read_endstops
);
149 void Endstops::load_config()
151 this->pins
[0].from_string( THEKERNEL
->config
->value(alpha_min_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
152 this->pins
[1].from_string( THEKERNEL
->config
->value(beta_min_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
153 this->pins
[2].from_string( THEKERNEL
->config
->value(gamma_min_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
154 this->pins
[3].from_string( THEKERNEL
->config
->value(alpha_max_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
155 this->pins
[4].from_string( THEKERNEL
->config
->value(beta_max_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
156 this->pins
[5].from_string( THEKERNEL
->config
->value(gamma_max_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
158 // These are the old ones in steps still here for backwards compatibility
159 this->fast_rates
[0] = THEKERNEL
->config
->value(alpha_fast_homing_rate_checksum
)->by_default(4000 )->as_number() / STEPS_PER_MM(0);
160 this->fast_rates
[1] = THEKERNEL
->config
->value(beta_fast_homing_rate_checksum
)->by_default(4000 )->as_number() / STEPS_PER_MM(1);
161 this->fast_rates
[2] = THEKERNEL
->config
->value(gamma_fast_homing_rate_checksum
)->by_default(6400 )->as_number() / STEPS_PER_MM(2);
162 this->slow_rates
[0] = THEKERNEL
->config
->value(alpha_slow_homing_rate_checksum
)->by_default(2000 )->as_number() / STEPS_PER_MM(0);
163 this->slow_rates
[1] = THEKERNEL
->config
->value(beta_slow_homing_rate_checksum
)->by_default(2000 )->as_number() / STEPS_PER_MM(1);
164 this->slow_rates
[2] = THEKERNEL
->config
->value(gamma_slow_homing_rate_checksum
)->by_default(3200 )->as_number() / STEPS_PER_MM(2);
165 this->retract_mm
[0] = THEKERNEL
->config
->value(alpha_homing_retract_checksum
)->by_default(400 )->as_number() / STEPS_PER_MM(0);
166 this->retract_mm
[1] = THEKERNEL
->config
->value(beta_homing_retract_checksum
)->by_default(400 )->as_number() / STEPS_PER_MM(1);
167 this->retract_mm
[2] = THEKERNEL
->config
->value(gamma_homing_retract_checksum
)->by_default(1600 )->as_number() / STEPS_PER_MM(2);
169 // 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
170 this->fast_rates
[0] = THEKERNEL
->config
->value(alpha_fast_homing_rate_mm_checksum
)->by_default(this->fast_rates
[0])->as_number();
171 this->fast_rates
[1] = THEKERNEL
->config
->value(beta_fast_homing_rate_mm_checksum
)->by_default(this->fast_rates
[1])->as_number();
172 this->fast_rates
[2] = THEKERNEL
->config
->value(gamma_fast_homing_rate_mm_checksum
)->by_default(this->fast_rates
[2])->as_number();
173 this->slow_rates
[0] = THEKERNEL
->config
->value(alpha_slow_homing_rate_mm_checksum
)->by_default(this->slow_rates
[0])->as_number();
174 this->slow_rates
[1] = THEKERNEL
->config
->value(beta_slow_homing_rate_mm_checksum
)->by_default(this->slow_rates
[1])->as_number();
175 this->slow_rates
[2] = THEKERNEL
->config
->value(gamma_slow_homing_rate_mm_checksum
)->by_default(this->slow_rates
[2])->as_number();
176 this->retract_mm
[0] = THEKERNEL
->config
->value(alpha_homing_retract_mm_checksum
)->by_default(this->retract_mm
[0])->as_number();
177 this->retract_mm
[1] = THEKERNEL
->config
->value(beta_homing_retract_mm_checksum
)->by_default(this->retract_mm
[1])->as_number();
178 this->retract_mm
[2] = THEKERNEL
->config
->value(gamma_homing_retract_mm_checksum
)->by_default(this->retract_mm
[2])->as_number();
180 this->debounce_count
= THEKERNEL
->config
->value(endstop_debounce_count_checksum
)->by_default(100)->as_number();
182 // get homing direction and convert to boolean where true is home to min, and false is home to max
183 int home_dir
= get_checksum(THEKERNEL
->config
->value(alpha_homing_direction_checksum
)->by_default("home_to_min")->as_string());
184 this->home_direction
[0] = home_dir
!= home_to_max_checksum
;
186 home_dir
= get_checksum(THEKERNEL
->config
->value(beta_homing_direction_checksum
)->by_default("home_to_min")->as_string());
187 this->home_direction
[1] = home_dir
!= home_to_max_checksum
;
189 home_dir
= get_checksum(THEKERNEL
->config
->value(gamma_homing_direction_checksum
)->by_default("home_to_min")->as_string());
190 this->home_direction
[2] = home_dir
!= home_to_max_checksum
;
192 this->homing_position
[0] = this->home_direction
[0] ? THEKERNEL
->config
->value(alpha_min_checksum
)->by_default(0)->as_number() : THEKERNEL
->config
->value(alpha_max_checksum
)->by_default(200)->as_number();
193 this->homing_position
[1] = this->home_direction
[1] ? THEKERNEL
->config
->value(beta_min_checksum
)->by_default(0)->as_number() : THEKERNEL
->config
->value(beta_max_checksum
)->by_default(200)->as_number();
194 this->homing_position
[2] = this->home_direction
[2] ? THEKERNEL
->config
->value(gamma_min_checksum
)->by_default(0)->as_number() : THEKERNEL
->config
->value(gamma_max_checksum
)->by_default(200)->as_number();
196 // used to set maximum movement on homing
197 this->alpha_max
= THEKERNEL
->config
->value(alpha_max_checksum
)->by_default(500)->as_number();
198 this->beta_max
= THEKERNEL
->config
->value(beta_max_checksum
)->by_default(500)->as_number();
199 this->gamma_max
= THEKERNEL
->config
->value(gamma_max_checksum
)->by_default(500)->as_number();
201 this->is_corexy
= THEKERNEL
->config
->value(corexy_homing_checksum
)->by_default(false)->as_bool();
202 this->is_delta
= THEKERNEL
->config
->value(delta_homing_checksum
)->by_default(false)->as_bool();
203 this->is_rdelta
= THEKERNEL
->config
->value(rdelta_homing_checksum
)->by_default(false)->as_bool();
204 this->is_scara
= THEKERNEL
->config
->value(scara_homing_checksum
)->by_default(false)->as_bool();
206 // see if an order has been specified, must be three characters, XYZ or YXZ etc
207 string order
= THEKERNEL
->config
->value(homing_order_checksum
)->by_default("")->as_string();
208 this->homing_order
= 0;
209 if(order
.size() == 3 && !(this->is_delta
|| this->is_rdelta
)) {
211 for(auto c
: order
) {
212 uint8_t i
= toupper(c
) - 'X';
213 if(i
> 2) { // bad value
214 this->homing_order
= 0;
217 homing_order
|= (i
<< shift
);
222 // endstop trim used by deltas to do soft adjusting
223 // on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
224 this->trim_mm
[0] = THEKERNEL
->config
->value(alpha_trim_checksum
)->by_default(0 )->as_number();
225 this->trim_mm
[1] = THEKERNEL
->config
->value(beta_trim_checksum
)->by_default(0 )->as_number();
226 this->trim_mm
[2] = THEKERNEL
->config
->value(gamma_trim_checksum
)->by_default(0 )->as_number();
229 this->limit_enable
[X_AXIS
] = THEKERNEL
->config
->value(alpha_limit_enable_checksum
)->by_default(false)->as_bool();
230 this->limit_enable
[Y_AXIS
] = THEKERNEL
->config
->value(beta_limit_enable_checksum
)->by_default(false)->as_bool();
231 this->limit_enable
[Z_AXIS
] = THEKERNEL
->config
->value(gamma_limit_enable_checksum
)->by_default(false)->as_bool();
233 // set to true by default for deltas due to trim, false on cartesians
234 this->move_to_origin_after_home
= THEKERNEL
->config
->value(move_to_origin_checksum
)->by_default(is_delta
)->as_bool();
236 if(this->limit_enable
[X_AXIS
] || this->limit_enable
[Y_AXIS
] || this->limit_enable
[Z_AXIS
]) {
237 register_for_event(ON_IDLE
);
238 if(this->is_delta
|| this->is_rdelta
) {
239 // we must enable all the limits not just one
240 this->limit_enable
[X_AXIS
] = true;
241 this->limit_enable
[Y_AXIS
] = true;
242 this->limit_enable
[Z_AXIS
] = true;
247 if(this->is_delta
|| this->is_rdelta
) {
248 // some things must be the same or they will die, so force it here to avoid config errors
249 this->fast_rates
[1] = this->fast_rates
[2] = this->fast_rates
[0];
250 this->slow_rates
[1] = this->slow_rates
[2] = this->slow_rates
[0];
251 this->retract_mm
[1] = this->retract_mm
[2] = this->retract_mm
[0];
252 this->home_direction
[1] = this->home_direction
[2] = this->home_direction
[0];
253 // NOTE homing_position for rdelta is the angle of the actuator not the cartesian position
254 if(!this->is_rdelta
) this->homing_position
[0] = this->homing_position
[1] = 0;
258 bool Endstops::debounced_get(int pin
)
260 uint8_t debounce
= 0;
261 while(this->pins
[pin
].get()) {
262 if ( ++debounce
>= this->debounce_count
) {
270 static const char *endstop_names
[] = {"min_x", "min_y", "min_z", "max_x", "max_y", "max_z"};
272 void Endstops::on_idle(void *argument
)
274 if(this->status
== LIMIT_TRIGGERED
) {
275 // if we were in limit triggered see if it has been cleared
276 for( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
277 if(this->limit_enable
[c
]) {
278 std::array
<int, 2> minmax
{{0, 3}};
279 // check min and max endstops
280 for (int i
: minmax
) {
282 if(this->pins
[n
].get()) {
283 // still triggered, so exit
290 if(++bounce_cnt
> 10) { // can use less as it calls on_idle in between
292 this->status
= NOT_HOMING
;
296 } else if(this->status
!= NOT_HOMING
) {
297 // don't check while homing
301 for( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
302 if(this->limit_enable
[c
] && STEPPER
[c
]->is_moving()) {
303 std::array
<int, 2> minmax
{{0, 3}};
304 // check min and max endstops
305 for (int i
: minmax
) {
307 if(debounced_get(n
)) {
309 THEKERNEL
->streams
->printf("Limit switch %s was hit - reset or M999 required\n", endstop_names
[n
]);
310 this->status
= LIMIT_TRIGGERED
;
311 // disables heaters and motors, ignores incoming Gcode and flushes block queue
312 THEKERNEL
->call_event(ON_HALT
, nullptr);
320 // if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move
321 // checks if triggered and only backs off if triggered
322 void Endstops::back_off_home(std::bitset
<3> axis
)
324 std::vector
<std::pair
<char, float>> params
;
325 this->status
= BACK_OFF_HOME
;
327 // these are handled differently
329 // Move off of the endstop using a regular relative move in Z only
330 params
.push_back({'Z', this->retract_mm
[Z_AXIS
] * (this->home_direction
[Z_AXIS
] ? 1 : -1)});
333 // cartesians, concatenate all the moves we need to do into one gcode
334 for( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
335 if(!axis
[c
]) continue; // only for axes we asked to move
337 // if not triggered no need to move off
338 if(this->limit_enable
[c
] && debounced_get(c
+ (this->home_direction
[c
] ? 0 : 3)) ) {
339 params
.push_back({c
+ 'X', this->retract_mm
[c
] * (this->home_direction
[c
] ? 1 : -1)});
344 if(!params
.empty()) {
345 // Move off of the endstop using a regular relative move
346 params
.insert(params
.begin(), {'G', 0});
347 // use X slow rate to move, Z should have a max speed set anyway
348 params
.push_back({'F', this->slow_rates
[X_AXIS
] * 60.0F
});
350 append_parameters(gcode_buf
, params
, sizeof(gcode_buf
));
351 Gcode
gc(gcode_buf
, &(StreamOutput::NullStream
));
352 THEROBOT
->push_state();
353 THEROBOT
->absolute_mode
= false; // needs to be relative mode
354 THEROBOT
->on_gcode_received(&gc
); // send to robot directly
355 // Wait for above to finish
356 THEKERNEL
->conveyor
->wait_for_empty_queue();
357 THEROBOT
->pop_state();
360 this->status
= NOT_HOMING
;
363 // If enabled will move the head to 0,0 after homing, but only if X and Y were set to home
364 void Endstops::move_to_origin()
366 if(!(axis_to_home
[X_AXIS
] && axis_to_home
[Y_AXIS
])) return; // ignore if X and Y not homing
368 // Do we need to check if we are already at 0,0? probably not as the G0 will not do anything if we are
369 // float pos[3]; THEROBOT->get_axis_position(pos); if(pos[0] == 0 && pos[1] == 0) return;
371 this->status
= MOVE_TO_ORIGIN
;
372 // Move to center using a regular move, use slower of X and Y fast rate
373 float rate
= std::min(this->fast_rates
[0], this->fast_rates
[1]) * 60.0F
;
375 snprintf(buf
, sizeof(buf
), "G53 G0 X0 Y0 F%1.4f", rate
); // must use machine coordinates in case G92 or WCS is in effect
376 THEROBOT
->push_state();
377 struct SerialMessage message
;
378 message
.message
= buf
;
379 message
.stream
= &(StreamOutput::NullStream
);
380 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
); // as it is a multi G code command
381 // Wait for above to finish
382 THEKERNEL
->conveyor
->wait_for_empty_queue();
383 THEROBOT
->pop_state();
384 this->status
= NOT_HOMING
;
387 // Called every millisecond in an ISR
388 uint32_t Endstops::read_endstops(uint32_t dummy
)
390 if(this->status
!= MOVING_TO_ENDSTOP_SLOW
&& this->status
!= MOVING_TO_ENDSTOP_FAST
) return 0; // not doing anything we need to monitor for
394 for ( int m
= X_AXIS
; m
<= Z_AXIS
; m
++ ) {
395 if(STEPPER
[m
]->is_moving()) {
396 // if it is moving then we check the associated endstop, and debounce it
397 if(this->pins
[m
+ (this->home_direction
[m
] ? 0 : 3)].get()) {
398 if(debounce
[m
] < debounce_count
) {
401 // we signal the motor to stop, which will preempt any moves on that axis
402 STEPPER
[m
]->stop_moving();
406 // The endstop was not hit yet
413 // corexy is different as the actuators are not directly related to the XY axis
414 // so we check the axis that is currently homing then stop all motors
415 for ( int m
= X_AXIS
; m
<= Z_AXIS
; m
++ ) {
416 if(axis_to_home
[m
]) {
417 if(this->pins
[m
+ (this->home_direction
[m
] ? 0 : 3)].get()) {
418 if(debounce
[m
] < debounce_count
) {
421 // we signal all the motors to stop, as on corexy X and Y motors will move for X and Y axis homing and we only hom eone axis at a time
422 STEPPER
[X_AXIS
]->stop_moving();
423 STEPPER
[Y_AXIS
]->stop_moving();
424 STEPPER
[Z_AXIS
]->stop_moving();
428 // The endstop was not hit yet
438 void Endstops::home(std::bitset
<3> a
)
440 // reset debounce counts
443 this->axis_to_home
= a
;
445 // Start moving the axes to the origin
446 this->status
= MOVING_TO_ENDSTOP_FAST
;
448 if(axis_to_home
[X_AXIS
] && axis_to_home
[Y_AXIS
]) {
449 // Home XY first so as not to slow them down by homing Z at the same time
450 float delta
[3] {alpha_max
, beta_max
, 0};
451 if(this->home_direction
[X_AXIS
]) delta
[X_AXIS
]= -delta
[X_AXIS
];
452 if(this->home_direction
[Y_AXIS
]) delta
[Y_AXIS
]= -delta
[Y_AXIS
];
453 float feed_rate
= std::min(fast_rates
[X_AXIS
], fast_rates
[Y_AXIS
]);
454 THEROBOT
->solo_move(delta
, feed_rate
);
456 // Wait for XY to have homed
457 THECONVEYOR
->wait_for_empty_queue();
459 } else if(axis_to_home
[X_AXIS
]) {
461 float delta
[3] {alpha_max
, 0, 0};
462 if(this->home_direction
[X_AXIS
]) delta
[X_AXIS
]= -delta
[X_AXIS
];
463 THEROBOT
->solo_move(delta
, fast_rates
[X_AXIS
]);
465 THECONVEYOR
->wait_for_empty_queue();
467 } else if(axis_to_home
[Y_AXIS
]) {
469 float delta
[3] {0, beta_max
, 0};
470 if(this->home_direction
[Y_AXIS
]) delta
[Y_AXIS
]= -delta
[Y_AXIS
];
471 THEROBOT
->solo_move(delta
, fast_rates
[Y_AXIS
]);
473 THECONVEYOR
->wait_for_empty_queue();
476 if(axis_to_home
[Z_AXIS
]) {
478 float delta
[3] {0, 0, gamma_max
};
479 if(this->home_direction
[Z_AXIS
]) delta
[Z_AXIS
]= -delta
[Z_AXIS
];
480 THEROBOT
->solo_move(delta
, fast_rates
[Z_AXIS
]);
482 THECONVEYOR
->wait_for_empty_queue();
485 float delta
[3]{0,0,0};
486 // use minimum feed rate of all three axes that are being homed (sub optimal)
487 float feed_rate
= slow_rates
[X_AXIS
];
488 // Move back a small distance for all homing axis
489 this->status
= MOVING_BACK
;
490 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
491 if(axis_to_home
[c
]) {
492 delta
[c
]= this->retract_mm
[c
];
493 if(!this->home_direction
[c
]) delta
[c
]= -delta
[c
];
494 feed_rate
= std::min(slow_rates
[c
], feed_rate
);
498 THEROBOT
->solo_move(delta
, feed_rate
);
499 // wait until finished
500 THECONVEYOR
->wait_for_empty_queue();
502 // Start moving the axes to the origin slowly
503 this->status
= MOVING_TO_ENDSTOP_SLOW
;
504 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
505 if(axis_to_home
[c
]) {
506 delta
[c
]= this->retract_mm
[c
];
507 if(this->home_direction
[c
]) delta
[c
]= -delta
[c
];
512 THEROBOT
->solo_move(delta
, feed_rate
);
513 // wait until finished
514 THECONVEYOR
->wait_for_empty_queue();
516 this->status
= NOT_HOMING
;
519 void Endstops::process_home_command(Gcode
* gcode
)
521 if( (gcode
->subcode
== 0 && THEKERNEL
->is_grbl_mode()) || (gcode
->subcode
== 2 && !THEKERNEL
->is_grbl_mode()) ) {
522 // G28 in grbl mode or G28.2 in normal mode will do a rapid to the predefined position
523 // TODO spec says if XYZ specified move to them first then move to MCS of specifed axis
525 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
526 struct SerialMessage message
;
527 message
.message
= buf
;
528 message
.stream
= &(StreamOutput::NullStream
);
529 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
); // as it is a multi G code command
532 } else if(THEKERNEL
->is_grbl_mode() && gcode
->subcode
== 2) { // G28.2 in grbl mode forces homing (triggered by $H)
533 // fall through so it does homing cycle
535 } else if(gcode
->subcode
== 1) { // G28.1 set pre defined position
536 // saves current position in absolute machine coordinates
537 THEROBOT
->get_axis_position(saved_position
);
540 } else if(gcode
->subcode
== 3) { // G28.3 is a smoothie special it sets manual homing
541 if(gcode
->get_num_args() == 0) {
542 THEROBOT
->reset_axis_position(0, 0, 0);
544 // do a manual homing based on given coordinates, no endstops required
545 if(gcode
->has_letter('X')) THEROBOT
->reset_axis_position(gcode
->get_value('X'), X_AXIS
);
546 if(gcode
->has_letter('Y')) THEROBOT
->reset_axis_position(gcode
->get_value('Y'), Y_AXIS
);
547 if(gcode
->has_letter('Z')) THEROBOT
->reset_axis_position(gcode
->get_value('Z'), Z_AXIS
);
551 } else if(gcode
->subcode
== 4) { // G28.4 is a smoothie special it sets manual homing based on the actuator position (used for rotary delta)
552 // do a manual homing based on given coordinates, no endstops required, NOTE does not support the multi actuator hack
553 ActuatorCoordinates ac
;
554 if(gcode
->has_letter('A')) ac
[0] = gcode
->get_value('A');
555 if(gcode
->has_letter('B')) ac
[1] = gcode
->get_value('B');
556 if(gcode
->has_letter('C')) ac
[2] = gcode
->get_value('C');
557 THEROBOT
->reset_actuator_position(ac
);
560 } else if(THEKERNEL
->is_grbl_mode()) {
561 gcode
->stream
->printf("error:Unsupported command\n");
565 // G28 is received, we have homing to do
567 // First wait for the queue to be empty
568 THEKERNEL
->conveyor
->wait_for_empty_queue();
570 // deltas, scaras always home Z axis only
571 bool home_in_z
= this->is_delta
|| this->is_rdelta
|| this->is_scara
;
573 // figure our which axis to home
577 if(!home_in_z
) { // ie not a delta
578 bool axis_speced
= ( gcode
->has_letter('X') || gcode
->has_letter('Y') || gcode
->has_letter('Z') );
579 // only enable homing if the endstop is defined,
580 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
581 if (this->pins
[c
+ (this->home_direction
[c
] ? 0 : 3)].connected() && (!axis_speced
|| gcode
->has_letter(c
+ 'X')) ) {
587 // Only Z axis homes (even though all actuators move this is handled by arm solution)
591 // save current actuator position so we can report how far we moved
592 ActuatorCoordinates start_pos
{
593 THEROBOT
->actuators
[X_AXIS
]->get_current_position(),
594 THEROBOT
->actuators
[Y_AXIS
]->get_current_position(),
595 THEROBOT
->actuators
[Z_AXIS
]->get_current_position()
599 THEKERNEL
->call_event(ON_ENABLE
, (void*)1); // turn all enable pins on
601 // do the actual homing
602 if(homing_order
!= 0) {
603 // if an order has been specified do it in the specified order
604 // homing order is 0b00ccbbaa where aa is 0,1,2 to specify the first axis, bb is the second and cc is the third
605 // eg 0b00100001 would be Y X Z, 0b00100100 would be X Y Z
606 for (uint8_t m
= homing_order
; m
!= 0; m
>>= 2) {
607 int a
= (m
& 0x03); // axis to home
608 if(haxis
[a
]) { // if axis is selected to home
613 // check if on_halt (eg kill)
614 if(THEKERNEL
->is_halted()) break;
617 } else if(is_corexy
) {
618 // corexy must home each axis individually
619 for (int a
= X_AXIS
; a
<= Z_AXIS
; ++a
) {
628 // they could all home at the same time
632 // check if on_halt (eg kill)
633 if(THEKERNEL
->is_halted()) {
634 if(!THEKERNEL
->is_grbl_mode()) {
635 THEKERNEL
->streams
->printf("Homing cycle aborted by kill\n");
640 // set the last probe position to the actuator units moved during this home
641 THEROBOT
->set_last_probe_position(
643 start_pos
[0] - THEROBOT
->actuators
[0]->get_current_position(),
644 start_pos
[1] - THEROBOT
->actuators
[1]->get_current_position(),
645 start_pos
[2] - THEROBOT
->actuators
[2]->get_current_position(),
648 if(home_in_z
) { // deltas only
649 // Here's where we would have been if the endstops were perfectly trimmed
650 // NOTE on a rotary delta home_offset is actuator position in degrees when homed and
651 // home_offset is the theta offset for each actuator, so M206 is used to set theta offset for each actuator in degrees
652 float ideal_position
[3] = {
653 this->homing_position
[X_AXIS
] + this->home_offset
[X_AXIS
],
654 this->homing_position
[Y_AXIS
] + this->home_offset
[Y_AXIS
],
655 this->homing_position
[Z_AXIS
] + this->home_offset
[Z_AXIS
]
658 bool has_endstop_trim
= this->is_delta
|| this->is_scara
;
659 if (has_endstop_trim
) {
660 ActuatorCoordinates ideal_actuator_position
;
661 THEROBOT
->arm_solution
->cartesian_to_actuator(ideal_position
, ideal_actuator_position
);
663 // We are actually not at the ideal position, but a trim away
664 ActuatorCoordinates real_actuator_position
= {
665 ideal_actuator_position
[X_AXIS
] - this->trim_mm
[X_AXIS
],
666 ideal_actuator_position
[Y_AXIS
] - this->trim_mm
[Y_AXIS
],
667 ideal_actuator_position
[Z_AXIS
] - this->trim_mm
[Z_AXIS
]
670 float real_position
[3];
671 THEROBOT
->arm_solution
->actuator_to_cartesian(real_actuator_position
, real_position
);
672 // Reset the actuator positions to correspond our real position
673 THEROBOT
->reset_axis_position(real_position
[0], real_position
[1], real_position
[2]);
676 // without endstop trim, real_position == ideal_position
678 // with a rotary delta we set the actuators angle then use the FK to calculate the resulting cartesian coordinates
679 ActuatorCoordinates real_actuator_position
= {ideal_position
[0], ideal_position
[1], ideal_position
[2]};
680 THEROBOT
->reset_actuator_position(real_actuator_position
);
683 // Reset the actuator positions to correspond our real position
684 THEROBOT
->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 (axis_to_home
[c
]) {
692 THEROBOT
->reset_axis_position(this->homing_position
[c
] + this->home_offset
[c
], c
);
697 // on some systems where 0,0 is bed center it is nice to have home goto 0,0 after homing
698 // default is off for cartesian on for deltas
700 // NOTE a rotary delta usually has optical or hall-effect endstops so it is safe to go past them a little bit
701 if(this->move_to_origin_after_home
) move_to_origin();
702 // if limit switches are enabled we must back off endstop after setting home
703 back_off_home(haxis
);
705 } else if(this->move_to_origin_after_home
|| this->limit_enable
[X_AXIS
]) {
706 // 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
707 // also need to back off endstops if limits are enabled
708 back_off_home(haxis
);
709 if(this->move_to_origin_after_home
) move_to_origin();
713 void Endstops::set_homing_offset(Gcode
*gcode
)
715 // Similar to M206 and G92 but sets Homing offsets based on current position
717 THEROBOT
->get_axis_position(cartesian
); // get actual position from robot
718 if (gcode
->has_letter('X')) {
719 home_offset
[0] -= (cartesian
[X_AXIS
] - gcode
->get_value('X'));
720 THEROBOT
->reset_axis_position(gcode
->get_value('X'), X_AXIS
);
722 if (gcode
->has_letter('Y')) {
723 home_offset
[1] -= (cartesian
[Y_AXIS
] - gcode
->get_value('Y'));
724 THEROBOT
->reset_axis_position(gcode
->get_value('Y'), Y_AXIS
);
726 if (gcode
->has_letter('Z')) {
727 home_offset
[2] -= (cartesian
[Z_AXIS
] - gcode
->get_value('Z'));
728 THEROBOT
->reset_axis_position(gcode
->get_value('Z'), Z_AXIS
);
731 gcode
->stream
->printf("Homing Offset: X %5.3f Y %5.3f Z %5.3f\n", home_offset
[0], home_offset
[1], home_offset
[2]);
734 // Start homing sequences by response to GCode commands
735 void Endstops::on_gcode_received(void *argument
)
737 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
738 if ( gcode
->has_g
&& gcode
->g
== 28) {
739 process_home_command(gcode
);
741 } else if (gcode
->has_m
) {
745 for (int i
= 0; i
< 6; ++i
) {
746 if(this->pins
[i
].connected())
747 gcode
->stream
->printf("%s:%d ", endstop_names
[i
], this->pins
[i
].get());
749 gcode
->add_nl
= true;
754 case 206: // M206 - set homing offset
755 if(is_rdelta
) return; // RotaryDeltaCalibration module will handle this
757 if (gcode
->has_letter('X')) home_offset
[0] = gcode
->get_value('X');
758 if (gcode
->has_letter('Y')) home_offset
[1] = gcode
->get_value('Y');
759 if (gcode
->has_letter('Z')) home_offset
[2] = gcode
->get_value('Z');
760 gcode
->stream
->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset
[0], home_offset
[1], home_offset
[2]);
763 case 306: // set homing offset based on current position
764 if(is_rdelta
) return; // RotaryDeltaCalibration module will handle this
766 set_homing_offset(gcode
);
769 case 500: // save settings
770 case 503: // print settings
772 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]);
774 gcode
->stream
->printf(";Theta offset (degrees):\nM206 A%1.5f B%1.5f C%1.5f\n", home_offset
[0], home_offset
[1], home_offset
[2]);
776 if (this->is_delta
|| this->is_scara
) {
777 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]);
778 gcode
->stream
->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position
[2]);
780 if(saved_position
[X_AXIS
] != 0 || saved_position
[Y_AXIS
] != 0) {
781 gcode
->stream
->printf(";predefined position:\nG28.1 X%1.4f Y%1.4f Z%1.4f\n", saved_position
[X_AXIS
], saved_position
[Y_AXIS
], saved_position
[Z_AXIS
]);
786 if (this->is_delta
|| this->is_scara
) { // M665 - set max gamma/z height
787 float gamma_max
= this->homing_position
[2];
788 if (gcode
->has_letter('Z')) {
789 this->homing_position
[2] = gamma_max
= gcode
->get_value('Z');
791 gcode
->stream
->printf("Max Z %8.3f ", gamma_max
);
792 gcode
->add_nl
= true;
797 if(this->is_delta
|| this->is_scara
) { // M666 - set trim for each axis in mm, NB negative mm trim is down
798 if (gcode
->has_letter('X')) trim_mm
[0] = gcode
->get_value('X');
799 if (gcode
->has_letter('Y')) trim_mm
[1] = gcode
->get_value('Y');
800 if (gcode
->has_letter('Z')) trim_mm
[2] = gcode
->get_value('Z');
802 // print the current trim values in mm
803 gcode
->stream
->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm
[0], trim_mm
[1], trim_mm
[2]);
812 void Endstops::on_get_public_data(void* argument
)
814 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
816 if(!pdr
->starts_with(endstops_checksum
)) return;
818 if(pdr
->second_element_is(trim_checksum
)) {
819 pdr
->set_data_ptr(&this->trim_mm
);
822 } else if(pdr
->second_element_is(home_offset_checksum
)) {
823 pdr
->set_data_ptr(&this->home_offset
);
826 } else if(pdr
->second_element_is(saved_position_checksum
)) {
827 pdr
->set_data_ptr(&this->saved_position
);
830 } else if(pdr
->second_element_is(get_homing_status_checksum
)) {
831 bool *homing
= static_cast<bool *>(pdr
->get_data_ptr());
832 *homing
= this->status
!= NOT_HOMING
;
837 void Endstops::on_set_public_data(void* argument
)
839 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
841 if(!pdr
->starts_with(endstops_checksum
)) return;
843 if(pdr
->second_element_is(trim_checksum
)) {
844 float *t
= static_cast<float*>(pdr
->get_data_ptr());
845 this->trim_mm
[0] = t
[0];
846 this->trim_mm
[1] = t
[1];
847 this->trim_mm
[2] = t
[2];
850 } else if(pdr
->second_element_is(home_offset_checksum
)) {
851 float *t
= static_cast<float*>(pdr
->get_data_ptr());
852 if(!isnan(t
[0])) this->home_offset
[0] = t
[0];
853 if(!isnan(t
[1])) this->home_offset
[1] = t
[1];
854 if(!isnan(t
[2])) this->home_offset
[2] = t
[2];