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