Merge branch 'multitool' of github.com:logxen/Smoothie into multitool
[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 "Conveyor.h"
15
16 ToolManager::ToolManager(){
17 active_tool = 0;
18 }
19
20 void ToolManager::on_module_loaded(){
21 this->register_for_event(ON_GCODE_RECEIVED);
22 }
23
24 void ToolManager::on_gcode_received(void *argument){
25 Gcode *gcode = static_cast<Gcode*>(argument);
26
27 if( gcode->has_letter('T') ){
28 int new_tool = gcode->get_value('T');
29 bool make_move = false;
30 if ( gcode->has_letter('F') ){
31 make_move = true;
32 }
33 gcode->mark_as_taken();
34 if(new_tool > (int)this->tools.size() || new_tool < 0){
35 // invalid tool
36 gcode->stream->printf(";;T%d invalid tool\r\n", new_tool);
37 } else {
38 if( gcode->has_letter('T') ){
39 int new_tool = gcode->get_value('T');
40 if(new_tool != this->active_tool){
41 void *returned_data;
42 bool ok = THEKERNEL->public_data->get_value( robot_checksum, current_position_checksum, &returned_data );
43 if(ok){
44 // save current position to return to after applying extruder offset
45 float *pos = static_cast<float *>(returned_data);
46 float current_pos[3];
47 for(int i=0;i<3;i++){
48 current_pos[i] = pos[i];
49 }
50 // update virtual tool position to the offset of the new tool and select it as active
51 float new_pos[3];
52 float *active_tool_offset = tools[this->active_tool]->get_offset();
53 float *new_tool_offset = tools[new_tool]->get_offset();
54 for(int i=0;i<3;i++){
55 new_pos[i] = current_pos[i] - active_tool_offset[i] + new_tool_offset[i];
56 }
57
58 this->tools[active_tool]->disable();
59 this->active_tool = new_tool;
60 this->tools[active_tool]->enable();
61
62 char buf[32];
63 snprintf(buf, 31, "G92 X%g Y%g Z%g", new_pos[X_AXIS], new_pos[Y_AXIS], new_pos[Z_AXIS]);
64 string s = buf;
65 Gcode *g = new Gcode(s, gcode->stream);
66 THEKERNEL->call_event(ON_GCODE_RECEIVED, g);
67 delete g;
68
69 if(make_move){
70 //move to old position
71 snprintf(buf, 25, "G0 X%g Y%g Z%g", current_pos[X_AXIS], current_pos[Y_AXIS], current_pos[Z_AXIS]);
72 s = buf;
73 g = new Gcode(s, gcode->stream);
74 THEKERNEL->call_event(ON_GCODE_RECEIVED, g);
75 delete g;
76 }
77 }
78 }
79 }
80 }
81 }
82 }
83
84 // Add a tool to the tool list
85 void ToolManager::add_tool(Tool* tool_to_add){
86 this->tools.push_back( tool_to_add );
87 }
88
89
90