made ToolManager properly aware of the first tool name
[clinton/Smoothieware.git] / src / modules / tools / toolmanager / ToolManager.cpp
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>
11 using namespace std;
12 #include <vector>
13 #include "ToolManager.h"
14 #include "PublicDataRequest.h"
15 #include "ToolManagerPublicAccess.h"
16 #include "Config.h"
17 #include "Robot.h"
18 #include "ConfigValue.h"
19 #include "Conveyor.h"
20 #include "checksumm.h"
21 #include "PublicData.h"
22 #include "Gcode.h"
23
24 #include "libs/SerialMessage.h"
25 #include "libs/StreamOutput.h"
26 #include "FileStream.h"
27
28 #include "modules/robot/RobotPublicAccess.h"
29
30 #define return_error_on_unhandled_gcode_checksum CHECKSUM("return_error_on_unhandled_gcode")
31
32 #define X_AXIS 0
33 #define Y_AXIS 1
34 #define Z_AXIS 2
35
36 ToolManager::ToolManager(){
37 active_tool = 0;
38 current_tool_name = CHECKSUM("hotend");
39 }
40
41 void ToolManager::on_module_loaded(){
42 this->on_config_reload(this);
43
44 this->register_for_event(ON_CONFIG_RELOAD);
45 this->register_for_event(ON_GCODE_RECEIVED);
46 this->register_for_event(ON_GET_PUBLIC_DATA);
47 this->register_for_event(ON_SET_PUBLIC_DATA);
48 }
49
50 void ToolManager::on_config_reload(void *argument){
51 return_error_on_unhandled_gcode = THEKERNEL->config->value( return_error_on_unhandled_gcode_checksum )->by_default(false)->as_bool();
52 }
53
54 void ToolManager::on_gcode_received(void *argument){
55 Gcode *gcode = static_cast<Gcode*>(argument);
56
57 if( gcode->has_letter('T') ){
58 int new_tool = gcode->get_value('T');
59 bool make_move = false;
60 if ( gcode->has_letter('F') ){
61 make_move = true;
62 }
63 gcode->mark_as_taken();
64 if(new_tool >= (int)this->tools.size() || new_tool < 0){
65 // invalid tool
66 if( return_error_on_unhandled_gcode ) {
67 char buf[32]; // should be big enough for any status
68 int n= snprintf(buf, sizeof(buf), "T%d invalid tool ", new_tool);
69 gcode->txt_after_ok.append(buf, n);
70 }
71 } else {
72
73 //send new_tool_offsets to robot
74 float new_pos[3];
75 float *active_tool_offset = tools[this->active_tool]->get_offset();
76 float *new_tool_offset = tools[new_tool]->get_offset();
77 THEKERNEL->robot->setToolOffset(new_tool_offset[0], new_tool_offset[1], new_tool_offset[2]);
78
79 if(new_tool != this->active_tool){
80 void *returned_data;
81 THEKERNEL->conveyor->wait_for_empty_queue();
82 bool ok = THEKERNEL->public_data->get_value( robot_checksum, current_position_checksum, &returned_data );
83 if(ok){
84 // save current position to return to after applying extruder offset
85 float *pos = static_cast<float *>(returned_data);
86 float current_pos[3];
87 for(int i=0;i<3;i++){
88 current_pos[i] = pos[i];
89 }
90 // update virtual tool position to the offset of the new tool and select it as active
91
92 for(int i=0;i<3;i++){
93 new_pos[i] = current_pos[i] - active_tool_offset[i] + new_tool_offset[i];
94 }
95
96 this->tools[active_tool]->disable();
97 this->active_tool = new_tool;
98 this->current_tool_name = this->tools[active_tool]->get_name();
99 this->tools[active_tool]->enable();
100
101 if(make_move){
102 //move to old position
103 char buf[32];
104 string s = buf;
105 Gcode *g = new Gcode(s, gcode->stream);
106 snprintf(buf, 31, "G0 X%g Y%g Z%g", current_pos[X_AXIS], current_pos[Y_AXIS], current_pos[Z_AXIS]);
107 s = buf;
108 g = new Gcode(s, gcode->stream);
109 THEKERNEL->call_event(ON_GCODE_RECEIVED, g);
110 delete g;
111 }
112 }
113 }
114 }
115 }
116 }
117
118 void ToolManager::on_get_public_data(void* argument){
119 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
120
121 if(!pdr->starts_with(tool_manager_checksum)) return;
122
123 // ok this is targeted at us, so send back the requested data
124 // this must be static as it will be accessed long after we have returned
125 static struct pad_toolmanager tool_return;
126 tool_return.current_tool_name= this->current_tool_name;
127
128 pdr->set_data_ptr(&tool_return);
129 pdr->set_taken();
130 }
131
132 void ToolManager::on_set_public_data(void* argument){
133 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
134
135 if(!pdr->starts_with(tool_manager_checksum)) return;
136
137 // ok this is targeted at us, so change tools
138 uint16_t tool_name= *static_cast<float*>(pdr->get_data_ptr());
139 // TODO: fire a tool change gcode
140 pdr->set_taken();
141 }
142
143 // Add a tool to the tool list
144 void ToolManager::add_tool(Tool* tool_to_add){
145 if(this->tools.size() == 0){
146 tool_to_add->enable();
147 this->current_tool_name = tool_to_add->get_name();
148 } else {
149 tool_to_add->disable();
150 }
151 this->tools.push_back( tool_to_add );
152 }
153
154
155