Allow TABS in config
[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
23
24 #define ALPHA_AXIS 0
25 #define BETA_AXIS 1
26 #define GAMMA_AXIS 2
27 #define X_AXIS 0
28 #define Y_AXIS 1
29 #define Z_AXIS 2
30
31 #define NOT_HOMING 0
32 #define MOVING_TO_ORIGIN_FAST 1
33 #define MOVING_BACK 2
34 #define MOVING_TO_ORIGIN_SLOW 3
35
36 #define endstops_module_enable_checksum CHECKSUM("endstops_enable")
37 #define corexy_homing_checksum CHECKSUM("corexy_homing")
38 #define delta_homing_checksum CHECKSUM("delta_homing")
39
40 #define alpha_min_endstop_checksum CHECKSUM("alpha_min_endstop")
41 #define beta_min_endstop_checksum CHECKSUM("beta_min_endstop")
42 #define gamma_min_endstop_checksum CHECKSUM("gamma_min_endstop")
43
44 #define alpha_max_endstop_checksum CHECKSUM("alpha_max_endstop")
45 #define beta_max_endstop_checksum CHECKSUM("beta_max_endstop")
46 #define gamma_max_endstop_checksum CHECKSUM("gamma_max_endstop")
47
48 #define alpha_trim_checksum CHECKSUM("alpha_trim")
49 #define beta_trim_checksum CHECKSUM("beta_trim")
50 #define gamma_trim_checksum CHECKSUM("gamma_trim")
51
52 // these values are in steps and should be deprecated
53 #define alpha_fast_homing_rate_checksum CHECKSUM("alpha_fast_homing_rate")
54 #define beta_fast_homing_rate_checksum CHECKSUM("beta_fast_homing_rate")
55 #define gamma_fast_homing_rate_checksum CHECKSUM("gamma_fast_homing_rate")
56
57 #define alpha_slow_homing_rate_checksum CHECKSUM("alpha_slow_homing_rate")
58 #define beta_slow_homing_rate_checksum CHECKSUM("beta_slow_homing_rate")
59 #define gamma_slow_homing_rate_checksum CHECKSUM("gamma_slow_homing_rate")
60
61 #define alpha_homing_retract_checksum CHECKSUM("alpha_homing_retract")
62 #define beta_homing_retract_checksum CHECKSUM("beta_homing_retract")
63 #define gamma_homing_retract_checksum CHECKSUM("gamma_homing_retract")
64 #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
65
66 // same as above but in user friendly mm/s and mm
67 #define alpha_fast_homing_rate_mm_checksum CHECKSUM("alpha_fast_homing_rate_mm_s")
68 #define beta_fast_homing_rate_mm_checksum CHECKSUM("beta_fast_homing_rate_mm_s")
69 #define gamma_fast_homing_rate_mm_checksum CHECKSUM("gamma_fast_homing_rate_mm_s")
70
71 #define alpha_slow_homing_rate_mm_checksum CHECKSUM("alpha_slow_homing_rate_mm_s")
72 #define beta_slow_homing_rate_mm_checksum CHECKSUM("beta_slow_homing_rate_mm_s")
73 #define gamma_slow_homing_rate_mm_checksum CHECKSUM("gamma_slow_homing_rate_mm_s")
74
75 #define alpha_homing_retract_mm_checksum CHECKSUM("alpha_homing_retract_mm")
76 #define beta_homing_retract_mm_checksum CHECKSUM("beta_homing_retract_mm")
77 #define gamma_homing_retract_mm_checksum CHECKSUM("gamma_homing_retract_mm")
78
79 #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
80
81 #define alpha_homing_direction_checksum CHECKSUM("alpha_homing_direction")
82 #define beta_homing_direction_checksum CHECKSUM("beta_homing_direction")
83 #define gamma_homing_direction_checksum CHECKSUM("gamma_homing_direction")
84 #define home_to_max_checksum CHECKSUM("home_to_max")
85 #define home_to_min_checksum CHECKSUM("home_to_min")
86 #define alpha_min_checksum CHECKSUM("alpha_min")
87 #define beta_min_checksum CHECKSUM("beta_min")
88 #define gamma_min_checksum CHECKSUM("gamma_min")
89
90 #define alpha_max_checksum CHECKSUM("alpha_max")
91 #define beta_max_checksum CHECKSUM("beta_max")
92 #define gamma_max_checksum CHECKSUM("gamma_max")
93
94 #define alpha_steps_per_mm_checksum CHECKSUM("alpha_steps_per_mm")
95 #define beta_steps_per_mm_checksum CHECKSUM("beta_steps_per_mm")
96 #define gamma_steps_per_mm_checksum CHECKSUM("gamma_steps_per_mm")
97
98 Endstops::Endstops()
99 {
100 this->status = NOT_HOMING;
101 home_offset[0] = home_offset[1] = home_offset[2] = 0.0F;
102 }
103
104 void Endstops::on_module_loaded()
105 {
106 // Do not do anything if not enabled
107 if ( THEKERNEL->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) {
108 return;
109 }
110
111 register_for_event(ON_CONFIG_RELOAD);
112 this->register_for_event(ON_GCODE_RECEIVED);
113
114 // Take StepperMotor objects from Robot and keep them here
115 this->steppers[0] = THEKERNEL->robot->alpha_stepper_motor;
116 this->steppers[1] = THEKERNEL->robot->beta_stepper_motor;
117 this->steppers[2] = THEKERNEL->robot->gamma_stepper_motor;
118 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->acceleration_ticks_per_second , this, &Endstops::acceleration_tick );
119
120 // Settings
121 this->on_config_reload(this);
122 }
123
124 // Get config
125 void Endstops::on_config_reload(void *argument)
126 {
127 this->pins[0].from_string( THEKERNEL->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
128 this->pins[1].from_string( THEKERNEL->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
129 this->pins[2].from_string( THEKERNEL->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
130 this->pins[3].from_string( THEKERNEL->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
131 this->pins[4].from_string( THEKERNEL->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
132 this->pins[5].from_string( THEKERNEL->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
133
134 // we need to know steps per mm for M206, also use them for all settings
135 this->steps_per_mm[0] = THEKERNEL->config->value(alpha_steps_per_mm_checksum )->as_number();
136 this->steps_per_mm[1] = THEKERNEL->config->value(beta_steps_per_mm_checksum )->as_number();
137 this->steps_per_mm[2] = THEKERNEL->config->value(gamma_steps_per_mm_checksum )->as_number();
138
139 //These are the old ones in steps still here for backwards compatibility
140 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number();
141 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number();
142 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number();
143 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number();
144 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number();
145 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number();
146 this->retract_steps[0] = THEKERNEL->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number();
147 this->retract_steps[1] = THEKERNEL->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number();
148 this->retract_steps[2] = THEKERNEL->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number();
149
150 // 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
151 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0] / steps_per_mm[0])->as_number() * steps_per_mm[0];
152 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[1] / steps_per_mm[1])->as_number() * steps_per_mm[1];
153 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2] / steps_per_mm[2])->as_number() * steps_per_mm[2];
154 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0] / steps_per_mm[0])->as_number() * steps_per_mm[0];
155 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[1] / steps_per_mm[1])->as_number() * steps_per_mm[1];
156 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2] / steps_per_mm[2])->as_number() * steps_per_mm[2];
157 this->retract_steps[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum )->by_default(this->retract_steps[0] / steps_per_mm[0])->as_number() * steps_per_mm[0];
158 this->retract_steps[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum )->by_default(this->retract_steps[1] / steps_per_mm[1])->as_number() * steps_per_mm[1];
159 this->retract_steps[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum )->by_default(this->retract_steps[2] / steps_per_mm[2])->as_number() * steps_per_mm[2];
160
161 this->debounce_count = THEKERNEL->config->value(endstop_debounce_count_checksum )->by_default(0)->as_number();
162
163
164 // get homing direction and convert to boolean where true is home to min, and false is home to max
165 int home_dir = get_checksum(THEKERNEL->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string());
166 this->home_direction[0] = home_dir != home_to_max_checksum;
167
168 home_dir = get_checksum(THEKERNEL->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string());
169 this->home_direction[1] = home_dir != home_to_max_checksum;
170
171 home_dir = get_checksum(THEKERNEL->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string());
172 this->home_direction[2] = home_dir != home_to_max_checksum;
173
174 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();
175 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();;
176 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();;
177
178 this->is_corexy = THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool();
179 this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool();
180
181 // endstop trim used by deltas to do soft adjusting, in mm, convert to steps, and negate depending on homing direction
182 // eg on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
183 int dirx = (this->home_direction[0] ? 1 : -1);
184 int diry = (this->home_direction[1] ? 1 : -1);
185 int dirz = (this->home_direction[2] ? 1 : -1);
186 this->trim[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[0] * dirx;
187 this->trim[1] = THEKERNEL->config->value(beta_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[1] * diry;
188 this->trim[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[2] * dirz;
189 }
190
191 void Endstops::wait_for_homed(char axes_to_move)
192 {
193 bool running = true;
194 unsigned int debounce[3] = {0, 0, 0};
195 while (running) {
196 running = false;
197 THEKERNEL->call_event(ON_IDLE);
198 for ( char c = 'X'; c <= 'Z'; c++ ) {
199 if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
200 if ( this->pins[c - 'X' + (this->home_direction[c - 'X'] ? 0 : 3)].get() ) {
201 if ( debounce[c - 'X'] < debounce_count ) {
202 debounce[c - 'X'] ++;
203 running = true;
204 } else if ( this->steppers[c - 'X']->moving ) {
205 this->steppers[c - 'X']->move(0, 0);
206 }
207 } else {
208 // The endstop was not hit yet
209 running = true;
210 debounce[c - 'X'] = 0;
211 }
212 }
213 }
214 }
215 }
216
217 // this homing works for cartesian and delta printers, not for HBots/CoreXY
218 void Endstops::do_homing(char axes_to_move)
219 {
220 // Start moving the axes to the origin
221 this->status = MOVING_TO_ORIGIN_FAST;
222 for ( char c = 'X'; c <= 'Z'; c++ ) {
223 if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
224 this->feed_rate[c - 'X']= this->fast_rates[c - 'X'];
225 this->steppers[c - 'X']->set_speed(0);
226 this->steppers[c - 'X']->move(this->home_direction[c - 'X'], 10000000);
227 }
228 }
229
230 // Wait for all axes to have homed
231 this->wait_for_homed(axes_to_move);
232
233 // Move back a small distance
234 this->status = MOVING_BACK;
235 bool inverted_dir;
236 for ( char c = 'X'; c <= 'Z'; c++ ) {
237 if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
238 inverted_dir = !this->home_direction[c - 'X'];
239 this->feed_rate[c - 'X']= this->slow_rates[c - 'X'];
240 this->steppers[c - 'X']->set_speed(0);
241 this->steppers[c - 'X']->move(inverted_dir, this->retract_steps[c - 'X']);
242 }
243 }
244
245 // Wait for moves to be done
246 for ( char c = 'X'; c <= 'Z'; c++ ) {
247 if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
248 while ( this->steppers[c - 'X']->moving ) {
249 THEKERNEL->call_event(ON_IDLE);
250 }
251 }
252 }
253
254 // Start moving the axes to the origin slowly
255 this->status = MOVING_TO_ORIGIN_SLOW;
256 for ( char c = 'X'; c <= 'Z'; c++ ) {
257 if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
258 this->feed_rate[c - 'X']= this->slow_rates[c - 'X'];
259 this->steppers[c - 'X']->set_speed(0);
260 this->steppers[c - 'X']->move(this->home_direction[c - 'X'], 10000000);
261 }
262 }
263
264 // Wait for all axes to have homed
265 this->wait_for_homed(axes_to_move);
266
267 if (this->is_delta) {
268 // move for soft trim
269 this->status = MOVING_BACK;
270 for ( char c = 'X'; c <= 'Z'; c++ ) {
271 if ( this->trim[c - 'X'] != 0 && ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
272 inverted_dir = !this->home_direction[c - 'X'];
273 // move up or down depending on sign of trim
274 if (this->trim[c - 'X'] < 0) inverted_dir = !inverted_dir;
275 this->feed_rate[c - 'X']= this->slow_rates[c - 'X'];
276 this->steppers[c - 'X']->set_speed(0);
277 this->steppers[c - 'X']->move(inverted_dir, this->trim[c - 'X']);
278 }
279 }
280
281 // Wait for moves to be done
282 for ( char c = 'X'; c <= 'Z'; c++ ) {
283 if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
284 //THEKERNEL->streams->printf("axis %c \r\n", c );
285 while ( this->steppers[c - 'X']->moving ) {
286 THEKERNEL->call_event(ON_IDLE);
287 }
288 }
289 }
290 }
291
292 // Homing is done
293 this->status = NOT_HOMING;
294 }
295
296 void Endstops::wait_for_homed_corexy(int axis)
297 {
298 bool running = true;
299 unsigned int debounce[3] = {0, 0, 0};
300 while (running) {
301 running = false;
302 THEKERNEL->call_event(ON_IDLE);
303 if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) {
304 if ( debounce[axis] < debounce_count ) {
305 debounce[axis] ++;
306 running = true;
307 } else {
308 // turn both off if running
309 if (this->steppers[X_AXIS]->moving) this->steppers[X_AXIS]->move(0, 0);
310 if (this->steppers[Y_AXIS]->moving) this->steppers[Y_AXIS]->move(0, 0);
311 }
312 } else {
313 // The endstop was not hit yet
314 running = true;
315 debounce[axis] = 0;
316 }
317 }
318 }
319
320 void Endstops::corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps)
321 {
322 this->status = MOVING_TO_ORIGIN_FAST;
323 this->feed_rate[X_AXIS]= fast_rate;
324 this->steppers[X_AXIS]->set_speed(0);
325 this->steppers[X_AXIS]->move(dirx, 10000000);
326 this->feed_rate[Y_AXIS]= fast_rate;
327 this->steppers[Y_AXIS]->set_speed(0);
328 this->steppers[Y_AXIS]->move(diry, 10000000);
329
330 // wait for primary axis
331 this->wait_for_homed_corexy(home_axis);
332
333 // Move back a small distance
334 this->status = MOVING_BACK;
335 this->feed_rate[X_AXIS]= slow_rate;
336 this->steppers[X_AXIS]->set_speed(0);
337 this->steppers[X_AXIS]->move(!dirx, retract_steps);
338 this->feed_rate[Y_AXIS]= slow_rate;
339 this->steppers[Y_AXIS]->set_speed(0);
340 this->steppers[Y_AXIS]->move(!diry, retract_steps);
341
342 // wait until done
343 while ( this->steppers[X_AXIS]->moving || this->steppers[Y_AXIS]->moving) {
344 THEKERNEL->call_event(ON_IDLE);
345 }
346
347 // Start moving the axes to the origin slowly
348 this->status = MOVING_TO_ORIGIN_SLOW;
349 this->feed_rate[X_AXIS]= slow_rate;
350 this->steppers[X_AXIS]->set_speed(0);
351 this->steppers[X_AXIS]->move(dirx, 10000000);
352 this->feed_rate[Y_AXIS]= slow_rate;
353 this->steppers[Y_AXIS]->set_speed(0);
354 this->steppers[Y_AXIS]->move(diry, 10000000);
355
356 // wait for primary axis
357 this->wait_for_homed_corexy(home_axis);
358 }
359
360 // this homing works for HBots/CoreXY
361 void Endstops::do_homing_corexy(char axes_to_move)
362 {
363 // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally
364 // 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
365 // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed
366 // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit
367
368 if((axes_to_move & 0x03) == 0x03) { // both X and Y need Homing
369 // determine which motor to turn and which way
370 bool dirx= this->home_direction[X_AXIS];
371 bool diry= this->home_direction[Y_AXIS];
372 int motor;
373 bool dir;
374 if(dirx && diry) { // min/min
375 motor= X_AXIS;
376 dir= true;
377 }else if(dirx && !diry) { // min/max
378 motor= Y_AXIS;
379 dir= true;
380 }else if(!dirx && diry) { // max/min
381 motor= Y_AXIS;
382 dir= false;
383 }else if(!dirx && !diry) { // max/max
384 motor= X_AXIS;
385 dir= false;
386 }
387
388 // then move both X and Y until one hits the endstop
389 this->status = MOVING_TO_ORIGIN_FAST;
390 this->feed_rate[motor]= this->fast_rates[motor]*1.4142;
391 this->steppers[motor]->set_speed(0); // need to allow for more ground covered when moving diagonally
392 this->steppers[motor]->move(dir, 10000000);
393 // wait until either X or Y hits the endstop
394 bool running= true;
395 while (running) {
396 THEKERNEL->call_event(ON_IDLE);
397 for(int m=X_AXIS;m<=Y_AXIS;m++) {
398 if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) {
399 // turn off motor
400 if(this->steppers[motor]->moving) this->steppers[motor]->move(0, 0);
401 running= false;
402 break;
403 }
404 }
405 }
406 }
407
408 // move individual axis
409 if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
410 bool dir= this->home_direction[X_AXIS];
411 corexy_home(X_AXIS, dir, dir, this->fast_rates[X_AXIS], this->slow_rates[X_AXIS], this->retract_steps[X_AXIS]);
412 }
413
414 if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
415 bool dir= this->home_direction[Y_AXIS];
416 corexy_home(Y_AXIS, dir, !dir, this->fast_rates[Y_AXIS], this->slow_rates[Y_AXIS], this->retract_steps[Y_AXIS]);
417 }
418
419 if (axes_to_move & 0x04) { // move Z
420 do_homing(0x04); // just home normally for Z
421 }
422
423 // Homing is done
424 this->status = NOT_HOMING;
425 }
426
427 // Start homing sequences by response to GCode commands
428 void Endstops::on_gcode_received(void *argument)
429 {
430 Gcode *gcode = static_cast<Gcode *>(argument);
431 if ( gcode->has_g) {
432 if ( gcode->g == 28 ) {
433 gcode->mark_as_taken();
434 // G28 is received, we have homing to do
435
436 // First wait for the queue to be empty
437 THEKERNEL->conveyor->wait_for_empty_queue();
438
439 // Do we move select axes or all of them
440 char axes_to_move = 0;
441 // only enable homing if the endstop is defined, deltas always home all axis
442 bool home_all = this->is_delta || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') );
443
444 for ( char c = 'X'; c <= 'Z'; c++ ) {
445 if ( (home_all || gcode->has_letter(c)) && this->pins[c - 'X' + (this->home_direction[c - 'X'] ? 0 : 3)].connected() ) {
446 axes_to_move += ( 1 << (c - 'X' ) );
447 }
448 }
449
450 // Enable the motors
451 THEKERNEL->stepper->turn_enable_pins_on();
452
453 // do the actual homing
454 if (is_corexy)
455 do_homing_corexy(axes_to_move);
456 else
457 do_homing(axes_to_move);
458
459 // Zero the ax(i/e)s position, add in the home offset
460 for ( int c = 0; c <= 2; c++ ) {
461 if ( (axes_to_move >> c) & 1 ) {
462 THEKERNEL->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c);
463 }
464 }
465 }
466 } else if (gcode->has_m) {
467 switch (gcode->m) {
468 case 119: {
469
470 int px = this->home_direction[0] ? 0 : 3;
471 int py = this->home_direction[1] ? 1 : 4;
472 int pz = this->home_direction[2] ? 2 : 5;
473 const char *mx = this->home_direction[0] ? "min" : "max";
474 const char *my = this->home_direction[1] ? "min" : "max";
475 const char *mz = this->home_direction[2] ? "min" : "max";
476
477 gcode->stream->printf("X %s:%d Y %s:%d Z %s:%d\n", mx, this->pins[px].get(), my, this->pins[py].get(), mz, this->pins[pz].get());
478 gcode->mark_as_taken();
479 }
480 break;
481
482 case 206: // M206 - set homing offset
483 if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X');
484 if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y');
485 if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z');
486 gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
487 gcode->mark_as_taken();
488 break;
489
490 case 500: // save settings
491 case 503: // print settings
492 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]);
493 if (is_delta) {
494 float mm[3];
495 trim2mm(mm);
496 gcode->stream->printf(";Trim (mm):\nM666 X%1.2f Y%1.2f Z%1.2f\n", mm[0], mm[1], mm[2]);
497 gcode->stream->printf(";Max Z\nM665 Z%1.2f\n", this->homing_position[2]);
498 }
499 gcode->mark_as_taken();
500 break;
501
502 case 665: { // M665 - set max gamma/z height
503 gcode->mark_as_taken();
504 float gamma_max = this->homing_position[2];
505 if (gcode->has_letter('Z')) {
506 this->homing_position[2] = gamma_max = gcode->get_value('Z');
507 }
508 gcode->stream->printf("Max Z %8.3f ", gamma_max);
509 gcode->add_nl = true;
510 }
511 break;
512
513
514 case 666: { // M666 - set trim for each axis in mm
515 float mm[3];
516 trim2mm(mm);
517
518 if (gcode->has_letter('X')) mm[0] = gcode->get_value('X');
519 if (gcode->has_letter('Y')) mm[1] = gcode->get_value('Y');
520 if (gcode->has_letter('Z')) mm[2] = gcode->get_value('Z');
521
522 int dirx = (this->home_direction[0] ? 1 : -1);
523 int diry = (this->home_direction[1] ? 1 : -1);
524 int dirz = (this->home_direction[2] ? 1 : -1);
525 trim[0] = lround(mm[0] * steps_per_mm[0]) * dirx; // convert back to steps
526 trim[1] = lround(mm[1] * steps_per_mm[1]) * diry;
527 trim[2] = lround(mm[2] * steps_per_mm[2]) * dirz;
528
529 // print the current trim values in mm and steps
530 gcode->stream->printf("X %5.3f (%d) Y %5.3f (%d) Z %5.3f (%d)\n", mm[0], trim[0], mm[1], trim[1], mm[2], trim[2]);
531 gcode->mark_as_taken();
532 }
533 break;
534
535 // NOTE this is to test accuracy of lead screws etc.
536 case 910: { // M910 - move specific number of raw steps
537 int x= 0, y=0 , z= 0, f= 200*16;
538 if (gcode->has_letter('F')) f = gcode->get_value('F');
539 if (gcode->has_letter('X')) {
540 x = gcode->get_value('X');
541 this->steppers[X_AXIS]->set_speed(f);
542 this->steppers[X_AXIS]->move(x<0, abs(x));
543 }
544 if (gcode->has_letter('Y')) {
545 y = gcode->get_value('Y');
546 this->steppers[Y_AXIS]->set_speed(f);
547 this->steppers[Y_AXIS]->move(y<0, abs(y));
548 }
549 if (gcode->has_letter('Z')) {
550 z = gcode->get_value('Z');
551 this->steppers[Z_AXIS]->set_speed(f);
552 this->steppers[Z_AXIS]->move(z<0, abs(z));
553 }
554 gcode->stream->printf("Moved X %d Y %d Z %d F %d steps\n", x, y, z, f);
555 gcode->mark_as_taken();
556 break;
557 }
558 }
559 }
560 }
561
562 void Endstops::trim2mm(float *mm)
563 {
564 int dirx = (this->home_direction[0] ? 1 : -1);
565 int diry = (this->home_direction[1] ? 1 : -1);
566 int dirz = (this->home_direction[2] ? 1 : -1);
567
568 mm[0] = this->trim[0] / this->steps_per_mm[0] * dirx; // convert to mm
569 mm[1] = this->trim[1] / this->steps_per_mm[1] * diry;
570 mm[2] = this->trim[2] / this->steps_per_mm[2] * dirz;
571 }
572
573 #define max(a,b) (((a) > (b)) ? (a) : (b))
574 // Called periodically to change the speed to match acceleration
575 uint32_t Endstops::acceleration_tick(uint32_t dummy)
576 {
577 if(this->status == NOT_HOMING) return(0); // nothing to do
578
579 // foreach stepper that is moving
580 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
581 if( !this->steppers[c]->moving ) continue;
582
583 uint32_t current_rate = this->steppers[c]->steps_per_second;
584 uint32_t target_rate = int(floor(this->feed_rate[c]));
585
586 if( current_rate < target_rate ){
587 uint32_t rate_increase = int(floor((THEKERNEL->planner->acceleration/THEKERNEL->stepper->acceleration_ticks_per_second)*this->steps_per_mm[c]));
588 current_rate = min( target_rate, current_rate + rate_increase );
589 }
590 if( current_rate > target_rate ){ current_rate = target_rate; }
591
592 // steps per second
593 this->steppers[c]->set_speed(max(current_rate, THEKERNEL->stepper->minimum_steps_per_second));
594 }
595
596 return 0;
597 }