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