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