Change the way tool offset is handled in robot
[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 "Tool.h"
15 #include "PublicDataRequest.h"
16 #include "ToolManagerPublicAccess.h"
17 #include "Config.h"
18 #include "Robot.h"
19 #include "ConfigValue.h"
20 #include "Conveyor.h"
21 #include "checksumm.h"
22 #include "PublicData.h"
23 #include "Gcode.h"
24
25 #include "libs/SerialMessage.h"
26 #include "libs/StreamOutput.h"
27 #include "FileStream.h"
28
29 #include "modules/robot/RobotPublicAccess.h"
30
31 #define return_error_on_unhandled_gcode_checksum CHECKSUM("return_error_on_unhandled_gcode")
32
33 ToolManager::ToolManager(){
34 active_tool = 0;
35 current_tool_name = CHECKSUM("hotend");
36 }
37
38 void ToolManager::on_module_loaded(){
39 this->on_config_reload(this);
40
41 this->register_for_event(ON_CONFIG_RELOAD);
42 this->register_for_event(ON_GCODE_RECEIVED);
43 this->register_for_event(ON_GET_PUBLIC_DATA);
44 this->register_for_event(ON_SET_PUBLIC_DATA);
45 }
46
47 void ToolManager::on_config_reload(void *argument){
48 return_error_on_unhandled_gcode = THEKERNEL->config->value( return_error_on_unhandled_gcode_checksum )->by_default(false)->as_bool();
49 }
50
51 void ToolManager::on_gcode_received(void *argument){
52 Gcode *gcode = static_cast<Gcode*>(argument);
53
54 if( gcode->has_letter('T') ){
55 int new_tool = gcode->get_value('T');
56 gcode->mark_as_taken();
57 if(new_tool >= (int)this->tools.size() || new_tool < 0){
58 // invalid tool
59 if( return_error_on_unhandled_gcode ) {
60 char buf[32]; // should be big enough for any status
61 int n= snprintf(buf, sizeof(buf), "T%d invalid tool ", new_tool);
62 gcode->txt_after_ok.append(buf, n);
63 }
64 } else {
65 if(new_tool != this->active_tool){
66 // We must wait for an empty queue before we can disable the current extruder
67 THEKERNEL->conveyor->wait_for_empty_queue();
68 this->tools[active_tool]->disable();
69 this->active_tool = new_tool;
70 this->current_tool_name = this->tools[active_tool]->get_name();
71 this->tools[active_tool]->enable();
72
73 //send new_tool_offsets to robot
74 const float *new_tool_offset = tools[new_tool]->get_offset();
75 THEKERNEL->robot->setToolOffset(new_tool_offset);
76 }
77 }
78 }
79 }
80
81 void ToolManager::on_get_public_data(void* argument){
82 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
83
84 if(!pdr->starts_with(tool_manager_checksum)) return;
85
86 // ok this is targeted at us, so send back the requested data
87 // this must be static as it will be accessed long after we have returned
88 static struct pad_toolmanager tool_return;
89 tool_return.current_tool_name= this->current_tool_name;
90
91 pdr->set_data_ptr(&tool_return);
92 pdr->set_taken();
93 }
94
95 void ToolManager::on_set_public_data(void* argument){
96 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
97
98 if(!pdr->starts_with(tool_manager_checksum)) return;
99
100 // ok this is targeted at us, so change tools
101 //uint16_t tool_name= *static_cast<float*>(pdr->get_data_ptr());
102 // TODO: fire a tool change gcode
103 pdr->set_taken();
104 }
105
106 // Add a tool to the tool list
107 void ToolManager::add_tool(Tool* tool_to_add){
108 if(this->tools.size() == 0){
109 tool_to_add->enable();
110 this->current_tool_name = tool_to_add->get_name();
111 //send new_tool_offsets to robot
112 const float *new_tool_offset = tool_to_add->get_offset();
113 THEKERNEL->robot->setToolOffset(new_tool_offset);
114 } else {
115 tool_to_add->disable();
116 }
117 this->tools.push_back( tool_to_add );
118 }
119
120
121