add public set for endstop trims
[clinton/Smoothieware.git] / src / modules / tools / endstops / Endstops.cpp
CommitLineData
df27a6a3 1/*
201bcb94
AW
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.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
201bcb94
AW
6*/
7
8#include "libs/Module.h"
9#include "libs/Kernel.h"
10#include "modules/communication/utils/Gcode.h"
3fceb8eb 11#include "modules/robot/Conveyor.h"
201bcb94
AW
12#include "Endstops.h"
13#include "libs/nuts_bolts.h"
750277f8 14#include "libs/Pin.h"
670fa10b 15#include "libs/StepperMotor.h"
201bcb94 16#include "wait_api.h" // mbed.h lib
61134a65
JM
17#include "Robot.h"
18#include "Stepper.h"
19#include "Config.h"
20#include "SlowTicker.h"
21#include "Planner.h"
7af0714f
JM
22#include "checksumm.h"
23#include "utils.h"
8d54c34c 24#include "ConfigValue.h"
fc7b9a7b 25#include "libs/StreamOutput.h"
9f6f04a5
JM
26#include "PublicDataRequest.h"
27#include "EndstopsPublicAccess.h"
201bcb94 28
33e4cc02
JM
29#define ALPHA_AXIS 0
30#define BETA_AXIS 1
31#define GAMMA_AXIS 2
9a993543
JM
32#define X_AXIS 0
33#define Y_AXIS 1
34#define Z_AXIS 2
33e4cc02
JM
35
36#define NOT_HOMING 0
37#define MOVING_TO_ORIGIN_FAST 1
38#define MOVING_BACK 2
39#define MOVING_TO_ORIGIN_SLOW 3
40
41#define endstops_module_enable_checksum CHECKSUM("endstops_enable")
42#define corexy_homing_checksum CHECKSUM("corexy_homing")
43#define delta_homing_checksum CHECKSUM("delta_homing")
44
45#define alpha_min_endstop_checksum CHECKSUM("alpha_min_endstop")
46#define beta_min_endstop_checksum CHECKSUM("beta_min_endstop")
47#define gamma_min_endstop_checksum CHECKSUM("gamma_min_endstop")
48
49#define alpha_max_endstop_checksum CHECKSUM("alpha_max_endstop")
50#define beta_max_endstop_checksum CHECKSUM("beta_max_endstop")
51#define gamma_max_endstop_checksum CHECKSUM("gamma_max_endstop")
52
53#define alpha_trim_checksum CHECKSUM("alpha_trim")
54#define beta_trim_checksum CHECKSUM("beta_trim")
55#define gamma_trim_checksum CHECKSUM("gamma_trim")
56
57// these values are in steps and should be deprecated
58#define alpha_fast_homing_rate_checksum CHECKSUM("alpha_fast_homing_rate")
59#define beta_fast_homing_rate_checksum CHECKSUM("beta_fast_homing_rate")
60#define gamma_fast_homing_rate_checksum CHECKSUM("gamma_fast_homing_rate")
61
62#define alpha_slow_homing_rate_checksum CHECKSUM("alpha_slow_homing_rate")
63#define beta_slow_homing_rate_checksum CHECKSUM("beta_slow_homing_rate")
64#define gamma_slow_homing_rate_checksum CHECKSUM("gamma_slow_homing_rate")
65
66#define alpha_homing_retract_checksum CHECKSUM("alpha_homing_retract")
67#define beta_homing_retract_checksum CHECKSUM("beta_homing_retract")
68#define gamma_homing_retract_checksum CHECKSUM("gamma_homing_retract")
69#define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
70
71// same as above but in user friendly mm/s and mm
72#define alpha_fast_homing_rate_mm_checksum CHECKSUM("alpha_fast_homing_rate_mm_s")
73#define beta_fast_homing_rate_mm_checksum CHECKSUM("beta_fast_homing_rate_mm_s")
74#define gamma_fast_homing_rate_mm_checksum CHECKSUM("gamma_fast_homing_rate_mm_s")
75
76#define alpha_slow_homing_rate_mm_checksum CHECKSUM("alpha_slow_homing_rate_mm_s")
77#define beta_slow_homing_rate_mm_checksum CHECKSUM("beta_slow_homing_rate_mm_s")
78#define gamma_slow_homing_rate_mm_checksum CHECKSUM("gamma_slow_homing_rate_mm_s")
79
80#define alpha_homing_retract_mm_checksum CHECKSUM("alpha_homing_retract_mm")
81#define beta_homing_retract_mm_checksum CHECKSUM("beta_homing_retract_mm")
82#define gamma_homing_retract_mm_checksum CHECKSUM("gamma_homing_retract_mm")
83
84#define endstop_debounce_count_checksum CHECKSUM("endstop_debounce_count")
85
86#define alpha_homing_direction_checksum CHECKSUM("alpha_homing_direction")
87#define beta_homing_direction_checksum CHECKSUM("beta_homing_direction")
88#define gamma_homing_direction_checksum CHECKSUM("gamma_homing_direction")
89#define home_to_max_checksum CHECKSUM("home_to_max")
90#define home_to_min_checksum CHECKSUM("home_to_min")
91#define alpha_min_checksum CHECKSUM("alpha_min")
92#define beta_min_checksum CHECKSUM("beta_min")
93#define gamma_min_checksum CHECKSUM("gamma_min")
94
95#define alpha_max_checksum CHECKSUM("alpha_max")
96#define beta_max_checksum CHECKSUM("beta_max")
97#define gamma_max_checksum CHECKSUM("gamma_max")
98
56ce2b5a
JM
99#define STEPS_PER_MM(a) (this->steppers[a]->steps_per_mm)
100
33e4cc02
JM
101Endstops::Endstops()
102{
201bcb94 103 this->status = NOT_HOMING;
33e4cc02 104 home_offset[0] = home_offset[1] = home_offset[2] = 0.0F;
201bcb94
AW
105}
106
33e4cc02
JM
107void Endstops::on_module_loaded()
108{
7dee696d 109 // Do not do anything if not enabled
314ab8f7 110 if ( THEKERNEL->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) {
33e4cc02
JM
111 return;
112 }
7d62445b 113
476dcb96 114 register_for_event(ON_CONFIG_RELOAD);
9f6f04a5
JM
115 register_for_event(ON_GCODE_RECEIVED);
116 register_for_event(ON_GET_PUBLIC_DATA);
7d6fe308 117 register_for_event(ON_SET_PUBLIC_DATA);
201bcb94
AW
118
119 // Take StepperMotor objects from Robot and keep them here
314ab8f7
MM
120 this->steppers[0] = THEKERNEL->robot->alpha_stepper_motor;
121 this->steppers[1] = THEKERNEL->robot->beta_stepper_motor;
122 this->steppers[2] = THEKERNEL->robot->gamma_stepper_motor;
64eaf21e 123 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->acceleration_ticks_per_second , this, &Endstops::acceleration_tick );
201bcb94 124
750277f8
AW
125 // Settings
126 this->on_config_reload(this);
201bcb94
AW
127}
128
750277f8 129// Get config
33e4cc02
JM
130void Endstops::on_config_reload(void *argument)
131{
56ce2b5a
JM
132 this->pins[0].from_string( THEKERNEL->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
133 this->pins[1].from_string( THEKERNEL->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
134 this->pins[2].from_string( THEKERNEL->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
135 this->pins[3].from_string( THEKERNEL->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
136 this->pins[4].from_string( THEKERNEL->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
137 this->pins[5].from_string( THEKERNEL->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
138
139 // These are the old ones in steps still here for backwards compatibility
140 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(0);
141 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_checksum )->by_default(4000 )->as_number() / STEPS_PER_MM(1);
142 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_checksum )->by_default(6400 )->as_number() / STEPS_PER_MM(2);
143 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(0);
144 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_checksum )->by_default(2000 )->as_number() / STEPS_PER_MM(1);
145 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_checksum )->by_default(3200 )->as_number() / STEPS_PER_MM(2);
146 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(0);
147 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_checksum )->by_default(400 )->as_number() / STEPS_PER_MM(1);
148 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_checksum )->by_default(1600 )->as_number() / STEPS_PER_MM(2);
5de98d7c
JM
149
150 // newer mm based config values override the old ones, convert to steps/mm and steps, defaults to what was set in the older config settings above
56ce2b5a
JM
151 this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0])->as_number();
152 this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[1])->as_number();
153 this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2])->as_number();
154 this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0])->as_number();
155 this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[1])->as_number();
156 this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2])->as_number();
157 this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum )->by_default(this->retract_mm[0])->as_number();
158 this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum )->by_default(this->retract_mm[1])->as_number();
159 this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum )->by_default(this->retract_mm[2])->as_number();
5de98d7c 160
06c48ee8 161 this->debounce_count = THEKERNEL->config->value(endstop_debounce_count_checksum )->by_default(100)->as_number();
5de98d7c
JM
162
163
409ff5b3 164 // get homing direction and convert to boolean where true is home to min, and false is home to max
314ab8f7 165 int home_dir = get_checksum(THEKERNEL->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 166 this->home_direction[0] = home_dir != home_to_max_checksum;
47bbe224 167
314ab8f7 168 home_dir = get_checksum(THEKERNEL->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 169 this->home_direction[1] = home_dir != home_to_max_checksum;
47bbe224 170
314ab8f7 171 home_dir = get_checksum(THEKERNEL->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 172 this->home_direction[2] = home_dir != home_to_max_checksum;
47bbe224 173
314ab8f7
MM
174 this->homing_position[0] = this->home_direction[0] ? THEKERNEL->config->value(alpha_min_checksum)->by_default(0)->as_number() : THEKERNEL->config->value(alpha_max_checksum)->by_default(200)->as_number();
175 this->homing_position[1] = this->home_direction[1] ? THEKERNEL->config->value(beta_min_checksum )->by_default(0)->as_number() : THEKERNEL->config->value(beta_max_checksum )->by_default(200)->as_number();;
176 this->homing_position[2] = this->home_direction[2] ? THEKERNEL->config->value(gamma_min_checksum)->by_default(0)->as_number() : THEKERNEL->config->value(gamma_max_checksum)->by_default(200)->as_number();;
f29b0272 177
314ab8f7
MM
178 this->is_corexy = THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool();
179 this->is_delta = THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool();
e678365e 180
56ce2b5a 181 // endstop trim used by deltas to do soft adjusting, in mm, negate depending on homing direction
e678365e 182 // eg on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
33e4cc02
JM
183 int dirx = (this->home_direction[0] ? 1 : -1);
184 int diry = (this->home_direction[1] ? 1 : -1);
185 int dirz = (this->home_direction[2] ? 1 : -1);
56ce2b5a
JM
186 this->trim_mm[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0 )->as_number() * dirx;
187 this->trim_mm[1] = THEKERNEL->config->value(beta_trim_checksum )->by_default(0 )->as_number() * diry;
188 this->trim_mm[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0 )->as_number() * dirz;
a0e0d592
BG
189}
190
33e4cc02
JM
191void Endstops::wait_for_homed(char axes_to_move)
192{
a0e0d592 193 bool running = true;
33e4cc02
JM
194 unsigned int debounce[3] = {0, 0, 0};
195 while (running) {
a0e0d592 196 running = false;
314ab8f7 197 THEKERNEL->call_event(ON_IDLE);
56ce2b5a
JM
198 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
199 if ( ( axes_to_move >> c ) & 1 ) {
200 if ( this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) {
201 if ( debounce[c] < debounce_count ) {
202 debounce[c]++;
a0e0d592 203 running = true;
56ce2b5a
JM
204 } else if ( this->steppers[c]->moving ) {
205 this->steppers[c]->move(0, 0);
a0e0d592 206 }
33e4cc02 207 } else {
a0e0d592
BG
208 // The endstop was not hit yet
209 running = true;
56ce2b5a 210 debounce[c] = 0;
323cca60 211 }
a0e0d592
BG
212 }
213 }
214 }
750277f8 215}
201bcb94 216
f29b0272 217// this homing works for cartesian and delta printers, not for HBots/CoreXY
33e4cc02
JM
218void Endstops::do_homing(char axes_to_move)
219{
f29b0272
JM
220 // Start moving the axes to the origin
221 this->status = MOVING_TO_ORIGIN_FAST;
56ce2b5a
JM
222 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
223 if ( ( axes_to_move >> c) & 1 ) {
224 this->feed_rate[c]= this->fast_rates[c];
225 this->steppers[c]->set_speed(0);
226 this->steppers[c]->move(this->home_direction[c], 10000000);
f29b0272
JM
227 }
228 }
229
230 // Wait for all axes to have homed
231 this->wait_for_homed(axes_to_move);
232
233 // Move back a small distance
234 this->status = MOVING_BACK;
235 bool inverted_dir;
56ce2b5a
JM
236 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
237 if ( ( axes_to_move >> c ) & 1 ) {
238 inverted_dir = !this->home_direction[c];
239 this->feed_rate[c]= this->slow_rates[c];
240 this->steppers[c]->set_speed(0);
241 this->steppers[c]->move(inverted_dir, this->retract_mm[c]*STEPS_PER_MM(c));
f29b0272
JM
242 }
243 }
244
33e4cc02 245 // Wait for moves to be done
56ce2b5a
JM
246 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
247 if ( ( axes_to_move >> c ) & 1 ) {
248 while ( this->steppers[c]->moving ) {
314ab8f7 249 THEKERNEL->call_event(ON_IDLE);
f29b0272
JM
250 }
251 }
252 }
253
254 // Start moving the axes to the origin slowly
255 this->status = MOVING_TO_ORIGIN_SLOW;
56ce2b5a
JM
256 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
257 if ( ( axes_to_move >> c ) & 1 ) {
258 this->feed_rate[c]= this->slow_rates[c];
259 this->steppers[c]->set_speed(0);
260 this->steppers[c]->move(this->home_direction[c], 10000000);
f29b0272
JM
261 }
262 }
263
264 // Wait for all axes to have homed
265 this->wait_for_homed(axes_to_move);
266
33e4cc02 267 if (this->is_delta) {
959ab59c
JM
268 // move for soft trim
269 this->status = MOVING_BACK;
56ce2b5a
JM
270 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
271 if ( this->trim_mm[c] != 0.0F && ( axes_to_move >> c ) & 1 ) {
272 inverted_dir = this->home_direction[c];
273 // move up or down depending on sign of trim, -ive is down away from home
274 if (this->trim_mm[c] < 0) inverted_dir = !inverted_dir;
275 this->feed_rate[c]= this->slow_rates[c];
276 this->steppers[c]->set_speed(0);
277 this->steppers[c]->move(inverted_dir, abs(round(this->trim_mm[c]*STEPS_PER_MM(c))));
959ab59c 278 }
f29b0272 279 }
f29b0272 280
959ab59c 281 // Wait for moves to be done
56ce2b5a
JM
282 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
283 if ( ( axes_to_move >> c ) & 1 ) {
314ab8f7 284 //THEKERNEL->streams->printf("axis %c \r\n", c );
56ce2b5a 285 while ( this->steppers[c]->moving ) {
314ab8f7 286 THEKERNEL->call_event(ON_IDLE);
959ab59c 287 }
f29b0272
JM
288 }
289 }
290 }
291
292 // Homing is done
293 this->status = NOT_HOMING;
294}
295
33e4cc02
JM
296void Endstops::wait_for_homed_corexy(int axis)
297{
3db88866 298 bool running = true;
33e4cc02
JM
299 unsigned int debounce[3] = {0, 0, 0};
300 while (running) {
3db88866 301 running = false;
314ab8f7 302 THEKERNEL->call_event(ON_IDLE);
33e4cc02
JM
303 if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) {
304 if ( debounce[axis] < debounce_count ) {
3db88866
JM
305 debounce[axis] ++;
306 running = true;
307 } else {
308 // turn both off if running
33e4cc02
JM
309 if (this->steppers[X_AXIS]->moving) this->steppers[X_AXIS]->move(0, 0);
310 if (this->steppers[Y_AXIS]->moving) this->steppers[Y_AXIS]->move(0, 0);
3db88866 311 }
33e4cc02 312 } else {
3db88866
JM
313 // The endstop was not hit yet
314 running = true;
315 debounce[axis] = 0;
316 }
317 }
318}
319
1ad23cd3 320void Endstops::corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps)
33e4cc02
JM
321{
322 this->status = MOVING_TO_ORIGIN_FAST;
64eaf21e
JM
323 this->feed_rate[X_AXIS]= fast_rate;
324 this->steppers[X_AXIS]->set_speed(0);
33e4cc02 325 this->steppers[X_AXIS]->move(dirx, 10000000);
64eaf21e
JM
326 this->feed_rate[Y_AXIS]= fast_rate;
327 this->steppers[Y_AXIS]->set_speed(0);
33e4cc02 328 this->steppers[Y_AXIS]->move(diry, 10000000);
f29b0272 329
33e4cc02
JM
330 // wait for primary axis
331 this->wait_for_homed_corexy(home_axis);
f29b0272 332
33e4cc02
JM
333 // Move back a small distance
334 this->status = MOVING_BACK;
64eaf21e
JM
335 this->feed_rate[X_AXIS]= slow_rate;
336 this->steppers[X_AXIS]->set_speed(0);
33e4cc02 337 this->steppers[X_AXIS]->move(!dirx, retract_steps);
64eaf21e
JM
338 this->feed_rate[Y_AXIS]= slow_rate;
339 this->steppers[Y_AXIS]->set_speed(0);
33e4cc02
JM
340 this->steppers[Y_AXIS]->move(!diry, retract_steps);
341
342 // wait until done
9a993543 343 while ( this->steppers[X_AXIS]->moving || this->steppers[Y_AXIS]->moving) {
314ab8f7 344 THEKERNEL->call_event(ON_IDLE);
f29b0272
JM
345 }
346
33e4cc02
JM
347 // Start moving the axes to the origin slowly
348 this->status = MOVING_TO_ORIGIN_SLOW;
64eaf21e
JM
349 this->feed_rate[X_AXIS]= slow_rate;
350 this->steppers[X_AXIS]->set_speed(0);
33e4cc02 351 this->steppers[X_AXIS]->move(dirx, 10000000);
64eaf21e
JM
352 this->feed_rate[Y_AXIS]= slow_rate;
353 this->steppers[Y_AXIS]->set_speed(0);
33e4cc02 354 this->steppers[Y_AXIS]->move(diry, 10000000);
f29b0272 355
33e4cc02
JM
356 // wait for primary axis
357 this->wait_for_homed_corexy(home_axis);
358}
f29b0272 359
33e4cc02
JM
360// this homing works for HBots/CoreXY
361void Endstops::do_homing_corexy(char axes_to_move)
362{
03dffc07 363 // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally
174d9961 364 // To move XY at the same time only one motor needs to turn, determine which motor and which direction based on min or max directions
03dffc07 365 // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed
174d9961 366 // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit
174d9961
JM
367
368 if((axes_to_move & 0x03) == 0x03) { // both X and Y need Homing
369 // determine which motor to turn and which way
370 bool dirx= this->home_direction[X_AXIS];
371 bool diry= this->home_direction[Y_AXIS];
372 int motor;
373 bool dir;
374 if(dirx && diry) { // min/min
375 motor= X_AXIS;
376 dir= true;
377 }else if(dirx && !diry) { // min/max
378 motor= Y_AXIS;
379 dir= true;
380 }else if(!dirx && diry) { // max/min
381 motor= Y_AXIS;
382 dir= false;
383 }else if(!dirx && !diry) { // max/max
384 motor= X_AXIS;
385 dir= false;
386 }
387
388 // then move both X and Y until one hits the endstop
389 this->status = MOVING_TO_ORIGIN_FAST;
64eaf21e
JM
390 this->feed_rate[motor]= this->fast_rates[motor]*1.4142;
391 this->steppers[motor]->set_speed(0); // need to allow for more ground covered when moving diagonally
174d9961
JM
392 this->steppers[motor]->move(dir, 10000000);
393 // wait until either X or Y hits the endstop
394 bool running= true;
395 while (running) {
314ab8f7 396 THEKERNEL->call_event(ON_IDLE);
174d9961
JM
397 for(int m=X_AXIS;m<=Y_AXIS;m++) {
398 if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) {
399 // turn off motor
400 if(this->steppers[motor]->moving) this->steppers[motor]->move(0, 0);
401 running= false;
402 break;
403 }
404 }
405 }
406 }
33e4cc02 407
174d9961 408 // move individual axis
33e4cc02 409 if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
413dd90c 410 bool dir= this->home_direction[X_AXIS];
56ce2b5a 411 corexy_home(X_AXIS, dir, dir, this->fast_rates[X_AXIS], this->slow_rates[X_AXIS], this->retract_mm[X_AXIS]*STEPS_PER_MM(X_AXIS));
f29b0272
JM
412 }
413
65191d91
JM
414 if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
415 bool dir= this->home_direction[Y_AXIS];
56ce2b5a 416 corexy_home(Y_AXIS, dir, !dir, this->fast_rates[Y_AXIS], this->slow_rates[Y_AXIS], this->retract_mm[Y_AXIS]*STEPS_PER_MM(Y_AXIS));
65191d91
JM
417 }
418
33e4cc02 419 if (axes_to_move & 0x04) { // move Z
f29b0272
JM
420 do_homing(0x04); // just home normally for Z
421 }
422
423 // Homing is done
424 this->status = NOT_HOMING;
425}
426
201bcb94 427// Start homing sequences by response to GCode commands
33e4cc02 428void Endstops::on_gcode_received(void *argument)
c339d634 429{
33e4cc02
JM
430 Gcode *gcode = static_cast<Gcode *>(argument);
431 if ( gcode->has_g) {
432 if ( gcode->g == 28 ) {
74b6303c 433 gcode->mark_as_taken();
df27a6a3 434 // G28 is received, we have homing to do
201bcb94
AW
435
436 // First wait for the queue to be empty
314ab8f7 437 THEKERNEL->conveyor->wait_for_empty_queue();
201bcb94
AW
438
439 // Do we move select axes or all of them
7dee696d 440 char axes_to_move = 0;
959ab59c 441 // only enable homing if the endstop is defined, deltas always home all axis
33e4cc02 442 bool home_all = this->is_delta || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') );
47bbe224 443
56ce2b5a
JM
444 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
445 if ( (home_all || gcode->has_letter(c+'X')) && this->pins[c + (this->home_direction[c] ? 0 : 3)].connected() ) {
446 axes_to_move += ( 1 << c );
33e4cc02 447 }
df27a6a3 448 }
3b948656 449
f6c04440 450 // Enable the motors
314ab8f7 451 THEKERNEL->stepper->turn_enable_pins_on();
f6c04440 452
f29b0272 453 // do the actual homing
33e4cc02 454 if (is_corexy)
f29b0272
JM
455 do_homing_corexy(axes_to_move);
456 else
457 do_homing(axes_to_move);
3ffe27fb 458
33e4cc02 459 // Zero the ax(i/e)s position, add in the home offset
56ce2b5a 460 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
33e4cc02 461 if ( (axes_to_move >> c) & 1 ) {
314ab8f7 462 THEKERNEL->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c);
3ffe27fb
AV
463 }
464 }
c339d634 465 }
33e4cc02
JM
466 } else if (gcode->has_m) {
467 switch (gcode->m) {
468 case 119: {
469
470 int px = this->home_direction[0] ? 0 : 3;
471 int py = this->home_direction[1] ? 1 : 4;
472 int pz = this->home_direction[2] ? 2 : 5;
473 const char *mx = this->home_direction[0] ? "min" : "max";
474 const char *my = this->home_direction[1] ? "min" : "max";
475 const char *mz = this->home_direction[2] ? "min" : "max";
476
bd96f4d7
JM
477 gcode->stream->printf("X %s:%d Y %s:%d Z %s:%d", mx, this->pins[px].get(), my, this->pins[py].get(), mz, this->pins[pz].get());
478 gcode->add_nl= true;
33e4cc02
JM
479 gcode->mark_as_taken();
480 }
481 break;
482
483 case 206: // M206 - set homing offset
484 if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X');
485 if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y');
486 if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z');
487 gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
488 gcode->mark_as_taken();
489 break;
490
491 case 500: // save settings
492 case 503: // print settings
493 gcode->stream->printf(";Home offset (mm):\nM206 X%1.2f Y%1.2f Z%1.2f\n", home_offset[0], home_offset[1], home_offset[2]);
494 if (is_delta) {
56ce2b5a
JM
495 gcode->stream->printf(";Trim (mm):\nM666 X%1.3f Y%1.3f Z%1.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
496 gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position[2]);
7a8fe6e0 497 }
33e4cc02 498 gcode->mark_as_taken();
c339d634 499 break;
47bbe224 500
ec4773e5 501 case 665: { // M665 - set max gamma/z height
33e4cc02 502 gcode->mark_as_taken();
1ad23cd3 503 float gamma_max = this->homing_position[2];
33e4cc02
JM
504 if (gcode->has_letter('Z')) {
505 this->homing_position[2] = gamma_max = gcode->get_value('Z');
ec4773e5 506 }
33e4cc02
JM
507 gcode->stream->printf("Max Z %8.3f ", gamma_max);
508 gcode->add_nl = true;
509 }
510 break;
ec4773e5 511
47bbe224 512
56ce2b5a
JM
513 case 666:
514 if(this->is_delta) { // M666 - set trim for each axis in mm, NB negative mm trim is down
515 if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X');
516 if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y');
517 if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z');
47bbe224 518
56ce2b5a
JM
519 // print the current trim values in mm
520 gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
521 gcode->mark_as_taken();
522 }
33e4cc02 523 break;
47bbe224 524
d4ee6ee2
JM
525 // NOTE this is to test accuracy of lead screws etc.
526 case 910: { // M910 - move specific number of raw steps
a4b04adc
JM
527 // Enable the motors
528 THEKERNEL->stepper->turn_enable_pins_on();
529
d4ee6ee2
JM
530 int x= 0, y=0 , z= 0, f= 200*16;
531 if (gcode->has_letter('F')) f = gcode->get_value('F');
532 if (gcode->has_letter('X')) {
533 x = gcode->get_value('X');
534 this->steppers[X_AXIS]->set_speed(f);
535 this->steppers[X_AXIS]->move(x<0, abs(x));
536 }
537 if (gcode->has_letter('Y')) {
538 y = gcode->get_value('Y');
539 this->steppers[Y_AXIS]->set_speed(f);
540 this->steppers[Y_AXIS]->move(y<0, abs(y));
541 }
542 if (gcode->has_letter('Z')) {
543 z = gcode->get_value('Z');
544 this->steppers[Z_AXIS]->set_speed(f);
545 this->steppers[Z_AXIS]->move(z<0, abs(z));
546 }
547 gcode->stream->printf("Moved X %d Y %d Z %d F %d steps\n", x, y, z, f);
548 gcode->mark_as_taken();
549 break;
550 }
201bcb94
AW
551 }
552 }
553}
554
64eaf21e
JM
555#define max(a,b) (((a) > (b)) ? (a) : (b))
556// Called periodically to change the speed to match acceleration
557uint32_t Endstops::acceleration_tick(uint32_t dummy)
558{
559 if(this->status == NOT_HOMING) return(0); // nothing to do
560
561 // foreach stepper that is moving
562 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
563 if( !this->steppers[c]->moving ) continue;
564
565 uint32_t current_rate = this->steppers[c]->steps_per_second;
56ce2b5a 566 uint32_t target_rate = int(floor(this->feed_rate[c]*STEPS_PER_MM(c)));
64eaf21e
JM
567
568 if( current_rate < target_rate ){
56ce2b5a 569 uint32_t rate_increase = int(floor((THEKERNEL->planner->acceleration/THEKERNEL->stepper->acceleration_ticks_per_second)*STEPS_PER_MM(c)));
64eaf21e
JM
570 current_rate = min( target_rate, current_rate + rate_increase );
571 }
572 if( current_rate > target_rate ){ current_rate = target_rate; }
573
574 // steps per second
575 this->steppers[c]->set_speed(max(current_rate, THEKERNEL->stepper->minimum_steps_per_second));
576 }
577
578 return 0;
579}
9f6f04a5
JM
580
581void Endstops::on_get_public_data(void* argument){
582 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
583
584 if(!pdr->starts_with(endstops_checksum)) return;
585
586 if(pdr->second_element_is(trim_checksum)) {
587 static float return_data[3];
588 return_data[0]= this->trim_mm[0];
589 return_data[1]= this->trim_mm[1];
590 return_data[2]= this->trim_mm[2];
591
592 pdr->set_data_ptr(&return_data);
593 pdr->set_taken();
594 }
595}
7d6fe308
JM
596
597void Endstops::on_set_public_data(void* argument){
598 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
599
600 if(!pdr->starts_with(endstops_checksum)) return;
601
602 if(pdr->second_element_is(trim_checksum)) {
603 float *t= static_cast<float*>(pdr->get_data_ptr());
604 this->trim_mm[0]= t[0];
605 this->trim_mm[1]= t[1];
606 this->trim_mm[2]= t[2];
607 pdr->set_taken();
608 }
609}