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" | |
88443c6b JM |
18 | #include "checksumm.h" |
19 | #include "ConfigValue.h" | |
20 | #include "SlowTicker.h" | |
21 | #include "Planner.h" | |
037c350d | 22 | #include "SerialMessage.h" |
9f6f04a5 JM |
23 | #include "PublicDataRequest.h" |
24 | #include "EndstopsPublicAccess.h" | |
25 | #include "PublicData.h" | |
ce9d2bda | 26 | #include "LevelingStrategy.h" |
a157d099 | 27 | #include "StepTicker.h" |
37102904 | 28 | #include "utils.h" |
a5542cae JM |
29 | |
30 | // strategies we know about | |
f6efadb0 | 31 | #include "DeltaCalibrationStrategy.h" |
a5542cae | 32 | #include "ThreePointStrategy.h" |
59d4d4ea | 33 | #include "DeltaGridStrategy.h" |
6c972e51 | 34 | #include "CartGridStrategy.h" |
88443c6b | 35 | |
88443c6b JM |
36 | #define enable_checksum CHECKSUM("enable") |
37 | #define probe_pin_checksum CHECKSUM("probe_pin") | |
7778d1ce | 38 | #define debounce_ms_checksum CHECKSUM("debounce_ms") |
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") |
2f46bd5c | 44 | #define max_z_checksum CHECKSUM("max_z") |
93f20a8c | 45 | #define reverse_z_direction_checksum CHECKSUM("reverse_z") |
6c37f7b4 | 46 | #define dwell_before_probing_checksum CHECKSUM("dwell_before_probing") |
88443c6b | 47 | |
681a62d7 | 48 | // from endstop section |
b7cd847e | 49 | #define delta_homing_checksum CHECKSUM("delta_homing") |
d8198a57 | 50 | #define rdelta_homing_checksum CHECKSUM("rdelta_homing") |
88443c6b JM |
51 | |
52 | #define X_AXIS 0 | |
53 | #define Y_AXIS 1 | |
54 | #define Z_AXIS 2 | |
55 | ||
c8bac202 | 56 | #define STEPPER THEROBOT->actuators |
dd0a7cfa | 57 | #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm()) |
56ce2b5a JM |
58 | #define Z_STEPS_PER_MM STEPS_PER_MM(Z_AXIS) |
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 | |
de5787e0 | 70 | this->config_load(); |
88443c6b | 71 | // register event-handlers |
88443c6b | 72 | register_for_event(ON_GCODE_RECEIVED); |
88443c6b | 73 | |
fc6c9aac | 74 | // we read the probe in this timer |
778093ce JM |
75 | probing= false; |
76 | THEKERNEL->slow_ticker->attach(1000, this, &ZProbe::read_probe); | |
88443c6b JM |
77 | } |
78 | ||
de5787e0 | 79 | void ZProbe::config_load() |
88443c6b | 80 | { |
681a62d7 | 81 | this->pin.from_string( THEKERNEL->config->value(zprobe_checksum, probe_pin_checksum)->by_default("nc" )->as_string())->as_input(); |
7778d1ce | 82 | this->debounce_ms = THEKERNEL->config->value(zprobe_checksum, debounce_ms_checksum)->by_default(0 )->as_number(); |
681a62d7 | 83 | |
ce9d2bda | 84 | // get strategies to load |
f6efadb0 JM |
85 | vector<uint16_t> modules; |
86 | THEKERNEL->config->get_module_list( &modules, leveling_strategy_checksum); | |
87 | for( auto cs : modules ){ | |
88 | if( THEKERNEL->config->value(leveling_strategy_checksum, cs, enable_checksum )->as_bool() ){ | |
89 | bool found= false; | |
9ad47eab JM |
90 | LevelingStrategy *ls= nullptr; |
91 | ||
f6efadb0 JM |
92 | // check with each known strategy and load it if it matches |
93 | switch(cs) { | |
94 | case delta_calibration_strategy_checksum: | |
9ad47eab | 95 | ls= new DeltaCalibrationStrategy(this); |
f6efadb0 JM |
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 | |
9ad47eab | 101 | ls= new ThreePointStrategy(this); |
a5542cae JM |
102 | found= true; |
103 | break; | |
104 | ||
59d4d4ea | 105 | case delta_grid_leveling_strategy_checksum: |
9ad47eab | 106 | ls= new DeltaGridStrategy(this); |
59d4d4ea JM |
107 | found= true; |
108 | break; | |
6c972e51 JL |
109 | |
110 | case cart_grid_leveling_strategy_checksum: | |
111 | ls= new CartGridStrategy(this); | |
112 | found= true; | |
113 | break; | |
f6efadb0 | 114 | } |
9ad47eab JM |
115 | if(found) { |
116 | if(ls->handleConfig()) { | |
117 | this->strategies.push_back(ls); | |
118 | }else{ | |
119 | delete ls; | |
120 | } | |
121 | } | |
f6efadb0 JM |
122 | } |
123 | } | |
ce9d2bda | 124 | |
57e927fa | 125 | // need to know if we need to use delta kinematics for homing |
ce9d2bda | 126 | this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool(); |
d8198a57 | 127 | this->is_rdelta = THEKERNEL->config->value(rdelta_homing_checksum)->by_default(false)->as_bool(); |
ce9d2bda | 128 | |
f6efadb0 | 129 | // default for backwards compatibility add DeltaCalibrationStrategy if a delta |
81c3ecbf | 130 | // may be deprecated |
f6efadb0 | 131 | if(this->strategies.empty()) { |
57e927fa JM |
132 | if(this->is_delta) { |
133 | this->strategies.push_back(new DeltaCalibrationStrategy(this)); | |
134 | this->strategies.back()->handleConfig(); | |
135 | } | |
037c350d | 136 | } |
681a62d7 | 137 | |
f3b66360 | 138 | this->probe_height = THEKERNEL->config->value(zprobe_checksum, probe_height_checksum)->by_default(5.0F)->as_number(); |
681a62d7 JM |
139 | this->slow_feedrate = THEKERNEL->config->value(zprobe_checksum, slow_feedrate_checksum)->by_default(5)->as_number(); // feedrate in mm/sec |
140 | this->fast_feedrate = THEKERNEL->config->value(zprobe_checksum, fast_feedrate_checksum)->by_default(100)->as_number(); // feedrate in mm/sec | |
b469231e | 141 | this->return_feedrate = THEKERNEL->config->value(zprobe_checksum, return_feedrate_checksum)->by_default(0)->as_number(); // feedrate in mm/sec |
bac3c948 | 142 | this->reverse_z = THEKERNEL->config->value(zprobe_checksum, reverse_z_direction_checksum)->by_default(false)->as_bool(); // Z probe moves in reverse direction |
2f46bd5c JM |
143 | this->max_z = THEKERNEL->config->value(zprobe_checksum, max_z_checksum)->by_default(NAN)->as_number(); // maximum zprobe distance |
144 | if(isnan(this->max_z)){ | |
145 | this->max_z = THEKERNEL->config->value(gamma_max_checksum)->by_default(200)->as_number(); // maximum zprobe distance | |
146 | } | |
6c37f7b4 | 147 | this->dwell_before_probing = THEKERNEL->config->value(zprobe_checksum, dwell_before_probing_checksum)->by_default(0)->as_number(); // dwell time in seconds before probing |
2f46bd5c | 148 | |
88443c6b JM |
149 | } |
150 | ||
6d142b73 | 151 | uint32_t ZProbe::read_probe(uint32_t dummy) |
88443c6b | 152 | { |
6d142b73 | 153 | if(!probing || probe_detected) return 0; |
88443c6b | 154 | |
8fe38353 JM |
155 | // we check all axis as it maybe a G38.2 X10 for instance, not just a probe in Z |
156 | if(STEPPER[X_AXIS]->is_moving() || STEPPER[Y_AXIS]->is_moving() || STEPPER[Z_AXIS]->is_moving()) { | |
6d142b73 JM |
157 | // if it is moving then we check the probe, and debounce it |
158 | if(this->pin.get()) { | |
7778d1ce | 159 | if(debounce < debounce_ms) { |
88443c6b JM |
160 | debounce++; |
161 | } else { | |
6d142b73 JM |
162 | // we signal the motors to stop, which will preempt any moves on that axis |
163 | // we do all motors as it may be a delta | |
164 | for(auto &a : THEROBOT->actuators) a->stop_moving(); | |
165 | probe_detected= true; | |
166 | debounce= 0; | |
88443c6b | 167 | } |
6d142b73 | 168 | |
88443c6b | 169 | } else { |
6d142b73 JM |
170 | // The endstop was not hit yet |
171 | debounce= 0; | |
88443c6b JM |
172 | } |
173 | } | |
6d142b73 JM |
174 | |
175 | return 0; | |
88443c6b | 176 | } |
88443c6b | 177 | |
6d142b73 | 178 | // single probe in Z with custom feedrate |
771fb7b2 | 179 | // returns boolean value indicating if probe was triggered |
6d142b73 | 180 | bool ZProbe::run_probe(float& mm, float feedrate, float max_dist, bool reverse) |
88443c6b | 181 | { |
89ef36ce | 182 | if(dwell_before_probing > .0001F) safe_delay_ms(dwell_before_probing*1000); |
183 | ||
965cc29a JM |
184 | if(this->pin.get()) { |
185 | // probe already triggered so abort | |
186 | return false; | |
187 | } | |
188 | ||
36a21d48 | 189 | float maxz= max_dist < 0 ? this->max_z*2 : max_dist; |
88443c6b | 190 | |
6d142b73 JM |
191 | probing= true; |
192 | probe_detected= false; | |
193 | debounce= 0; | |
194 | ||
195 | // save current actuator position so we can report how far we moved | |
eb0d75d3 | 196 | float z_start_pos= THEROBOT->actuators[Z_AXIS]->get_current_position(); |
6d142b73 | 197 | |
88443c6b | 198 | // move Z down |
8fd0f38d | 199 | bool dir= (!reverse_z != reverse); // xor |
6d142b73 | 200 | float delta[3]= {0,0,0}; |
7778d1ce | 201 | delta[Z_AXIS]= dir ? -maxz : maxz; |
121094a5 | 202 | THEROBOT->delta_move(delta, feedrate, 3); |
b7cd847e | 203 | |
6d142b73 | 204 | // wait until finished |
04782655 | 205 | THECONVEYOR->wait_for_idle(); |
7d6fe308 | 206 | |
6d142b73 | 207 | // now see how far we moved, get delta in z we moved |
29e809e0 | 208 | // NOTE this works for deltas as well as all three actuators move the same amount in Z |
eb0d75d3 | 209 | mm= z_start_pos - THEROBOT->actuators[2]->get_current_position(); |
6d142b73 JM |
210 | |
211 | // set the last probe position to the actuator units moved during this home | |
eb0d75d3 | 212 | THEROBOT->set_last_probe_position(std::make_tuple(0, 0, mm, probe_detected?1:0)); |
6d142b73 | 213 | |
7778d1ce JM |
214 | probing= false; |
215 | ||
216 | if(probe_detected) { | |
217 | // if the probe stopped the move we need to correct the last_milestone as it did not reach where it thought | |
218 | THEROBOT->reset_position_from_current_actuator_position(); | |
219 | } | |
6d142b73 JM |
220 | |
221 | return probe_detected; | |
88443c6b JM |
222 | } |
223 | ||
71f0df19 JM |
224 | // do probe then return to start position |
225 | bool ZProbe::run_probe_return(float& mm, float feedrate, float max_dist, bool reverse) | |
681a62d7 | 226 | { |
eb0d75d3 | 227 | float save_z_pos= THEROBOT->get_axis_position(Z_AXIS); |
71f0df19 JM |
228 | |
229 | bool ok= run_probe(mm, feedrate, max_dist, reverse); | |
230 | ||
681a62d7 | 231 | // move probe back to where it was |
b469231e RM |
232 | float fr; |
233 | if(this->return_feedrate != 0) { // use return_feedrate if set | |
234 | fr = this->return_feedrate; | |
235 | } else { | |
236 | fr = this->slow_feedrate*2; // nominally twice slow feedrate | |
237 | if(fr > this->fast_feedrate) fr = this->fast_feedrate; // unless that is greater than fast feedrate | |
238 | } | |
239 | ||
c42bad8f | 240 | // absolute move back to saved starting position |
eb0d75d3 | 241 | coordinated_move(NAN, NAN, save_z_pos, fr, false); |
681a62d7 | 242 | |
71f0df19 | 243 | return ok; |
681a62d7 JM |
244 | } |
245 | ||
6d142b73 | 246 | bool ZProbe::doProbeAt(float &mm, float x, float y) |
97832d6d | 247 | { |
97832d6d JM |
248 | // move to xy |
249 | coordinated_move(x, y, NAN, getFastFeedrate()); | |
71f0df19 | 250 | return run_probe_return(mm, slow_feedrate); |
97832d6d | 251 | } |
fc7b9a7b | 252 | |
88443c6b JM |
253 | void ZProbe::on_gcode_received(void *argument) |
254 | { | |
255 | Gcode *gcode = static_cast<Gcode *>(argument); | |
88443c6b | 256 | |
57e927fa | 257 | if( gcode->has_g && gcode->g >= 29 && gcode->g <= 32) { |
36a21d48 | 258 | |
25dc6344 | 259 | // make sure the probe is defined and not already triggered before moving motors |
36a21d48 | 260 | if(!this->pin.connected()) { |
7b18a698 | 261 | gcode->stream->printf("ZProbe pin not configured.\n"); |
25dc6344 JM |
262 | return; |
263 | } | |
965cc29a | 264 | |
25dc6344 JM |
265 | if(this->pin.get()) { |
266 | gcode->stream->printf("ZProbe triggered before move, aborting command.\n"); | |
267 | return; | |
268 | } | |
269 | ||
681a62d7 | 270 | if( gcode->g == 30 ) { // simple Z probe |
8fe38353 | 271 | // first wait for all moves to finish |
04782655 | 272 | THEKERNEL->conveyor->wait_for_idle(); |
88443c6b | 273 | |
71f0df19 | 274 | bool set_z= (gcode->has_letter('Z') && !is_rdelta); |
771fb7b2 | 275 | bool probe_result; |
93f20a8c JM |
276 | bool reverse= (gcode->has_letter('R') && gcode->get_value('R') != 0); // specify to probe in reverse direction |
277 | float rate= gcode->has_letter('F') ? gcode->get_value('F') / 60 : this->slow_feedrate; | |
7778d1ce | 278 | float mm; |
71f0df19 JM |
279 | |
280 | // if not setting Z then return probe to where it started, otherwise leave it where it is | |
281 | probe_result = (set_z ? run_probe(mm, rate, -1, reverse) : run_probe_return(mm, rate, -1, reverse)); | |
771fb7b2 RM |
282 | |
283 | if(probe_result) { | |
71f0df19 | 284 | // the result is in actuator coordinates moved |
28177f27 | 285 | gcode->stream->printf("Z:%1.4f\n", THEKERNEL->robot->from_millimeters(mm)); |
078f76e0 | 286 | |
71f0df19 | 287 | if(set_z) { |
fd2341bc JM |
288 | // set current Z to the specified value, shortcut for G92 Znnn |
289 | char buf[32]; | |
290 | int n = snprintf(buf, sizeof(buf), "G92 Z%f", gcode->get_value('Z')); | |
291 | string g(buf, n); | |
292 | Gcode gc(g, &(StreamOutput::NullStream)); | |
293 | THEKERNEL->call_event(ON_GCODE_RECEIVED, &gc); | |
bd96f4d7 | 294 | } |
078f76e0 | 295 | |
681a62d7 | 296 | } else { |
bd96f4d7 | 297 | gcode->stream->printf("ZProbe not triggered\n"); |
88443c6b | 298 | } |
fc7b9a7b | 299 | |
ce9d2bda | 300 | } else { |
aaf0c0ee | 301 | if(!gcode->has_letter('P')) { |
fc92f0ac JM |
302 | // find the first strategy to handle the gcode |
303 | for(auto s : strategies){ | |
304 | if(s->handleGcode(gcode)) { | |
305 | return; | |
306 | } | |
307 | } | |
308 | gcode->stream->printf("No strategy found to handle G%d\n", gcode->g); | |
309 | ||
310 | }else{ | |
aaf0c0ee JM |
311 | // P paramater selects which strategy to send the code to |
312 | // they are loaded in the order they are defined in config, 0 being the first, 1 being the second and so on. | |
313 | uint16_t i= gcode->get_value('P'); | |
314 | if(i < strategies.size()) { | |
fc92f0ac | 315 | if(!strategies[i]->handleGcode(gcode)){ |
aaf0c0ee | 316 | gcode->stream->printf("strategy #%d did not handle G%d\n", i, gcode->g); |
fc92f0ac | 317 | } |
ce9d2bda | 318 | return; |
fc92f0ac JM |
319 | |
320 | }else{ | |
aaf0c0ee | 321 | gcode->stream->printf("strategy #%d is not loaded\n", i); |
037c350d | 322 | } |
fc7b9a7b | 323 | } |
88443c6b JM |
324 | } |
325 | ||
6c0193b3 | 326 | } else if(gcode->has_g && gcode->g == 38 ) { // G38.2 Straight Probe with error, G38.3 straight probe without error |
36a21d48 | 327 | // linuxcnc/grbl style probe http://www.linuxcnc.org/docs/2.5/html/gcode/gcode.html#sec:G38-probe |
dca67b99 JR |
328 | if(gcode->subcode < 2 || gcode->subcode > 5) { |
329 | gcode->stream->printf("error:Only G38.2, G38.3, G38.4, and G38.5 are supported\n"); | |
36a21d48 JM |
330 | return; |
331 | } | |
332 | ||
333 | // make sure the probe is defined and not already triggered before moving motors | |
334 | if(!this->pin.connected()) { | |
e714bd32 | 335 | gcode->stream->printf("error:ZProbe not connected.\n"); |
36a21d48 JM |
336 | return; |
337 | } | |
17f26e01 | 338 | |
dca67b99 JR |
339 | if(gcode->subcode == 4 || gcode->subcode == 5) { |
340 | if(!this->pin.get()) { | |
341 | gcode->stream->printf("error:ZProbe triggered before move, aborting command.\n"); | |
342 | return; | |
343 | } | |
344 | } else { | |
345 | if(this->pin.get()) { | |
346 | gcode->stream->printf("error:ZProbe triggered before move, aborting command.\n"); | |
347 | return; | |
348 | } | |
36a21d48 JM |
349 | } |
350 | ||
8fe38353 | 351 | // first wait for all moves to finish |
04782655 | 352 | THEKERNEL->conveyor->wait_for_idle(); |
36a21d48 | 353 | |
78cfd180 | 354 | float x= NAN, y=NAN, z=NAN; |
36a21d48 | 355 | if(gcode->has_letter('X')) { |
78cfd180 JM |
356 | x= gcode->get_value('X'); |
357 | } | |
36a21d48 | 358 | |
78cfd180 JM |
359 | if(gcode->has_letter('Y')) { |
360 | y= gcode->get_value('Y'); | |
361 | } | |
36a21d48 | 362 | |
78cfd180 JM |
363 | if(gcode->has_letter('Z')) { |
364 | z= gcode->get_value('Z'); | |
365 | } | |
36a21d48 | 366 | |
78cfd180 | 367 | if(isnan(x) && isnan(y) && isnan(z)) { |
e714bd32 | 368 | gcode->stream->printf("error:at least one of X Y or Z must be specified\n"); |
78cfd180 | 369 | return; |
36a21d48 | 370 | } |
7484e84a | 371 | |
dca67b99 | 372 | if(gcode->subcode == 4 || gcode->subcode == 5) { |
dca67b99 JR |
373 | pin.set_inverting(pin.is_inverting() != 1); |
374 | } | |
375 | ||
78cfd180 JM |
376 | probe_XYZ(gcode, x, y, z); |
377 | ||
dca67b99 | 378 | if(gcode->subcode == 4 || gcode->subcode == 5) { |
dca67b99 JR |
379 | pin.set_inverting(pin.is_inverting() != 1); |
380 | } | |
381 | ||
36a21d48 JM |
382 | return; |
383 | ||
88443c6b JM |
384 | } else if(gcode->has_m) { |
385 | // M code processing here | |
3434eac0 RM |
386 | int c; |
387 | switch (gcode->m) { | |
388 | case 119: | |
389 | c = this->pin.get(); | |
390 | gcode->stream->printf(" Probe: %d", c); | |
391 | gcode->add_nl = true; | |
392 | break; | |
393 | ||
394 | case 670: | |
395 | if (gcode->has_letter('S')) this->slow_feedrate = gcode->get_value('S'); | |
396 | if (gcode->has_letter('K')) this->fast_feedrate = gcode->get_value('K'); | |
397 | if (gcode->has_letter('R')) this->return_feedrate = gcode->get_value('R'); | |
398 | if (gcode->has_letter('Z')) this->max_z = gcode->get_value('Z'); | |
399 | if (gcode->has_letter('H')) this->probe_height = gcode->get_value('H'); | |
39f1d9bd JM |
400 | if (gcode->has_letter('I')) { // NOTE this is temporary and toggles the invertion status of the pin |
401 | invert_override= (gcode->get_value('I') != 0); | |
402 | pin.set_inverting(pin.is_inverting() != invert_override); // XOR so inverted pin is not inverted and vice versa | |
403 | } | |
6c37f7b4 | 404 | if (gcode->has_letter('D')) this->dwell_before_probing = gcode->get_value('D'); |
3434eac0 RM |
405 | break; |
406 | ||
407 | case 500: // save settings | |
408 | case 503: // print settings | |
6c37f7b4 | 409 | gcode->stream->printf(";Probe feedrates Slow/fast(K)/Return (mm/sec) max_z (mm) height (mm) dwell (s):\nM670 S%1.2f K%1.2f R%1.2f Z%1.2f H%1.2f D%1.2f\n", |
410 | this->slow_feedrate, this->fast_feedrate, this->return_feedrate, this->max_z, this->probe_height, this->dwell_before_probing); | |
fc92f0ac | 411 | |
3434eac0 RM |
412 | // fall through is intended so leveling strategies can handle m-codes too |
413 | ||
414 | default: | |
415 | for(auto s : strategies){ | |
416 | if(s->handleGcode(gcode)) { | |
417 | return; | |
418 | } | |
ce9d2bda | 419 | } |
bd96f4d7 | 420 | } |
88443c6b JM |
421 | } |
422 | } | |
423 | ||
778093ce | 424 | // special way to probe in the X or Y or Z direction using planned moves, should work with any kinematics |
78cfd180 | 425 | void ZProbe::probe_XYZ(Gcode *gcode, float x, float y, float z) |
37102904 | 426 | { |
778093ce JM |
427 | // enable the probe checking in the timer |
428 | probing= true; | |
429 | probe_detected= false; | |
c8bac202 | 430 | THEROBOT->disable_segmentation= true; // we must disable segmentation as this won't work with it enabled (beware on deltas probing in X or Y) |
37102904 | 431 | |
73cad2e1 JM |
432 | // get probe feedrate in mm/min and convert to mm/sec if specified |
433 | float rate = (gcode->has_letter('F')) ? gcode->get_value('F')/60 : this->slow_feedrate; | |
37102904 | 434 | |
778093ce | 435 | // do a regular move which will stop as soon as the probe is triggered, or the distance is reached |
78cfd180 | 436 | coordinated_move(x, y, z, rate, true); |
37102904 | 437 | |
778093ce JM |
438 | // coordinated_move returns when the move is finished |
439 | ||
440 | // disable probe checking | |
441 | probing= false; | |
c8bac202 | 442 | THEROBOT->disable_segmentation= false; |
37102904 | 443 | |
8fe38353 JM |
444 | // if the probe stopped the move we need to correct the last_milestone as it did not reach where it thought |
445 | // this also sets last_milestone to the machine coordinates it stopped at | |
446 | THEROBOT->reset_position_from_current_actuator_position(); | |
37102904 | 447 | float pos[3]; |
8fe38353 | 448 | THEROBOT->get_axis_position(pos, 3); |
37102904 | 449 | |
778093ce | 450 | uint8_t probeok= this->probe_detected ? 1 : 0; |
b76a9926 | 451 | |
17f26e01 | 452 | // print results using the GRBL format |
28177f27 | 453 | gcode->stream->printf("[PRB:%1.3f,%1.3f,%1.3f:%d]\n", THEKERNEL->robot->from_millimeters(pos[X_AXIS]), THEKERNEL->robot->from_millimeters(pos[Y_AXIS]), THEKERNEL->robot->from_millimeters(pos[Z_AXIS]), probeok); |
c8bac202 | 454 | THEROBOT->set_last_probe_position(std::make_tuple(pos[X_AXIS], pos[Y_AXIS], pos[Z_AXIS], probeok)); |
b76a9926 | 455 | |
dca67b99 JR |
456 | if(probeok == 0 && (gcode->subcode == 2 || gcode->subcode == 4)) { |
457 | // issue error if probe was not triggered and subcode is 2 or 4 | |
39c0196b | 458 | gcode->stream->printf("ALARM: Probe fail\n"); |
93f20a8c | 459 | THEKERNEL->call_event(ON_HALT, nullptr); |
778093ce | 460 | } |
37102904 JM |
461 | } |
462 | ||
681a62d7 JM |
463 | // issue a coordinated move directly to robot, and return when done |
464 | // Only move the coordinates that are passed in as not nan | |
6d142b73 | 465 | // NOTE must use G53 to force move in machine coordinates and ignore any WCS offsets |
037c350d | 466 | void ZProbe::coordinated_move(float x, float y, float z, float feedrate, bool relative) |
681a62d7 | 467 | { |
3aa7e94e JM |
468 | #define CMDLEN 128 |
469 | char *cmd= new char[CMDLEN]; // use heap here to reduce stack usage | |
037c350d JM |
470 | |
471 | if(relative) strcpy(cmd, "G91 G0 "); | |
e0be983d | 472 | else strcpy(cmd, "G53 G0 "); // G53 forces movement in machine coordinate system |
037c350d | 473 | |
681a62d7 | 474 | if(!isnan(x)) { |
eb0d75d3 | 475 | size_t n= strlen(cmd); |
3aa7e94e | 476 | snprintf(&cmd[n], CMDLEN-n, " X%1.3f", x); |
681a62d7 JM |
477 | } |
478 | if(!isnan(y)) { | |
eb0d75d3 | 479 | size_t n= strlen(cmd); |
3aa7e94e | 480 | snprintf(&cmd[n], CMDLEN-n, " Y%1.3f", y); |
681a62d7 JM |
481 | } |
482 | if(!isnan(z)) { | |
eb0d75d3 | 483 | size_t n= strlen(cmd); |
3aa7e94e | 484 | snprintf(&cmd[n], CMDLEN-n, " Z%1.3f", z); |
eb0d75d3 JM |
485 | } |
486 | ||
487 | { | |
488 | size_t n= strlen(cmd); | |
489 | // use specified feedrate (mm/sec) | |
3aa7e94e | 490 | snprintf(&cmd[n], CMDLEN-n, " F%1.1f", feedrate * 60); // feed rate is converted to mm/min |
681a62d7 JM |
491 | } |
492 | ||
037c350d JM |
493 | if(relative) strcat(cmd, " G90"); |
494 | ||
b9281d2b | 495 | //THEKERNEL->streams->printf("DEBUG: move: %s: %u\n", cmd, strlen(cmd)); |
681a62d7 | 496 | |
037c350d | 497 | // send as a command line as may have multiple G codes in it |
049c9aae | 498 | THEROBOT->push_state(); |
037c350d JM |
499 | struct SerialMessage message; |
500 | message.message = cmd; | |
bb9951c2 JM |
501 | delete [] cmd; |
502 | ||
037c350d JM |
503 | message.stream = &(StreamOutput::NullStream); |
504 | THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); | |
04782655 | 505 | THEKERNEL->conveyor->wait_for_idle(); |
049c9aae | 506 | THEROBOT->pop_state(); |
bb9951c2 | 507 | |
681a62d7 JM |
508 | } |
509 | ||
510 | // issue home command | |
511 | void ZProbe::home() | |
512 | { | |
9a6cabd8 | 513 | Gcode gc(THEKERNEL->is_grbl_mode() ? "G28.2" : "G28", &(StreamOutput::NullStream)); |
681a62d7 JM |
514 | THEKERNEL->call_event(ON_GCODE_RECEIVED, &gc); |
515 | } |