Commit | Line | Data |
---|---|---|
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 | ||
33e4cc02 JM |
18 | #define ALPHA_AXIS 0 |
19 | #define BETA_AXIS 1 | |
20 | #define GAMMA_AXIS 2 | |
9a993543 JM |
21 | #define X_AXIS 0 |
22 | #define Y_AXIS 1 | |
23 | #define Z_AXIS 2 | |
33e4cc02 JM |
24 | |
25 | #define NOT_HOMING 0 | |
26 | #define MOVING_TO_ORIGIN_FAST 1 | |
27 | #define MOVING_BACK 2 | |
28 | #define MOVING_TO_ORIGIN_SLOW 3 | |
29 | ||
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") | |
33 | ||
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") | |
37 | ||
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") | |
41 | ||
42 | #define alpha_trim_checksum CHECKSUM("alpha_trim") | |
43 | #define beta_trim_checksum CHECKSUM("beta_trim") | |
44 | #define gamma_trim_checksum CHECKSUM("gamma_trim") | |
45 | ||
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") | |
50 | ||
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") | |
54 | ||
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") | |
59 | ||
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") | |
64 | ||
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") | |
68 | ||
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") | |
72 | ||
73 | #define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count") | |
74 | ||
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") | |
83 | ||
84 | #define alpha_max_checksum CHECKSUM("alpha_max") | |
85 | #define beta_max_checksum CHECKSUM("beta_max") | |
86 | #define gamma_max_checksum CHECKSUM("gamma_max") | |
87 | ||
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") | |
91 | ||
92 | Endstops::Endstops() | |
93 | { | |
201bcb94 | 94 | this->status = NOT_HOMING; |
33e4cc02 | 95 | home_offset[0] = home_offset[1] = home_offset[2] = 0.0F; |
201bcb94 AW |
96 | } |
97 | ||
33e4cc02 JM |
98 | void Endstops::on_module_loaded() |
99 | { | |
7dee696d | 100 | // Do not do anything if not enabled |
33e4cc02 JM |
101 | if ( this->kernel->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) { |
102 | return; | |
103 | } | |
7d62445b | 104 | |
476dcb96 | 105 | register_for_event(ON_CONFIG_RELOAD); |
201bcb94 AW |
106 | this->register_for_event(ON_GCODE_RECEIVED); |
107 | ||
108 | // Take StepperMotor objects from Robot and keep them here | |
109 | this->steppers[0] = this->kernel->robot->alpha_stepper_motor; | |
110 | this->steppers[1] = this->kernel->robot->beta_stepper_motor; | |
111 | this->steppers[2] = this->kernel->robot->gamma_stepper_motor; | |
112 | ||
750277f8 AW |
113 | // Settings |
114 | this->on_config_reload(this); | |
c339d634 | 115 | |
201bcb94 AW |
116 | } |
117 | ||
750277f8 | 118 | // Get config |
33e4cc02 JM |
119 | void Endstops::on_config_reload(void *argument) |
120 | { | |
cc183397 A |
121 | this->pins[0].from_string( this->kernel->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); |
122 | this->pins[1].from_string( this->kernel->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
123 | this->pins[2].from_string( this->kernel->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
124 | this->pins[3].from_string( this->kernel->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
125 | this->pins[4].from_string( this->kernel->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
126 | this->pins[5].from_string( this->kernel->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input(); | |
5de98d7c JM |
127 | |
128 | // we need to know steps per mm for M206, also use them for all settings | |
47bbe224 JM |
129 | this->steps_per_mm[0] = this->kernel->config->value(alpha_steps_per_mm_checksum )->as_number(); |
130 | this->steps_per_mm[1] = this->kernel->config->value(beta_steps_per_mm_checksum )->as_number(); | |
131 | this->steps_per_mm[2] = this->kernel->config->value(gamma_steps_per_mm_checksum )->as_number(); | |
132 | ||
5de98d7c JM |
133 | this->fast_rates[0] = this->kernel->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number(); |
134 | this->fast_rates[1] = this->kernel->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number(); | |
135 | this->fast_rates[2] = this->kernel->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number(); | |
136 | this->slow_rates[0] = this->kernel->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number(); | |
137 | this->slow_rates[1] = this->kernel->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number(); | |
138 | this->slow_rates[2] = this->kernel->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number(); | |
139 | this->retract_steps[0] = this->kernel->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number(); | |
140 | this->retract_steps[1] = this->kernel->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number(); | |
141 | this->retract_steps[2] = this->kernel->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number(); | |
142 | ||
143 | // 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 | |
33e4cc02 JM |
144 | 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]; |
145 | 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]; | |
146 | 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]; | |
147 | 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]; | |
148 | 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]; | |
149 | 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]; | |
150 | 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]; | |
151 | 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]; | |
152 | 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]; | |
5de98d7c | 153 | |
9a993543 | 154 | this->debounce_count = this->kernel->config->value(endstop_debounce_count_checksum )->by_default(0)->as_number(); |
5de98d7c JM |
155 | |
156 | ||
409ff5b3 JM |
157 | // get homing direction and convert to boolean where true is home to min, and false is home to max |
158 | int home_dir = get_checksum(this->kernel->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string()); | |
eb09fdbf | 159 | this->home_direction[0] = home_dir != home_to_max_checksum; |
47bbe224 | 160 | |
409ff5b3 | 161 | home_dir = get_checksum(this->kernel->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string()); |
eb09fdbf | 162 | this->home_direction[1] = home_dir != home_to_max_checksum; |
47bbe224 | 163 | |
409ff5b3 | 164 | home_dir = get_checksum(this->kernel->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string()); |
eb09fdbf | 165 | this->home_direction[2] = home_dir != home_to_max_checksum; |
47bbe224 | 166 | |
33e4cc02 JM |
167 | 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(); |
168 | 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();; | |
169 | 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 |
170 | |
171 | this->is_corexy = this->kernel->config->value(corexy_homing_checksum)->by_default(false)->as_bool(); | |
959ab59c | 172 | this->is_delta = this->kernel->config->value(delta_homing_checksum)->by_default(false)->as_bool(); |
e678365e JM |
173 | |
174 | // endstop trim used by deltas to do soft adjusting, in mm, convert to steps, and negate depending on homing direction | |
175 | // eg on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up | |
33e4cc02 JM |
176 | int dirx = (this->home_direction[0] ? 1 : -1); |
177 | int diry = (this->home_direction[1] ? 1 : -1); | |
178 | int dirz = (this->home_direction[2] ? 1 : -1); | |
179 | this->trim[0] = this->kernel->config->value(alpha_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[0] * dirx; | |
180 | this->trim[1] = this->kernel->config->value(beta_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[1] * diry; | |
181 | this->trim[2] = this->kernel->config->value(gamma_trim_checksum )->by_default(0 )->as_number() * steps_per_mm[2] * dirz; | |
a0e0d592 BG |
182 | } |
183 | ||
33e4cc02 JM |
184 | void Endstops::wait_for_homed(char axes_to_move) |
185 | { | |
a0e0d592 | 186 | bool running = true; |
33e4cc02 JM |
187 | unsigned int debounce[3] = {0, 0, 0}; |
188 | while (running) { | |
a0e0d592 | 189 | running = false; |
f3e93777 | 190 | this->kernel->call_event(ON_IDLE); |
33e4cc02 JM |
191 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
192 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
193 | if ( this->pins[c - 'X' + (this->home_direction[c - 'X'] ? 0 : 3)].get() ) { | |
194 | if ( debounce[c - 'X'] < debounce_count ) { | |
a0e0d592 BG |
195 | debounce[c - 'X'] ++; |
196 | running = true; | |
33e4cc02 JM |
197 | } else if ( this->steppers[c - 'X']->moving ) { |
198 | this->steppers[c - 'X']->move(0, 0); | |
a0e0d592 | 199 | } |
33e4cc02 | 200 | } else { |
a0e0d592 BG |
201 | // The endstop was not hit yet |
202 | running = true; | |
203 | debounce[c - 'X'] = 0; | |
323cca60 | 204 | } |
a0e0d592 BG |
205 | } |
206 | } | |
207 | } | |
750277f8 | 208 | } |
201bcb94 | 209 | |
f29b0272 | 210 | // this homing works for cartesian and delta printers, not for HBots/CoreXY |
33e4cc02 JM |
211 | void Endstops::do_homing(char axes_to_move) |
212 | { | |
f29b0272 JM |
213 | // Start moving the axes to the origin |
214 | this->status = MOVING_TO_ORIGIN_FAST; | |
33e4cc02 JM |
215 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
216 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
f29b0272 | 217 | this->steppers[c - 'X']->set_speed(this->fast_rates[c - 'X']); |
33e4cc02 | 218 | this->steppers[c - 'X']->move(this->home_direction[c - 'X'], 10000000); |
f29b0272 JM |
219 | } |
220 | } | |
221 | ||
222 | // Wait for all axes to have homed | |
223 | this->wait_for_homed(axes_to_move); | |
224 | ||
225 | // Move back a small distance | |
226 | this->status = MOVING_BACK; | |
227 | bool inverted_dir; | |
33e4cc02 JM |
228 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
229 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
f29b0272 JM |
230 | inverted_dir = !this->home_direction[c - 'X']; |
231 | this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']); | |
33e4cc02 | 232 | this->steppers[c - 'X']->move(inverted_dir, this->retract_steps[c - 'X']); |
f29b0272 JM |
233 | } |
234 | } | |
235 | ||
33e4cc02 JM |
236 | // Wait for moves to be done |
237 | for ( char c = 'X'; c <= 'Z'; c++ ) { | |
238 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
239 | while ( this->steppers[c - 'X']->moving ) { | |
f29b0272 JM |
240 | this->kernel->call_event(ON_IDLE); |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | // Start moving the axes to the origin slowly | |
246 | this->status = MOVING_TO_ORIGIN_SLOW; | |
33e4cc02 JM |
247 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
248 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
249 | this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']); | |
250 | this->steppers[c - 'X']->move(this->home_direction[c - 'X'], 10000000); | |
f29b0272 JM |
251 | } |
252 | } | |
253 | ||
254 | // Wait for all axes to have homed | |
255 | this->wait_for_homed(axes_to_move); | |
256 | ||
33e4cc02 | 257 | if (this->is_delta) { |
959ab59c JM |
258 | // move for soft trim |
259 | this->status = MOVING_BACK; | |
33e4cc02 JM |
260 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
261 | if ( this->trim[c - 'X'] != 0 && ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
959ab59c JM |
262 | inverted_dir = !this->home_direction[c - 'X']; |
263 | // move up or down depending on sign of trim | |
33e4cc02 | 264 | if (this->trim[c - 'X'] < 0) inverted_dir = !inverted_dir; |
959ab59c | 265 | this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']); |
33e4cc02 | 266 | this->steppers[c - 'X']->move(inverted_dir, this->trim[c - 'X']); |
959ab59c | 267 | } |
f29b0272 | 268 | } |
f29b0272 | 269 | |
959ab59c | 270 | // Wait for moves to be done |
33e4cc02 JM |
271 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
272 | if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) { | |
959ab59c | 273 | //this->kernel->streams->printf("axis %c \r\n", c ); |
33e4cc02 | 274 | while ( this->steppers[c - 'X']->moving ) { |
959ab59c JM |
275 | this->kernel->call_event(ON_IDLE); |
276 | } | |
f29b0272 JM |
277 | } |
278 | } | |
279 | } | |
280 | ||
281 | // Homing is done | |
282 | this->status = NOT_HOMING; | |
283 | } | |
284 | ||
33e4cc02 JM |
285 | void Endstops::wait_for_homed_corexy(int axis) |
286 | { | |
3db88866 | 287 | bool running = true; |
33e4cc02 JM |
288 | unsigned int debounce[3] = {0, 0, 0}; |
289 | while (running) { | |
3db88866 JM |
290 | running = false; |
291 | this->kernel->call_event(ON_IDLE); | |
33e4cc02 JM |
292 | if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) { |
293 | if ( debounce[axis] < debounce_count ) { | |
3db88866 JM |
294 | debounce[axis] ++; |
295 | running = true; | |
296 | } else { | |
297 | // turn both off if running | |
33e4cc02 JM |
298 | if (this->steppers[X_AXIS]->moving) this->steppers[X_AXIS]->move(0, 0); |
299 | if (this->steppers[Y_AXIS]->moving) this->steppers[Y_AXIS]->move(0, 0); | |
3db88866 | 300 | } |
33e4cc02 | 301 | } else { |
3db88866 JM |
302 | // The endstop was not hit yet |
303 | running = true; | |
304 | debounce[axis] = 0; | |
305 | } | |
306 | } | |
307 | } | |
308 | ||
33e4cc02 JM |
309 | void Endstops::corexy_home(int home_axis, bool dirx, bool diry, double fast_rate, double slow_rate, unsigned int retract_steps) |
310 | { | |
311 | this->status = MOVING_TO_ORIGIN_FAST; | |
312 | this->steppers[X_AXIS]->set_speed(fast_rate); | |
313 | this->steppers[X_AXIS]->move(dirx, 10000000); | |
314 | this->steppers[Y_AXIS]->set_speed(fast_rate); | |
315 | this->steppers[Y_AXIS]->move(diry, 10000000); | |
f29b0272 | 316 | |
33e4cc02 JM |
317 | // wait for primary axis |
318 | this->wait_for_homed_corexy(home_axis); | |
f29b0272 | 319 | |
33e4cc02 JM |
320 | // Move back a small distance |
321 | this->status = MOVING_BACK; | |
322 | this->steppers[X_AXIS]->set_speed(slow_rate); | |
323 | this->steppers[X_AXIS]->move(!dirx, retract_steps); | |
324 | this->steppers[Y_AXIS]->set_speed(slow_rate); | |
325 | this->steppers[Y_AXIS]->move(!diry, retract_steps); | |
326 | ||
327 | // wait until done | |
9a993543 | 328 | while ( this->steppers[X_AXIS]->moving || this->steppers[Y_AXIS]->moving) { |
33e4cc02 | 329 | this->kernel->call_event(ON_IDLE); |
f29b0272 JM |
330 | } |
331 | ||
33e4cc02 JM |
332 | // Start moving the axes to the origin slowly |
333 | this->status = MOVING_TO_ORIGIN_SLOW; | |
334 | this->steppers[X_AXIS]->set_speed(slow_rate); | |
335 | this->steppers[X_AXIS]->move(dirx, 10000000); | |
336 | this->steppers[Y_AXIS]->set_speed(slow_rate); | |
337 | this->steppers[Y_AXIS]->move(diry, 10000000); | |
f29b0272 | 338 | |
33e4cc02 JM |
339 | // wait for primary axis |
340 | this->wait_for_homed_corexy(home_axis); | |
341 | } | |
f29b0272 | 342 | |
33e4cc02 JM |
343 | // this homing works for HBots/CoreXY |
344 | void Endstops::do_homing_corexy(char axes_to_move) | |
345 | { | |
174d9961 JM |
346 | // TODO should really make order configurable, and selectr whether to allow XY to home at the same time, diagonally |
347 | // 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 | |
348 | // allow to move until an endstop triggers, then stop that motor. | |
349 | // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit | |
174d9961 JM |
350 | |
351 | if((axes_to_move & 0x03) == 0x03) { // both X and Y need Homing | |
352 | // determine which motor to turn and which way | |
353 | bool dirx= this->home_direction[X_AXIS]; | |
354 | bool diry= this->home_direction[Y_AXIS]; | |
355 | int motor; | |
356 | bool dir; | |
357 | if(dirx && diry) { // min/min | |
358 | motor= X_AXIS; | |
359 | dir= true; | |
360 | }else if(dirx && !diry) { // min/max | |
361 | motor= Y_AXIS; | |
362 | dir= true; | |
363 | }else if(!dirx && diry) { // max/min | |
364 | motor= Y_AXIS; | |
365 | dir= false; | |
366 | }else if(!dirx && !diry) { // max/max | |
367 | motor= X_AXIS; | |
368 | dir= false; | |
369 | } | |
370 | ||
371 | // then move both X and Y until one hits the endstop | |
372 | this->status = MOVING_TO_ORIGIN_FAST; | |
373 | this->steppers[motor]->set_speed(this->fast_rates[motor]); | |
374 | this->steppers[motor]->move(dir, 10000000); | |
375 | // wait until either X or Y hits the endstop | |
376 | bool running= true; | |
377 | while (running) { | |
378 | this->kernel->call_event(ON_IDLE); | |
379 | for(int m=X_AXIS;m<=Y_AXIS;m++) { | |
380 | if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) { | |
381 | // turn off motor | |
382 | if(this->steppers[motor]->moving) this->steppers[motor]->move(0, 0); | |
383 | running= false; | |
384 | break; | |
385 | } | |
386 | } | |
387 | } | |
388 | } | |
33e4cc02 | 389 | |
174d9961 | 390 | // move individual axis |
33e4cc02 | 391 | if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction |
413dd90c JM |
392 | bool dir= this->home_direction[X_AXIS]; |
393 | corexy_home(X_AXIS, dir, dir, this->fast_rates[X_AXIS], this->slow_rates[X_AXIS], this->retract_steps[X_AXIS]); | |
f29b0272 JM |
394 | } |
395 | ||
65191d91 JM |
396 | if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions |
397 | bool dir= this->home_direction[Y_AXIS]; | |
398 | corexy_home(Y_AXIS, dir, !dir, this->fast_rates[Y_AXIS], this->slow_rates[Y_AXIS], this->retract_steps[Y_AXIS]); | |
399 | } | |
400 | ||
33e4cc02 | 401 | if (axes_to_move & 0x04) { // move Z |
f29b0272 JM |
402 | do_homing(0x04); // just home normally for Z |
403 | } | |
404 | ||
405 | // Homing is done | |
406 | this->status = NOT_HOMING; | |
407 | } | |
408 | ||
201bcb94 | 409 | // Start homing sequences by response to GCode commands |
33e4cc02 | 410 | void Endstops::on_gcode_received(void *argument) |
c339d634 | 411 | { |
33e4cc02 JM |
412 | Gcode *gcode = static_cast<Gcode *>(argument); |
413 | if ( gcode->has_g) { | |
414 | if ( gcode->g == 28 ) { | |
74b6303c | 415 | gcode->mark_as_taken(); |
df27a6a3 | 416 | // G28 is received, we have homing to do |
201bcb94 AW |
417 | |
418 | // First wait for the queue to be empty | |
40de2562 | 419 | this->kernel->conveyor->wait_for_empty_queue(); |
201bcb94 AW |
420 | |
421 | // Do we move select axes or all of them | |
7dee696d | 422 | char axes_to_move = 0; |
959ab59c | 423 | // only enable homing if the endstop is defined, deltas always home all axis |
33e4cc02 | 424 | bool home_all = this->is_delta || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') ); |
47bbe224 | 425 | |
33e4cc02 JM |
426 | for ( char c = 'X'; c <= 'Z'; c++ ) { |
427 | if ( (home_all || gcode->has_letter(c)) && this->pins[c - 'X' + (this->home_direction[c - 'X'] ? 0 : 3)].connected() ) { | |
428 | axes_to_move += ( 1 << (c - 'X' ) ); | |
429 | } | |
df27a6a3 | 430 | } |
3b948656 | 431 | |
f6c04440 AW |
432 | // Enable the motors |
433 | this->kernel->stepper->turn_enable_pins_on(); | |
434 | ||
f29b0272 | 435 | // do the actual homing |
33e4cc02 | 436 | if (is_corexy) |
f29b0272 JM |
437 | do_homing_corexy(axes_to_move); |
438 | else | |
439 | do_homing(axes_to_move); | |
3ffe27fb | 440 | |
33e4cc02 JM |
441 | // Zero the ax(i/e)s position, add in the home offset |
442 | for ( int c = 0; c <= 2; c++ ) { | |
443 | if ( (axes_to_move >> c) & 1 ) { | |
444 | this->kernel->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c); | |
3ffe27fb AV |
445 | } |
446 | } | |
c339d634 | 447 | } |
33e4cc02 JM |
448 | } else if (gcode->has_m) { |
449 | switch (gcode->m) { | |
450 | case 119: { | |
451 | ||
452 | int px = this->home_direction[0] ? 0 : 3; | |
453 | int py = this->home_direction[1] ? 1 : 4; | |
454 | int pz = this->home_direction[2] ? 2 : 5; | |
455 | const char *mx = this->home_direction[0] ? "min" : "max"; | |
456 | const char *my = this->home_direction[1] ? "min" : "max"; | |
457 | const char *mz = this->home_direction[2] ? "min" : "max"; | |
458 | ||
459 | 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()); | |
460 | gcode->mark_as_taken(); | |
461 | } | |
462 | break; | |
463 | ||
464 | case 206: // M206 - set homing offset | |
465 | if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X'); | |
466 | if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y'); | |
467 | if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z'); | |
468 | gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]); | |
469 | gcode->mark_as_taken(); | |
470 | break; | |
471 | ||
472 | case 500: // save settings | |
473 | case 503: // print settings | |
474 | 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]); | |
475 | if (is_delta) { | |
476 | double mm[3]; | |
477 | trim2mm(mm); | |
478 | gcode->stream->printf(";Trim (mm):\nM666 X%1.2f Y%1.2f Z%1.2f\n", mm[0], mm[1], mm[2]); | |
479 | gcode->stream->printf(";Max Z\nM665 Z%1.2f\n", this->homing_position[2]); | |
7a8fe6e0 | 480 | } |
33e4cc02 | 481 | gcode->mark_as_taken(); |
c339d634 | 482 | break; |
47bbe224 | 483 | |
ec4773e5 | 484 | case 665: { // M665 - set max gamma/z height |
33e4cc02 JM |
485 | gcode->mark_as_taken(); |
486 | double gamma_max = this->homing_position[2]; | |
487 | if (gcode->has_letter('Z')) { | |
488 | this->homing_position[2] = gamma_max = gcode->get_value('Z'); | |
ec4773e5 | 489 | } |
33e4cc02 JM |
490 | gcode->stream->printf("Max Z %8.3f ", gamma_max); |
491 | gcode->add_nl = true; | |
492 | } | |
493 | break; | |
ec4773e5 | 494 | |
47bbe224 | 495 | |
33e4cc02 JM |
496 | case 666: { // M666 - set trim for each axis in mm |
497 | double mm[3]; | |
498 | trim2mm(mm); | |
47bbe224 | 499 | |
33e4cc02 JM |
500 | if (gcode->has_letter('X')) mm[0] = gcode->get_value('X'); |
501 | if (gcode->has_letter('Y')) mm[1] = gcode->get_value('Y'); | |
502 | if (gcode->has_letter('Z')) mm[2] = gcode->get_value('Z'); | |
47bbe224 | 503 | |
33e4cc02 JM |
504 | int dirx = (this->home_direction[0] ? 1 : -1); |
505 | int diry = (this->home_direction[1] ? 1 : -1); | |
506 | int dirz = (this->home_direction[2] ? 1 : -1); | |
507 | trim[0] = lround(mm[0] * steps_per_mm[0]) * dirx; // convert back to steps | |
508 | trim[1] = lround(mm[1] * steps_per_mm[1]) * diry; | |
509 | trim[2] = lround(mm[2] * steps_per_mm[2]) * dirz; | |
510 | ||
511 | // print the current trim values in mm and steps | |
512 | 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]); | |
513 | gcode->mark_as_taken(); | |
514 | } | |
515 | break; | |
47bbe224 | 516 | |
201bcb94 AW |
517 | } |
518 | } | |
519 | } | |
520 | ||
33e4cc02 JM |
521 | void Endstops::trim2mm(double *mm) |
522 | { | |
523 | int dirx = (this->home_direction[0] ? 1 : -1); | |
524 | int diry = (this->home_direction[1] ? 1 : -1); | |
525 | int dirz = (this->home_direction[2] ? 1 : -1); | |
526 | ||
527 | mm[0] = this->trim[0] / this->steps_per_mm[0] * dirx; // convert to mm | |
528 | mm[1] = this->trim[1] / this->steps_per_mm[1] * diry; | |
529 | mm[2] = this->trim[2] / this->steps_per_mm[2] * dirz; | |
530 | } | |
531 |