Increase glcd refresh rate to 10Hz
[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
AW
16#include "wait_api.h" // mbed.h lib
17
18Endstops::Endstops(){
19 this->status = NOT_HOMING;
20}
21
22void Endstops::on_module_loaded() {
7dee696d 23 // Do not do anything if not enabled
7d62445b
L
24 if( this->kernel->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ){ return; }
25
476dcb96 26 register_for_event(ON_CONFIG_RELOAD);
201bcb94
AW
27 this->register_for_event(ON_GCODE_RECEIVED);
28
29 // Take StepperMotor objects from Robot and keep them here
30 this->steppers[0] = this->kernel->robot->alpha_stepper_motor;
31 this->steppers[1] = this->kernel->robot->beta_stepper_motor;
32 this->steppers[2] = this->kernel->robot->gamma_stepper_motor;
33
750277f8
AW
34 // Settings
35 this->on_config_reload(this);
c339d634 36
201bcb94
AW
37}
38
3857da41
AW
39//#pragma GCC push_options
40//#pragma GCC optimize ("O0")
41
750277f8
AW
42// Get config
43void Endstops::on_config_reload(void* argument){
cc183397
A
44 this->pins[0].from_string( this->kernel->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
45 this->pins[1].from_string( this->kernel->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
46 this->pins[2].from_string( this->kernel->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_string())->as_input();
47 this->pins[3].from_string( this->kernel->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
48 this->pins[4].from_string( this->kernel->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
49 this->pins[5].from_string( this->kernel->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_string())->as_input();
3857da41
AW
50 this->fast_rates[0] = this->kernel->config->value(alpha_fast_homing_rate_checksum )->by_default(500 )->as_number();
51 this->fast_rates[1] = this->kernel->config->value(beta_fast_homing_rate_checksum )->by_default(500 )->as_number();
52 this->fast_rates[2] = this->kernel->config->value(gamma_fast_homing_rate_checksum )->by_default(5 )->as_number();
53 this->slow_rates[0] = this->kernel->config->value(alpha_slow_homing_rate_checksum )->by_default(100 )->as_number();
54 this->slow_rates[1] = this->kernel->config->value(beta_slow_homing_rate_checksum )->by_default(100 )->as_number();
55 this->slow_rates[2] = this->kernel->config->value(gamma_slow_homing_rate_checksum )->by_default(5 )->as_number();
56 this->retract_steps[0] = this->kernel->config->value(alpha_homing_retract_checksum )->by_default(30 )->as_number();
57 this->retract_steps[1] = this->kernel->config->value(beta_homing_retract_checksum )->by_default(30 )->as_number();
58 this->retract_steps[2] = this->kernel->config->value(gamma_homing_retract_checksum )->by_default(10 )->as_number();
6256fbd6
MM
59 this->trim[0] = this->kernel->config->value(alpha_trim_checksum )->by_default(0 )->as_number();
60 this->trim[1] = this->kernel->config->value(beta_trim_checksum )->by_default(0 )->as_number();
61 this->trim[2] = this->kernel->config->value(gamma_trim_checksum )->by_default(0 )->as_number();
3857da41 62 this->debounce_count = this->kernel->config->value(endstop_debounce_count_checksum )->by_default(100 )->as_number();
409ff5b3
JM
63
64 // get homing direction and convert to boolean where true is home to min, and false is home to max
65 int home_dir = get_checksum(this->kernel->config->value(alpha_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 66 this->home_direction[0] = home_dir != home_to_max_checksum;
409ff5b3
JM
67
68 home_dir = get_checksum(this->kernel->config->value(beta_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 69 this->home_direction[1] = home_dir != home_to_max_checksum;
409ff5b3
JM
70
71 home_dir = get_checksum(this->kernel->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string());
eb09fdbf 72 this->home_direction[2] = home_dir != home_to_max_checksum;
409ff5b3
JM
73
74 this->homing_position[0] = this->home_direction[0]?this->kernel->config->value(alpha_min_checksum)->by_default(0)->as_number():this->kernel->config->value(alpha_max_checksum)->by_default(200)->as_number();
75 this->homing_position[1] = this->home_direction[1]?this->kernel->config->value(beta_min_checksum )->by_default(0)->as_number():this->kernel->config->value(beta_max_checksum )->by_default(200)->as_number();;
76 this->homing_position[2] = this->home_direction[2]?this->kernel->config->value(gamma_min_checksum)->by_default(0)->as_number():this->kernel->config->value(gamma_max_checksum)->by_default(200)->as_number();;
a0e0d592
BG
77}
78
3857da41
AW
79//#pragma GCC pop_options
80
81void Endstops::wait_for_homed(char axes_to_move){
a0e0d592
BG
82 bool running = true;
83 unsigned int debounce[3] = {0,0,0};
84 while(running){
85 running = false;
f3e93777 86 this->kernel->call_event(ON_IDLE);
a0e0d592
BG
87 for( char c = 'X'; c <= 'Z'; c++ ){
88 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
409ff5b3 89 if( this->pins[c - 'X' + (this->home_direction[c - 'X']?0:3)].get() ){
a0e0d592
BG
90 if( debounce[c - 'X'] < debounce_count ) {
91 debounce[c - 'X'] ++;
92 running = true;
93 } else if ( this->steppers[c - 'X']->moving ){
94 this->steppers[c - 'X']->move(0,0);
f3e93777 95 //this->kernel->streams->printf("move done %c\r\n", c);
a0e0d592
BG
96 }
97 }else{
98 // The endstop was not hit yet
99 running = true;
100 debounce[c - 'X'] = 0;
323cca60 101 }
a0e0d592
BG
102 }
103 }
104 }
750277f8 105}
201bcb94
AW
106
107// Start homing sequences by response to GCode commands
c339d634
MM
108void Endstops::on_gcode_received(void* argument)
109{
201bcb94 110 Gcode* gcode = static_cast<Gcode*>(argument);
c339d634
MM
111 if( gcode->has_g)
112 {
113 if( gcode->g == 28 )
114 {
74b6303c 115 gcode->mark_as_taken();
df27a6a3 116 // G28 is received, we have homing to do
201bcb94
AW
117
118 // First wait for the queue to be empty
40de2562 119 this->kernel->conveyor->wait_for_empty_queue();
201bcb94
AW
120
121 // Do we move select axes or all of them
7dee696d
JM
122 char axes_to_move = 0;
123 // only enable homing if the endstop is defined
124 bool home_all= !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') );
125
3b948656 126 for( char c = 'X'; c <= 'Z'; c++ ){
60baf878 127 if( (home_all || gcode->has_letter(c)) && this->pins[c - 'X' + (this->home_direction[c - 'X']?0:3)].connected() ){ axes_to_move += ( 1 << (c - 'X' ) ); }
df27a6a3 128 }
3b948656 129
f6c04440
AW
130 // Enable the motors
131 this->kernel->stepper->turn_enable_pins_on();
132
201bcb94 133 // Start moving the axes to the origin
df27a6a3 134 this->status = MOVING_TO_ORIGIN_FAST;
201bcb94 135 for( char c = 'X'; c <= 'Z'; c++ ){
3b948656 136 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
f3e93777 137 gcode->stream->printf("homing axis %c\r\n", c);
3ffe27fb 138 this->steppers[c - 'X']->set_speed(this->fast_rates[c - 'X']);
409ff5b3 139 this->steppers[c - 'X']->move(this->home_direction[c - 'X'],10000000);
201bcb94
AW
140 }
141 }
142
143 // Wait for all axes to have homed
a0e0d592 144 this->wait_for_homed(axes_to_move);
201bcb94 145
f3e93777 146 gcode->stream->printf("test a\r\n");
3b948656 147 // Move back a small distance
df27a6a3 148 this->status = MOVING_BACK;
3ffe27fb 149 int inverted_dir;
3b948656
AW
150 for( char c = 'X'; c <= 'Z'; c++ ){
151 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
409ff5b3 152 inverted_dir = -(this->home_direction[c - 'X'] - 1);
df27a6a3 153 this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']);
3ffe27fb 154 this->steppers[c - 'X']->move(inverted_dir,this->retract_steps[c - 'X']);
3b948656
AW
155 }
156 }
157
f3e93777 158 gcode->stream->printf("test b\r\n");
3b948656
AW
159 // Wait for moves to be done
160 for( char c = 'X'; c <= 'Z'; c++ ){
df27a6a3 161 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
06288536 162 this->kernel->streams->printf("axis %c \r\n", c );
f3e93777
AW
163 while( this->steppers[c - 'X']->moving ){
164 this->kernel->call_event(ON_IDLE);
165 }
3b948656
AW
166 }
167 }
168
f3e93777 169 gcode->stream->printf("test c\r\n");
201bcb94 170
9b0ac510 171 // Start moving the axes to the origin slowly
df27a6a3 172 this->status = MOVING_TO_ORIGIN_SLOW;
9b0ac510
AW
173 for( char c = 'X'; c <= 'Z'; c++ ){
174 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
df27a6a3 175 this->steppers[c - 'X']->set_speed(this->slow_rates[c -'X']);
409ff5b3 176 this->steppers[c - 'X']->move(this->home_direction[c - 'X'],10000000);
9b0ac510
AW
177 }
178 }
179
180 // Wait for all axes to have homed
a0e0d592 181 this->wait_for_homed(axes_to_move);
9b0ac510 182
6256fbd6
MM
183 // move for soft trim
184 this->status = MOVING_BACK;
185 for( char c = 'X'; c <= 'Z'; c++ ){
186 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
187 inverted_dir = -(this->home_direction[c - 'X'] - 1);
188 this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']);
189 this->steppers[c - 'X']->move(inverted_dir,this->trim[c - 'X']);
190 }
191 }
192
193 // Wait for moves to be done
194 for( char c = 'X'; c <= 'Z'; c++ ){
195 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
196 this->kernel->streams->printf("axis %c \r\n", c );
197 while( this->steppers[c - 'X']->moving ){
198 this->kernel->call_event(ON_IDLE);
199 }
200 }
201 }
202
201bcb94
AW
203 // Homing is done
204 this->status = NOT_HOMING;
3ffe27fb
AV
205
206 // Zero the ax(i/e)s position
207 for( char c = 'X'; c <= 'Z'; c++ ){
208 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
209
210 this->kernel->robot->reset_axis_position(this->homing_position[c - 'X'], c - 'X');
211 }
212 }
213
c339d634
MM
214 }
215 }
f3e93777
AW
216 else if (gcode->has_m){
217 switch(gcode->m){
c339d634 218 case 119:
f3e93777 219 gcode->stream->printf("X min:%d max:%d Y min:%d max:%d Z min:%d max:%d\n", this->pins[0].get(), this->pins[3].get(), this->pins[1].get(), this->pins[4].get(), this->pins[2].get(), this->pins[5].get() );
74b6303c 220 gcode->mark_as_taken();
c339d634 221 break;
201bcb94
AW
222 }
223 }
224}
225