Merge remote-tracking branch 'upstream/edge' into upstream-master
[clinton/Smoothieware.git] / src / modules / tools / endstops / Endstops.cpp
CommitLineData
df27a6a3 1/*
201bcb94
AW
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.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
201bcb94
AW
6*/
7
8#include "libs/Module.h"
9#include "libs/Kernel.h"
10#include "modules/communication/utils/Gcode.h"
3fceb8eb 11#include "modules/robot/Conveyor.h"
201bcb94
AW
12#include "Endstops.h"
13#include "libs/nuts_bolts.h"
750277f8 14#include "libs/Pin.h"
670fa10b 15#include "libs/StepperMotor.h"
201bcb94 16#include "wait_api.h" // mbed.h lib
61134a65
JM
17#include "Robot.h"
18#include "Stepper.h"
19#include "Config.h"
20#include "SlowTicker.h"
21#include "Planner.h"
7af0714f
JM
22#include "checksumm.h"
23#include "utils.h"
8d54c34c 24#include "ConfigValue.h"
fc7b9a7b 25#include "libs/StreamOutput.h"
9f6f04a5
JM
26#include "PublicDataRequest.h"
27#include "EndstopsPublicAccess.h"
3c947f85 28#include "StreamOutputPool.h"
a157d099 29#include "StepTicker.h"
7552475b 30#include "BaseSolution.h"
201bcb94 31
80605954
JM
32#include <ctype.h>
33
33e4cc02
JM
34#define ALPHA_AXIS 0
35#define BETA_AXIS 1
36#define GAMMA_AXIS 2
9a993543
JM
37#define X_AXIS 0
38#define Y_AXIS 1
39#define Z_AXIS 2
33e4cc02 40
33e4cc02
JM
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")
3e1f5b74 44#define scara_homing_checksum CHECKSUM("scara_homing")
33e4cc02
JM
45
46#define alpha_min_endstop_checksum CHECKSUM("alpha_min_endstop")
47#define beta_min_endstop_checksum CHECKSUM("beta_min_endstop")
48#define gamma_min_endstop_checksum CHECKSUM("gamma_min_endstop")
49
50#define alpha_max_endstop_checksum CHECKSUM("alpha_max_endstop")
51#define beta_max_endstop_checksum CHECKSUM("beta_max_endstop")
52#define gamma_max_endstop_checksum CHECKSUM("gamma_max_endstop")
53
54#define alpha_trim_checksum CHECKSUM("alpha_trim")
55#define beta_trim_checksum CHECKSUM("beta_trim")
56#define gamma_trim_checksum CHECKSUM("gamma_trim")
57
58// these values are in steps and should be deprecated
59#define alpha_fast_homing_rate_checksum CHECKSUM("alpha_fast_homing_rate")
60#define beta_fast_homing_rate_checksum CHECKSUM("beta_fast_homing_rate")
61#define gamma_fast_homing_rate_checksum CHECKSUM("gamma_fast_homing_rate")
62
63#define alpha_slow_homing_rate_checksum CHECKSUM("alpha_slow_homing_rate")
64#define beta_slow_homing_rate_checksum CHECKSUM("beta_slow_homing_rate")
65#define gamma_slow_homing_rate_checksum CHECKSUM("gamma_slow_homing_rate")
66
67#define alpha_homing_retract_checksum CHECKSUM("alpha_homing_retract")
68#define beta_homing_retract_checksum CHECKSUM("beta_homing_retract")
69#define gamma_homing_retract_checksum CHECKSUM("gamma_homing_retract")
33e4cc02
JM
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
3c947f85
JM
99#define alpha_limit_enable_checksum CHECKSUM("alpha_limit_enable")
100#define beta_limit_enable_checksum CHECKSUM("beta_limit_enable")
101#define gamma_limit_enable_checksum CHECKSUM("gamma_limit_enable")
102
80605954 103#define homing_order_checksum CHECKSUM("homing_order")
2ddf75fd 104#define move_to_origin_checksum CHECKSUM("move_to_origin_after_home")
80605954 105
dd0a7cfa
JM
106#define STEPPER THEKERNEL->robot->actuators
107#define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
56ce2b5a 108
2b3cedc7 109
3c947f85
JM
110// Homing States
111enum{
2ddf75fd
JM
112 MOVING_TO_ENDSTOP_FAST, // homing move
113 MOVING_BACK, // homing move
114 MOVING_TO_ENDSTOP_SLOW, // homing move
fafb45df 115 NOT_HOMING,
3c947f85 116 BACK_OFF_HOME,
2ddf75fd 117 MOVE_TO_ORIGIN,
3c947f85
JM
118 LIMIT_TRIGGERED
119};
120
33e4cc02
JM
121Endstops::Endstops()
122{
201bcb94 123 this->status = NOT_HOMING;
33e4cc02 124 home_offset[0] = home_offset[1] = home_offset[2] = 0.0F;
201bcb94
AW
125}
126
33e4cc02
JM
127void Endstops::on_module_loaded()
128{
7dee696d 129 // Do not do anything if not enabled
314ab8f7 130 if ( THEKERNEL->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) {
4e722c00 131 delete this;
33e4cc02
JM
132 return;
133 }
7d62445b 134
9f6f04a5
JM
135 register_for_event(ON_GCODE_RECEIVED);
136 register_for_event(ON_GET_PUBLIC_DATA);
7d6fe308 137 register_for_event(ON_SET_PUBLIC_DATA);
201bcb94 138
a157d099 139 THEKERNEL->step_ticker->register_acceleration_tick_handler([this](){acceleration_tick(); });
201bcb94 140
750277f8
AW
141 // Settings
142 this->on_config_reload(this);
201bcb94
AW
143}
144
750277f8 145// Get config
33e4cc02
JM
146void Endstops::on_config_reload(void *argument)
147{
56ce2b5a
JM
148 this->pins[0].from_string( THEKERNEL->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
149 this->pins[1].from_string( THEKERNEL->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
150 this->pins[2].from_string( THEKERNEL->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
151 this->pins[3].from_string( THEKERNEL->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
152 this->pins[4].from_string( THEKERNEL->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
153 this->pins[5].from_string( THEKERNEL->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
154
155 // These are the old ones in steps still here for backwards compatibility
156 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(0);
157 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(1);
158 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number() / STEPS_PER_MM(2);
159 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(0);
160 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(1);
161 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number() / STEPS_PER_MM(2);
162 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(0);
163 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(1);
164 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number() / STEPS_PER_MM(2);
5de98d7c
JM
165
166 // 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
56ce2b5a
JM
167 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0])->as_number();
168 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[1])->as_number();
169 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2])->as_number();
170 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0])->as_number();
171 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[1])->as_number();
172 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2])->as_number();
173 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum )->by_default(this->retract_mm[0])->as_number();
174 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum )->by_default(this->retract_mm[1])->as_number();
175 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum )->by_default(this->retract_mm[2])->as_number();
5de98d7c 176
06c48ee8 177 this->debounce_count = THEKERNEL->config->value(endstop_debounce_count_checksum )->by_default(100)->as_number();
5de98d7c 178
409ff5b3 179 // get homing direction and convert to boolean where true is home to min, and false is home to max
314ab8f7 180 int home_dir = get_checksum(THEKERNEL->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 181 this->home_direction[0] = home_dir != home_to_max_checksum;
47bbe224 182
314ab8f7 183 home_dir = get_checksum(THEKERNEL->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 184 this->home_direction[1] = home_dir != home_to_max_checksum;
47bbe224 185
314ab8f7 186 home_dir = get_checksum(THEKERNEL->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 187 this->home_direction[2] = home_dir != home_to_max_checksum;
47bbe224 188
314ab8f7 189 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();
f3b66360
JM
190 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();
191 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();
f29b0272 192
314ab8f7
MM
193 this->is_corexy = THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool();
194 this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool();
504f0e3e
JM
195 this->is_scara = THEKERNEL->config->value(scara_homing_checksum)->by_default(false)->as_bool();
196
80605954
JM
197 // see if an order has been specified, must be three characters, XYZ or YXZ etc
198 string order= THEKERNEL->config->value(homing_order_checksum)->by_default("")->as_string();
199 this->homing_order= 0;
200 if(order.size() == 3 && !this->is_delta) {
201 int shift= 0;
202 for(auto c : order) {
203 uint8_t i= toupper(c) - 'X';
204 if(i > 2) { // bad value
205 this->homing_order= 0;
206 break;
207 }
208 homing_order |= (i << shift);
209 shift += 2;
210 }
211 }
212
192bca32
JM
213 // endstop trim used by deltas to do soft adjusting
214 // on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
215 this->trim_mm[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0 )->as_number();
216 this->trim_mm[1] = THEKERNEL->config->value(beta_trim_checksum )->by_default(0 )->as_number();
217 this->trim_mm[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0 )->as_number();
3c947f85
JM
218
219 // limits enabled
220 this->limit_enable[X_AXIS]= THEKERNEL->config->value(alpha_limit_enable_checksum)->by_default(false)->as_bool();
221 this->limit_enable[Y_AXIS]= THEKERNEL->config->value(beta_limit_enable_checksum)->by_default(false)->as_bool();
222 this->limit_enable[Z_AXIS]= THEKERNEL->config->value(gamma_limit_enable_checksum)->by_default(false)->as_bool();
223
2ddf75fd 224 this->move_to_origin_after_home= THEKERNEL->config->value(move_to_origin_checksum)->by_default(false)->as_bool();
2b3cedc7 225
3c947f85
JM
226 if(this->limit_enable[X_AXIS] || this->limit_enable[Y_AXIS] || this->limit_enable[Z_AXIS]){
227 register_for_event(ON_IDLE);
7f3e6350
JM
228 if(this->is_delta) {
229 // we must enable all the limits not just one
230 this->limit_enable[X_AXIS]= true;
231 this->limit_enable[Y_AXIS]= true;
232 this->limit_enable[Z_AXIS]= true;
233 }
3c947f85 234 }
44127aca
JM
235
236 // NOTE this may also be true of scara. TBD
237 if(this->is_delta) {
238 // some things must be the same or they will die, so force it here to avoid config errors
239 this->fast_rates[1]= this->fast_rates[2]= this->fast_rates[0];
240 this->slow_rates[1]= this->slow_rates[2]= this->slow_rates[0];
241 this->retract_mm[1]= this->retract_mm[2]= this->retract_mm[0];
242 this->home_direction[1]= this->home_direction[2]= this->home_direction[0];
243 this->homing_position[0]= this->homing_position[1]= 0;
44127aca 244 }
3c947f85
JM
245}
246
28166daf
JM
247bool Endstops::debounced_get(int pin)
248{
249 uint8_t debounce= 0;
250 while(this->pins[pin].get()) {
251 if ( ++debounce >= this->debounce_count ) {
252 // pin triggered
253 return true;
254 }
255 }
256 return false;
257}
258
4d149ebb 259static const char *endstop_names[]= {"min_x", "min_y", "min_z", "max_x", "max_y", "max_z"};
3c947f85
JM
260
261void Endstops::on_idle(void *argument)
262{
c3d7feb6
JM
263 if(this->status == LIMIT_TRIGGERED) {
264 // if we were in limit triggered see if it has been cleared
265 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
266 if(this->limit_enable[c]) {
267 std::array<int, 2> minmax{{0, 3}};
268 // check min and max endstops
269 for (int i : minmax) {
270 int n= c+i;
271 if(this->pins[n].get()) {
272 // still triggered, so exit
ee1c3181 273 bounce_cnt= 0;
c3d7feb6
JM
274 return;
275 }
276 }
277 }
278 }
ee1c3181
JM
279 if(++bounce_cnt > 10) { // can use less as it calls on_idle in between
280 // clear the state
281 this->status= NOT_HOMING;
282 }
4befe777 283 return;
c3d7feb6
JM
284
285 }else if(this->status != NOT_HOMING) {
286 // don't check while homing
287 return;
288 }
3c947f85
JM
289
290 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
291 if(this->limit_enable[c] && STEPPER[c]->is_moving()) {
292 std::array<int, 2> minmax{{0, 3}};
293 // check min and max endstops
294 for (int i : minmax) {
295 int n= c+i;
28166daf
JM
296 if(debounced_get(n)) {
297 // endstop triggered
298 THEKERNEL->streams->printf("Limit switch %s was hit - reset or M999 required\n", endstop_names[n]);
299 this->status= LIMIT_TRIGGERED;
300 // disables heaters and motors, ignores incoming Gcode and flushes block queue
301 THEKERNEL->call_event(ON_HALT, nullptr);
302 return;
3c947f85
JM
303 }
304 }
305 }
306 }
307}
308
309// if limit switches are enabled, then we must move off of the endstop otherwise we won't be able to move
5bfcd44a
JM
310// checks if triggered and only backs off if triggered
311void Endstops::back_off_home(char axes_to_move)
3c947f85 312{
7c801094 313 std::vector<std::pair<char,float>> params;
3c947f85 314 this->status = BACK_OFF_HOME;
7f3e6350
JM
315
316 // these are handled differently
44127aca 317 if((is_delta || is_scara) && this->limit_enable[X_AXIS]) {
7f3e6350 318 // Move off of the endstop using a regular relative move in Z only
7c801094 319 params.push_back({'Z', this->retract_mm[Z_AXIS]*(this->home_direction[Z_AXIS]?1:-1)});
44127aca
JM
320
321 }else{
7f3e6350 322 // cartesians, concatenate all the moves we need to do into one gcode
44127aca
JM
323 for( int c = X_AXIS; c <= Z_AXIS; c++ ) {
324 if( ((axes_to_move >> c ) & 1) == 0) continue; // only for axes we asked to move
7f3e6350
JM
325
326 // if not triggered no need to move off
28166daf 327 if(this->limit_enable[c] && debounced_get(c + (this->home_direction[c] ? 0 : 3)) ) {
7c801094 328 params.push_back({c+'X', this->retract_mm[c]*(this->home_direction[c]?1:-1)});
44127aca 329 }
3c947f85
JM
330 }
331 }
7f3e6350
JM
332
333 if(!params.empty()) {
334 // Move off of the endstop using a regular relative move
7c801094 335 params.insert(params.begin(), {'G', 0});
7f3e6350 336 // use X slow rate to move, Z should have a max speed set anyway
7c801094
JM
337 params.push_back({'F', this->slow_rates[X_AXIS]*60.0F});
338 char gcode_buf[64];
339 append_parameters(gcode_buf, params, sizeof(gcode_buf));
7f3e6350
JM
340 Gcode gc(gcode_buf, &(StreamOutput::NullStream));
341 bool oldmode= THEKERNEL->robot->absolute_mode;
342 THEKERNEL->robot->absolute_mode= false; // needs to be relative mode
343 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
344 THEKERNEL->robot->absolute_mode= oldmode; // restore mode
345 // Wait for above to finish
346 THEKERNEL->conveyor->wait_for_empty_queue();
347 }
348
3c947f85 349 this->status = NOT_HOMING;
a0e0d592
BG
350}
351
2b3cedc7 352// If enabled will move the head to 0,0 after homing, but only if X and Y were set to home
2ddf75fd 353void Endstops::move_to_origin(char axes_to_move)
2b3cedc7
JM
354{
355 if( (axes_to_move&0x03) != 3 ) return; // ignore if X and Y not homing
356
fafb45df
JM
357 // Do we need to check if we are already at 0,0? probably not as the G0 will not do anything if we are
358 // float pos[3]; THEKERNEL->robot->get_axis_position(pos); if(pos[0] == 0 && pos[1] == 0) return;
359
2ddf75fd 360 this->status = MOVE_TO_ORIGIN;
2b3cedc7 361 // Move to center using a regular move, use slower of X and Y fast rate
2955fdbb 362 float rate= std::min(this->fast_rates[0], this->fast_rates[1])*60.0F;
2b3cedc7
JM
363 char buf[32];
364 snprintf(buf, sizeof(buf), "G0 X0 Y0 F%1.4f", rate);
365 Gcode gc(buf, &(StreamOutput::NullStream));
366 THEKERNEL->robot->on_gcode_received(&gc); // send to robot directly
367
368 // Wait for above to finish
369 THEKERNEL->conveyor->wait_for_empty_queue();
370 this->status = NOT_HOMING;
371}
372
798295c1 373bool Endstops::wait_for_homed(char axes_to_move)
33e4cc02 374{
a0e0d592 375 bool running = true;
33e4cc02
JM
376 unsigned int debounce[3] = {0, 0, 0};
377 while (running) {
a0e0d592 378 running = false;
314ab8f7 379 THEKERNEL->call_event(ON_IDLE);
798295c1
JM
380
381 // check if on_halt (eg kill)
73706276 382 if(THEKERNEL->is_halted()) return false;
798295c1 383
56ce2b5a
JM
384 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
385 if ( ( axes_to_move >> c ) & 1 ) {
386 if ( this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) {
387 if ( debounce[c] < debounce_count ) {
388 debounce[c]++;
a0e0d592 389 running = true;
dd0a7cfa
JM
390 } else if ( STEPPER[c]->is_moving() ) {
391 STEPPER[c]->move(0, 0);
e0b58122 392 axes_to_move &= ~(1<<c); // no need to check it again
a0e0d592 393 }
33e4cc02 394 } else {
a0e0d592
BG
395 // The endstop was not hit yet
396 running = true;
56ce2b5a 397 debounce[c] = 0;
323cca60 398 }
a0e0d592
BG
399 }
400 }
401 }
798295c1 402 return true;
750277f8 403}
201bcb94 404
81f02e89 405void Endstops::do_homing_cartesian(char axes_to_move)
33e4cc02 406{
798295c1 407 // check if on_halt (eg kill)
73706276 408 if(THEKERNEL->is_halted()) return;
798295c1 409
80605954 410 // this homing works for cartesian and delta printers
f29b0272 411 // Start moving the axes to the origin
2ddf75fd 412 this->status = MOVING_TO_ENDSTOP_FAST;
56ce2b5a
JM
413 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
414 if ( ( axes_to_move >> c) & 1 ) {
415 this->feed_rate[c]= this->fast_rates[c];
6f6677fc 416 STEPPER[c]->move(this->home_direction[c], 10000000, 0);
f29b0272
JM
417 }
418 }
419
420 // Wait for all axes to have homed
798295c1 421 if(!this->wait_for_homed(axes_to_move)) return;
f29b0272
JM
422
423 // Move back a small distance
424 this->status = MOVING_BACK;
425 bool inverted_dir;
56ce2b5a
JM
426 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
427 if ( ( axes_to_move >> c ) & 1 ) {
428 inverted_dir = !this->home_direction[c];
429 this->feed_rate[c]= this->slow_rates[c];
6f6677fc 430 STEPPER[c]->move(inverted_dir, this->retract_mm[c]*STEPS_PER_MM(c), 0);
f29b0272
JM
431 }
432 }
433
33e4cc02 434 // Wait for moves to be done
56ce2b5a
JM
435 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
436 if ( ( axes_to_move >> c ) & 1 ) {
dd0a7cfa 437 while ( STEPPER[c]->is_moving() ) {
314ab8f7 438 THEKERNEL->call_event(ON_IDLE);
ca287785 439 if(THEKERNEL->is_halted()) return;
f29b0272
JM
440 }
441 }
442 }
443
444 // Start moving the axes to the origin slowly
2ddf75fd 445 this->status = MOVING_TO_ENDSTOP_SLOW;
56ce2b5a
JM
446 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
447 if ( ( axes_to_move >> c ) & 1 ) {
448 this->feed_rate[c]= this->slow_rates[c];
6f6677fc 449 STEPPER[c]->move(this->home_direction[c], 10000000, 0);
f29b0272
JM
450 }
451 }
452
453 // Wait for all axes to have homed
798295c1 454 if(!this->wait_for_homed(axes_to_move)) return;
f29b0272
JM
455}
456
798295c1 457bool Endstops::wait_for_homed_corexy(int axis)
33e4cc02 458{
3db88866 459 bool running = true;
33e4cc02
JM
460 unsigned int debounce[3] = {0, 0, 0};
461 while (running) {
3db88866 462 running = false;
314ab8f7 463 THEKERNEL->call_event(ON_IDLE);
798295c1
JM
464
465 // check if on_halt (eg kill)
73706276 466 if(THEKERNEL->is_halted()) return false;
798295c1 467
33e4cc02
JM
468 if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) {
469 if ( debounce[axis] < debounce_count ) {
3db88866
JM
470 debounce[axis] ++;
471 running = true;
472 } else {
473 // turn both off if running
dd0a7cfa
JM
474 if (STEPPER[X_AXIS]->is_moving()) STEPPER[X_AXIS]->move(0, 0);
475 if (STEPPER[Y_AXIS]->is_moving()) STEPPER[Y_AXIS]->move(0, 0);
3db88866 476 }
33e4cc02 477 } else {
3db88866
JM
478 // The endstop was not hit yet
479 running = true;
480 debounce[axis] = 0;
481 }
482 }
798295c1 483 return true;
3db88866
JM
484}
485
1ad23cd3 486void Endstops::corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps)
33e4cc02 487{
798295c1 488 // check if on_halt (eg kill)
73706276 489 if(THEKERNEL->is_halted()) return;
798295c1 490
2ddf75fd 491 this->status = MOVING_TO_ENDSTOP_FAST;
64eaf21e 492 this->feed_rate[X_AXIS]= fast_rate;
6f6677fc 493 STEPPER[X_AXIS]->move(dirx, 10000000, 0);
64eaf21e 494 this->feed_rate[Y_AXIS]= fast_rate;
6f6677fc 495 STEPPER[Y_AXIS]->move(diry, 10000000, 0);
f29b0272 496
33e4cc02 497 // wait for primary axis
798295c1 498 if(!this->wait_for_homed_corexy(home_axis)) return;
f29b0272 499
33e4cc02
JM
500 // Move back a small distance
501 this->status = MOVING_BACK;
64eaf21e 502 this->feed_rate[X_AXIS]= slow_rate;
6f6677fc 503 STEPPER[X_AXIS]->move(!dirx, retract_steps, 0);
64eaf21e 504 this->feed_rate[Y_AXIS]= slow_rate;
6f6677fc 505 STEPPER[Y_AXIS]->move(!diry, retract_steps, 0);
33e4cc02
JM
506
507 // wait until done
dd0a7cfa 508 while ( STEPPER[X_AXIS]->is_moving() || STEPPER[Y_AXIS]->is_moving()) {
314ab8f7 509 THEKERNEL->call_event(ON_IDLE);
ca287785 510 if(THEKERNEL->is_halted()) return;
f29b0272
JM
511 }
512
33e4cc02 513 // Start moving the axes to the origin slowly
2ddf75fd 514 this->status = MOVING_TO_ENDSTOP_SLOW;
64eaf21e 515 this->feed_rate[X_AXIS]= slow_rate;
6f6677fc 516 STEPPER[X_AXIS]->move(dirx, 10000000, 0);
64eaf21e 517 this->feed_rate[Y_AXIS]= slow_rate;
6f6677fc 518 STEPPER[Y_AXIS]->move(diry, 10000000, 0);
f29b0272 519
33e4cc02 520 // wait for primary axis
798295c1 521 if(!this->wait_for_homed_corexy(home_axis)) return;
33e4cc02 522}
f29b0272 523
33e4cc02
JM
524// this homing works for HBots/CoreXY
525void Endstops::do_homing_corexy(char axes_to_move)
526{
03dffc07 527 // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally
174d9961 528 // 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
03dffc07 529 // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed
174d9961 530 // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit
174d9961
JM
531
532 if((axes_to_move & 0x03) == 0x03) { // both X and Y need Homing
533 // determine which motor to turn and which way
534 bool dirx= this->home_direction[X_AXIS];
535 bool diry= this->home_direction[Y_AXIS];
536 int motor;
537 bool dir;
538 if(dirx && diry) { // min/min
539 motor= X_AXIS;
540 dir= true;
541 }else if(dirx && !diry) { // min/max
542 motor= Y_AXIS;
543 dir= true;
544 }else if(!dirx && diry) { // max/min
545 motor= Y_AXIS;
546 dir= false;
547 }else if(!dirx && !diry) { // max/max
548 motor= X_AXIS;
549 dir= false;
550 }
551
552 // then move both X and Y until one hits the endstop
2ddf75fd 553 this->status = MOVING_TO_ENDSTOP_FAST;
6f6677fc 554 // need to allow for more ground covered when moving diagonally
64eaf21e 555 this->feed_rate[motor]= this->fast_rates[motor]*1.4142;
6f6677fc 556 STEPPER[motor]->move(dir, 10000000, 0);
174d9961
JM
557 // wait until either X or Y hits the endstop
558 bool running= true;
559 while (running) {
314ab8f7 560 THEKERNEL->call_event(ON_IDLE);
ca287785 561 if(THEKERNEL->is_halted()) return;
174d9961
JM
562 for(int m=X_AXIS;m<=Y_AXIS;m++) {
563 if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) {
564 // turn off motor
dd0a7cfa 565 if(STEPPER[motor]->is_moving()) STEPPER[motor]->move(0, 0);
174d9961
JM
566 running= false;
567 break;
568 }
569 }
570 }
571 }
33e4cc02 572
174d9961 573 // move individual axis
33e4cc02 574 if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
413dd90c 575 bool dir= this->home_direction[X_AXIS];
56ce2b5a 576 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));
f29b0272
JM
577 }
578
65191d91
JM
579 if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
580 bool dir= this->home_direction[Y_AXIS];
56ce2b5a 581 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));
65191d91
JM
582 }
583
33e4cc02 584 if (axes_to_move & 0x04) { // move Z
81f02e89 585 do_homing_cartesian(0x04); // just home normally for Z
f29b0272 586 }
f29b0272
JM
587}
588
81f02e89
JM
589void Endstops::home(char axes_to_move)
590{
72833629
JM
591 // not a block move so disable the last tick setting
592 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
593 STEPPER[c]->set_moved_last_block(false);
594 }
595
81f02e89
JM
596 if (is_corexy){
597 // corexy/HBot homing
598 do_homing_corexy(axes_to_move);
599 }else{
600 // cartesian/delta homing
601 do_homing_cartesian(axes_to_move);
602 }
0058d8d4
JM
603
604 // make sure all steppers are off (especially if aborted)
605 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
606 STEPPER[c]->move(0, 0);
607 }
ca287785 608 this->status = NOT_HOMING;
81f02e89
JM
609}
610
201bcb94 611// Start homing sequences by response to GCode commands
33e4cc02 612void Endstops::on_gcode_received(void *argument)
c339d634 613{
33e4cc02
JM
614 Gcode *gcode = static_cast<Gcode *>(argument);
615 if ( gcode->has_g) {
616 if ( gcode->g == 28 ) {
6e92ab91 617
df27a6a3 618 // G28 is received, we have homing to do
201bcb94
AW
619
620 // First wait for the queue to be empty
314ab8f7 621 THEKERNEL->conveyor->wait_for_empty_queue();
201bcb94
AW
622
623 // Do we move select axes or all of them
7dee696d 624 char axes_to_move = 0;
3e1f5b74
QH
625 // only enable homing if the endstop is defined, deltas, scaras always home all axis
626 bool home_all = this->is_delta || this->is_scara || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') );
47bbe224 627
56ce2b5a
JM
628 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
629 if ( (home_all || gcode->has_letter(c+'X')) && this->pins[c + (this->home_direction[c] ? 0 : 3)].connected() ) {
630 axes_to_move += ( 1 << c );
33e4cc02 631 }
df27a6a3 632 }
3b948656 633
f6c04440 634 // Enable the motors
314ab8f7 635 THEKERNEL->stepper->turn_enable_pins_on();
f6c04440 636
f29b0272 637 // do the actual homing
80605954
JM
638 if(homing_order != 0){
639 // if an order has been specified do it in the specified order
640 // homing order is 0b00ccbbaa where aa is 0,1,2 to specify the first axis, bb is the second and cc is the third
641 // eg 0b00100001 would be Y X Z, 0b00100100 would be X Y Z
642 for (uint8_t m = homing_order; m != 0; m >>= 2) {
643 int a= (1 << (m & 0x03)); // axis to move
798295c1 644 if((a & axes_to_move) != 0){
81f02e89 645 home(a);
798295c1
JM
646 }
647 // check if on_halt (eg kill)
aa896868 648 if(THEKERNEL->is_halted()) break;
80605954 649 }
798295c1 650
80605954
JM
651 }else {
652 // they all home at the same time
81f02e89 653 home(axes_to_move);
80605954 654 }
3ffe27fb 655
798295c1 656 // check if on_halt (eg kill)
aa896868
JM
657 if(THEKERNEL->is_halted()){
658 THEKERNEL->streams->printf("Homing cycle aborted by kill\n");
659 return;
660 }
798295c1 661
cef9acea
JM
662 if(home_all) {
663 // for deltas this may be important rather than setting each individually
7552475b
OL
664
665 // Here's where we would have been if the endstops were perfectly trimmed
666 float ideal_position[3] = {
cef9acea
JM
667 this->homing_position[X_AXIS] + this->home_offset[X_AXIS],
668 this->homing_position[Y_AXIS] + this->home_offset[Y_AXIS],
7552475b
OL
669 this->homing_position[Z_AXIS] + this->home_offset[Z_AXIS]
670 };
671
672 bool has_endstop_trim = this->is_delta || this->is_scara;
673 if (has_endstop_trim) {
674 float ideal_actuator_position[3];
675 THEKERNEL->robot->arm_solution->cartesian_to_actuator(ideal_position, ideal_actuator_position);
676
677 // We are actually not at the ideal position, but a trim away
678 float real_actuator_position[3] = {
679 ideal_actuator_position[X_AXIS] - this->trim_mm[X_AXIS],
680 ideal_actuator_position[Y_AXIS] - this->trim_mm[Y_AXIS],
681 ideal_actuator_position[Z_AXIS] - this->trim_mm[Z_AXIS]
682 };
683
684 float real_position[3];
685 THEKERNEL->robot->arm_solution->actuator_to_cartesian(real_actuator_position, real_position);
686 // Reset the actuator positions to correspond our real position
687 THEKERNEL->robot->reset_axis_position(real_position[0], real_position[1], real_position[2]);
688 } else {
689 // without endstop trim, real_position == ideal_position
690 // Reset the actuator positions to correspond our real position
691 THEKERNEL->robot->reset_axis_position(ideal_position[0], ideal_position[1], ideal_position[2]);
692 }
693 } else {
cef9acea
JM
694 // Zero the ax(i/e)s position, add in the home offset
695 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
696 if ( (axes_to_move >> c) & 1 ) {
697 THEKERNEL->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c);
698 }
3ffe27fb
AV
699 }
700 }
3c947f85 701
2b3cedc7
JM
702 // on some systems where 0,0 is bed center it is noce to have home goto 0,0 after homing
703 // default is off
7942fa09 704 if(!is_delta && this->move_to_origin_after_home) move_to_origin(axes_to_move);
2b3cedc7 705
3c947f85 706 // if limit switches are enabled we must back off endstop after setting home
5bfcd44a 707 back_off_home(axes_to_move);
7942fa09
JM
708
709 // deltas are not left at 0,0 becuase of the trim settings, so move to 0,0 if requested
710 if(is_delta && this->move_to_origin_after_home) move_to_origin(axes_to_move);
c339d634 711 }
81f02e89 712
33e4cc02
JM
713 } else if (gcode->has_m) {
714 switch (gcode->m) {
715 case 119: {
ef7bd372
JM
716 for (int i = 0; i < 6; ++i) {
717 if(this->pins[i].connected())
718 gcode->stream->printf("%s:%d ", endstop_names[i], this->pins[i].get());
719 }
bd96f4d7 720 gcode->add_nl= true;
6e92ab91 721
33e4cc02
JM
722 }
723 break;
724
725 case 206: // M206 - set homing offset
504f0e3e
JM
726 if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X');
727 if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y');
728 if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z');
729 gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
6e92ab91 730
504f0e3e 731 break;
0e4bf280 732
9ae8c7e4 733 case 306: // Similar to M206 and G92 but sets Homing offsets based on current position, Would be M207 but that is taken
504f0e3e
JM
734 {
735 float cartesian[3];
0e4bf280 736 THEKERNEL->robot->get_axis_position(cartesian); // get actual position from robot
9ae8c7e4
JM
737 if (gcode->has_letter('X')){
738 home_offset[0] -= (cartesian[X_AXIS] - gcode->get_value('X'));
739 THEKERNEL->robot->reset_axis_position(gcode->get_value('X'), X_AXIS);
740 }
741 if (gcode->has_letter('Y')) {
742 home_offset[1] -= (cartesian[Y_AXIS] - gcode->get_value('Y'));
743 THEKERNEL->robot->reset_axis_position(gcode->get_value('Y'), Y_AXIS);
744 }
745 if (gcode->has_letter('Z')) {
746 home_offset[2] -= (cartesian[Z_AXIS] - gcode->get_value('Z'));
747 THEKERNEL->robot->reset_axis_position(gcode->get_value('Z'), Z_AXIS);
748 }
749
750 gcode->stream->printf("Homing Offset: X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
6e92ab91 751
0e4bf280 752 }
33e4cc02
JM
753 break;
754
755 case 500: // save settings
756 case 503: // print settings
757 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]);
d0280b9d 758 if (this->is_delta || this->is_scara) {
56ce2b5a
JM
759 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]);
760 gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position[2]);
7a8fe6e0 761 }
c339d634 762 break;
47bbe224 763
ec4773e5 764 case 665: { // M665 - set max gamma/z height
6e92ab91 765
1ad23cd3 766 float gamma_max = this->homing_position[2];
33e4cc02
JM
767 if (gcode->has_letter('Z')) {
768 this->homing_position[2] = gamma_max = gcode->get_value('Z');
ec4773e5 769 }
33e4cc02
JM
770 gcode->stream->printf("Max Z %8.3f ", gamma_max);
771 gcode->add_nl = true;
772 }
773 break;
ec4773e5 774
47bbe224 775
56ce2b5a 776 case 666:
3e1f5b74 777 if(this->is_delta || this->is_scara) { // M666 - set trim for each axis in mm, NB negative mm trim is down
56ce2b5a
JM
778 if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X');
779 if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y');
780 if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z');
47bbe224 781
56ce2b5a
JM
782 // print the current trim values in mm
783 gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
6e92ab91 784
56ce2b5a 785 }
33e4cc02 786 break;
47bbe224 787
d4ee6ee2 788 // NOTE this is to test accuracy of lead screws etc.
fc320ac5 789 case 1910: { // M1910 - move specific number of raw steps
b023850b
JM
790 if(gcode->subcode == 0) {
791 // Enable the motors
792 THEKERNEL->stepper->turn_enable_pins_on();
793
794 int x= 0, y=0 , z= 0, f= 200*16;
795 if (gcode->has_letter('F')) f = gcode->get_value('F');
796 if (gcode->has_letter('X')) {
797 x = gcode->get_value('X');
798 STEPPER[X_AXIS]->move(x<0, abs(x), f);
799 }
800 if (gcode->has_letter('Y')) {
801 y = gcode->get_value('Y');
802 STEPPER[Y_AXIS]->move(y<0, abs(y), f);
803 }
804 if (gcode->has_letter('Z')) {
805 z = gcode->get_value('Z');
806 STEPPER[Z_AXIS]->move(z<0, abs(z), f);
807 }
808 gcode->stream->printf("Moving X %d Y %d Z %d steps at F %d steps/sec\n", x, y, z, f);
809
810 }else if(gcode->subcode == 1) {
811 // stop any that are moving
812 for (int i = 0; i < 3; ++i) {
813 if(STEPPER[i]->is_moving()) STEPPER[i]->move(0, 0);
814 }
d4ee6ee2 815 }
d4ee6ee2
JM
816 break;
817 }
201bcb94
AW
818 }
819 }
820}
821
64eaf21e 822// Called periodically to change the speed to match acceleration
a157d099 823void Endstops::acceleration_tick(void)
64eaf21e 824{
a157d099 825 if(this->status >= NOT_HOMING) return; // nothing to do, only do this when moving for homing sequence
64eaf21e
JM
826
827 // foreach stepper that is moving
828 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
dd0a7cfa 829 if( !STEPPER[c]->is_moving() ) continue;
64eaf21e 830
dd0a7cfa 831 uint32_t current_rate = STEPPER[c]->get_steps_per_second();
c8f4ee77 832 uint32_t target_rate = floorf(this->feed_rate[c]*STEPS_PER_MM(c));
c5fe1787 833 float acc= (c==Z_AXIS) ? THEKERNEL->planner->get_z_acceleration() : THEKERNEL->planner->get_acceleration();
64eaf21e 834 if( current_rate < target_rate ){
a157d099 835 uint32_t rate_increase = floorf((acc/THEKERNEL->acceleration_ticks_per_second)*STEPS_PER_MM(c));
64eaf21e
JM
836 current_rate = min( target_rate, current_rate + rate_increase );
837 }
838 if( current_rate > target_rate ){ current_rate = target_rate; }
839
840 // steps per second
6f6677fc 841 STEPPER[c]->set_speed(current_rate);
64eaf21e
JM
842 }
843
a157d099 844 return;
64eaf21e 845}
9f6f04a5
JM
846
847void Endstops::on_get_public_data(void* argument){
848 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
849
850 if(!pdr->starts_with(endstops_checksum)) return;
851
852 if(pdr->second_element_is(trim_checksum)) {
86fa0b93 853 pdr->set_data_ptr(&this->trim_mm);
ea5c6d92
JM
854 pdr->set_taken();
855
856 }else if(pdr->second_element_is(home_offset_checksum)) {
86fa0b93 857 pdr->set_data_ptr(&this->home_offset);
9f6f04a5
JM
858 pdr->set_taken();
859 }
860}
7d6fe308
JM
861
862void Endstops::on_set_public_data(void* argument){
863 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
864
865 if(!pdr->starts_with(endstops_checksum)) return;
866
867 if(pdr->second_element_is(trim_checksum)) {
868 float *t= static_cast<float*>(pdr->get_data_ptr());
869 this->trim_mm[0]= t[0];
870 this->trim_mm[1]= t[1];
871 this->trim_mm[2]= t[2];
872 pdr->set_taken();
ea5c6d92
JM
873
874 }else if(pdr->second_element_is(home_offset_checksum)) {
875 float *t= static_cast<float*>(pdr->get_data_ptr());
876 if(!isnan(t[0])) this->home_offset[0]= t[0];
877 if(!isnan(t[1])) this->home_offset[1]= t[1];
878 if(!isnan(t[2])) this->home_offset[2]= t[2];
7d6fe308
JM
879 }
880}