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/>.
8 #include "libs/Module.h"
9 #include "libs/Kernel.h"
13 #include "ToolManager.h"
16 ToolManager::ToolManager(){
20 void ToolManager::on_module_loaded(){
21 this->register_for_event(ON_GCODE_RECEIVED
);
24 void ToolManager::on_gcode_received(void *argument
){
25 Gcode
*gcode
= static_cast<Gcode
*>(argument
);
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') ){
33 gcode
->mark_as_taken();
34 if(new_tool
> (int)this->tools
.size() || new_tool
< 0){
36 gcode
->stream
->printf(";;T%d invalid tool\r\n", new_tool
);
38 if( gcode
->has_letter('T') ){
39 int new_tool
= gcode
->get_value('T');
40 if(new_tool
!= this->active_tool
){
42 bool ok
= THEKERNEL
->public_data
->get_value( robot_checksum
, current_position_checksum
, &returned_data
);
44 // save current position to return to after applying extruder offset
45 float *pos
= static_cast<float *>(returned_data
);
48 current_pos
[i
] = pos
[i
];
50 // update virtual tool position to the offset of the new tool and select it as active
52 float *active_tool_offset
= tools
[this->active_tool
]->get_offset();
53 float *new_tool_offset
= tools
[new_tool
]->get_offset();
55 new_pos
[i
] = current_pos
[i
] - active_tool_offset
[i
] + new_tool_offset
[i
];
58 this->tools
[active_tool
]->disable();
59 this->active_tool
= new_tool
;
60 this->tools
[active_tool
]->enable();
63 snprintf(buf
, 31, "G92 X%g Y%g Z%g", new_pos
[X_AXIS
], new_pos
[Y_AXIS
], new_pos
[Z_AXIS
]);
65 Gcode
*g
= new Gcode(s
, gcode
->stream
);
66 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, g
);
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
]);
73 g
= new Gcode(s
, gcode
->stream
);
74 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, g
);
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
);