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
20 #include "SlowTicker.h"
22 #include "checksumm.h"
24 #include "ConfigValue.h"
25 #include "libs/StreamOutput.h"
26 #include "PublicDataRequest.h"
27 #include "EndstopsPublicAccess.h"
28 #include "StreamOutputPool.h"
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")
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")
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")
50 #define alpha_trim_checksum CHECKSUM("alpha_trim")
51 #define beta_trim_checksum CHECKSUM("beta_trim")
52 #define gamma_trim_checksum CHECKSUM("gamma_trim")
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")
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")
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")
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")
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")
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")
81 #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
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")
92 #define alpha_max_checksum CHECKSUM("alpha_max")
93 #define beta_max_checksum CHECKSUM("beta_max")
94 #define gamma_max_checksum CHECKSUM("gamma_max")
96 #define alpha_limit_enable_checksum CHECKSUM("alpha_limit_enable")
97 #define beta_limit_enable_checksum CHECKSUM("beta_limit_enable")
98 #define gamma_limit_enable_checksum CHECKSUM("gamma_limit_enable")
100 #define STEPPER THEKERNEL->robot->actuators
101 #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
106 MOVING_TO_ORIGIN_FAST
,
108 MOVING_TO_ORIGIN_SLOW
,
115 this->status
= NOT_HOMING
;
116 home_offset
[0] = home_offset
[1] = home_offset
[2] = 0.0F
;
119 void Endstops::on_module_loaded()
121 // Do not do anything if not enabled
122 if ( THEKERNEL
->config
->value( endstops_module_enable_checksum
)->by_default(true)->as_bool() == false ) {
127 register_for_event(ON_GCODE_RECEIVED
);
128 register_for_event(ON_GET_PUBLIC_DATA
);
129 register_for_event(ON_SET_PUBLIC_DATA
);
131 THEKERNEL
->slow_ticker
->attach( THEKERNEL
->stepper
->get_acceleration_ticks_per_second() , this, &Endstops::acceleration_tick
);
134 this->on_config_reload(this);
138 void Endstops::on_config_reload(void *argument
)
140 this->pins
[0].from_string( THEKERNEL
->config
->value(alpha_min_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
141 this->pins
[1].from_string( THEKERNEL
->config
->value(beta_min_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
142 this->pins
[2].from_string( THEKERNEL
->config
->value(gamma_min_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
143 this->pins
[3].from_string( THEKERNEL
->config
->value(alpha_max_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
144 this->pins
[4].from_string( THEKERNEL
->config
->value(beta_max_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
145 this->pins
[5].from_string( THEKERNEL
->config
->value(gamma_max_endstop_checksum
)->by_default("nc" )->as_string())->as_input();
147 // These are the old ones in steps still here for backwards compatibility
148 this->fast_rates
[0] = THEKERNEL
->config
->value(alpha_fast_homing_rate_checksum
)->by_default(4000 )->as_number() / STEPS_PER_MM(0);
149 this->fast_rates
[1] = THEKERNEL
->config
->value(beta_fast_homing_rate_checksum
)->by_default(4000 )->as_number() / STEPS_PER_MM(1);
150 this->fast_rates
[2] = THEKERNEL
->config
->value(gamma_fast_homing_rate_checksum
)->by_default(6400 )->as_number() / STEPS_PER_MM(2);
151 this->slow_rates
[0] = THEKERNEL
->config
->value(alpha_slow_homing_rate_checksum
)->by_default(2000 )->as_number() / STEPS_PER_MM(0);
152 this->slow_rates
[1] = THEKERNEL
->config
->value(beta_slow_homing_rate_checksum
)->by_default(2000 )->as_number() / STEPS_PER_MM(1);
153 this->slow_rates
[2] = THEKERNEL
->config
->value(gamma_slow_homing_rate_checksum
)->by_default(3200 )->as_number() / STEPS_PER_MM(2);
154 this->retract_mm
[0] = THEKERNEL
->config
->value(alpha_homing_retract_checksum
)->by_default(400 )->as_number() / STEPS_PER_MM(0);
155 this->retract_mm
[1] = THEKERNEL
->config
->value(beta_homing_retract_checksum
)->by_default(400 )->as_number() / STEPS_PER_MM(1);
156 this->retract_mm
[2] = THEKERNEL
->config
->value(gamma_homing_retract_checksum
)->by_default(1600 )->as_number() / STEPS_PER_MM(2);
158 // 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
159 this->fast_rates
[0] = THEKERNEL
->config
->value(alpha_fast_homing_rate_mm_checksum
)->by_default(this->fast_rates
[0])->as_number();
160 this->fast_rates
[1] = THEKERNEL
->config
->value(beta_fast_homing_rate_mm_checksum
)->by_default(this->fast_rates
[1])->as_number();
161 this->fast_rates
[2] = THEKERNEL
->config
->value(gamma_fast_homing_rate_mm_checksum
)->by_default(this->fast_rates
[2])->as_number();
162 this->slow_rates
[0] = THEKERNEL
->config
->value(alpha_slow_homing_rate_mm_checksum
)->by_default(this->slow_rates
[0])->as_number();
163 this->slow_rates
[1] = THEKERNEL
->config
->value(beta_slow_homing_rate_mm_checksum
)->by_default(this->slow_rates
[1])->as_number();
164 this->slow_rates
[2] = THEKERNEL
->config
->value(gamma_slow_homing_rate_mm_checksum
)->by_default(this->slow_rates
[2])->as_number();
165 this->retract_mm
[0] = THEKERNEL
->config
->value(alpha_homing_retract_mm_checksum
)->by_default(this->retract_mm
[0])->as_number();
166 this->retract_mm
[1] = THEKERNEL
->config
->value(beta_homing_retract_mm_checksum
)->by_default(this->retract_mm
[1])->as_number();
167 this->retract_mm
[2] = THEKERNEL
->config
->value(gamma_homing_retract_mm_checksum
)->by_default(this->retract_mm
[2])->as_number();
169 this->debounce_count
= THEKERNEL
->config
->value(endstop_debounce_count_checksum
)->by_default(100)->as_number();
172 // get homing direction and convert to boolean where true is home to min, and false is home to max
173 int home_dir
= get_checksum(THEKERNEL
->config
->value(alpha_homing_direction_checksum
)->by_default("home_to_min")->as_string());
174 this->home_direction
[0] = home_dir
!= home_to_max_checksum
;
176 home_dir
= get_checksum(THEKERNEL
->config
->value(beta_homing_direction_checksum
)->by_default("home_to_min")->as_string());
177 this->home_direction
[1] = home_dir
!= home_to_max_checksum
;
179 home_dir
= get_checksum(THEKERNEL
->config
->value(gamma_homing_direction_checksum
)->by_default("home_to_min")->as_string());
180 this->home_direction
[2] = home_dir
!= home_to_max_checksum
;
182 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();
183 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();;
184 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();;
186 this->is_corexy
= THEKERNEL
->config
->value(corexy_homing_checksum
)->by_default(false)->as_bool();
187 this->is_delta
= THEKERNEL
->config
->value(delta_homing_checksum
)->by_default(false)->as_bool();
189 // endstop trim used by deltas to do soft adjusting
190 // on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
191 this->trim_mm
[0] = THEKERNEL
->config
->value(alpha_trim_checksum
)->by_default(0 )->as_number();
192 this->trim_mm
[1] = THEKERNEL
->config
->value(beta_trim_checksum
)->by_default(0 )->as_number();
193 this->trim_mm
[2] = THEKERNEL
->config
->value(gamma_trim_checksum
)->by_default(0 )->as_number();
196 this->limit_enable
[X_AXIS
]= THEKERNEL
->config
->value(alpha_limit_enable_checksum
)->by_default(false)->as_bool();
197 this->limit_enable
[Y_AXIS
]= THEKERNEL
->config
->value(beta_limit_enable_checksum
)->by_default(false)->as_bool();
198 this->limit_enable
[Z_AXIS
]= THEKERNEL
->config
->value(gamma_limit_enable_checksum
)->by_default(false)->as_bool();
200 if(this->limit_enable
[X_AXIS
] || this->limit_enable
[Y_AXIS
] || this->limit_enable
[Z_AXIS
]){
201 register_for_event(ON_IDLE
);
205 static const char *endstop_names
[]= {"MIN_X", "MIN_Y", "MIN_Z", "MAX_X", "MAX_Y", "MAX_Z"};
207 void Endstops::on_idle(void *argument
)
209 if(this->status
!= NOT_HOMING
) return; // don't check while homing or if a LIMIT was triggered
211 for( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
212 if(this->limit_enable
[c
] && STEPPER
[c
]->is_moving()) {
213 std::array
<int, 2> minmax
{{0, 3}};
214 // check min and max endstops
215 for (int i
: minmax
) {
218 while(this->pins
[n
].get()) {
219 if ( ++debounce
>= debounce_count
) {
221 THEKERNEL
->pauser
->take();
222 THEKERNEL
->streams
->printf("Limit switch %s was hit - reset or press play button\n", endstop_names
[n
]);
223 this->status
= LIMIT_TRIGGERED
;
232 // if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move
233 // TODO should check if triggered and only back off if triggered
234 void Endstops::back_off_home()
236 this->status
= BACK_OFF_HOME
;
237 for( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
238 if(this->limit_enable
[c
]) {
239 // Move off of the endstop using a regular relative move
241 snprintf(buf
, sizeof(buf
), "G0 %c%1.4f F%1.4f", c
+'X', this->retract_mm
[c
]*(this->home_direction
[c
]?1:-1), this->slow_rates
[c
]*60.0F
);
242 Gcode
gc(buf
, &(StreamOutput::NullStream
));
243 bool oldmode
= THEKERNEL
->robot
->absolute_mode
;
244 THEKERNEL
->robot
->absolute_mode
= false; // needs to be relative mode
245 THEKERNEL
->robot
->on_gcode_received(&gc
); // send to robot directly
246 THEKERNEL
->robot
->absolute_mode
= oldmode
; // restore mode
249 // Wait for above to finish
250 THEKERNEL
->conveyor
->wait_for_empty_queue();
251 this->status
= NOT_HOMING
;
254 void Endstops::wait_for_homed(char axes_to_move
)
257 unsigned int debounce
[3] = {0, 0, 0};
260 THEKERNEL
->call_event(ON_IDLE
);
261 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
262 if ( ( axes_to_move
>> c
) & 1 ) {
263 if ( this->pins
[c
+ (this->home_direction
[c
] ? 0 : 3)].get() ) {
264 if ( debounce
[c
] < debounce_count
) {
267 } else if ( STEPPER
[c
]->is_moving() ) {
268 STEPPER
[c
]->move(0, 0);
271 // The endstop was not hit yet
280 // this homing works for cartesian and delta printers, not for HBots/CoreXY
281 void Endstops::do_homing(char axes_to_move
)
283 // Start moving the axes to the origin
284 this->status
= MOVING_TO_ORIGIN_FAST
;
285 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
286 if ( ( axes_to_move
>> c
) & 1 ) {
287 this->feed_rate
[c
]= this->fast_rates
[c
];
288 STEPPER
[c
]->set_speed(0);
289 STEPPER
[c
]->move(this->home_direction
[c
], 10000000);
293 // Wait for all axes to have homed
294 this->wait_for_homed(axes_to_move
);
296 // Move back a small distance
297 this->status
= MOVING_BACK
;
299 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
300 if ( ( axes_to_move
>> c
) & 1 ) {
301 inverted_dir
= !this->home_direction
[c
];
302 this->feed_rate
[c
]= this->slow_rates
[c
];
303 STEPPER
[c
]->set_speed(0);
304 STEPPER
[c
]->move(inverted_dir
, this->retract_mm
[c
]*STEPS_PER_MM(c
));
308 // Wait for moves to be done
309 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
310 if ( ( axes_to_move
>> c
) & 1 ) {
311 while ( STEPPER
[c
]->is_moving() ) {
312 THEKERNEL
->call_event(ON_IDLE
);
317 // Start moving the axes to the origin slowly
318 this->status
= MOVING_TO_ORIGIN_SLOW
;
319 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
320 if ( ( axes_to_move
>> c
) & 1 ) {
321 this->feed_rate
[c
]= this->slow_rates
[c
];
322 STEPPER
[c
]->set_speed(0);
323 STEPPER
[c
]->move(this->home_direction
[c
], 10000000);
327 // Wait for all axes to have homed
328 this->wait_for_homed(axes_to_move
);
330 if (this->is_delta
) {
331 // move for soft trim
332 this->status
= MOVING_BACK
;
333 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
334 if ( this->trim_mm
[c
] != 0.0F
&& ( axes_to_move
>> c
) & 1 ) {
335 inverted_dir
= this->home_direction
[c
];
336 // move up or down depending on sign of trim, -ive is down away from home
337 if (this->trim_mm
[c
] < 0) inverted_dir
= !inverted_dir
;
338 this->feed_rate
[c
]= this->slow_rates
[c
];
339 STEPPER
[c
]->set_speed(0);
340 STEPPER
[c
]->move(inverted_dir
, abs(round(this->trim_mm
[c
]*STEPS_PER_MM(c
))));
344 // Wait for moves to be done
345 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
346 if ( ( axes_to_move
>> c
) & 1 ) {
347 //THEKERNEL->streams->printf("axis %c \r\n", c );
348 while ( STEPPER
[c
]->is_moving() ) {
349 THEKERNEL
->call_event(ON_IDLE
);
356 this->status
= NOT_HOMING
;
359 void Endstops::wait_for_homed_corexy(int axis
)
362 unsigned int debounce
[3] = {0, 0, 0};
365 THEKERNEL
->call_event(ON_IDLE
);
366 if ( this->pins
[axis
+ (this->home_direction
[axis
] ? 0 : 3)].get() ) {
367 if ( debounce
[axis
] < debounce_count
) {
371 // turn both off if running
372 if (STEPPER
[X_AXIS
]->is_moving()) STEPPER
[X_AXIS
]->move(0, 0);
373 if (STEPPER
[Y_AXIS
]->is_moving()) STEPPER
[Y_AXIS
]->move(0, 0);
376 // The endstop was not hit yet
383 void Endstops::corexy_home(int home_axis
, bool dirx
, bool diry
, float fast_rate
, float slow_rate
, unsigned int retract_steps
)
385 this->status
= MOVING_TO_ORIGIN_FAST
;
386 this->feed_rate
[X_AXIS
]= fast_rate
;
387 STEPPER
[X_AXIS
]->set_speed(0);
388 STEPPER
[X_AXIS
]->move(dirx
, 10000000);
389 this->feed_rate
[Y_AXIS
]= fast_rate
;
390 STEPPER
[Y_AXIS
]->set_speed(0);
391 STEPPER
[Y_AXIS
]->move(diry
, 10000000);
393 // wait for primary axis
394 this->wait_for_homed_corexy(home_axis
);
396 // Move back a small distance
397 this->status
= MOVING_BACK
;
398 this->feed_rate
[X_AXIS
]= slow_rate
;
399 STEPPER
[X_AXIS
]->set_speed(0);
400 STEPPER
[X_AXIS
]->move(!dirx
, retract_steps
);
401 this->feed_rate
[Y_AXIS
]= slow_rate
;
402 STEPPER
[Y_AXIS
]->set_speed(0);
403 STEPPER
[Y_AXIS
]->move(!diry
, retract_steps
);
406 while ( STEPPER
[X_AXIS
]->is_moving() || STEPPER
[Y_AXIS
]->is_moving()) {
407 THEKERNEL
->call_event(ON_IDLE
);
410 // Start moving the axes to the origin slowly
411 this->status
= MOVING_TO_ORIGIN_SLOW
;
412 this->feed_rate
[X_AXIS
]= slow_rate
;
413 STEPPER
[X_AXIS
]->set_speed(0);
414 STEPPER
[X_AXIS
]->move(dirx
, 10000000);
415 this->feed_rate
[Y_AXIS
]= slow_rate
;
416 STEPPER
[Y_AXIS
]->set_speed(0);
417 STEPPER
[Y_AXIS
]->move(diry
, 10000000);
419 // wait for primary axis
420 this->wait_for_homed_corexy(home_axis
);
423 // this homing works for HBots/CoreXY
424 void Endstops::do_homing_corexy(char axes_to_move
)
426 // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally
427 // 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
428 // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed
429 // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit
431 if((axes_to_move
& 0x03) == 0x03) { // both X and Y need Homing
432 // determine which motor to turn and which way
433 bool dirx
= this->home_direction
[X_AXIS
];
434 bool diry
= this->home_direction
[Y_AXIS
];
437 if(dirx
&& diry
) { // min/min
440 }else if(dirx
&& !diry
) { // min/max
443 }else if(!dirx
&& diry
) { // max/min
446 }else if(!dirx
&& !diry
) { // max/max
451 // then move both X and Y until one hits the endstop
452 this->status
= MOVING_TO_ORIGIN_FAST
;
453 this->feed_rate
[motor
]= this->fast_rates
[motor
]*1.4142;
454 STEPPER
[motor
]->set_speed(0); // need to allow for more ground covered when moving diagonally
455 STEPPER
[motor
]->move(dir
, 10000000);
456 // wait until either X or Y hits the endstop
459 THEKERNEL
->call_event(ON_IDLE
);
460 for(int m
=X_AXIS
;m
<=Y_AXIS
;m
++) {
461 if(this->pins
[m
+ (this->home_direction
[m
] ? 0 : 3)].get()) {
463 if(STEPPER
[motor
]->is_moving()) STEPPER
[motor
]->move(0, 0);
471 // move individual axis
472 if (axes_to_move
& 0x01) { // Home X, which means both X and Y in same direction
473 bool dir
= this->home_direction
[X_AXIS
];
474 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
));
477 if (axes_to_move
& 0x02) { // Home Y, which means both X and Y in different directions
478 bool dir
= this->home_direction
[Y_AXIS
];
479 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
));
482 if (axes_to_move
& 0x04) { // move Z
483 do_homing(0x04); // just home normally for Z
487 this->status
= NOT_HOMING
;
490 // Start homing sequences by response to GCode commands
491 void Endstops::on_gcode_received(void *argument
)
493 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
495 if ( gcode
->g
== 28 ) {
496 gcode
->mark_as_taken();
497 // G28 is received, we have homing to do
499 // First wait for the queue to be empty
500 THEKERNEL
->conveyor
->wait_for_empty_queue();
502 // Do we move select axes or all of them
503 char axes_to_move
= 0;
504 // only enable homing if the endstop is defined, deltas always home all axis
505 bool home_all
= this->is_delta
|| !( gcode
->has_letter('X') || gcode
->has_letter('Y') || gcode
->has_letter('Z') );
507 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
508 if ( (home_all
|| gcode
->has_letter(c
+'X')) && this->pins
[c
+ (this->home_direction
[c
] ? 0 : 3)].connected() ) {
509 axes_to_move
+= ( 1 << c
);
514 THEKERNEL
->stepper
->turn_enable_pins_on();
516 // do the actual homing
518 do_homing_corexy(axes_to_move
);
520 do_homing(axes_to_move
);
522 // Zero the ax(i/e)s position, add in the home offset
523 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
524 if ( (axes_to_move
>> c
) & 1 ) {
525 THEKERNEL
->robot
->reset_axis_position(this->homing_position
[c
] + this->home_offset
[c
], c
);
529 // if limit switches are enabled we must back off endstop after setting home
530 // TODO should maybe be done before setting home so X0 does not retrigger?
533 } else if (gcode
->has_m
) {
536 for (int i
= 0; i
< 6; ++i
) {
537 if(this->pins
[i
].connected())
538 gcode
->stream
->printf("%s:%d ", endstop_names
[i
], this->pins
[i
].get());
541 gcode
->mark_as_taken();
545 case 206: // M206 - set homing offset
546 if (gcode
->has_letter('X')) home_offset
[0] = gcode
->get_value('X');
547 if (gcode
->has_letter('Y')) home_offset
[1] = gcode
->get_value('Y');
548 if (gcode
->has_letter('Z')) home_offset
[2] = gcode
->get_value('Z');
549 gcode
->stream
->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset
[0], home_offset
[1], home_offset
[2]);
550 gcode
->mark_as_taken();
553 case 500: // save settings
554 case 503: // print settings
555 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]);
557 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]);
558 gcode
->stream
->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position
[2]);
560 gcode
->mark_as_taken();
563 case 665: { // M665 - set max gamma/z height
564 gcode
->mark_as_taken();
565 float gamma_max
= this->homing_position
[2];
566 if (gcode
->has_letter('Z')) {
567 this->homing_position
[2] = gamma_max
= gcode
->get_value('Z');
569 gcode
->stream
->printf("Max Z %8.3f ", gamma_max
);
570 gcode
->add_nl
= true;
576 if(this->is_delta
) { // M666 - set trim for each axis in mm, NB negative mm trim is down
577 if (gcode
->has_letter('X')) trim_mm
[0] = gcode
->get_value('X');
578 if (gcode
->has_letter('Y')) trim_mm
[1] = gcode
->get_value('Y');
579 if (gcode
->has_letter('Z')) trim_mm
[2] = gcode
->get_value('Z');
581 // print the current trim values in mm
582 gcode
->stream
->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm
[0], trim_mm
[1], trim_mm
[2]);
583 gcode
->mark_as_taken();
587 // NOTE this is to test accuracy of lead screws etc.
588 case 910: { // M910 - move specific number of raw steps
590 THEKERNEL
->stepper
->turn_enable_pins_on();
592 int x
= 0, y
=0 , z
= 0, f
= 200*16;
593 if (gcode
->has_letter('F')) f
= gcode
->get_value('F');
594 if (gcode
->has_letter('X')) {
595 x
= gcode
->get_value('X');
596 STEPPER
[X_AXIS
]->set_speed(f
);
597 STEPPER
[X_AXIS
]->move(x
<0, abs(x
));
599 if (gcode
->has_letter('Y')) {
600 y
= gcode
->get_value('Y');
601 STEPPER
[Y_AXIS
]->set_speed(f
);
602 STEPPER
[Y_AXIS
]->move(y
<0, abs(y
));
604 if (gcode
->has_letter('Z')) {
605 z
= gcode
->get_value('Z');
606 STEPPER
[Z_AXIS
]->set_speed(f
);
607 STEPPER
[Z_AXIS
]->move(z
<0, abs(z
));
609 gcode
->stream
->printf("Moved X %d Y %d Z %d F %d steps\n", x
, y
, z
, f
);
610 gcode
->mark_as_taken();
617 #define max(a,b) (((a) > (b)) ? (a) : (b))
618 // Called periodically to change the speed to match acceleration
619 uint32_t Endstops::acceleration_tick(uint32_t dummy
)
621 if(this->status
== NOT_HOMING
) return(0); // nothing to do
623 // foreach stepper that is moving
624 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
625 if( !STEPPER
[c
]->is_moving() ) continue;
627 uint32_t current_rate
= STEPPER
[c
]->get_steps_per_second();
628 uint32_t target_rate
= int(floor(this->feed_rate
[c
]*STEPS_PER_MM(c
)));
630 if( current_rate
< target_rate
){
631 uint32_t rate_increase
= int(floor((THEKERNEL
->planner
->get_acceleration()/THEKERNEL
->stepper
->get_acceleration_ticks_per_second())*STEPS_PER_MM(c
)));
632 current_rate
= min( target_rate
, current_rate
+ rate_increase
);
634 if( current_rate
> target_rate
){ current_rate
= target_rate
; }
637 STEPPER
[c
]->set_speed(max(current_rate
, THEKERNEL
->stepper
->get_minimum_steps_per_second()));
643 void Endstops::on_get_public_data(void* argument
){
644 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
646 if(!pdr
->starts_with(endstops_checksum
)) return;
648 if(pdr
->second_element_is(trim_checksum
)) {
649 pdr
->set_data_ptr(&this->trim_mm
);
652 }else if(pdr
->second_element_is(home_offset_checksum
)) {
653 pdr
->set_data_ptr(&this->home_offset
);
658 void Endstops::on_set_public_data(void* argument
){
659 PublicDataRequest
* pdr
= static_cast<PublicDataRequest
*>(argument
);
661 if(!pdr
->starts_with(endstops_checksum
)) return;
663 if(pdr
->second_element_is(trim_checksum
)) {
664 float *t
= static_cast<float*>(pdr
->get_data_ptr());
665 this->trim_mm
[0]= t
[0];
666 this->trim_mm
[1]= t
[1];
667 this->trim_mm
[2]= t
[2];
670 }else if(pdr
->second_element_is(home_offset_checksum
)) {
671 float *t
= static_cast<float*>(pdr
->get_data_ptr());
672 if(!isnan(t
[0])) this->home_offset
[0]= t
[0];
673 if(!isnan(t
[1])) this->home_offset
[1]= t
[1];
674 if(!isnan(t
[2])) this->home_offset
[2]= t
[2];