Merge branch 'upstreamedge' into feature/add-xy-maxspeed
[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 "ToolManager.h"
11 #include "Tool.h"
12 #include "PublicDataRequest.h"
13 #include "ToolManagerPublicAccess.h"
14 #include "Config.h"
15 #include "Robot.h"
16 #include "ConfigValue.h"
17 #include "Conveyor.h"
18 #include "checksumm.h"
19 #include "PublicData.h"
20 #include "Gcode.h"
21
22 #include "libs/SerialMessage.h"
23 #include "libs/StreamOutput.h"
24 #include "FileStream.h"
25
26 #include <math.h>
27
28 ToolManager::ToolManager()
29 {
30 active_tool = 0;
31 current_tool_name = CHECKSUM("hotend");
32 }
33
34 void ToolManager::on_module_loaded()
35 {
36
37 this->register_for_event(ON_GCODE_RECEIVED);
38 this->register_for_event(ON_GET_PUBLIC_DATA);
39 this->register_for_event(ON_SET_PUBLIC_DATA);
40 }
41
42 void ToolManager::on_gcode_received(void *argument)
43 {
44 Gcode *gcode = static_cast<Gcode*>(argument);
45
46 if( gcode->has_letter('T') ) {
47 int new_tool = gcode->get_value('T');
48 if(new_tool >= (int)this->tools.size() || new_tool < 0) {
49 // invalid tool
50 char buf[32]; // should be big enough for any status
51 int n = snprintf(buf, sizeof(buf), "T%d invalid tool ", new_tool);
52 gcode->txt_after_ok.append(buf, n);
53
54 } else {
55 if(new_tool != this->active_tool) {
56 // We must wait for an empty queue before we can disable the current extruder
57 THEKERNEL->conveyor->wait_for_idle();
58 this->tools[active_tool]->deselect();
59 this->active_tool = new_tool;
60 this->current_tool_name = this->tools[active_tool]->get_name();
61 this->tools[active_tool]->select();
62
63 //send new_tool_offsets to robot
64 const float *new_tool_offset = tools[new_tool]->get_offset();
65 THEROBOT->setToolOffset(new_tool_offset);
66 }
67 }
68 }
69 }
70
71 void ToolManager::on_get_public_data(void* argument)
72 {
73 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
74
75 if(!pdr->starts_with(tool_manager_checksum)) return;
76
77 if(pdr->second_element_is(is_active_tool_checksum)) {
78
79 // check that we control the given tool
80 bool managed = false;
81 for(auto t : tools) {
82 uint16_t n = t->get_name();
83 if(pdr->third_element_is(n)) {
84 managed = true;
85 break;
86 }
87 }
88
89 // we are not managing this tool so do not answer
90 if(!managed) return;
91
92 pdr->set_data_ptr(&this->current_tool_name);
93 pdr->set_taken();
94
95 }else if(pdr->second_element_is(get_active_tool_checksum)) {
96 pdr->set_data_ptr(&this->active_tool);
97 pdr->set_taken();
98 }
99 }
100
101 void ToolManager::on_set_public_data(void* argument)
102 {
103 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
104
105 if(!pdr->starts_with(tool_manager_checksum)) return;
106
107 // ok this is targeted at us, so change tools
108 //uint16_t tool_name= *static_cast<float*>(pdr->get_data_ptr());
109 // TODO: fire a tool change gcode
110 //pdr->set_taken();
111 }
112
113 // Add a tool to the tool list
114 void ToolManager::add_tool(Tool* tool_to_add)
115 {
116 if(this->tools.size() == 0) {
117 tool_to_add->select();
118 this->current_tool_name = tool_to_add->get_name();
119 //send new_tool_offsets to robot
120 const float *new_tool_offset = tool_to_add->get_offset();
121 THEROBOT->setToolOffset(new_tool_offset);
122 } else {
123 tool_to_add->deselect();
124 }
125 this->tools.push_back( tool_to_add );
126 }
127
128
129