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