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/>.
8 #include "libs/Module.h"
9 #include "libs/Kernel.h"
10 #include "modules/communication/utils/Gcode.h"
11 #include "modules/robot/Conveyor.h"
13 #include "libs/nuts_bolts.h"
15 #include "libs/StepperMotor.h"
16 #include "wait_api.h" // mbed.h lib
26 #define MOVING_TO_ORIGIN_FAST 1
28 #define MOVING_TO_ORIGIN_SLOW 3
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")
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")
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")
42 #define alpha_trim_checksum CHECKSUM("alpha_trim")
43 #define beta_trim_checksum CHECKSUM("beta_trim")
44 #define gamma_trim_checksum CHECKSUM("gamma_trim")
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")
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")
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")
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")
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")
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")
73 #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
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")
84 #define alpha_max_checksum CHECKSUM("alpha_max")
85 #define beta_max_checksum CHECKSUM("beta_max")
86 #define gamma_max_checksum CHECKSUM("gamma_max")
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")
94 this->status
= NOT_HOMING
;
95 home_offset
[0] = home_offset
[1] = home_offset
[2] = 0.0F
;
98 void Endstops::on_module_loaded()
100 // Do not do anything if not enabled
101 if ( THEKERNEL
->config
->value( endstops_module_enable_checksum
)->by_default(true)->as_bool() == false ) {
105 register_for_event(ON_CONFIG_RELOAD
);
106 this->register_for_event(ON_GCODE_RECEIVED
);
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
);
115 this->on_config_reload(this);
119 void Endstops::on_config_reload(void *argument
)
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();
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();
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();
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];
155 this->debounce_count
= THEKERNEL
->config
->value(endstop_debounce_count_checksum
)->by_default(0)->as_number();
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
;
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
;
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
;
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();;
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();
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
;
185 void Endstops::wait_for_homed(char axes_to_move
)
188 unsigned int debounce
[3] = {0, 0, 0};
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'] ++;
198 } else if ( this->steppers
[c
- 'X']->moving
) {
199 this->steppers
[c
- 'X']->move(0, 0);
202 // The endstop was not hit yet
204 debounce
[c
- 'X'] = 0;
211 // this homing works for cartesian and delta printers, not for HBots/CoreXY
212 void Endstops::do_homing(char axes_to_move
)
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);
224 // Wait for all axes to have homed
225 this->wait_for_homed(axes_to_move
);
227 // Move back a small distance
228 this->status
= MOVING_BACK
;
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']);
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
);
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);
258 // Wait for all axes to have homed
259 this->wait_for_homed(axes_to_move
);
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']);
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
);
287 this->status
= NOT_HOMING
;
290 void Endstops::wait_for_homed_corexy(int axis
)
293 unsigned int debounce
[3] = {0, 0, 0};
296 THEKERNEL
->call_event(ON_IDLE
);
297 if ( this->pins
[axis
+ (this->home_direction
[axis
] ? 0 : 3)].get() ) {
298 if ( debounce
[axis
] < debounce_count
) {
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);
307 // The endstop was not hit yet
314 void Endstops::corexy_home(int home_axis
, bool dirx
, bool diry
, float fast_rate
, float slow_rate
, unsigned int retract_steps
)
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);
324 // wait for primary axis
325 this->wait_for_homed_corexy(home_axis
);
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
);
337 while ( this->steppers
[X_AXIS
]->moving
|| this->steppers
[Y_AXIS
]->moving
) {
338 THEKERNEL
->call_event(ON_IDLE
);
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);
350 // wait for primary axis
351 this->wait_for_homed_corexy(home_axis
);
354 // this homing works for HBots/CoreXY
355 void Endstops::do_homing_corexy(char axes_to_move
)
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
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
];
368 if(dirx
&& diry
) { // min/min
371 }else if(dirx
&& !diry
) { // min/max
374 }else if(!dirx
&& diry
) { // max/min
377 }else if(!dirx
&& !diry
) { // max/max
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
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()) {
394 if(this->steppers
[motor
]->moving
) this->steppers
[motor
]->move(0, 0);
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
]);
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
]);
413 if (axes_to_move
& 0x04) { // move Z
414 do_homing(0x04); // just home normally for Z
418 this->status
= NOT_HOMING
;
421 // Start homing sequences by response to GCode commands
422 void Endstops::on_gcode_received(void *argument
)
424 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
426 if ( gcode
->g
== 28 ) {
427 gcode
->mark_as_taken();
428 // G28 is received, we have homing to do
430 // First wait for the queue to be empty
431 THEKERNEL
->conveyor
->wait_for_empty_queue();
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') );
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' ) );
445 THEKERNEL
->stepper
->turn_enable_pins_on();
447 // do the actual homing
449 do_homing_corexy(axes_to_move
);
451 do_homing(axes_to_move
);
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
);
460 } else if (gcode
->has_m
) {
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";
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();
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();
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]);
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]);
493 gcode
->mark_as_taken();
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');
502 gcode
->stream
->printf("Max Z %8.3f ", gamma_max
);
503 gcode
->add_nl
= true;
508 case 666: { // M666 - set trim for each axis in mm
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');
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
;
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();
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
));
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
));
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
));
548 gcode
->stream
->printf("Moved X %d Y %d Z %d F %d steps\n", x
, y
, z
, f
);
549 gcode
->mark_as_taken();
556 void Endstops::trim2mm(float *mm
)
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);
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
;
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
)
571 if(this->status
== NOT_HOMING
) return(0); // nothing to do
573 // foreach stepper that is moving
574 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
575 if( !this->steppers
[c
]->moving
) continue;
577 uint32_t current_rate
= this->steppers
[c
]->steps_per_second
;
578 uint32_t target_rate
= int(floor(this->feed_rate
[c
]));
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
);
584 if( current_rate
> target_rate
){ current_rate
= target_rate
; }
587 this->steppers
[c
]->set_speed(max(current_rate
, THEKERNEL
->stepper
->minimum_steps_per_second
));