Commit | Line | Data |
---|---|---|
88443c6b JM |
1 | /* |
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/>. | |
6 | */ | |
7 | ||
8 | #include "ZProbe.h" | |
9 | ||
10 | #include "Kernel.h" | |
11 | #include "BaseSolution.h" | |
12 | #include "Config.h" | |
13 | #include "Robot.h" | |
14 | #include "StepperMotor.h" | |
15 | #include "StreamOutputPool.h" | |
16 | #include "Gcode.h" | |
17 | #include "Conveyor.h" | |
18 | #include "Stepper.h" | |
19 | #include "checksumm.h" | |
20 | #include "ConfigValue.h" | |
21 | #include "SlowTicker.h" | |
22 | #include "Planner.h" | |
037c350d | 23 | #include "SerialMessage.h" |
9f6f04a5 JM |
24 | #include "PublicDataRequest.h" |
25 | #include "EndstopsPublicAccess.h" | |
26 | #include "PublicData.h" | |
ce9d2bda | 27 | #include "LevelingStrategy.h" |
a157d099 | 28 | #include "StepTicker.h" |
37102904 | 29 | #include "utils.h" |
a5542cae JM |
30 | |
31 | // strategies we know about | |
f6efadb0 | 32 | #include "DeltaCalibrationStrategy.h" |
a5542cae | 33 | #include "ThreePointStrategy.h" |
43b9b200 | 34 | #include "ZGridStrategy.h" |
88443c6b | 35 | |
88443c6b JM |
36 | #define enable_checksum CHECKSUM("enable") |
37 | #define probe_pin_checksum CHECKSUM("probe_pin") | |
38 | #define debounce_count_checksum CHECKSUM("debounce_count") | |
681a62d7 JM |
39 | #define slow_feedrate_checksum CHECKSUM("slow_feedrate") |
40 | #define fast_feedrate_checksum CHECKSUM("fast_feedrate") | |
b469231e | 41 | #define return_feedrate_checksum CHECKSUM("return_feedrate") |
681a62d7 | 42 | #define probe_height_checksum CHECKSUM("probe_height") |
f3b66360 | 43 | #define gamma_max_checksum CHECKSUM("gamma_max") |
93f20a8c | 44 | #define reverse_z_direction_checksum CHECKSUM("reverse_z") |
88443c6b | 45 | |
681a62d7 | 46 | // from endstop section |
b7cd847e | 47 | #define delta_homing_checksum CHECKSUM("delta_homing") |
d8198a57 | 48 | #define rdelta_homing_checksum CHECKSUM("rdelta_homing") |
88443c6b JM |
49 | |
50 | #define X_AXIS 0 | |
51 | #define Y_AXIS 1 | |
52 | #define Z_AXIS 2 | |
53 | ||
dd0a7cfa JM |
54 | #define STEPPER THEKERNEL->robot->actuators |
55 | #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm()) | |
56ce2b5a JM |
56 | #define Z_STEPS_PER_MM STEPS_PER_MM(Z_AXIS) |
57 | ||
7d6fe308 JM |
58 | #define abs(a) ((a<0) ? -a : a) |
59 | ||
88443c6b JM |
60 | void ZProbe::on_module_loaded() |
61 | { | |
62 | // if the module is disabled -> do nothing | |
56ce2b5a | 63 | if(!THEKERNEL->config->value( zprobe_checksum, enable_checksum )->by_default(false)->as_bool()) { |
88443c6b JM |
64 | // as this module is not needed free up the resource |
65 | delete this; | |
66 | return; | |
67 | } | |
88443c6b JM |
68 | |
69 | // load settings | |
70 | this->on_config_reload(this); | |
71 | // register event-handlers | |
88443c6b | 72 | register_for_event(ON_GCODE_RECEIVED); |
88443c6b | 73 | |
a157d099 | 74 | THEKERNEL->step_ticker->register_acceleration_tick_handler([this](){acceleration_tick(); }); |
778093ce JM |
75 | |
76 | // we read the probe in this timer, currently only for G38 probes. | |
77 | probing= false; | |
78 | THEKERNEL->slow_ticker->attach(1000, this, &ZProbe::read_probe); | |
88443c6b JM |
79 | } |
80 | ||
81 | void ZProbe::on_config_reload(void *argument) | |
82 | { | |
681a62d7 JM |
83 | this->pin.from_string( THEKERNEL->config->value(zprobe_checksum, probe_pin_checksum)->by_default("nc" )->as_string())->as_input(); |
84 | this->debounce_count = THEKERNEL->config->value(zprobe_checksum, debounce_count_checksum)->by_default(0 )->as_number(); | |
85 | ||
ce9d2bda | 86 | // get strategies to load |
f6efadb0 JM |
87 | vector<uint16_t> modules; |
88 | THEKERNEL->config->get_module_list( &modules, leveling_strategy_checksum); | |
89 | for( auto cs : modules ){ | |
90 | if( THEKERNEL->config->value(leveling_strategy_checksum, cs, enable_checksum )->as_bool() ){ | |
91 | bool found= false; | |
92 | // check with each known strategy and load it if it matches | |
93 | switch(cs) { | |
94 | case delta_calibration_strategy_checksum: | |
95 | this->strategies.push_back(new DeltaCalibrationStrategy(this)); | |
96 | found= true; | |
97 | break; | |
a5542cae JM |
98 | |
99 | case three_point_leveling_strategy_checksum: | |
100 | // NOTE this strategy is mutually exclusive with the delta calibration strategy | |
101 | this->strategies.push_back(new ThreePointStrategy(this)); | |
102 | found= true; | |
103 | break; | |
104 | ||
43b9b200 QH |
105 | case ZGrid_leveling_checksum: |
106 | this->strategies.push_back(new ZGridStrategy(this)); | |
107 | found= true; | |
108 | break; | |
109 | ||
f6efadb0 | 110 | // add other strategies here |
f6efadb0 | 111 | //case zheight_map_strategy: |
a5542cae | 112 | // this->strategies.push_back(new ZHeightMapStrategy(this)); |
f6efadb0 JM |
113 | // found= true; |
114 | // break; | |
115 | } | |
116 | if(found) this->strategies.back()->handleConfig(); | |
117 | } | |
118 | } | |
ce9d2bda | 119 | |
57e927fa | 120 | // need to know if we need to use delta kinematics for homing |
ce9d2bda | 121 | this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool(); |
d8198a57 | 122 | this->is_rdelta = THEKERNEL->config->value(rdelta_homing_checksum)->by_default(false)->as_bool(); |
ce9d2bda | 123 | |
f6efadb0 | 124 | // default for backwards compatibility add DeltaCalibrationStrategy if a delta |
57e927fa | 125 | // will be deprecated |
f6efadb0 | 126 | if(this->strategies.empty()) { |
57e927fa JM |
127 | if(this->is_delta) { |
128 | this->strategies.push_back(new DeltaCalibrationStrategy(this)); | |
129 | this->strategies.back()->handleConfig(); | |
130 | } | |
037c350d | 131 | } |
681a62d7 | 132 | |
f3b66360 | 133 | this->probe_height = THEKERNEL->config->value(zprobe_checksum, probe_height_checksum)->by_default(5.0F)->as_number(); |
681a62d7 JM |
134 | this->slow_feedrate = THEKERNEL->config->value(zprobe_checksum, slow_feedrate_checksum)->by_default(5)->as_number(); // feedrate in mm/sec |
135 | this->fast_feedrate = THEKERNEL->config->value(zprobe_checksum, fast_feedrate_checksum)->by_default(100)->as_number(); // feedrate in mm/sec | |
b469231e | 136 | this->return_feedrate = THEKERNEL->config->value(zprobe_checksum, return_feedrate_checksum)->by_default(0)->as_number(); // feedrate in mm/sec |
f3b66360 | 137 | this->max_z = THEKERNEL->config->value(gamma_max_checksum)->by_default(500)->as_number(); // maximum zprobe distance |
93f20a8c | 138 | this->reverse_z = THEKERNEL->config->value(reverse_z_direction_checksum)->by_default(false)->as_bool(); // Z probe moves in reverse direction (upside down rdelta) |
88443c6b JM |
139 | } |
140 | ||
fa7bcf7e | 141 | bool ZProbe::wait_for_probe(int& steps) |
88443c6b JM |
142 | { |
143 | unsigned int debounce = 0; | |
144 | while(true) { | |
145 | THEKERNEL->call_event(ON_IDLE); | |
aa896868 JM |
146 | if(THEKERNEL->is_halted()){ |
147 | // aborted by kill | |
148 | return false; | |
149 | } | |
150 | ||
d8198a57 JM |
151 | bool delta= is_delta || is_rdelta; |
152 | ||
88443c6b | 153 | // if no stepper is moving, moves are finished and there was no touch |
d8198a57 | 154 | if( !STEPPER[Z_AXIS]->is_moving() && (!delta || (!STEPPER[Y_AXIS]->is_moving() && !STEPPER[Z_AXIS]->is_moving())) ) { |
88443c6b JM |
155 | return false; |
156 | } | |
157 | ||
d8198a57 | 158 | // if the probe is active... |
88443c6b JM |
159 | if( this->pin.get() ) { |
160 | //...increase debounce counter... | |
161 | if( debounce < debounce_count) { | |
162 | // ...but only if the counter hasn't reached the max. value | |
163 | debounce++; | |
164 | } else { | |
165 | // ...otherwise stop the steppers, return its remaining steps | |
fa7bcf7e JM |
166 | if(STEPPER[Z_AXIS]->is_moving()){ |
167 | steps= STEPPER[Z_AXIS]->get_stepped(); | |
168 | STEPPER[Z_AXIS]->move(0, 0); | |
169 | } | |
d8198a57 | 170 | if(delta) { |
fa7bcf7e JM |
171 | for( int i = X_AXIS; i <= Y_AXIS; i++ ) { |
172 | if ( STEPPER[i]->is_moving() ) { | |
173 | STEPPER[i]->move(0, 0); | |
174 | } | |
88443c6b JM |
175 | } |
176 | } | |
177 | return true; | |
178 | } | |
179 | } else { | |
180 | // The probe was not hit yet, reset debounce counter | |
181 | debounce = 0; | |
182 | } | |
183 | } | |
184 | } | |
88443c6b | 185 | |
771fb7b2 RM |
186 | // single probe with custom feedrate |
187 | // returns boolean value indicating if probe was triggered | |
93f20a8c | 188 | bool ZProbe::run_probe(int& steps, float feedrate, float max_dist, bool reverse) |
88443c6b | 189 | { |
72833629 JM |
190 | // not a block move so disable the last tick setting |
191 | for ( int c = X_AXIS; c <= Z_AXIS; c++ ) { | |
192 | STEPPER[c]->set_moved_last_block(false); | |
193 | } | |
194 | ||
88443c6b JM |
195 | // Enable the motors |
196 | THEKERNEL->stepper->turn_enable_pins_on(); | |
771fb7b2 | 197 | this->current_feedrate = feedrate * Z_STEPS_PER_MM; // steps/sec |
36a21d48 | 198 | float maxz= max_dist < 0 ? this->max_z*2 : max_dist; |
88443c6b JM |
199 | |
200 | // move Z down | |
93f20a8c JM |
201 | bool dir= !reverse_z; |
202 | if(reverse) dir= !dir; // specified to move in opposite Z direction | |
203 | STEPPER[Z_AXIS]->move(dir, maxz * Z_STEPS_PER_MM, 0); // probe in specified direction, no more than maxz | |
d8198a57 | 204 | if(this->is_delta || this->is_rdelta) { |
b7cd847e | 205 | // for delta need to move all three actuators |
93f20a8c JM |
206 | STEPPER[X_AXIS]->move(dir, maxz * STEPS_PER_MM(X_AXIS), 0); |
207 | STEPPER[Y_AXIS]->move(dir, maxz * STEPS_PER_MM(Y_AXIS), 0); | |
b7cd847e JM |
208 | } |
209 | ||
797b4403 | 210 | // start acceleration processing |
7d6fe308 JM |
211 | this->running = true; |
212 | ||
fa7bcf7e | 213 | bool r = wait_for_probe(steps); |
681a62d7 | 214 | this->running = false; |
db00af9a JM |
215 | STEPPER[X_AXIS]->move(0, 0); |
216 | STEPPER[Y_AXIS]->move(0, 0); | |
217 | STEPPER[Z_AXIS]->move(0, 0); | |
88443c6b JM |
218 | return r; |
219 | } | |
220 | ||
93f20a8c | 221 | bool ZProbe::return_probe(int steps, bool reverse) |
681a62d7 JM |
222 | { |
223 | // move probe back to where it was | |
b469231e RM |
224 | |
225 | float fr; | |
226 | if(this->return_feedrate != 0) { // use return_feedrate if set | |
227 | fr = this->return_feedrate; | |
228 | } else { | |
229 | fr = this->slow_feedrate*2; // nominally twice slow feedrate | |
230 | if(fr > this->fast_feedrate) fr = this->fast_feedrate; // unless that is greater than fast feedrate | |
231 | } | |
232 | ||
903725bb | 233 | this->current_feedrate = fr * Z_STEPS_PER_MM; // feedrate in steps/sec |
681a62d7 | 234 | bool dir= steps < 0; |
93f20a8c | 235 | if(reverse) dir= !dir; |
681a62d7 JM |
236 | steps= abs(steps); |
237 | ||
d8198a57 | 238 | bool delta= (this->is_delta || this->is_rdelta); |
6f6677fc | 239 | STEPPER[Z_AXIS]->move(dir, steps, 0); |
d8198a57 | 240 | if(delta) { |
6f6677fc JM |
241 | STEPPER[X_AXIS]->move(dir, steps, 0); |
242 | STEPPER[Y_AXIS]->move(dir, steps, 0); | |
681a62d7 | 243 | } |
7d6fe308 JM |
244 | |
245 | this->running = true; | |
d8198a57 | 246 | while(STEPPER[Z_AXIS]->is_moving() || (delta && (STEPPER[X_AXIS]->is_moving() || STEPPER[Y_AXIS]->is_moving())) ) { |
681a62d7 JM |
247 | // wait for it to complete |
248 | THEKERNEL->call_event(ON_IDLE); | |
db00af9a JM |
249 | if(THEKERNEL->is_halted()){ |
250 | // aborted by kill | |
251 | break; | |
252 | } | |
681a62d7 JM |
253 | } |
254 | ||
255 | this->running = false; | |
db00af9a JM |
256 | STEPPER[X_AXIS]->move(0, 0); |
257 | STEPPER[Y_AXIS]->move(0, 0); | |
258 | STEPPER[Z_AXIS]->move(0, 0); | |
681a62d7 JM |
259 | |
260 | return true; | |
261 | } | |
262 | ||
97832d6d JM |
263 | bool ZProbe::doProbeAt(int &steps, float x, float y) |
264 | { | |
265 | int s; | |
266 | // move to xy | |
267 | coordinated_move(x, y, NAN, getFastFeedrate()); | |
268 | if(!run_probe(s)) return false; | |
269 | ||
270 | // return to original Z | |
271 | return_probe(s); | |
272 | steps = s; | |
273 | ||
274 | return true; | |
275 | } | |
fc7b9a7b | 276 | |
0e44e7d7 JM |
277 | float ZProbe::probeDistance(float x, float y) |
278 | { | |
279 | int s; | |
280 | if(!doProbeAt(s, x, y)) return NAN; | |
281 | return zsteps_to_mm(s); | |
282 | } | |
283 | ||
88443c6b JM |
284 | void ZProbe::on_gcode_received(void *argument) |
285 | { | |
286 | Gcode *gcode = static_cast<Gcode *>(argument); | |
88443c6b | 287 | |
57e927fa | 288 | if( gcode->has_g && gcode->g >= 29 && gcode->g <= 32) { |
36a21d48 | 289 | |
25dc6344 | 290 | // make sure the probe is defined and not already triggered before moving motors |
36a21d48 | 291 | if(!this->pin.connected()) { |
25dc6344 JM |
292 | gcode->stream->printf("ZProbe not connected.\n"); |
293 | return; | |
294 | } | |
295 | if(this->pin.get()) { | |
296 | gcode->stream->printf("ZProbe triggered before move, aborting command.\n"); | |
297 | return; | |
298 | } | |
299 | ||
681a62d7 | 300 | if( gcode->g == 30 ) { // simple Z probe |
88443c6b JM |
301 | // first wait for an empty queue i.e. no moves left |
302 | THEKERNEL->conveyor->wait_for_empty_queue(); | |
303 | ||
681a62d7 | 304 | int steps; |
771fb7b2 | 305 | bool probe_result; |
93f20a8c JM |
306 | bool reverse= (gcode->has_letter('R') && gcode->get_value('R') != 0); // specify to probe in reverse direction |
307 | float rate= gcode->has_letter('F') ? gcode->get_value('F') / 60 : this->slow_feedrate; | |
308 | probe_result = run_probe(steps, rate, -1, reverse); | |
771fb7b2 RM |
309 | |
310 | if(probe_result) { | |
cea695db | 311 | gcode->stream->printf("Z:%1.4f C:%d\n", zsteps_to_mm(steps), steps); |
bd96f4d7 | 312 | // move back to where it started, unless a Z is specified |
93f20a8c | 313 | if(gcode->has_letter('Z') && !is_rdelta) { |
bd96f4d7 JM |
314 | // set Z to the specified value, and leave probe where it is |
315 | THEKERNEL->robot->reset_axis_position(gcode->get_value('Z'), Z_AXIS); | |
681a62d7 | 316 | } else { |
93f20a8c | 317 | return_probe(steps, reverse); |
bd96f4d7 | 318 | } |
681a62d7 | 319 | } else { |
bd96f4d7 | 320 | gcode->stream->printf("ZProbe not triggered\n"); |
88443c6b | 321 | } |
fc7b9a7b | 322 | |
ce9d2bda | 323 | } else { |
aaf0c0ee | 324 | if(!gcode->has_letter('P')) { |
fc92f0ac JM |
325 | // find the first strategy to handle the gcode |
326 | for(auto s : strategies){ | |
327 | if(s->handleGcode(gcode)) { | |
328 | return; | |
329 | } | |
330 | } | |
331 | gcode->stream->printf("No strategy found to handle G%d\n", gcode->g); | |
332 | ||
333 | }else{ | |
aaf0c0ee JM |
334 | // P paramater selects which strategy to send the code to |
335 | // they are loaded in the order they are defined in config, 0 being the first, 1 being the second and so on. | |
336 | uint16_t i= gcode->get_value('P'); | |
337 | if(i < strategies.size()) { | |
fc92f0ac | 338 | if(!strategies[i]->handleGcode(gcode)){ |
aaf0c0ee | 339 | gcode->stream->printf("strategy #%d did not handle G%d\n", i, gcode->g); |
fc92f0ac | 340 | } |
ce9d2bda | 341 | return; |
fc92f0ac JM |
342 | |
343 | }else{ | |
aaf0c0ee | 344 | gcode->stream->printf("strategy #%d is not loaded\n", i); |
037c350d | 345 | } |
fc7b9a7b | 346 | } |
88443c6b JM |
347 | } |
348 | ||
6c0193b3 | 349 | } else if(gcode->has_g && gcode->g == 38 ) { // G38.2 Straight Probe with error, G38.3 straight probe without error |
36a21d48 JM |
350 | // linuxcnc/grbl style probe http://www.linuxcnc.org/docs/2.5/html/gcode/gcode.html#sec:G38-probe |
351 | if(gcode->subcode != 2 && gcode->subcode != 3) { | |
07186543 | 352 | gcode->stream->printf("error:Only G38.2 and G38.3 are supported\n"); |
36a21d48 JM |
353 | return; |
354 | } | |
355 | ||
356 | // make sure the probe is defined and not already triggered before moving motors | |
357 | if(!this->pin.connected()) { | |
e714bd32 | 358 | gcode->stream->printf("error:ZProbe not connected.\n"); |
36a21d48 JM |
359 | return; |
360 | } | |
17f26e01 | 361 | |
36a21d48 | 362 | if(this->pin.get()) { |
e714bd32 | 363 | gcode->stream->printf("error:ZProbe triggered before move, aborting command.\n"); |
36a21d48 JM |
364 | return; |
365 | } | |
366 | ||
367 | // first wait for an empty queue i.e. no moves left | |
368 | THEKERNEL->conveyor->wait_for_empty_queue(); | |
369 | ||
7484e84a JM |
370 | // turn off any compensation transform |
371 | auto savect= THEKERNEL->robot->compensationTransform; | |
372 | THEKERNEL->robot->compensationTransform= nullptr; | |
373 | ||
36a21d48 JM |
374 | if(gcode->has_letter('X')) { |
375 | // probe in the X axis | |
a2f1ce04 | 376 | probe_XYZ(gcode, X_AXIS); |
36a21d48 JM |
377 | |
378 | }else if(gcode->has_letter('Y')) { | |
379 | // probe in the Y axis | |
a2f1ce04 | 380 | probe_XYZ(gcode, Y_AXIS); |
36a21d48 JM |
381 | |
382 | }else if(gcode->has_letter('Z')) { | |
a2f1ce04 JM |
383 | // probe in the Z axis |
384 | probe_XYZ(gcode, Z_AXIS); | |
36a21d48 | 385 | |
36a21d48 | 386 | }else{ |
e714bd32 | 387 | gcode->stream->printf("error:at least one of X Y or Z must be specified\n"); |
36a21d48 | 388 | } |
7484e84a JM |
389 | |
390 | // restore compensationTransform | |
391 | THEKERNEL->robot->compensationTransform= savect; | |
392 | ||
36a21d48 JM |
393 | return; |
394 | ||
88443c6b JM |
395 | } else if(gcode->has_m) { |
396 | // M code processing here | |
3434eac0 RM |
397 | int c; |
398 | switch (gcode->m) { | |
399 | case 119: | |
400 | c = this->pin.get(); | |
401 | gcode->stream->printf(" Probe: %d", c); | |
402 | gcode->add_nl = true; | |
403 | break; | |
404 | ||
405 | case 670: | |
406 | if (gcode->has_letter('S')) this->slow_feedrate = gcode->get_value('S'); | |
407 | if (gcode->has_letter('K')) this->fast_feedrate = gcode->get_value('K'); | |
408 | if (gcode->has_letter('R')) this->return_feedrate = gcode->get_value('R'); | |
409 | if (gcode->has_letter('Z')) this->max_z = gcode->get_value('Z'); | |
410 | if (gcode->has_letter('H')) this->probe_height = gcode->get_value('H'); | |
411 | break; | |
412 | ||
413 | case 500: // save settings | |
414 | case 503: // print settings | |
fc92f0ac JM |
415 | gcode->stream->printf(";Probe feedrates Slow/fast(K)/Return (mm/sec) max_z (mm) height (mm):\nM670 S%1.2f K%1.2f R%1.2f Z%1.2f H%1.2f\n", |
416 | this->slow_feedrate, this->fast_feedrate, this->return_feedrate, this->max_z, this->probe_height); | |
417 | ||
3434eac0 RM |
418 | // fall through is intended so leveling strategies can handle m-codes too |
419 | ||
420 | default: | |
421 | for(auto s : strategies){ | |
422 | if(s->handleGcode(gcode)) { | |
423 | return; | |
424 | } | |
ce9d2bda | 425 | } |
bd96f4d7 | 426 | } |
88443c6b JM |
427 | } |
428 | } | |
429 | ||
778093ce JM |
430 | uint32_t ZProbe::read_probe(uint32_t dummy) |
431 | { | |
432 | if(!probing || probe_detected) return 0; | |
433 | ||
434 | // TODO add debounce/noise filter | |
435 | if(this->pin.get()) { | |
436 | probe_detected= true; | |
437 | // now tell all the stepper_motors to stop | |
438 | for(auto &a : THEKERNEL->robot->actuators) a->force_finish_move(); | |
439 | } | |
440 | return 0; | |
441 | } | |
442 | ||
443 | // special way to probe in the X or Y or Z direction using planned moves, should work with any kinematics | |
a2f1ce04 | 444 | void ZProbe::probe_XYZ(Gcode *gcode, int axis) |
37102904 | 445 | { |
778093ce JM |
446 | // enable the probe checking in the timer |
447 | probing= true; | |
448 | probe_detected= false; | |
449 | THEKERNEL->robot->disable_segmentation= true; // we must disable segmentation as this won't work with it enabled (beware on deltas probing in X or Y) | |
37102904 JM |
450 | |
451 | // get probe feedrate if specified | |
452 | float rate = (gcode->has_letter('F')) ? gcode->get_value('F')*60 : this->slow_feedrate; | |
453 | ||
778093ce JM |
454 | // do a regular move which will stop as soon as the probe is triggered, or the distance is reached |
455 | switch(axis) { | |
456 | case X_AXIS: coordinated_move(gcode->get_value('X'), 0, 0, rate, true); break; | |
457 | case Y_AXIS: coordinated_move(0, gcode->get_value('Y'), 0, rate, true); break; | |
458 | case Z_AXIS: coordinated_move(0, 0, gcode->get_value('Z'), rate, true); break; | |
37102904 JM |
459 | } |
460 | ||
778093ce JM |
461 | // coordinated_move returns when the move is finished |
462 | ||
463 | // disable probe checking | |
464 | probing= false; | |
465 | THEKERNEL->robot->disable_segmentation= false; | |
37102904 JM |
466 | |
467 | float pos[3]; | |
468 | { | |
469 | // get the current position | |
470 | ActuatorCoordinates current_position{ | |
471 | THEKERNEL->robot->actuators[X_AXIS]->get_current_position(), | |
472 | THEKERNEL->robot->actuators[Y_AXIS]->get_current_position(), | |
473 | THEKERNEL->robot->actuators[Z_AXIS]->get_current_position() | |
474 | }; | |
475 | ||
476 | // get machine position from the actuator position using FK | |
477 | THEKERNEL->robot->arm_solution->actuator_to_cartesian(current_position, pos); | |
478 | } | |
479 | ||
778093ce | 480 | uint8_t probeok= this->probe_detected ? 1 : 0; |
b76a9926 | 481 | |
17f26e01 | 482 | // print results using the GRBL format |
b76a9926 | 483 | gcode->stream->printf("[PRB:%1.3f,%1.3f,%1.3f:%d]\n", pos[X_AXIS], pos[Y_AXIS], pos[Z_AXIS], probeok); |
4440e123 | 484 | THEKERNEL->robot->set_last_probe_position(std::make_tuple(pos[X_AXIS], pos[Y_AXIS], pos[Z_AXIS], probeok)); |
b76a9926 | 485 | |
93f20a8c JM |
486 | if(!probeok && gcode->subcode == 2) { |
487 | // issue error if probe was not triggered and subcode == 2 | |
488 | gcode->stream->printf("ALARM:Probe fail\n"); | |
489 | THEKERNEL->call_event(ON_HALT, nullptr); | |
37102904 | 490 | |
93f20a8c JM |
491 | }else if(probeok){ |
492 | // if the probe stopped the move we need to correct the last_milestone as it did not reach where it thought | |
493 | THEKERNEL->robot->reset_position_from_current_actuator_position(); | |
778093ce | 494 | } |
37102904 JM |
495 | } |
496 | ||
88443c6b | 497 | // Called periodically to change the speed to match acceleration |
a157d099 | 498 | void ZProbe::acceleration_tick(void) |
88443c6b | 499 | { |
a157d099 | 500 | if(!this->running) return; // nothing to do |
fa7bcf7e | 501 | if(STEPPER[Z_AXIS]->is_moving()) accelerate(Z_AXIS); |
88443c6b | 502 | |
d8198a57 | 503 | if(is_delta || is_rdelta) { |
fa7bcf7e JM |
504 | // deltas needs to move all actuators |
505 | for ( int c = X_AXIS; c <= Y_AXIS; c++ ) { | |
506 | if( !STEPPER[c]->is_moving() ) continue; | |
507 | accelerate(c); | |
508 | } | |
509 | } | |
88443c6b | 510 | |
a157d099 | 511 | return; |
fa7bcf7e | 512 | } |
88443c6b | 513 | |
fa7bcf7e JM |
514 | void ZProbe::accelerate(int c) |
515 | { uint32_t current_rate = STEPPER[c]->get_steps_per_second(); | |
c8f4ee77 | 516 | uint32_t target_rate = floorf(this->current_feedrate); |
88443c6b | 517 | |
c5fe1787 JM |
518 | // Z may have a different acceleration to X and Y |
519 | float acc= (c==Z_AXIS) ? THEKERNEL->planner->get_z_acceleration() : THEKERNEL->planner->get_acceleration(); | |
fa7bcf7e | 520 | if( current_rate < target_rate ) { |
a157d099 | 521 | uint32_t rate_increase = floorf((acc / THEKERNEL->acceleration_ticks_per_second) * STEPS_PER_MM(c)); |
fa7bcf7e JM |
522 | current_rate = min( target_rate, current_rate + rate_increase ); |
523 | } | |
524 | if( current_rate > target_rate ) { | |
525 | current_rate = target_rate; | |
88443c6b JM |
526 | } |
527 | ||
fa7bcf7e | 528 | // steps per second |
d6ba35b8 | 529 | STEPPER[c]->set_speed(current_rate); |
88443c6b | 530 | } |
681a62d7 JM |
531 | |
532 | // issue a coordinated move directly to robot, and return when done | |
533 | // Only move the coordinates that are passed in as not nan | |
e0be983d | 534 | // NOTE must use G53 to force move in machine coordiantes and ignore any WCS offsetts |
037c350d | 535 | void ZProbe::coordinated_move(float x, float y, float z, float feedrate, bool relative) |
681a62d7 JM |
536 | { |
537 | char buf[32]; | |
037c350d JM |
538 | char cmd[64]; |
539 | ||
540 | if(relative) strcpy(cmd, "G91 G0 "); | |
e0be983d | 541 | else strcpy(cmd, "G53 G0 "); // G53 forces movement in machine coordinate system |
037c350d | 542 | |
681a62d7 | 543 | if(!isnan(x)) { |
037c350d | 544 | int n = snprintf(buf, sizeof(buf), " X%1.3f", x); |
681a62d7 JM |
545 | strncat(cmd, buf, n); |
546 | } | |
547 | if(!isnan(y)) { | |
037c350d | 548 | int n = snprintf(buf, sizeof(buf), " Y%1.3f", y); |
681a62d7 JM |
549 | strncat(cmd, buf, n); |
550 | } | |
551 | if(!isnan(z)) { | |
037c350d | 552 | int n = snprintf(buf, sizeof(buf), " Z%1.3f", z); |
681a62d7 JM |
553 | strncat(cmd, buf, n); |
554 | } | |
555 | ||
556 | // use specified feedrate (mm/sec) | |
037c350d | 557 | int n = snprintf(buf, sizeof(buf), " F%1.1f", feedrate * 60); // feed rate is converted to mm/min |
681a62d7 | 558 | strncat(cmd, buf, n); |
037c350d JM |
559 | if(relative) strcat(cmd, " G90"); |
560 | ||
561 | //THEKERNEL->streams->printf("DEBUG: move: %s\n", cmd); | |
681a62d7 | 562 | |
037c350d JM |
563 | // send as a command line as may have multiple G codes in it |
564 | struct SerialMessage message; | |
565 | message.message = cmd; | |
566 | message.stream = &(StreamOutput::NullStream); | |
567 | THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); | |
681a62d7 JM |
568 | THEKERNEL->conveyor->wait_for_empty_queue(); |
569 | } | |
570 | ||
571 | // issue home command | |
572 | void ZProbe::home() | |
573 | { | |
574 | Gcode gc("G28", &(StreamOutput::NullStream)); | |
575 | THEKERNEL->call_event(ON_GCODE_RECEIVED, &gc); | |
576 | } | |
57e927fa JM |
577 | |
578 | float ZProbe::zsteps_to_mm(float steps) | |
579 | { | |
580 | return steps / Z_STEPS_PER_MM; | |
581 | } |