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" |
a5542cae JM |
28 | |
29 | // strategies we know about | |
f6efadb0 | 30 | #include "DeltaCalibrationStrategy.h" |
a5542cae | 31 | #include "ThreePointStrategy.h" |
88443c6b | 32 | |
88443c6b JM |
33 | #define enable_checksum CHECKSUM("enable") |
34 | #define probe_pin_checksum CHECKSUM("probe_pin") | |
35 | #define debounce_count_checksum CHECKSUM("debounce_count") | |
681a62d7 JM |
36 | #define slow_feedrate_checksum CHECKSUM("slow_feedrate") |
37 | #define fast_feedrate_checksum CHECKSUM("fast_feedrate") | |
681a62d7 | 38 | #define probe_height_checksum CHECKSUM("probe_height") |
f3b66360 | 39 | #define gamma_max_checksum CHECKSUM("gamma_max") |
88443c6b | 40 | |
681a62d7 | 41 | // from endstop section |
b7cd847e | 42 | #define delta_homing_checksum CHECKSUM("delta_homing") |
88443c6b JM |
43 | |
44 | #define X_AXIS 0 | |
45 | #define Y_AXIS 1 | |
46 | #define Z_AXIS 2 | |
47 | ||
dd0a7cfa JM |
48 | #define STEPPER THEKERNEL->robot->actuators |
49 | #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm()) | |
56ce2b5a JM |
50 | #define Z_STEPS_PER_MM STEPS_PER_MM(Z_AXIS) |
51 | ||
7d6fe308 JM |
52 | #define abs(a) ((a<0) ? -a : a) |
53 | ||
88443c6b JM |
54 | void ZProbe::on_module_loaded() |
55 | { | |
56 | // if the module is disabled -> do nothing | |
56ce2b5a | 57 | if(!THEKERNEL->config->value( zprobe_checksum, enable_checksum )->by_default(false)->as_bool()) { |
88443c6b JM |
58 | // as this module is not needed free up the resource |
59 | delete this; | |
60 | return; | |
61 | } | |
681a62d7 | 62 | this->running = false; |
88443c6b JM |
63 | |
64 | // load settings | |
65 | this->on_config_reload(this); | |
66 | // register event-handlers | |
88443c6b | 67 | register_for_event(ON_GCODE_RECEIVED); |
88443c6b | 68 | |
38bf9a1c | 69 | THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->get_acceleration_ticks_per_second() , this, &ZProbe::acceleration_tick ); |
88443c6b JM |
70 | } |
71 | ||
72 | void ZProbe::on_config_reload(void *argument) | |
73 | { | |
681a62d7 JM |
74 | this->pin.from_string( THEKERNEL->config->value(zprobe_checksum, probe_pin_checksum)->by_default("nc" )->as_string())->as_input(); |
75 | this->debounce_count = THEKERNEL->config->value(zprobe_checksum, debounce_count_checksum)->by_default(0 )->as_number(); | |
76 | ||
ce9d2bda | 77 | // get strategies to load |
f6efadb0 JM |
78 | vector<uint16_t> modules; |
79 | THEKERNEL->config->get_module_list( &modules, leveling_strategy_checksum); | |
80 | for( auto cs : modules ){ | |
81 | if( THEKERNEL->config->value(leveling_strategy_checksum, cs, enable_checksum )->as_bool() ){ | |
82 | bool found= false; | |
83 | // check with each known strategy and load it if it matches | |
84 | switch(cs) { | |
85 | case delta_calibration_strategy_checksum: | |
86 | this->strategies.push_back(new DeltaCalibrationStrategy(this)); | |
87 | found= true; | |
88 | break; | |
a5542cae JM |
89 | |
90 | case three_point_leveling_strategy_checksum: | |
91 | // NOTE this strategy is mutually exclusive with the delta calibration strategy | |
92 | this->strategies.push_back(new ThreePointStrategy(this)); | |
93 | found= true; | |
94 | break; | |
95 | ||
f6efadb0 | 96 | // add other strategies here |
f6efadb0 | 97 | //case zheight_map_strategy: |
a5542cae | 98 | // this->strategies.push_back(new ZHeightMapStrategy(this)); |
f6efadb0 JM |
99 | // found= true; |
100 | // break; | |
101 | } | |
102 | if(found) this->strategies.back()->handleConfig(); | |
103 | } | |
104 | } | |
ce9d2bda | 105 | |
57e927fa | 106 | // need to know if we need to use delta kinematics for homing |
ce9d2bda | 107 | this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool(); |
ce9d2bda | 108 | |
f6efadb0 | 109 | // default for backwards compatibility add DeltaCalibrationStrategy if a delta |
57e927fa | 110 | // will be deprecated |
f6efadb0 | 111 | if(this->strategies.empty()) { |
57e927fa JM |
112 | if(this->is_delta) { |
113 | this->strategies.push_back(new DeltaCalibrationStrategy(this)); | |
114 | this->strategies.back()->handleConfig(); | |
115 | } | |
037c350d | 116 | } |
681a62d7 | 117 | |
f3b66360 | 118 | this->probe_height = THEKERNEL->config->value(zprobe_checksum, probe_height_checksum)->by_default(5.0F)->as_number(); |
681a62d7 JM |
119 | this->slow_feedrate = THEKERNEL->config->value(zprobe_checksum, slow_feedrate_checksum)->by_default(5)->as_number(); // feedrate in mm/sec |
120 | this->fast_feedrate = THEKERNEL->config->value(zprobe_checksum, fast_feedrate_checksum)->by_default(100)->as_number(); // feedrate in mm/sec | |
f3b66360 | 121 | this->max_z = THEKERNEL->config->value(gamma_max_checksum)->by_default(500)->as_number(); // maximum zprobe distance |
88443c6b JM |
122 | } |
123 | ||
fa7bcf7e | 124 | bool ZProbe::wait_for_probe(int& steps) |
88443c6b JM |
125 | { |
126 | unsigned int debounce = 0; | |
127 | while(true) { | |
128 | THEKERNEL->call_event(ON_IDLE); | |
129 | // if no stepper is moving, moves are finished and there was no touch | |
fa7bcf7e | 130 | if( !STEPPER[Z_AXIS]->is_moving() && (!is_delta || (!STEPPER[Y_AXIS]->is_moving() && !STEPPER[Z_AXIS]->is_moving())) ) { |
88443c6b JM |
131 | return false; |
132 | } | |
133 | ||
134 | // if the touchprobe is active... | |
135 | if( this->pin.get() ) { | |
136 | //...increase debounce counter... | |
137 | if( debounce < debounce_count) { | |
138 | // ...but only if the counter hasn't reached the max. value | |
139 | debounce++; | |
140 | } else { | |
141 | // ...otherwise stop the steppers, return its remaining steps | |
fa7bcf7e JM |
142 | if(STEPPER[Z_AXIS]->is_moving()){ |
143 | steps= STEPPER[Z_AXIS]->get_stepped(); | |
144 | STEPPER[Z_AXIS]->move(0, 0); | |
145 | } | |
146 | if(is_delta) { | |
147 | for( int i = X_AXIS; i <= Y_AXIS; i++ ) { | |
148 | if ( STEPPER[i]->is_moving() ) { | |
149 | STEPPER[i]->move(0, 0); | |
150 | } | |
88443c6b JM |
151 | } |
152 | } | |
153 | return true; | |
154 | } | |
155 | } else { | |
156 | // The probe was not hit yet, reset debounce counter | |
157 | debounce = 0; | |
158 | } | |
159 | } | |
160 | } | |
88443c6b JM |
161 | |
162 | // single probe and report amount moved | |
681a62d7 | 163 | bool ZProbe::run_probe(int& steps, bool fast) |
88443c6b JM |
164 | { |
165 | // Enable the motors | |
166 | THEKERNEL->stepper->turn_enable_pins_on(); | |
56ce2b5a | 167 | this->current_feedrate = (fast ? this->fast_feedrate : this->slow_feedrate) * Z_STEPS_PER_MM; // steps/sec |
f3b66360 | 168 | float maxz= this->max_z*2; |
88443c6b JM |
169 | |
170 | // move Z down | |
dd0a7cfa | 171 | STEPPER[Z_AXIS]->set_speed(0); // will be increased by acceleration tick |
f3b66360 | 172 | STEPPER[Z_AXIS]->move(true, maxz * Z_STEPS_PER_MM); // always probes down, no more than 2*maxz |
b7cd847e JM |
173 | if(this->is_delta) { |
174 | // for delta need to move all three actuators | |
dd0a7cfa | 175 | STEPPER[X_AXIS]->set_speed(0); |
f3b66360 | 176 | STEPPER[X_AXIS]->move(true, maxz * STEPS_PER_MM(X_AXIS)); |
dd0a7cfa | 177 | STEPPER[Y_AXIS]->set_speed(0); |
f3b66360 | 178 | STEPPER[Y_AXIS]->move(true, maxz * STEPS_PER_MM(Y_AXIS)); |
b7cd847e JM |
179 | } |
180 | ||
fa7bcf7e | 181 | // start acceration hrprocessing |
7d6fe308 JM |
182 | this->running = true; |
183 | ||
fa7bcf7e | 184 | bool r = wait_for_probe(steps); |
681a62d7 | 185 | this->running = false; |
88443c6b JM |
186 | return r; |
187 | } | |
188 | ||
681a62d7 JM |
189 | bool ZProbe::return_probe(int steps) |
190 | { | |
191 | // move probe back to where it was | |
c58d32a8 | 192 | this->current_feedrate = this->slow_feedrate*2 * Z_STEPS_PER_MM; // feedrate in steps/sec |
681a62d7 JM |
193 | bool dir= steps < 0; |
194 | steps= abs(steps); | |
195 | ||
dd0a7cfa JM |
196 | STEPPER[Z_AXIS]->set_speed(0); // will be increased by acceleration tick |
197 | STEPPER[Z_AXIS]->move(dir, steps); | |
681a62d7 | 198 | if(this->is_delta) { |
dd0a7cfa JM |
199 | STEPPER[X_AXIS]->set_speed(0); |
200 | STEPPER[X_AXIS]->move(dir, steps); | |
201 | STEPPER[Y_AXIS]->set_speed(0); | |
202 | STEPPER[Y_AXIS]->move(dir, steps); | |
681a62d7 | 203 | } |
7d6fe308 JM |
204 | |
205 | this->running = true; | |
fa7bcf7e | 206 | while(STEPPER[Z_AXIS]->is_moving() || (is_delta && (STEPPER[X_AXIS]->is_moving() || STEPPER[Y_AXIS]->is_moving())) ) { |
681a62d7 JM |
207 | // wait for it to complete |
208 | THEKERNEL->call_event(ON_IDLE); | |
209 | } | |
210 | ||
211 | this->running = false; | |
212 | ||
213 | return true; | |
214 | } | |
215 | ||
97832d6d JM |
216 | bool ZProbe::doProbeAt(int &steps, float x, float y) |
217 | { | |
218 | int s; | |
219 | // move to xy | |
220 | coordinated_move(x, y, NAN, getFastFeedrate()); | |
221 | if(!run_probe(s)) return false; | |
222 | ||
223 | // return to original Z | |
224 | return_probe(s); | |
225 | steps = s; | |
226 | ||
227 | return true; | |
228 | } | |
fc7b9a7b | 229 | |
0e44e7d7 JM |
230 | float ZProbe::probeDistance(float x, float y) |
231 | { | |
232 | int s; | |
233 | if(!doProbeAt(s, x, y)) return NAN; | |
234 | return zsteps_to_mm(s); | |
235 | } | |
236 | ||
88443c6b JM |
237 | void ZProbe::on_gcode_received(void *argument) |
238 | { | |
239 | Gcode *gcode = static_cast<Gcode *>(argument); | |
88443c6b | 240 | |
57e927fa | 241 | if( gcode->has_g && gcode->g >= 29 && gcode->g <= 32) { |
25dc6344 JM |
242 | // make sure the probe is defined and not already triggered before moving motors |
243 | if(!this->pin.connected()) { | |
244 | gcode->stream->printf("ZProbe not connected.\n"); | |
245 | return; | |
246 | } | |
247 | if(this->pin.get()) { | |
248 | gcode->stream->printf("ZProbe triggered before move, aborting command.\n"); | |
249 | return; | |
250 | } | |
251 | ||
681a62d7 | 252 | if( gcode->g == 30 ) { // simple Z probe |
bd96f4d7 | 253 | gcode->mark_as_taken(); |
88443c6b JM |
254 | // first wait for an empty queue i.e. no moves left |
255 | THEKERNEL->conveyor->wait_for_empty_queue(); | |
256 | ||
681a62d7 JM |
257 | int steps; |
258 | if(run_probe(steps)) { | |
56ce2b5a | 259 | gcode->stream->printf("Z:%1.4f C:%d\n", steps / Z_STEPS_PER_MM, steps); |
bd96f4d7 JM |
260 | // move back to where it started, unless a Z is specified |
261 | if(gcode->has_letter('Z')) { | |
262 | // set Z to the specified value, and leave probe where it is | |
263 | THEKERNEL->robot->reset_axis_position(gcode->get_value('Z'), Z_AXIS); | |
681a62d7 JM |
264 | } else { |
265 | return_probe(steps); | |
bd96f4d7 | 266 | } |
681a62d7 | 267 | } else { |
bd96f4d7 | 268 | gcode->stream->printf("ZProbe not triggered\n"); |
88443c6b | 269 | } |
fc7b9a7b | 270 | |
ce9d2bda | 271 | } else { |
57e927fa | 272 | // find a strategy to handle the gcode |
ce9d2bda JM |
273 | for(auto s : strategies){ |
274 | if(s->handleGcode(gcode)) { | |
275 | gcode->mark_as_taken(); | |
276 | return; | |
037c350d | 277 | } |
fc7b9a7b | 278 | } |
ce9d2bda | 279 | gcode->stream->printf("No strategy found to handle G%d\n", gcode->g); |
88443c6b JM |
280 | } |
281 | ||
282 | } else if(gcode->has_m) { | |
283 | // M code processing here | |
bd96f4d7 | 284 | if(gcode->m == 119) { |
681a62d7 | 285 | int c = this->pin.get(); |
bd96f4d7 JM |
286 | gcode->stream->printf(" Probe: %d", c); |
287 | gcode->add_nl = true; | |
288 | gcode->mark_as_taken(); | |
681a62d7 | 289 | |
ce9d2bda JM |
290 | }else { |
291 | for(auto s : strategies){ | |
292 | if(s->handleGcode(gcode)) { | |
293 | gcode->mark_as_taken(); | |
294 | return; | |
295 | } | |
296 | } | |
bd96f4d7 | 297 | } |
88443c6b JM |
298 | } |
299 | } | |
300 | ||
301 | #define max(a,b) (((a) > (b)) ? (a) : (b)) | |
302 | // Called periodically to change the speed to match acceleration | |
303 | uint32_t ZProbe::acceleration_tick(uint32_t dummy) | |
304 | { | |
305 | if(!this->running) return(0); // nothing to do | |
fa7bcf7e | 306 | if(STEPPER[Z_AXIS]->is_moving()) accelerate(Z_AXIS); |
88443c6b | 307 | |
fa7bcf7e JM |
308 | if(is_delta) { |
309 | // deltas needs to move all actuators | |
310 | for ( int c = X_AXIS; c <= Y_AXIS; c++ ) { | |
311 | if( !STEPPER[c]->is_moving() ) continue; | |
312 | accelerate(c); | |
313 | } | |
314 | } | |
88443c6b | 315 | |
fa7bcf7e JM |
316 | return 0; |
317 | } | |
88443c6b | 318 | |
fa7bcf7e JM |
319 | void ZProbe::accelerate(int c) |
320 | { uint32_t current_rate = STEPPER[c]->get_steps_per_second(); | |
321 | uint32_t target_rate = int(floor(this->current_feedrate)); | |
88443c6b | 322 | |
c5fe1787 JM |
323 | // Z may have a different acceleration to X and Y |
324 | float acc= (c==Z_AXIS) ? THEKERNEL->planner->get_z_acceleration() : THEKERNEL->planner->get_acceleration(); | |
fa7bcf7e | 325 | if( current_rate < target_rate ) { |
c5fe1787 | 326 | uint32_t rate_increase = int(floor((acc / THEKERNEL->stepper->get_acceleration_ticks_per_second()) * STEPS_PER_MM(c))); |
fa7bcf7e JM |
327 | current_rate = min( target_rate, current_rate + rate_increase ); |
328 | } | |
329 | if( current_rate > target_rate ) { | |
330 | current_rate = target_rate; | |
88443c6b JM |
331 | } |
332 | ||
fa7bcf7e JM |
333 | // steps per second |
334 | STEPPER[c]->set_speed(max(current_rate, THEKERNEL->stepper->get_minimum_steps_per_second())); | |
88443c6b | 335 | } |
681a62d7 JM |
336 | |
337 | // issue a coordinated move directly to robot, and return when done | |
338 | // Only move the coordinates that are passed in as not nan | |
037c350d | 339 | void ZProbe::coordinated_move(float x, float y, float z, float feedrate, bool relative) |
681a62d7 JM |
340 | { |
341 | char buf[32]; | |
037c350d JM |
342 | char cmd[64]; |
343 | ||
344 | if(relative) strcpy(cmd, "G91 G0 "); | |
345 | else strcpy(cmd, "G0 "); | |
346 | ||
681a62d7 | 347 | if(!isnan(x)) { |
037c350d | 348 | int n = snprintf(buf, sizeof(buf), " X%1.3f", x); |
681a62d7 JM |
349 | strncat(cmd, buf, n); |
350 | } | |
351 | if(!isnan(y)) { | |
037c350d | 352 | int n = snprintf(buf, sizeof(buf), " Y%1.3f", y); |
681a62d7 JM |
353 | strncat(cmd, buf, n); |
354 | } | |
355 | if(!isnan(z)) { | |
037c350d | 356 | int n = snprintf(buf, sizeof(buf), " Z%1.3f", z); |
681a62d7 JM |
357 | strncat(cmd, buf, n); |
358 | } | |
359 | ||
360 | // use specified feedrate (mm/sec) | |
037c350d | 361 | int n = snprintf(buf, sizeof(buf), " F%1.1f", feedrate * 60); // feed rate is converted to mm/min |
681a62d7 | 362 | strncat(cmd, buf, n); |
037c350d JM |
363 | if(relative) strcat(cmd, " G90"); |
364 | ||
365 | //THEKERNEL->streams->printf("DEBUG: move: %s\n", cmd); | |
681a62d7 | 366 | |
037c350d JM |
367 | // send as a command line as may have multiple G codes in it |
368 | struct SerialMessage message; | |
369 | message.message = cmd; | |
370 | message.stream = &(StreamOutput::NullStream); | |
371 | THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message ); | |
681a62d7 JM |
372 | THEKERNEL->conveyor->wait_for_empty_queue(); |
373 | } | |
374 | ||
375 | // issue home command | |
376 | void ZProbe::home() | |
377 | { | |
378 | Gcode gc("G28", &(StreamOutput::NullStream)); | |
379 | THEKERNEL->call_event(ON_GCODE_RECEIVED, &gc); | |
380 | } | |
57e927fa JM |
381 | |
382 | float ZProbe::zsteps_to_mm(float steps) | |
383 | { | |
384 | return steps / Z_STEPS_PER_MM; | |
385 | } |