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