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")
40 // from endstop section
41 #define delta_homing_checksum CHECKSUM("delta_homing")
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)
51 #define abs(a) ((a<0) ? -a : a)
53 void ZProbe::on_module_loaded()
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
61 this->running
= false;
64 this->on_config_reload(this);
65 // register event-handlers
66 register_for_event(ON_GCODE_RECEIVED
);
68 THEKERNEL
->slow_ticker
->attach( THEKERNEL
->stepper
->get_acceleration_ticks_per_second() , this, &ZProbe::acceleration_tick
);
71 void ZProbe::on_config_reload(void *argument
)
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();
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() ){
82 // check with each known strategy and load it if it matches
84 case delta_calibration_strategy_checksum
:
85 this->strategies
.push_back(new DeltaCalibrationStrategy(this));
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));
95 // add other strategies here
96 //case zheight_map_strategy:
97 // this->strategies.push_back(new ZHeightMapStrategy(this));
101 if(found
) this->strategies
.back()->handleConfig();
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();
108 // default for backwards compatibility add DeltaCalibrationStrategy if a delta
109 // will be deprecated
110 if(this->strategies
.empty()) {
112 this->strategies
.push_back(new DeltaCalibrationStrategy(this));
113 this->strategies
.back()->handleConfig();
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
122 bool ZProbe::wait_for_probe(int steps
[3])
124 unsigned int debounce
= 0;
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() ) {
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
139 // ...otherwise stop the steppers, return its remaining steps
140 for( int i
= X_AXIS
; i
<= Z_AXIS
; i
++ ) {
142 if ( STEPPER
[i
]->is_moving() ) {
143 steps
[i
] = STEPPER
[i
]->get_stepped();
144 STEPPER
[i
]->move(0, 0);
150 // The probe was not hit yet, reset debounce counter
156 // single probe and report amount moved
157 bool ZProbe::run_probe(int& steps
, bool fast
)
160 THEKERNEL
->stepper
->turn_enable_pins_on();
161 this->current_feedrate
= (fast
? this->fast_feedrate
: this->slow_feedrate
) * Z_STEPS_PER_MM
; // steps/sec
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
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
));
174 this->running
= true;
177 bool r
= wait_for_probe(s
);
178 steps
= s
[Z_AXIS
]; // only need z
179 this->running
= false;
183 bool ZProbe::return_probe(int steps
)
185 // move probe back to where it was
186 this->current_feedrate
= this->fast_feedrate
* Z_STEPS_PER_MM
; // feedrate in steps/sec
190 STEPPER
[Z_AXIS
]->set_speed(0); // will be increased by acceleration tick
191 STEPPER
[Z_AXIS
]->move(dir
, steps
);
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
);
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
);
205 this->running
= false;
210 bool ZProbe::doProbeAt(int &steps
, float x
, float y
)
214 coordinated_move(x
, y
, NAN
, getFastFeedrate());
215 if(!run_probe(s
)) return false;
217 // return to original Z
224 float ZProbe::probeDistance(float x
, float y
)
227 if(!doProbeAt(s
, x
, y
)) return NAN
;
228 return zsteps_to_mm(s
);
231 void ZProbe::on_gcode_received(void *argument
)
233 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
235 if( gcode
->has_g
&& gcode
->g
>= 29 && gcode
->g
<= 32) {
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();
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");
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
);
259 gcode
->stream
->printf("ZProbe not triggered\n");
263 // find a strategy to handle the gcode
264 for(auto s
: strategies
){
265 if(s
->handleGcode(gcode
)) {
266 gcode
->mark_as_taken();
270 gcode
->stream
->printf("No strategy found to handle G%d\n", gcode
->g
);
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();
282 for(auto s
: strategies
){
283 if(s
->handleGcode(gcode
)) {
284 gcode
->mark_as_taken();
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
)
296 if(!this->running
) return(0); // nothing to do
298 // foreach stepper that is moving
299 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
300 if( !STEPPER
[c
]->is_moving() ) continue;
302 uint32_t current_rate
= STEPPER
[c
]->get_steps_per_second();
303 uint32_t target_rate
= int(floor(this->current_feedrate
));
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
);
309 if( current_rate
> target_rate
) {
310 current_rate
= target_rate
;
314 STEPPER
[c
]->set_speed(max(current_rate
, THEKERNEL
->stepper
->get_minimum_steps_per_second()));
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
)
327 if(relative
) strcpy(cmd
, "G91 G0 ");
328 else strcpy(cmd
, "G0 ");
331 int n
= snprintf(buf
, sizeof(buf
), " X%1.3f", x
);
332 strncat(cmd
, buf
, n
);
335 int n
= snprintf(buf
, sizeof(buf
), " Y%1.3f", y
);
336 strncat(cmd
, buf
, n
);
339 int n
= snprintf(buf
, sizeof(buf
), " Z%1.3f", z
);
340 strncat(cmd
, buf
, n
);
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");
348 //THEKERNEL->streams->printf("DEBUG: move: %s\n", cmd);
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();
358 // issue home command
361 Gcode
gc("G28", &(StreamOutput::NullStream
));
362 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, &gc
);
365 float ZProbe::zsteps_to_mm(float steps
)
367 return steps
/ Z_STEPS_PER_MM
;