cleanup a bunch of fields that should be private
[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;
38bf9a1c 123 THEKERNEL->slow_ticker->attach( THEKERNEL->stepper->get_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
192bca32
JM
181 // endstop trim used by deltas to do soft adjusting
182 // on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
183 this->trim_mm[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0 )->as_number();
184 this->trim_mm[1] = THEKERNEL->config->value(beta_trim_checksum )->by_default(0 )->as_number();
185 this->trim_mm[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0 )->as_number();
a0e0d592
BG
186}
187
33e4cc02
JM
188void Endstops::wait_for_homed(char axes_to_move)
189{
a0e0d592 190 bool running = true;
33e4cc02
JM
191 unsigned int debounce[3] = {0, 0, 0};
192 while (running) {
a0e0d592 193 running = false;
314ab8f7 194 THEKERNEL->call_event(ON_IDLE);
56ce2b5a
JM
195 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
196 if ( ( axes_to_move >> c ) & 1 ) {
197 if ( this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) {
198 if ( debounce[c] < debounce_count ) {
199 debounce[c]++;
a0e0d592 200 running = true;
56ce2b5a
JM
201 } else if ( this->steppers[c]->moving ) {
202 this->steppers[c]->move(0, 0);
a0e0d592 203 }
33e4cc02 204 } else {
a0e0d592
BG
205 // The endstop was not hit yet
206 running = true;
56ce2b5a 207 debounce[c] = 0;
323cca60 208 }
a0e0d592
BG
209 }
210 }
211 }
750277f8 212}
201bcb94 213
f29b0272 214// this homing works for cartesian and delta printers, not for HBots/CoreXY
33e4cc02
JM
215void Endstops::do_homing(char axes_to_move)
216{
f29b0272
JM
217 // Start moving the axes to the origin
218 this->status = MOVING_TO_ORIGIN_FAST;
56ce2b5a
JM
219 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
220 if ( ( axes_to_move >> c) & 1 ) {
221 this->feed_rate[c]= this->fast_rates[c];
222 this->steppers[c]->set_speed(0);
223 this->steppers[c]->move(this->home_direction[c], 10000000);
f29b0272
JM
224 }
225 }
226
227 // Wait for all axes to have homed
228 this->wait_for_homed(axes_to_move);
229
230 // Move back a small distance
231 this->status = MOVING_BACK;
232 bool inverted_dir;
56ce2b5a
JM
233 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
234 if ( ( axes_to_move >> c ) & 1 ) {
235 inverted_dir = !this->home_direction[c];
236 this->feed_rate[c]= this->slow_rates[c];
237 this->steppers[c]->set_speed(0);
238 this->steppers[c]->move(inverted_dir, this->retract_mm[c]*STEPS_PER_MM(c));
f29b0272
JM
239 }
240 }
241
33e4cc02 242 // Wait for moves to be done
56ce2b5a
JM
243 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
244 if ( ( axes_to_move >> c ) & 1 ) {
245 while ( this->steppers[c]->moving ) {
314ab8f7 246 THEKERNEL->call_event(ON_IDLE);
f29b0272
JM
247 }
248 }
249 }
250
251 // Start moving the axes to the origin slowly
252 this->status = MOVING_TO_ORIGIN_SLOW;
56ce2b5a
JM
253 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
254 if ( ( axes_to_move >> c ) & 1 ) {
255 this->feed_rate[c]= this->slow_rates[c];
256 this->steppers[c]->set_speed(0);
257 this->steppers[c]->move(this->home_direction[c], 10000000);
f29b0272
JM
258 }
259 }
260
261 // Wait for all axes to have homed
262 this->wait_for_homed(axes_to_move);
263
33e4cc02 264 if (this->is_delta) {
959ab59c
JM
265 // move for soft trim
266 this->status = MOVING_BACK;
56ce2b5a
JM
267 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
268 if ( this->trim_mm[c] != 0.0F && ( axes_to_move >> c ) & 1 ) {
269 inverted_dir = this->home_direction[c];
270 // move up or down depending on sign of trim, -ive is down away from home
271 if (this->trim_mm[c] < 0) inverted_dir = !inverted_dir;
272 this->feed_rate[c]= this->slow_rates[c];
273 this->steppers[c]->set_speed(0);
274 this->steppers[c]->move(inverted_dir, abs(round(this->trim_mm[c]*STEPS_PER_MM(c))));
959ab59c 275 }
f29b0272 276 }
f29b0272 277
959ab59c 278 // Wait for moves to be done
56ce2b5a
JM
279 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
280 if ( ( axes_to_move >> c ) & 1 ) {
314ab8f7 281 //THEKERNEL->streams->printf("axis %c \r\n", c );
56ce2b5a 282 while ( this->steppers[c]->moving ) {
314ab8f7 283 THEKERNEL->call_event(ON_IDLE);
959ab59c 284 }
f29b0272
JM
285 }
286 }
287 }
288
289 // Homing is done
290 this->status = NOT_HOMING;
291}
292
33e4cc02
JM
293void Endstops::wait_for_homed_corexy(int axis)
294{
3db88866 295 bool running = true;
33e4cc02
JM
296 unsigned int debounce[3] = {0, 0, 0};
297 while (running) {
3db88866 298 running = false;
314ab8f7 299 THEKERNEL->call_event(ON_IDLE);
33e4cc02
JM
300 if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) {
301 if ( debounce[axis] < debounce_count ) {
3db88866
JM
302 debounce[axis] ++;
303 running = true;
304 } else {
305 // turn both off if running
33e4cc02
JM
306 if (this->steppers[X_AXIS]->moving) this->steppers[X_AXIS]->move(0, 0);
307 if (this->steppers[Y_AXIS]->moving) this->steppers[Y_AXIS]->move(0, 0);
3db88866 308 }
33e4cc02 309 } else {
3db88866
JM
310 // The endstop was not hit yet
311 running = true;
312 debounce[axis] = 0;
313 }
314 }
315}
316
1ad23cd3 317void Endstops::corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps)
33e4cc02
JM
318{
319 this->status = MOVING_TO_ORIGIN_FAST;
64eaf21e
JM
320 this->feed_rate[X_AXIS]= fast_rate;
321 this->steppers[X_AXIS]->set_speed(0);
33e4cc02 322 this->steppers[X_AXIS]->move(dirx, 10000000);
64eaf21e
JM
323 this->feed_rate[Y_AXIS]= fast_rate;
324 this->steppers[Y_AXIS]->set_speed(0);
33e4cc02 325 this->steppers[Y_AXIS]->move(diry, 10000000);
f29b0272 326
33e4cc02
JM
327 // wait for primary axis
328 this->wait_for_homed_corexy(home_axis);
f29b0272 329
33e4cc02
JM
330 // Move back a small distance
331 this->status = MOVING_BACK;
64eaf21e
JM
332 this->feed_rate[X_AXIS]= slow_rate;
333 this->steppers[X_AXIS]->set_speed(0);
33e4cc02 334 this->steppers[X_AXIS]->move(!dirx, retract_steps);
64eaf21e
JM
335 this->feed_rate[Y_AXIS]= slow_rate;
336 this->steppers[Y_AXIS]->set_speed(0);
33e4cc02
JM
337 this->steppers[Y_AXIS]->move(!diry, retract_steps);
338
339 // wait until done
9a993543 340 while ( this->steppers[X_AXIS]->moving || this->steppers[Y_AXIS]->moving) {
314ab8f7 341 THEKERNEL->call_event(ON_IDLE);
f29b0272
JM
342 }
343
33e4cc02
JM
344 // Start moving the axes to the origin slowly
345 this->status = MOVING_TO_ORIGIN_SLOW;
64eaf21e
JM
346 this->feed_rate[X_AXIS]= slow_rate;
347 this->steppers[X_AXIS]->set_speed(0);
33e4cc02 348 this->steppers[X_AXIS]->move(dirx, 10000000);
64eaf21e
JM
349 this->feed_rate[Y_AXIS]= slow_rate;
350 this->steppers[Y_AXIS]->set_speed(0);
33e4cc02 351 this->steppers[Y_AXIS]->move(diry, 10000000);
f29b0272 352
33e4cc02
JM
353 // wait for primary axis
354 this->wait_for_homed_corexy(home_axis);
355}
f29b0272 356
33e4cc02
JM
357// this homing works for HBots/CoreXY
358void Endstops::do_homing_corexy(char axes_to_move)
359{
03dffc07 360 // TODO should really make order configurable, and select whether to allow XY to home at the same time, diagonally
174d9961 361 // 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 362 // allow to move until an endstop triggers, then stop that motor. Speed up when moving diagonally to match X or Y speed
174d9961 363 // continue moving in the direction not yet triggered (which means two motors turning) until endstop hit
174d9961
JM
364
365 if((axes_to_move & 0x03) == 0x03) { // both X and Y need Homing
366 // determine which motor to turn and which way
367 bool dirx= this->home_direction[X_AXIS];
368 bool diry= this->home_direction[Y_AXIS];
369 int motor;
370 bool dir;
371 if(dirx && diry) { // min/min
372 motor= X_AXIS;
373 dir= true;
374 }else if(dirx && !diry) { // min/max
375 motor= Y_AXIS;
376 dir= true;
377 }else if(!dirx && diry) { // max/min
378 motor= Y_AXIS;
379 dir= false;
380 }else if(!dirx && !diry) { // max/max
381 motor= X_AXIS;
382 dir= false;
383 }
384
385 // then move both X and Y until one hits the endstop
386 this->status = MOVING_TO_ORIGIN_FAST;
64eaf21e
JM
387 this->feed_rate[motor]= this->fast_rates[motor]*1.4142;
388 this->steppers[motor]->set_speed(0); // need to allow for more ground covered when moving diagonally
174d9961
JM
389 this->steppers[motor]->move(dir, 10000000);
390 // wait until either X or Y hits the endstop
391 bool running= true;
392 while (running) {
314ab8f7 393 THEKERNEL->call_event(ON_IDLE);
174d9961
JM
394 for(int m=X_AXIS;m<=Y_AXIS;m++) {
395 if(this->pins[m + (this->home_direction[m] ? 0 : 3)].get()) {
396 // turn off motor
397 if(this->steppers[motor]->moving) this->steppers[motor]->move(0, 0);
398 running= false;
399 break;
400 }
401 }
402 }
403 }
33e4cc02 404
174d9961 405 // move individual axis
33e4cc02 406 if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
413dd90c 407 bool dir= this->home_direction[X_AXIS];
56ce2b5a 408 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
409 }
410
65191d91
JM
411 if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
412 bool dir= this->home_direction[Y_AXIS];
56ce2b5a 413 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
414 }
415
33e4cc02 416 if (axes_to_move & 0x04) { // move Z
f29b0272
JM
417 do_homing(0x04); // just home normally for Z
418 }
419
420 // Homing is done
421 this->status = NOT_HOMING;
422}
423
201bcb94 424// Start homing sequences by response to GCode commands
33e4cc02 425void Endstops::on_gcode_received(void *argument)
c339d634 426{
33e4cc02
JM
427 Gcode *gcode = static_cast<Gcode *>(argument);
428 if ( gcode->has_g) {
429 if ( gcode->g == 28 ) {
74b6303c 430 gcode->mark_as_taken();
df27a6a3 431 // G28 is received, we have homing to do
201bcb94
AW
432
433 // First wait for the queue to be empty
314ab8f7 434 THEKERNEL->conveyor->wait_for_empty_queue();
201bcb94
AW
435
436 // Do we move select axes or all of them
7dee696d 437 char axes_to_move = 0;
959ab59c 438 // only enable homing if the endstop is defined, deltas always home all axis
33e4cc02 439 bool home_all = this->is_delta || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') );
47bbe224 440
56ce2b5a
JM
441 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
442 if ( (home_all || gcode->has_letter(c+'X')) && this->pins[c + (this->home_direction[c] ? 0 : 3)].connected() ) {
443 axes_to_move += ( 1 << c );
33e4cc02 444 }
df27a6a3 445 }
3b948656 446
f6c04440 447 // Enable the motors
314ab8f7 448 THEKERNEL->stepper->turn_enable_pins_on();
f6c04440 449
f29b0272 450 // do the actual homing
33e4cc02 451 if (is_corexy)
f29b0272
JM
452 do_homing_corexy(axes_to_move);
453 else
454 do_homing(axes_to_move);
3ffe27fb 455
33e4cc02 456 // Zero the ax(i/e)s position, add in the home offset
56ce2b5a 457 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
33e4cc02 458 if ( (axes_to_move >> c) & 1 ) {
314ab8f7 459 THEKERNEL->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c);
3ffe27fb
AV
460 }
461 }
c339d634 462 }
33e4cc02
JM
463 } else if (gcode->has_m) {
464 switch (gcode->m) {
465 case 119: {
466
467 int px = this->home_direction[0] ? 0 : 3;
468 int py = this->home_direction[1] ? 1 : 4;
469 int pz = this->home_direction[2] ? 2 : 5;
470 const char *mx = this->home_direction[0] ? "min" : "max";
471 const char *my = this->home_direction[1] ? "min" : "max";
472 const char *mz = this->home_direction[2] ? "min" : "max";
473
bd96f4d7
JM
474 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());
475 gcode->add_nl= true;
33e4cc02
JM
476 gcode->mark_as_taken();
477 }
478 break;
479
480 case 206: // M206 - set homing offset
481 if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X');
482 if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y');
483 if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z');
484 gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
485 gcode->mark_as_taken();
486 break;
487
488 case 500: // save settings
489 case 503: // print settings
490 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]);
491 if (is_delta) {
56ce2b5a
JM
492 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]);
493 gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position[2]);
7a8fe6e0 494 }
33e4cc02 495 gcode->mark_as_taken();
c339d634 496 break;
47bbe224 497
ec4773e5 498 case 665: { // M665 - set max gamma/z height
33e4cc02 499 gcode->mark_as_taken();
1ad23cd3 500 float gamma_max = this->homing_position[2];
33e4cc02
JM
501 if (gcode->has_letter('Z')) {
502 this->homing_position[2] = gamma_max = gcode->get_value('Z');
ec4773e5 503 }
33e4cc02
JM
504 gcode->stream->printf("Max Z %8.3f ", gamma_max);
505 gcode->add_nl = true;
506 }
507 break;
ec4773e5 508
47bbe224 509
56ce2b5a
JM
510 case 666:
511 if(this->is_delta) { // M666 - set trim for each axis in mm, NB negative mm trim is down
512 if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X');
513 if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y');
514 if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z');
47bbe224 515
56ce2b5a
JM
516 // print the current trim values in mm
517 gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
518 gcode->mark_as_taken();
519 }
33e4cc02 520 break;
47bbe224 521
d4ee6ee2
JM
522 // NOTE this is to test accuracy of lead screws etc.
523 case 910: { // M910 - move specific number of raw steps
a4b04adc
JM
524 // Enable the motors
525 THEKERNEL->stepper->turn_enable_pins_on();
526
d4ee6ee2
JM
527 int x= 0, y=0 , z= 0, f= 200*16;
528 if (gcode->has_letter('F')) f = gcode->get_value('F');
529 if (gcode->has_letter('X')) {
530 x = gcode->get_value('X');
531 this->steppers[X_AXIS]->set_speed(f);
532 this->steppers[X_AXIS]->move(x<0, abs(x));
533 }
534 if (gcode->has_letter('Y')) {
535 y = gcode->get_value('Y');
536 this->steppers[Y_AXIS]->set_speed(f);
537 this->steppers[Y_AXIS]->move(y<0, abs(y));
538 }
539 if (gcode->has_letter('Z')) {
540 z = gcode->get_value('Z');
541 this->steppers[Z_AXIS]->set_speed(f);
542 this->steppers[Z_AXIS]->move(z<0, abs(z));
543 }
544 gcode->stream->printf("Moved X %d Y %d Z %d F %d steps\n", x, y, z, f);
545 gcode->mark_as_taken();
546 break;
547 }
201bcb94
AW
548 }
549 }
550}
551
64eaf21e
JM
552#define max(a,b) (((a) > (b)) ? (a) : (b))
553// Called periodically to change the speed to match acceleration
554uint32_t Endstops::acceleration_tick(uint32_t dummy)
555{
556 if(this->status == NOT_HOMING) return(0); // nothing to do
557
558 // foreach stepper that is moving
559 for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
560 if( !this->steppers[c]->moving ) continue;
561
562 uint32_t current_rate = this->steppers[c]->steps_per_second;
56ce2b5a 563 uint32_t target_rate = int(floor(this->feed_rate[c]*STEPS_PER_MM(c)));
64eaf21e
JM
564
565 if( current_rate < target_rate ){
38bf9a1c 566 uint32_t rate_increase = int(floor((THEKERNEL->planner->get_acceleration()/THEKERNEL->stepper->get_acceleration_ticks_per_second())*STEPS_PER_MM(c)));
64eaf21e
JM
567 current_rate = min( target_rate, current_rate + rate_increase );
568 }
569 if( current_rate > target_rate ){ current_rate = target_rate; }
570
571 // steps per second
38bf9a1c 572 this->steppers[c]->set_speed(max(current_rate, THEKERNEL->stepper->get_minimum_steps_per_second()));
64eaf21e
JM
573 }
574
575 return 0;
576}
9f6f04a5
JM
577
578void Endstops::on_get_public_data(void* argument){
579 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
580
581 if(!pdr->starts_with(endstops_checksum)) return;
582
583 if(pdr->second_element_is(trim_checksum)) {
584 static float return_data[3];
585 return_data[0]= this->trim_mm[0];
586 return_data[1]= this->trim_mm[1];
587 return_data[2]= this->trim_mm[2];
588
589 pdr->set_data_ptr(&return_data);
590 pdr->set_taken();
591 }
592}
7d6fe308
JM
593
594void Endstops::on_set_public_data(void* argument){
595 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
596
597 if(!pdr->starts_with(endstops_checksum)) return;
598
599 if(pdr->second_element_is(trim_checksum)) {
600 float *t= static_cast<float*>(pdr->get_data_ptr());
601 this->trim_mm[0]= t[0];
602 this->trim_mm[1]= t[1];
603 this->trim_mm[2]= t[2];
604 pdr->set_taken();
605 }
606}