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