improved error response of ToolManager
[clinton/Smoothieware.git] / src / modules / tools / toolmanager / ToolManager.cpp
CommitLineData
14ecdbd7
AW
1/*
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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6*/
7
8#include "libs/Module.h"
9#include "libs/Kernel.h"
10#include <math.h>
11using namespace std;
12#include <vector>
18067d90 13#include "ToolManager.h"
8adf2390 14#include "Conveyor.h"
14ecdbd7 15
f4a68aec
L
16#define return_error_on_unhandled_gcode_checksum CHECKSUM("return_error_on_unhandled_gcode")
17
92031952 18ToolManager::ToolManager(){
8adf2390
L
19 active_tool = 0;
20}
14ecdbd7 21
92031952 22void ToolManager::on_module_loaded(){
f4a68aec
L
23 this->on_config_reload(this);
24
25 this->register_for_event(ON_CONFIG_RELOAD);
8adf2390 26 this->register_for_event(ON_GCODE_RECEIVED);
8adf2390
L
27}
28
f4a68aec
L
29void ToolManager::on_config_reload(void *argument){
30 return_error_on_unhandled_gcode = THEKERNEL->config->value( return_error_on_unhandled_gcode_checksum )->by_default(false)->as_bool();
31}
32
92031952 33void ToolManager::on_gcode_received(void *argument){
8adf2390
L
34 Gcode *gcode = static_cast<Gcode*>(argument);
35
36 if( gcode->has_letter('T') ){
37 int new_tool = gcode->get_value('T');
5a6dd23d
L
38 bool make_move = false;
39 if ( gcode->has_letter('F') ){
40 make_move = true;
41 }
8adf2390 42 gcode->mark_as_taken();
c974b296 43 if(new_tool >= (int)this->tools.size() || new_tool < 0){
8adf2390 44 // invalid tool
f4a68aec
L
45 if( return_error_on_unhandled_gcode ) {
46 char buf[32]; // should be big enough for any status
47 int n= snprintf(buf, sizeof(buf), "T%d invalid tool ", new_tool);
48 gcode->txt_after_ok.append(buf, n);
49 }
8adf2390 50 } else {
c974b296
L
51 if(new_tool != this->active_tool){
52 void *returned_data;
53 bool ok = THEKERNEL->public_data->get_value( robot_checksum, current_position_checksum, &returned_data );
54 if(ok){
55 // save current position to return to after applying extruder offset
56 float *pos = static_cast<float *>(returned_data);
57 float current_pos[3];
58 for(int i=0;i<3;i++){
59 current_pos[i] = pos[i];
60 }
61 // update virtual tool position to the offset of the new tool and select it as active
62 float new_pos[3];
63 float *active_tool_offset = tools[this->active_tool]->get_offset();
64 float *new_tool_offset = tools[new_tool]->get_offset();
65 for(int i=0;i<3;i++){
66 new_pos[i] = current_pos[i] - active_tool_offset[i] + new_tool_offset[i];
67 }
68
69 this->tools[active_tool]->disable();
70 this->active_tool = new_tool;
71 this->tools[active_tool]->enable();
14ecdbd7 72
c974b296
L
73 char buf[32];
74 snprintf(buf, 31, "G92 X%g Y%g Z%g", new_pos[X_AXIS], new_pos[Y_AXIS], new_pos[Z_AXIS]);
75 string s = buf;
76 Gcode *g = new Gcode(s, gcode->stream);
77 THEKERNEL->call_event(ON_GCODE_RECEIVED, g);
78 delete g;
8adf2390 79
c974b296
L
80 if(make_move){
81 //move to old position
82 snprintf(buf, 31, "G0 X%g Y%g Z%g", current_pos[X_AXIS], current_pos[Y_AXIS], current_pos[Z_AXIS]);
83 s = buf;
84 g = new Gcode(s, gcode->stream);
5a6dd23d
L
85 THEKERNEL->call_event(ON_GCODE_RECEIVED, g);
86 delete g;
5a6dd23d 87 }
8adf2390
L
88 }
89 }
90 }
91 }
92}
14ecdbd7
AW
93
94// Add a tool to the tool list
92031952 95void ToolManager::add_tool(Tool* tool_to_add){
c974b296
L
96 if(this->tools.size() == 0){
97 tool_to_add->enable();
98 } else {
99 tool_to_add->disable();
100 }
14ecdbd7
AW
101 this->tools.push_back( tool_to_add );
102}
103
104
105