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 // TODO: report invalid selection
37 gcode
->stream
->printf(";;T%d invalid tool\r\n", new_tool
);
39 if(new_tool
!= this->active_tool
){
41 bool ok
= THEKERNEL
->public_data
->get_value( robot_checksum
, current_position_checksum
, &returned_data
);
43 // save current position to return to after applying extruder offset
44 float *pos
= static_cast<float *>(returned_data
);
47 current_pos
[i
] = pos
[i
];
49 // update virtual tool position to the offset of the new tool and select it as active
51 float *active_tool_offset
= tools
[this->active_tool
]->get_offset();
52 float *new_tool_offset
= tools
[new_tool
]->get_offset();
54 new_pos
[i
] = current_pos
[i
] - active_tool_offset
[i
] + new_tool_offset
[i
];
57 this->tools
[active_tool
]->disable();
58 this->active_tool
= new_tool
;
59 this->tools
[active_tool
]->enable();
62 snprintf(buf
, 31, "G92 X%g Y%g Z%g", new_pos
[X_AXIS
], new_pos
[Y_AXIS
], new_pos
[Z_AXIS
]);
64 Gcode
*g
= new Gcode(s
, gcode
->stream
);
65 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, g
);
69 //move to old position
70 snprintf(buf
, 31, "G0 X%g Y%g Z%g", current_pos
[X_AXIS
], current_pos
[Y_AXIS
], current_pos
[Z_AXIS
]);
72 g
= new Gcode(s
, gcode
->stream
);
73 THEKERNEL
->call_event(ON_GCODE_RECEIVED
, g
);
82 // Add a tool to the tool list
83 void ToolManager::add_tool(Tool
* tool_to_add
){
84 if(this->tools
.size() == 0){
85 tool_to_add
->enable();
87 tool_to_add
->disable();
89 this->tools
.push_back( tool_to_add
);