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/>.
11 #include "BaseSolution.h"
14 #include "StepperMotor.h"
15 #include "StreamOutputPool.h"
19 #include "checksumm.h"
20 #include "ConfigValue.h"
21 #include "SlowTicker.h"
23 #include "SerialMessage.h"
24 #include "PublicDataRequest.h"
25 #include "EndstopsPublicAccess.h"
26 #include "PublicData.h"
27 #include "LevelingStrategy.h"
29 // strategies we know about
30 #include "DeltaCalibrationStrategy.h"
31 #include "ThreePointStrategy.h"
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 #define gamma_max_checksum CHECKSUM("gamma_max")
41 // from endstop section
42 #define delta_homing_checksum CHECKSUM("delta_homing")
48 #define STEPPER THEKERNEL->robot->actuators
49 #define STEPS_PER_MM(a) (STEPPER[a]->get_steps_per_mm())
50 #define Z_STEPS_PER_MM STEPS_PER_MM(Z_AXIS)
52 #define abs(a) ((a<0) ? -a : a)
54 void ZProbe::on_module_loaded()
56 // if the module is disabled -> do nothing
57 if(!THEKERNEL
->config
->value( zprobe_checksum
, enable_checksum
)->by_default(false)->as_bool()) {
58 // as this module is not needed free up the resource
62 this->running
= false;
65 this->on_config_reload(this);
66 // register event-handlers
67 register_for_event(ON_GCODE_RECEIVED
);
69 THEKERNEL
->slow_ticker
->attach( THEKERNEL
->stepper
->get_acceleration_ticks_per_second() , this, &ZProbe::acceleration_tick
);
72 void ZProbe::on_config_reload(void *argument
)
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();
77 // get strategies to load
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() ){
83 // check with each known strategy and load it if it matches
85 case delta_calibration_strategy_checksum
:
86 this->strategies
.push_back(new DeltaCalibrationStrategy(this));
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));
96 // add other strategies here
97 //case zheight_map_strategy:
98 // this->strategies.push_back(new ZHeightMapStrategy(this));
102 if(found
) this->strategies
.back()->handleConfig();
106 // need to know if we need to use delta kinematics for homing
107 this->is_delta
= THEKERNEL
->config
->value(delta_homing_checksum
)->by_default(false)->as_bool();
109 // default for backwards compatibility add DeltaCalibrationStrategy if a delta
110 // will be deprecated
111 if(this->strategies
.empty()) {
113 this->strategies
.push_back(new DeltaCalibrationStrategy(this));
114 this->strategies
.back()->handleConfig();
118 this->probe_height
= THEKERNEL
->config
->value(zprobe_checksum
, probe_height_checksum
)->by_default(5.0F
)->as_number();
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
121 this->max_z
= THEKERNEL
->config
->value(gamma_max_checksum
)->by_default(500)->as_number(); // maximum zprobe distance
124 bool ZProbe::wait_for_probe(int& steps
)
126 unsigned int debounce
= 0;
128 THEKERNEL
->call_event(ON_IDLE
);
129 // if no stepper is moving, moves are finished and there was no touch
130 if( !STEPPER
[Z_AXIS
]->is_moving() && (!is_delta
|| (!STEPPER
[Y_AXIS
]->is_moving() && !STEPPER
[Z_AXIS
]->is_moving())) ) {
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
141 // ...otherwise stop the steppers, return its remaining steps
142 if(STEPPER
[Z_AXIS
]->is_moving()){
143 steps
= STEPPER
[Z_AXIS
]->get_stepped();
144 STEPPER
[Z_AXIS
]->move(0, 0);
147 for( int i
= X_AXIS
; i
<= Y_AXIS
; i
++ ) {
148 if ( STEPPER
[i
]->is_moving() ) {
149 STEPPER
[i
]->move(0, 0);
156 // The probe was not hit yet, reset debounce counter
162 // single probe and report amount moved
163 bool ZProbe::run_probe(int& steps
, bool fast
)
166 THEKERNEL
->stepper
->turn_enable_pins_on();
167 this->current_feedrate
= (fast
? this->fast_feedrate
: this->slow_feedrate
) * Z_STEPS_PER_MM
; // steps/sec
168 float maxz
= this->max_z
*2;
171 STEPPER
[Z_AXIS
]->set_speed(0); // will be increased by acceleration tick
172 STEPPER
[Z_AXIS
]->move(true, maxz
* Z_STEPS_PER_MM
); // always probes down, no more than 2*maxz
174 // for delta need to move all three actuators
175 STEPPER
[X_AXIS
]->set_speed(0);
176 STEPPER
[X_AXIS
]->move(true, maxz
* STEPS_PER_MM(X_AXIS
));
177 STEPPER
[Y_AXIS
]->set_speed(0);
178 STEPPER
[Y_AXIS
]->move(true, maxz
* STEPS_PER_MM(Y_AXIS
));
181 // start acceration hrprocessing
182 this->running
= true;
184 bool r
= wait_for_probe(steps
);
185 this->running
= false;
189 bool ZProbe::return_probe(int steps
)
191 // move probe back to where it was
192 this->current_feedrate
= this->slow_feedrate
*2 * Z_STEPS_PER_MM
; // feedrate in steps/sec
196 STEPPER
[Z_AXIS
]->set_speed(0); // will be increased by acceleration tick
197 STEPPER
[Z_AXIS
]->move(dir
, steps
);
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
);
205 this->running
= true;
206 while(STEPPER
[Z_AXIS
]->is_moving() || (is_delta
&& (STEPPER
[X_AXIS
]->is_moving() || STEPPER
[Y_AXIS
]->is_moving())) ) {
207 // wait for it to complete
208 THEKERNEL
->call_event(ON_IDLE
);
211 this->running
= false;
216 bool ZProbe::doProbeAt(int &steps
, float x
, float y
)
220 coordinated_move(x
, y
, NAN
, getFastFeedrate());
221 if(!run_probe(s
)) return false;
223 // return to original Z
230 float ZProbe::probeDistance(float x
, float y
)
233 if(!doProbeAt(s
, x
, y
)) return NAN
;
234 return zsteps_to_mm(s
);
237 void ZProbe::on_gcode_received(void *argument
)
239 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
241 if( gcode
->has_g
&& gcode
->g
>= 29 && gcode
->g
<= 32) {
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");
247 if(this->pin
.get()) {
248 gcode
->stream
->printf("ZProbe triggered before move, aborting command.\n");
252 if( gcode
->g
== 30 ) { // simple Z probe
253 gcode
->mark_as_taken();
254 // first wait for an empty queue i.e. no moves left
255 THEKERNEL
->conveyor
->wait_for_empty_queue();
258 if(run_probe(steps
)) {
259 gcode
->stream
->printf("Z:%1.4f C:%d\n", steps
/ Z_STEPS_PER_MM
, steps
);
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
);
268 gcode
->stream
->printf("ZProbe not triggered\n");
272 // find a strategy to handle the gcode
273 for(auto s
: strategies
){
274 if(s
->handleGcode(gcode
)) {
275 gcode
->mark_as_taken();
279 gcode
->stream
->printf("No strategy found to handle G%d\n", gcode
->g
);
282 } else if(gcode
->has_m
) {
283 // M code processing here
284 if(gcode
->m
== 119) {
285 int c
= this->pin
.get();
286 gcode
->stream
->printf(" Probe: %d", c
);
287 gcode
->add_nl
= true;
288 gcode
->mark_as_taken();
291 for(auto s
: strategies
){
292 if(s
->handleGcode(gcode
)) {
293 gcode
->mark_as_taken();
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
)
305 if(!this->running
) return(0); // nothing to do
306 if(STEPPER
[Z_AXIS
]->is_moving()) accelerate(Z_AXIS
);
309 // deltas needs to move all actuators
310 for ( int c
= X_AXIS
; c
<= Y_AXIS
; c
++ ) {
311 if( !STEPPER
[c
]->is_moving() ) continue;
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
));
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();
325 if( current_rate
< target_rate
) {
326 uint32_t rate_increase
= int(floor((acc
/ THEKERNEL
->stepper
->get_acceleration_ticks_per_second()) * STEPS_PER_MM(c
)));
327 current_rate
= min( target_rate
, current_rate
+ rate_increase
);
329 if( current_rate
> target_rate
) {
330 current_rate
= target_rate
;
334 STEPPER
[c
]->set_speed(max(current_rate
, THEKERNEL
->stepper
->get_minimum_steps_per_second()));
337 // issue a coordinated move directly to robot, and return when done
338 // Only move the coordinates that are passed in as not nan
339 void ZProbe::coordinated_move(float x
, float y
, float z
, float feedrate
, bool relative
)
344 if(relative
) strcpy(cmd
, "G91 G0 ");
345 else strcpy(cmd
, "G0 ");
348 int n
= snprintf(buf
, sizeof(buf
), " X%1.3f", x
);
349 strncat(cmd
, buf
, n
);
352 int n
= snprintf(buf
, sizeof(buf
), " Y%1.3f", y
);
353 strncat(cmd
, buf
, n
);
356 int n
= snprintf(buf
, sizeof(buf
), " Z%1.3f", z
);
357 strncat(cmd
, buf
, n
);
360 // use specified feedrate (mm/sec)
361 int n
= snprintf(buf
, sizeof(buf
), " F%1.1f", feedrate
* 60); // feed rate is converted to mm/min
362 strncat(cmd
, buf
, n
);
363 if(relative
) strcat(cmd
, " G90");
365 //THEKERNEL->streams->printf("DEBUG: move: %s\n", cmd);
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
);
372 THEKERNEL
->conveyor
->wait_for_empty_queue();
375 // issue home command
378 Gcode
gc("G28", &(StreamOutput::NullStream
));
379 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, &gc
);
382 float ZProbe::zsteps_to_mm(float steps
)
384 return steps
/ Z_STEPS_PER_MM
;