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