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