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"
28 #include "DeltaCalibrationStrategy.h"
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")
37 // from endstop section
38 #define delta_homing_checksum CHECKSUM("delta_homing")
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)
48 #define abs(a) ((a<0) ? -a : a)
50 void ZProbe::on_module_loaded()
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
58 this->running
= false;
61 this->on_config_reload(this);
62 // register event-handlers
63 register_for_event(ON_GCODE_RECEIVED
);
65 THEKERNEL
->slow_ticker
->attach( THEKERNEL
->stepper
->get_acceleration_ticks_per_second() , this, &ZProbe::acceleration_tick
);
68 void ZProbe::on_config_reload(void *argument
)
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();
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() ){
79 // check with each known strategy and load it if it matches
81 case delta_calibration_strategy_checksum
:
82 this->strategies
.push_back(new DeltaCalibrationStrategy(this));
85 // add other strategies here
86 //case three_point_plane_checksum:
87 // this->strategies.push_back(new TheePointPlaneStrategey(this));
90 //case zheight_map_strategy:
91 // this->strategies.push_back(new ZHeightMapStratergy(this));
95 if(found
) this->strategies
.back()->handleConfig();
99 this->is_delta
= THEKERNEL
->config
->value(delta_homing_checksum
)->by_default(false)->as_bool();
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();
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
113 bool ZProbe::wait_for_probe(int steps
[3])
115 unsigned int debounce
= 0;
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() ) {
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
130 // ...otherwise stop the steppers, return its remaining steps
131 for( int i
= X_AXIS
; i
<= Z_AXIS
; i
++ ) {
133 if ( STEPPER
[i
]->is_moving() ) {
134 steps
[i
] = STEPPER
[i
]->get_stepped();
135 STEPPER
[i
]->move(0, 0);
141 // The probe was not hit yet, reset debounce counter
147 // single probe and report amount moved
148 bool ZProbe::run_probe(int& steps
, bool fast
)
151 THEKERNEL
->stepper
->turn_enable_pins_on();
152 this->current_feedrate
= (fast
? this->fast_feedrate
: this->slow_feedrate
) * Z_STEPS_PER_MM
; // steps/sec
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
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
));
165 this->running
= true;
168 bool r
= wait_for_probe(s
);
169 steps
= s
[Z_AXIS
]; // only need z
170 this->running
= false;
174 bool ZProbe::return_probe(int steps
)
176 // move probe back to where it was
177 this->current_feedrate
= this->fast_feedrate
* Z_STEPS_PER_MM
; // feedrate in steps/sec
181 STEPPER
[Z_AXIS
]->set_speed(0); // will be increased by acceleration tick
182 STEPPER
[Z_AXIS
]->move(dir
, steps
);
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
);
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
);
196 this->running
= false;
202 void ZProbe::on_gcode_received(void *argument
)
204 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
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();
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");
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
);
230 gcode
->stream
->printf("ZProbe not triggered\n");
234 for(auto s
: strategies
){
235 if(s
->handleGcode(gcode
)) {
236 gcode
->mark_as_taken();
240 gcode
->stream
->printf("No strategy found to handle G%d\n", gcode
->g
);
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();
252 for(auto s
: strategies
){
253 if(s
->handleGcode(gcode
)) {
254 gcode
->mark_as_taken();
258 gcode
->stream
->printf("No strategy found to handle M%d\n", gcode
->m
);
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
)
267 if(!this->running
) return(0); // nothing to do
269 // foreach stepper that is moving
270 for ( int c
= X_AXIS
; c
<= Z_AXIS
; c
++ ) {
271 if( !STEPPER
[c
]->is_moving() ) continue;
273 uint32_t current_rate
= STEPPER
[c
]->get_steps_per_second();
274 uint32_t target_rate
= int(floor(this->current_feedrate
));
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
);
280 if( current_rate
> target_rate
) {
281 current_rate
= target_rate
;
285 STEPPER
[c
]->set_speed(max(current_rate
, THEKERNEL
->stepper
->get_minimum_steps_per_second()));
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
)
298 if(relative
) strcpy(cmd
, "G91 G0 ");
299 else strcpy(cmd
, "G0 ");
302 int n
= snprintf(buf
, sizeof(buf
), " X%1.3f", x
);
303 strncat(cmd
, buf
, n
);
306 int n
= snprintf(buf
, sizeof(buf
), " Y%1.3f", y
);
307 strncat(cmd
, buf
, n
);
310 int n
= snprintf(buf
, sizeof(buf
), " Z%1.3f", z
);
311 strncat(cmd
, buf
, n
);
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");
319 //THEKERNEL->streams->printf("DEBUG: move: %s\n", cmd);
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();
329 // issue home command
332 Gcode
gc("G28", &(StreamOutput::NullStream
));
333 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, &gc
);