Merge pull request #210 from wolfmanjm/feature/hbot-homing
[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
AW
16#include "wait_api.h" // mbed.h lib
17
18Endstops::Endstops(){
19 this->status = NOT_HOMING;
20}
21
22void Endstops::on_module_loaded() {
7dee696d 23 // Do not do anything if not enabled
7d62445b
L
24 if( this->kernel->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ){ return; }
25
476dcb96 26 register_for_event(ON_CONFIG_RELOAD);
201bcb94
AW
27 this->register_for_event(ON_GCODE_RECEIVED);
28
29 // Take StepperMotor objects from Robot and keep them here
30 this->steppers[0] = this->kernel->robot->alpha_stepper_motor;
31 this->steppers[1] = this->kernel->robot->beta_stepper_motor;
32 this->steppers[2] = this->kernel->robot->gamma_stepper_motor;
33
750277f8
AW
34 // Settings
35 this->on_config_reload(this);
c339d634 36
201bcb94
AW
37}
38
750277f8
AW
39// Get config
40void Endstops::on_config_reload(void* argument){
cc183397
A
41 this->pins[0].from_string( this->kernel->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
42 this->pins[1].from_string( this->kernel->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
43 this->pins[2].from_string( this->kernel->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
44 this->pins[3].from_string( this->kernel->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
45 this->pins[4].from_string( this->kernel->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
46 this->pins[5].from_string( this->kernel->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
5de98d7c
JM
47
48 // we need to know steps per mm for M206, also use them for all settings
47bbe224
JM
49 this->steps_per_mm[0] = this->kernel->config->value(alpha_steps_per_mm_checksum )->as_number();
50 this->steps_per_mm[1] = this->kernel->config->value(beta_steps_per_mm_checksum )->as_number();
51 this->steps_per_mm[2] = this->kernel->config->value(gamma_steps_per_mm_checksum )->as_number();
52
5de98d7c
JM
53 this->fast_rates[0] = this->kernel->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number();
54 this->fast_rates[1] = this->kernel->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number();
55 this->fast_rates[2] = this->kernel->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number();
56 this->slow_rates[0] = this->kernel->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number();
57 this->slow_rates[1] = this->kernel->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number();
58 this->slow_rates[2] = this->kernel->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number();
59 this->retract_steps[0] = this->kernel->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number();
60 this->retract_steps[1] = this->kernel->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number();
61 this->retract_steps[2] = this->kernel->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number();
62
63 // 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
64 this->fast_rates[0]= this->kernel->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0] / steps_per_mm[0])->as_number() * steps_per_mm[0];
65 this->fast_rates[1]= this->kernel->config->value(beta_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[1] / steps_per_mm[1])->as_number() * steps_per_mm[1];
66 this->fast_rates[2]= this->kernel->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2] / steps_per_mm[2])->as_number() * steps_per_mm[2];
67 this->slow_rates[0]= this->kernel->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0] / steps_per_mm[0])->as_number() * steps_per_mm[0];
68 this->slow_rates[1]= this->kernel->config->value(beta_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[1] / steps_per_mm[1])->as_number() * steps_per_mm[1];
69 this->slow_rates[2]= this->kernel->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2] / steps_per_mm[2])->as_number() * steps_per_mm[2];
70 this->retract_steps[0]= this->kernel->config->value(alpha_homing_retract_mm_checksum )->by_default(this->retract_steps[0]/steps_per_mm[0])->as_number() * steps_per_mm[0];
71 this->retract_steps[1]= this->kernel->config->value(beta_homing_retract_mm_checksum )->by_default(this->retract_steps[1]/steps_per_mm[1])->as_number() * steps_per_mm[1];
72 this->retract_steps[2]= this->kernel->config->value(gamma_homing_retract_mm_checksum )->by_default(this->retract_steps[2]/steps_per_mm[2])->as_number() * steps_per_mm[2];
73
74 // endstop trim used by deltas to do soft adjusting, in mm, convert to steps
75 this->trim[0] = this->kernel->config->value(alpha_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[0];
76 this->trim[1] = this->kernel->config->value(beta_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[0];
77 this->trim[2] = this->kernel->config->value(gamma_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[0];
78
79 this->debounce_count = this->kernel->config->value(endstop_debounce_count_checksum )->by_default(100)->as_number();
80
81
409ff5b3
JM
82 // get homing direction and convert to boolean where true is home to min, and false is home to max
83 int home_dir = get_checksum(this->kernel->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 84 this->home_direction[0] = home_dir != home_to_max_checksum;
47bbe224 85
409ff5b3 86 home_dir = get_checksum(this->kernel->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 87 this->home_direction[1] = home_dir != home_to_max_checksum;
47bbe224 88
409ff5b3 89 home_dir = get_checksum(this->kernel->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 90 this->home_direction[2] = home_dir != home_to_max_checksum;
47bbe224 91
409ff5b3
JM
92 this->homing_position[0] = this->home_direction[0]?this->kernel->config->value(alpha_min_checksum)->by_default(0)->as_number():this->kernel->config->value(alpha_max_checksum)->by_default(200)->as_number();
93 this->homing_position[1] = this->home_direction[1]?this->kernel->config->value(beta_min_checksum )->by_default(0)->as_number():this->kernel->config->value(beta_max_checksum )->by_default(200)->as_number();;
94 this->homing_position[2] = this->home_direction[2]?this->kernel->config->value(gamma_min_checksum)->by_default(0)->as_number():this->kernel->config->value(gamma_max_checksum)->by_default(200)->as_number();;
f29b0272
JM
95
96 this->is_corexy = this->kernel->config->value(corexy_homing_checksum)->by_default(false)->as_bool();
a0e0d592
BG
97}
98
3857da41 99void Endstops::wait_for_homed(char axes_to_move){
a0e0d592
BG
100 bool running = true;
101 unsigned int debounce[3] = {0,0,0};
102 while(running){
103 running = false;
f3e93777 104 this->kernel->call_event(ON_IDLE);
a0e0d592
BG
105 for( char c = 'X'; c <= 'Z'; c++ ){
106 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
409ff5b3 107 if( this->pins[c - 'X' + (this->home_direction[c - 'X']?0:3)].get() ){
a0e0d592
BG
108 if( debounce[c - 'X'] < debounce_count ) {
109 debounce[c - 'X'] ++;
110 running = true;
111 } else if ( this->steppers[c - 'X']->moving ){
112 this->steppers[c - 'X']->move(0,0);
a0e0d592
BG
113 }
114 }else{
115 // The endstop was not hit yet
116 running = true;
117 debounce[c - 'X'] = 0;
323cca60 118 }
a0e0d592
BG
119 }
120 }
121 }
750277f8 122}
201bcb94 123
f29b0272
JM
124// this homing works for cartesian and delta printers, not for HBots/CoreXY
125void Endstops::do_homing(char axes_to_move) {
126 // Start moving the axes to the origin
127 this->status = MOVING_TO_ORIGIN_FAST;
128 for( char c = 'X'; c <= 'Z'; c++ ){
129 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
130 this->steppers[c - 'X']->set_speed(this->fast_rates[c - 'X']);
131 this->steppers[c - 'X']->move(this->home_direction[c - 'X'],10000000);
132 }
133 }
134
135 // Wait for all axes to have homed
136 this->wait_for_homed(axes_to_move);
137
138 // Move back a small distance
139 this->status = MOVING_BACK;
140 bool inverted_dir;
141 for( char c = 'X'; c <= 'Z'; c++ ){
142 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
143 inverted_dir = !this->home_direction[c - 'X'];
144 this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']);
145 this->steppers[c - 'X']->move(inverted_dir,this->retract_steps[c - 'X']);
146 }
147 }
148
149 // Wait for moves to be done
150 for( char c = 'X'; c <= 'Z'; c++ ){
151 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
152 while( this->steppers[c - 'X']->moving ){
153 this->kernel->call_event(ON_IDLE);
154 }
155 }
156 }
157
158 // Start moving the axes to the origin slowly
159 this->status = MOVING_TO_ORIGIN_SLOW;
160 for( char c = 'X'; c <= 'Z'; c++ ){
161 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
162 this->steppers[c - 'X']->set_speed(this->slow_rates[c -'X']);
163 this->steppers[c - 'X']->move(this->home_direction[c - 'X'],10000000);
164 }
165 }
166
167 // Wait for all axes to have homed
168 this->wait_for_homed(axes_to_move);
169
170 // move for soft trim
171 this->status = MOVING_BACK;
172 for( char c = 'X'; c <= 'Z'; c++ ){
173 if( this->trim[c - 'X'] != 0 && ( axes_to_move >> ( c - 'X' ) ) & 1 ){
174 inverted_dir = !this->home_direction[c - 'X'];
175 // move up or down depending on sign of trim
176 if(this->trim[c - 'X'] < 0) inverted_dir= !inverted_dir;
177 this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']);
178 this->steppers[c - 'X']->move(inverted_dir,this->trim[c - 'X']);
179 }
180 }
181
182 // Wait for moves to be done
183 for( char c = 'X'; c <= 'Z'; c++ ){
184 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
0bbccf64 185 //this->kernel->streams->printf("axis %c \r\n", c );
f29b0272
JM
186 while( this->steppers[c - 'X']->moving ){
187 this->kernel->call_event(ON_IDLE);
188 }
189 }
190 }
191
192 // Homing is done
193 this->status = NOT_HOMING;
194}
195
196#define X_AXIS 0
197#define Y_AXIS 1
198#define Z_AXIS 2
199
3db88866
JM
200void Endstops::wait_for_homed_corexy(int axis){
201 bool running = true;
202 unsigned int debounce[3] = {0,0,0};
203 while(running){
204 running = false;
205 this->kernel->call_event(ON_IDLE);
206 if( this->pins[axis + (this->home_direction[axis]?0:3)].get() ){
207 if( debounce[axis] < debounce_count ) {
208 debounce[axis] ++;
209 running = true;
210 } else {
211 // turn both off if running
212 if(this->steppers[X_AXIS]->moving) this->steppers[X_AXIS]->move(0,0);
213 if(this->steppers[Y_AXIS]->moving) this->steppers[Y_AXIS]->move(0,0);
214 }
215 }else{
216 // The endstop was not hit yet
217 running = true;
218 debounce[axis] = 0;
219 }
220 }
221}
222
f29b0272
JM
223// this homing works for HBots/CoreXY
224void Endstops::do_homing_corexy(char axes_to_move) {
225 // Start moving the axes to the origin
226 if(axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
227 this->status = MOVING_TO_ORIGIN_FAST;
228 this->steppers[X_AXIS]->set_speed(this->fast_rates[X_AXIS]);
229 this->steppers[X_AXIS]->move(this->home_direction[X_AXIS], 10000000);
230 this->steppers[Y_AXIS]->set_speed(this->fast_rates[X_AXIS]);
231 this->steppers[Y_AXIS]->move(this->home_direction[X_AXIS], 10000000);
232
233 // wait for X
3db88866 234 this->wait_for_homed_corexy(X_AXIS);
f29b0272
JM
235
236 // Move back a small distance
237 this->status = MOVING_BACK;
238 this->steppers[X_AXIS]->set_speed(this->slow_rates[X_AXIS]);
239 this->steppers[X_AXIS]->move(!this->home_direction[X_AXIS], this->retract_steps[X_AXIS]);
240 this->steppers[Y_AXIS]->set_speed(this->slow_rates[X_AXIS]);
241 this->steppers[Y_AXIS]->move(!this->home_direction[X_AXIS], this->retract_steps[X_AXIS]);
242
243 // wait until done
244 while( this->steppers[X_AXIS]->moving ){ this->kernel->call_event(ON_IDLE); }
4e8346a4 245 while( this->steppers[Y_AXIS]->moving ){ this->kernel->call_event(ON_IDLE); }
f29b0272
JM
246
247 // Start moving the axes to the origin slowly
248 this->status = MOVING_TO_ORIGIN_SLOW;
249 this->steppers[X_AXIS]->set_speed(this->slow_rates[X_AXIS]);
250 this->steppers[X_AXIS]->move(this->home_direction[X_AXIS], 10000000);
251 this->steppers[Y_AXIS]->set_speed(this->slow_rates[X_AXIS]);
252 this->steppers[Y_AXIS]->move(this->home_direction[X_AXIS], 10000000);
253
254 // wait for X
3db88866 255 this->wait_for_homed_corexy(X_AXIS);
f29b0272
JM
256 }
257
258 if(axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
259 this->status = MOVING_TO_ORIGIN_FAST;
260 this->steppers[X_AXIS]->set_speed(this->fast_rates[Y_AXIS]);
261 this->steppers[X_AXIS]->move(this->home_direction[Y_AXIS], 10000000);
262 this->steppers[Y_AXIS]->set_speed(this->fast_rates[Y_AXIS]); // yes I use X_axis speed as they need to go at the same speed
263 this->steppers[Y_AXIS]->move(!this->home_direction[Y_AXIS], 10000000);
264
265 // wait for Y
3db88866 266 this->wait_for_homed_corexy(Y_AXIS);
f29b0272
JM
267
268 // Move back a small distance
269 this->status = MOVING_BACK;
270 this->steppers[X_AXIS]->set_speed(this->slow_rates[Y_AXIS]);
271 this->steppers[X_AXIS]->move(!this->home_direction[Y_AXIS], this->retract_steps[Y_AXIS]);
272 this->steppers[Y_AXIS]->set_speed(this->slow_rates[Y_AXIS]);
273 this->steppers[Y_AXIS]->move(this->home_direction[Y_AXIS], this->retract_steps[Y_AXIS]);
274
275 // wait until done
4e8346a4 276 while( this->steppers[X_AXIS]->moving ){ this->kernel->call_event(ON_IDLE); }
f29b0272
JM
277 while( this->steppers[Y_AXIS]->moving ){ this->kernel->call_event(ON_IDLE); }
278
279 // Start moving the axes to the origin slowly
280 this->status = MOVING_TO_ORIGIN_SLOW;
281 this->steppers[X_AXIS]->set_speed(this->slow_rates[Y_AXIS]);
282 this->steppers[X_AXIS]->move(this->home_direction[Y_AXIS], 10000000);
283 this->steppers[Y_AXIS]->set_speed(this->slow_rates[Y_AXIS]);
284 this->steppers[Y_AXIS]->move(!this->home_direction[Y_AXIS], 10000000);
285
286 // wait for Y
3db88866 287 this->wait_for_homed_corexy(Y_AXIS);
f29b0272
JM
288 }
289
290 if(axes_to_move & 0x04) { // move Z
291 do_homing(0x04); // just home normally for Z
292 }
293
294 // Homing is done
295 this->status = NOT_HOMING;
296}
297
201bcb94 298// Start homing sequences by response to GCode commands
c339d634
MM
299void Endstops::on_gcode_received(void* argument)
300{
201bcb94 301 Gcode* gcode = static_cast<Gcode*>(argument);
c339d634
MM
302 if( gcode->has_g)
303 {
304 if( gcode->g == 28 )
305 {
74b6303c 306 gcode->mark_as_taken();
df27a6a3 307 // G28 is received, we have homing to do
201bcb94
AW
308
309 // First wait for the queue to be empty
40de2562 310 this->kernel->conveyor->wait_for_empty_queue();
201bcb94
AW
311
312 // Do we move select axes or all of them
7dee696d
JM
313 char axes_to_move = 0;
314 // only enable homing if the endstop is defined
315 bool home_all= !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') );
47bbe224 316
3b948656 317 for( char c = 'X'; c <= 'Z'; c++ ){
60baf878 318 if( (home_all || gcode->has_letter(c)) && this->pins[c - 'X' + (this->home_direction[c - 'X']?0:3)].connected() ){ axes_to_move += ( 1 << (c - 'X' ) ); }
df27a6a3 319 }
3b948656 320
f6c04440
AW
321 // Enable the motors
322 this->kernel->stepper->turn_enable_pins_on();
323
f29b0272
JM
324 // do the actual homing
325 if(is_corexy)
326 do_homing_corexy(axes_to_move);
327 else
328 do_homing(axes_to_move);
3ffe27fb
AV
329
330 // Zero the ax(i/e)s position
331 for( char c = 'X'; c <= 'Z'; c++ ){
332 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
333
334 this->kernel->robot->reset_axis_position(this->homing_position[c - 'X'], c - 'X');
335 }
336 }
337
c339d634
MM
338 }
339 }
f3e93777
AW
340 else if (gcode->has_m){
341 switch(gcode->m){
c339d634 342 case 119:
7a8fe6e0
JM
343 {
344
345 int px= this->home_direction[0] ? 0 : 3;
346 int py= this->home_direction[1] ? 1 : 4;
347 int pz= this->home_direction[2] ? 2 : 5;
348 const char* mx= this->home_direction[0] ? "min" : "max";
349 const char* my= this->home_direction[1] ? "min" : "max";
350 const char* mz= this->home_direction[2] ? "min" : "max";
351
352 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());
353 gcode->mark_as_taken();
354 }
c339d634 355 break;
47bbe224
JM
356
357 case 206: // M206 - set trim for each axis in mm
358 {
7a8fe6e0
JM
359 int dirx= (this->home_direction[0] ? 1 : -1);
360 int diry= (this->home_direction[1] ? 1 : -1);
361 int dirz= (this->home_direction[2] ? 1 : -1);
47bbe224 362 double mm[3];
7a8fe6e0
JM
363 mm[0]= trim[0]/steps_per_mm[0] * dirx; // convert to mm
364 mm[1]= trim[1]/steps_per_mm[1] * diry;
365 mm[2]= trim[2]/steps_per_mm[2] * dirz;
47bbe224
JM
366
367 if(gcode->has_letter('X')) mm[0]= gcode->get_value('X');
368 if(gcode->has_letter('Y')) mm[1]= gcode->get_value('Y');
369 if(gcode->has_letter('Z')) mm[2]= gcode->get_value('Z');
370
7a8fe6e0
JM
371 trim[0]= lround(mm[0]*steps_per_mm[0]) * dirx; // convert back to steps
372 trim[1]= lround(mm[1]*steps_per_mm[1]) * diry;
373 trim[2]= lround(mm[2]*steps_per_mm[2]) * dirz;
47bbe224
JM
374
375 // print the current trim values in mm and steps
7a8fe6e0 376 char buf[64];
47bbe224
JM
377 int n= snprintf(buf, sizeof(buf), "X:%5.3f (%d) Y:%5.3f (%d) Z:%5.3f (%d) ", mm[0], trim[0], mm[1], trim[1], mm[2], trim[2]);
378 gcode->txt_after_ok.append(buf, n);
379 gcode->mark_as_taken();
380 }
381 break;
382
201bcb94
AW
383 }
384 }
385}
386