Merge branch 'edge'
[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
29 #define ALPHA_AXIS 0
30 #define BETA_AXIS 1
31 #define GAMMA_AXIS 2
32 #define X_AXIS 0
33 #define Y_AXIS 1
34 #define Z_AXIS 2
35
36 #define NOT_HOMING 0
37 #define MOVING_TO_ORIGIN_FAST 1
38 #define MOVING_BACK 2
39 #define MOVING_TO_ORIGIN_SLOW 3
40
41 #define endstops_module_enable_checksum CHECKSUM("endstops_enable")
42 #define corexy_homing_checksum CHECKSUM("corexy_homing")
43 #define delta_homing_checksum CHECKSUM("delta_homing")
44
45 #define alpha_min_endstop_checksum CHECKSUM("alpha_min_endstop")
46 #define beta_min_endstop_checksum CHECKSUM("beta_min_endstop")
47 #define gamma_min_endstop_checksum CHECKSUM("gamma_min_endstop")
48
49 #define alpha_max_endstop_checksum CHECKSUM("alpha_max_endstop")
50 #define beta_max_endstop_checksum CHECKSUM("beta_max_endstop")
51 #define gamma_max_endstop_checksum CHECKSUM("gamma_max_endstop")
52
53 #define alpha_trim_checksum CHECKSUM("alpha_trim")
54 #define beta_trim_checksum CHECKSUM("beta_trim")
55 #define gamma_trim_checksum CHECKSUM("gamma_trim")
56
57 // these values are in steps and should be deprecated
58 #define alpha_fast_homing_rate_checksum CHECKSUM("alpha_fast_homing_rate")
59 #define beta_fast_homing_rate_checksum CHECKSUM("beta_fast_homing_rate")
60 #define gamma_fast_homing_rate_checksum CHECKSUM("gamma_fast_homing_rate")
61
62 #define alpha_slow_homing_rate_checksum CHECKSUM("alpha_slow_homing_rate")
63 #define beta_slow_homing_rate_checksum CHECKSUM("beta_slow_homing_rate")
64 #define gamma_slow_homing_rate_checksum CHECKSUM("gamma_slow_homing_rate")
65
66 #define alpha_homing_retract_checksum CHECKSUM("alpha_homing_retract")
67 #define beta_homing_retract_checksum CHECKSUM("beta_homing_retract")
68 #define gamma_homing_retract_checksum CHECKSUM("gamma_homing_retract")
69 #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
70
71 // same as above but in user friendly mm/s and mm
72 #define alpha_fast_homing_rate_mm_checksum CHECKSUM("alpha_fast_homing_rate_mm_s")
73 #define beta_fast_homing_rate_mm_checksum CHECKSUM("beta_fast_homing_rate_mm_s")
74 #define gamma_fast_homing_rate_mm_checksum CHECKSUM("gamma_fast_homing_rate_mm_s")
75
76 #define alpha_slow_homing_rate_mm_checksum CHECKSUM("alpha_slow_homing_rate_mm_s")
77 #define beta_slow_homing_rate_mm_checksum CHECKSUM("beta_slow_homing_rate_mm_s")
78 #define gamma_slow_homing_rate_mm_checksum CHECKSUM("gamma_slow_homing_rate_mm_s")
79
80 #define alpha_homing_retract_mm_checksum CHECKSUM("alpha_homing_retract_mm")
81 #define beta_homing_retract_mm_checksum CHECKSUM("beta_homing_retract_mm")
82 #define gamma_homing_retract_mm_checksum CHECKSUM("gamma_homing_retract_mm")
83
84 #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
85
86 #define alpha_homing_direction_checksum CHECKSUM("alpha_homing_direction")
87 #define beta_homing_direction_checksum CHECKSUM("beta_homing_direction")
88 #define gamma_homing_direction_checksum CHECKSUM("gamma_homing_direction")
89 #define home_to_max_checksum CHECKSUM("home_to_max")
90 #define home_to_min_checksum CHECKSUM("home_to_min")
91 #define alpha_min_checksum CHECKSUM("alpha_min")
92 #define beta_min_checksum CHECKSUM("beta_min")
93 #define gamma_min_checksum CHECKSUM("gamma_min")
94
95 #define alpha_max_checksum CHECKSUM("alpha_max")
96 #define beta_max_checksum CHECKSUM("beta_max")
97 #define gamma_max_checksum CHECKSUM("gamma_max")
98
99 #define STEPPER THEKERNEL->robot->actuators
100 #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
101
102 Endstops::Endstops()
103 {
104 this->status = NOT_HOMING;
105 home_offset[0] = home_offset[1] = home_offset[2] = 0.0F;
106 }
107
108 void Endstops::on_module_loaded()
109 {
110 // Do not do anything if not enabled
111 if ( THEKERNEL->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) {
112 delete this;
113 return;
114 }
115
116 register_for_event(ON_GCODE_RECEIVED);
117 register_for_event(ON_GET_PUBLIC_DATA);
118 register_for_event(ON_SET_PUBLIC_DATA);
119
120 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->get_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 // These are the old ones in steps still here for backwards compatibility
137 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(0);
138 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(1);
139 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number() / STEPS_PER_MM(2);
140 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(0);
141 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(1);
142 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number() / STEPS_PER_MM(2);
143 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(0);
144 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(1);
145 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number() / STEPS_PER_MM(2);
146
147 // 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
148 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0])->as_number();
149 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[1])->as_number();
150 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2])->as_number();
151 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0])->as_number();
152 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[1])->as_number();
153 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2])->as_number();
154 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum )->by_default(this->retract_mm[0])->as_number();
155 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum )->by_default(this->retract_mm[1])->as_number();
156 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum )->by_default(this->retract_mm[2])->as_number();
157
158 this->debounce_count = THEKERNEL->config->value(endstop_debounce_count_checksum )->by_default(100)->as_number();
159
160
161 // get homing direction and convert to boolean where true is home to min, and false is home to max
162 int home_dir = get_checksum(THEKERNEL->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string());
163 this->home_direction[0] = home_dir != home_to_max_checksum;
164
165 home_dir = get_checksum(THEKERNEL->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string());
166 this->home_direction[1] = home_dir != home_to_max_checksum;
167
168 home_dir = get_checksum(THEKERNEL->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string());
169 this->home_direction[2] = home_dir != home_to_max_checksum;
170
171 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();
172 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();;
173 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();;
174
175 this->is_corexy = THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool();
176 this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool();
177
178 // endstop trim used by deltas to do soft adjusting
179 // on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
180 this->trim_mm[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0 )->as_number();
181 this->trim_mm[1] = THEKERNEL->config->value(beta_trim_checksum )->by_default(0 )->as_number();
182 this->trim_mm[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0 )->as_number();
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 ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
193 if ( ( axes_to_move >> c ) & 1 ) {
194 if ( this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) {
195 if ( debounce[c] < debounce_count ) {
196 debounce[c]++;
197 running = true;
198 } else if ( STEPPER[c]->is_moving() ) {
199 STEPPER[c]->move(0, 0);
200 }
201 } else {
202 // The endstop was not hit yet
203 running = true;
204 debounce[c] = 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 ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
217 if ( ( axes_to_move >> c) & 1 ) {
218 this->feed_rate[c]= this->fast_rates[c];
219 STEPPER[c]->set_speed(0);
220 STEPPER[c]->move(this->home_direction[c], 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 ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
231 if ( ( axes_to_move >> c ) & 1 ) {
232 inverted_dir = !this->home_direction[c];
233 this->feed_rate[c]= this->slow_rates[c];
234 STEPPER[c]->set_speed(0);
235 STEPPER[c]->move(inverted_dir, this->retract_mm[c]*STEPS_PER_MM(c));
236 }
237 }
238
239 // Wait for moves to be done
240 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
241 if ( ( axes_to_move >> c ) & 1 ) {
242 while ( STEPPER[c]->is_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 ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
251 if ( ( axes_to_move >> c ) & 1 ) {
252 this->feed_rate[c]= this->slow_rates[c];
253 STEPPER[c]->set_speed(0);
254 STEPPER[c]->move(this->home_direction[c], 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 ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
265 if ( this->trim_mm[c] != 0.0F && ( axes_to_move >> c ) & 1 ) {
266 inverted_dir = this->home_direction[c];
267 // move up or down depending on sign of trim, -ive is down away from home
268 if (this->trim_mm[c] < 0) inverted_dir = !inverted_dir;
269 this->feed_rate[c]= this->slow_rates[c];
270 STEPPER[c]->set_speed(0);
271 STEPPER[c]->move(inverted_dir, abs(round(this->trim_mm[c]*STEPS_PER_MM(c))));
272 }
273 }
274
275 // Wait for moves to be done
276 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
277 if ( ( axes_to_move >> c ) & 1 ) {
278 //THEKERNEL->streams->printf("axis %c \r\n", c );
279 while ( STEPPER[c]->is_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 (STEPPER[X_AXIS]->is_moving()) STEPPER[X_AXIS]->move(0, 0);
304 if (STEPPER[Y_AXIS]->is_moving()) STEPPER[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 STEPPER[X_AXIS]->set_speed(0);
319 STEPPER[X_AXIS]->move(dirx, 10000000);
320 this->feed_rate[Y_AXIS]= fast_rate;
321 STEPPER[Y_AXIS]->set_speed(0);
322 STEPPER[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 STEPPER[X_AXIS]->set_speed(0);
331 STEPPER[X_AXIS]->move(!dirx, retract_steps);
332 this->feed_rate[Y_AXIS]= slow_rate;
333 STEPPER[Y_AXIS]->set_speed(0);
334 STEPPER[Y_AXIS]->move(!diry, retract_steps);
335
336 // wait until done
337 while ( STEPPER[X_AXIS]->is_moving() || STEPPER[Y_AXIS]->is_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 STEPPER[X_AXIS]->set_speed(0);
345 STEPPER[X_AXIS]->move(dirx, 10000000);
346 this->feed_rate[Y_AXIS]= slow_rate;
347 STEPPER[Y_AXIS]->set_speed(0);
348 STEPPER[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 STEPPER[motor]->set_speed(0); // need to allow for more ground covered when moving diagonally
386 STEPPER[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(STEPPER[motor]->is_moving()) STEPPER[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_mm[X_AXIS]*STEPS_PER_MM(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_mm[Y_AXIS]*STEPS_PER_MM(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 ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
439 if ( (home_all || gcode->has_letter(c+'X')) && this->pins[c + (this->home_direction[c] ? 0 : 3)].connected() ) {
440 axes_to_move += ( 1 << c );
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 = X_AXIS; c <= Z_AXIS; 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", mx, this->pins[px].get(), my, this->pins[py].get(), mz, this->pins[pz].get());
472 gcode->add_nl= true;
473 gcode->mark_as_taken();
474 }
475 break;
476
477 case 206: // M206 - set homing offset
478 if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X');
479 if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y');
480 if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z');
481 gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
482 gcode->mark_as_taken();
483 break;
484
485 case 500: // save settings
486 case 503: // print settings
487 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]);
488 if (is_delta) {
489 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]);
490 gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position[2]);
491 }
492 gcode->mark_as_taken();
493 break;
494
495 case 665: { // M665 - set max gamma/z height
496 gcode->mark_as_taken();
497 float gamma_max = this->homing_position[2];
498 if (gcode->has_letter('Z')) {
499 this->homing_position[2] = gamma_max = gcode->get_value('Z');
500 }
501 gcode->stream->printf("Max Z %8.3f ", gamma_max);
502 gcode->add_nl = true;
503 }
504 break;
505
506
507 case 666:
508 if(this->is_delta) { // M666 - set trim for each axis in mm, NB negative mm trim is down
509 if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X');
510 if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y');
511 if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z');
512
513 // print the current trim values in mm
514 gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
515 gcode->mark_as_taken();
516 }
517 break;
518
519 // NOTE this is to test accuracy of lead screws etc.
520 case 910: { // M910 - move specific number of raw steps
521 // Enable the motors
522 THEKERNEL->stepper->turn_enable_pins_on();
523
524 int x= 0, y=0 , z= 0, f= 200*16;
525 if (gcode->has_letter('F')) f = gcode->get_value('F');
526 if (gcode->has_letter('X')) {
527 x = gcode->get_value('X');
528 STEPPER[X_AXIS]->set_speed(f);
529 STEPPER[X_AXIS]->move(x<0, abs(x));
530 }
531 if (gcode->has_letter('Y')) {
532 y = gcode->get_value('Y');
533 STEPPER[Y_AXIS]->set_speed(f);
534 STEPPER[Y_AXIS]->move(y<0, abs(y));
535 }
536 if (gcode->has_letter('Z')) {
537 z = gcode->get_value('Z');
538 STEPPER[Z_AXIS]->set_speed(f);
539 STEPPER[Z_AXIS]->move(z<0, abs(z));
540 }
541 gcode->stream->printf("Moved X %d Y %d Z %d F %d steps\n", x, y, z, f);
542 gcode->mark_as_taken();
543 break;
544 }
545 }
546 }
547 }
548
549 #define max(a,b) (((a) > (b)) ? (a) : (b))
550 // Called periodically to change the speed to match acceleration
551 uint32_t Endstops::acceleration_tick(uint32_t dummy)
552 {
553 if(this->status == NOT_HOMING) return(0); // nothing to do
554
555 // foreach stepper that is moving
556 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
557 if( !STEPPER[c]->is_moving() ) continue;
558
559 uint32_t current_rate = STEPPER[c]->get_steps_per_second();
560 uint32_t target_rate = int(floor(this->feed_rate[c]*STEPS_PER_MM(c)));
561
562 if( current_rate < target_rate ){
563 uint32_t rate_increase = int(floor((THEKERNEL->planner->get_acceleration()/THEKERNEL->stepper->get_acceleration_ticks_per_second())*STEPS_PER_MM(c)));
564 current_rate = min( target_rate, current_rate + rate_increase );
565 }
566 if( current_rate > target_rate ){ current_rate = target_rate; }
567
568 // steps per second
569 STEPPER[c]->set_speed(max(current_rate, THEKERNEL->stepper->get_minimum_steps_per_second()));
570 }
571
572 return 0;
573 }
574
575 void Endstops::on_get_public_data(void* argument){
576 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
577
578 if(!pdr->starts_with(endstops_checksum)) return;
579
580 if(pdr->second_element_is(trim_checksum)) {
581 pdr->set_data_ptr(&this->trim_mm);
582 pdr->set_taken();
583
584 }else if(pdr->second_element_is(home_offset_checksum)) {
585 pdr->set_data_ptr(&this->home_offset);
586 pdr->set_taken();
587 }
588 }
589
590 void Endstops::on_set_public_data(void* argument){
591 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
592
593 if(!pdr->starts_with(endstops_checksum)) return;
594
595 if(pdr->second_element_is(trim_checksum)) {
596 float *t= static_cast<float*>(pdr->get_data_ptr());
597 this->trim_mm[0]= t[0];
598 this->trim_mm[1]= t[1];
599 this->trim_mm[2]= t[2];
600 pdr->set_taken();
601
602 }else if(pdr->second_element_is(home_offset_checksum)) {
603 float *t= static_cast<float*>(pdr->get_data_ptr());
604 if(!isnan(t[0])) this->home_offset[0]= t[0];
605 if(!isnan(t[1])) this->home_offset[1]= t[1];
606 if(!isnan(t[2])) this->home_offset[2]= t[2];
607 }
608 }