TemperatureControl: static Pins
[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"
11#include "modules/robot/Player.h"
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
18Endstops::Endstops(){
19 this->status = NOT_HOMING;
20}
21
22void 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
750277f8
AW
36// Get config
37void Endstops::on_config_reload(void* argument){
c339d634
MM
38 this->pins[0] = this->kernel->config->value(alpha_min_endstop_checksum )->by_default("nc" )->as_pin()->as_input()->pull_up();
39 this->pins[1] = this->kernel->config->value(beta_min_endstop_checksum )->by_default("nc" )->as_pin()->as_input()->pull_up();
40 this->pins[2] = this->kernel->config->value(gamma_min_endstop_checksum )->by_default("nc" )->as_pin()->as_input()->pull_up();
41 this->pins[3] = this->kernel->config->value(alpha_max_endstop_checksum )->by_default("nc" )->as_pin()->as_input()->pull_up();
42 this->pins[4] = this->kernel->config->value(beta_max_endstop_checksum )->by_default("nc" )->as_pin()->as_input()->pull_up();
43 this->pins[5] = this->kernel->config->value(gamma_max_endstop_checksum )->by_default("nc" )->as_pin()->as_input()->pull_up();
4026a515
AW
44 this->fast_rates[0] = this->kernel->config->value(alpha_fast_homing_rate_checksum )->by_default("500" )->as_number();
45 this->fast_rates[1] = this->kernel->config->value(beta_fast_homing_rate_checksum )->by_default("500" )->as_number();
f6c04440 46 this->fast_rates[2] = this->kernel->config->value(gamma_fast_homing_rate_checksum )->by_default("5" )->as_number();
4026a515
AW
47 this->slow_rates[0] = this->kernel->config->value(alpha_slow_homing_rate_checksum )->by_default("100" )->as_number();
48 this->slow_rates[1] = this->kernel->config->value(beta_slow_homing_rate_checksum )->by_default("100" )->as_number();
f6c04440 49 this->slow_rates[2] = this->kernel->config->value(gamma_slow_homing_rate_checksum )->by_default("5" )->as_number();
4026a515
AW
50 this->retract_steps[0] = this->kernel->config->value(alpha_homing_retract_checksum )->by_default("30" )->as_number();
51 this->retract_steps[1] = this->kernel->config->value(beta_homing_retract_checksum )->by_default("30" )->as_number();
f6c04440 52 this->retract_steps[2] = this->kernel->config->value(gamma_homing_retract_checksum )->by_default("10" )->as_number();
a0e0d592
BG
53 this->debounce_count = this->kernel->config->value(endstop_debounce_count_checksum )->by_default("100" )->as_number();
54}
55
56void Endstops::wait_for_homed(char axes_to_move)
57{
58 bool running = true;
59 unsigned int debounce[3] = {0,0,0};
60 while(running){
61 running = false;
62 for( char c = 'X'; c <= 'Z'; c++ ){
63 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
323cca60 64 if( this->pins[c - 'X']->get() ){
a0e0d592
BG
65 if( debounce[c - 'X'] < debounce_count ) {
66 debounce[c - 'X'] ++;
67 running = true;
68 } else if ( this->steppers[c - 'X']->moving ){
69 this->steppers[c - 'X']->move(0,0);
70 printf("move done %c\r\n", c);
71 }
72 }else{
73 // The endstop was not hit yet
74 running = true;
75 debounce[c - 'X'] = 0;
323cca60 76 }
a0e0d592
BG
77 }
78 }
79 }
750277f8 80}
201bcb94
AW
81
82// Start homing sequences by response to GCode commands
c339d634
MM
83void Endstops::on_gcode_received(void* argument)
84{
201bcb94 85 Gcode* gcode = static_cast<Gcode*>(argument);
c339d634
MM
86 if( gcode->has_g)
87 {
88 if( gcode->g == 28 )
89 {
df27a6a3 90 // G28 is received, we have homing to do
201bcb94
AW
91
92 // First wait for the queue to be empty
93 while(this->kernel->player->queue.size() > 0) { wait_us(500); }
94
95 // Do we move select axes or all of them
3b948656
AW
96 char axes_to_move = ( ( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') ) ? 0x00 : 0xff );
97 for( char c = 'X'; c <= 'Z'; c++ ){
98 if( gcode->has_letter(c) && this->pins[c - 'X']->connected() ){ axes_to_move += ( 1 << (c - 'X' ) ); }
df27a6a3 99 }
3b948656 100
f6c04440
AW
101 // Enable the motors
102 this->kernel->stepper->turn_enable_pins_on();
103
201bcb94 104 // Start moving the axes to the origin
df27a6a3 105 this->status = MOVING_TO_ORIGIN_FAST;
201bcb94 106 for( char c = 'X'; c <= 'Z'; c++ ){
3b948656 107 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
df27a6a3 108 this->steppers[c - 'X']->set_speed(this->fast_rates[c -'X']);
f6c04440 109 this->steppers[c - 'X']->move(1,10000000);
201bcb94
AW
110 }
111 }
112
113 // Wait for all axes to have homed
a0e0d592 114 this->wait_for_homed(axes_to_move);
201bcb94 115
3b948656
AW
116 printf("test a\r\n");
117 // Move back a small distance
df27a6a3 118 this->status = MOVING_BACK;
3b948656
AW
119 for( char c = 'X'; c <= 'Z'; c++ ){
120 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
df27a6a3 121 this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']);
f6c04440 122 this->steppers[c - 'X']->move(0,this->retract_steps[c - 'X']);
3b948656
AW
123 }
124 }
125
126 printf("test b\r\n");
127 // Wait for moves to be done
128 for( char c = 'X'; c <= 'Z'; c++ ){
df27a6a3 129 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
3b948656
AW
130 printf("axis %c \r\n", c );
131 while( this->steppers[c - 'X']->moving ){ }
132 }
133 }
134
135 printf("test c\r\n");
201bcb94 136
9b0ac510 137 // Start moving the axes to the origin slowly
df27a6a3 138 this->status = MOVING_TO_ORIGIN_SLOW;
9b0ac510
AW
139 for( char c = 'X'; c <= 'Z'; c++ ){
140 if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
df27a6a3 141 this->steppers[c - 'X']->set_speed(this->slow_rates[c -'X']);
f6c04440 142 this->steppers[c - 'X']->move(1,10000000);
9b0ac510
AW
143 }
144 }
145
146 // Wait for all axes to have homed
a0e0d592 147 this->wait_for_homed(axes_to_move);
9b0ac510 148
201bcb94
AW
149 // Homing is done
150 this->status = NOT_HOMING;
c339d634
MM
151 }
152 }
153 else if (gcode->has_m)
154 {
155 switch(gcode->m)
156 {
157 case 119:
158 gcode->stream->printf("X min:%d max:%d Y min:%d max:%d Z min:%d max:%d\n",
159 this->pins[0]->get(),
160 this->pins[3]->get(),
161 this->pins[1]->get(),
162 this->pins[4]->get(),
163 this->pins[2]->get(),
164 this->pins[5]->get()
165 );
166 break;
201bcb94
AW
167 }
168 }
169}
170