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