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