fixed minor merging problems
[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
f4a68aec
L
29#define return_error_on_unhandled_gcode_checksum CHECKSUM("return_error_on_unhandled_gcode")
30
92031952 31ToolManager::ToolManager(){
8adf2390 32 active_tool = 0;
38b9f24a 33 current_tool_name = CHECKSUM("hotend");
8adf2390 34}
14ecdbd7 35
92031952 36void ToolManager::on_module_loaded(){
f4a68aec
L
37 this->on_config_reload(this);
38
8adf2390 39 this->register_for_event(ON_GCODE_RECEIVED);
38b9f24a
L
40 this->register_for_event(ON_GET_PUBLIC_DATA);
41 this->register_for_event(ON_SET_PUBLIC_DATA);
8adf2390
L
42}
43
f4a68aec
L
44void ToolManager::on_config_reload(void *argument){
45 return_error_on_unhandled_gcode = THEKERNEL->config->value( return_error_on_unhandled_gcode_checksum )->by_default(false)->as_bool();
46}
47
92031952 48void ToolManager::on_gcode_received(void *argument){
8adf2390
L
49 Gcode *gcode = static_cast<Gcode*>(argument);
50
51 if( gcode->has_letter('T') ){
52 int new_tool = gcode->get_value('T');
c974b296 53 if(new_tool >= (int)this->tools.size() || new_tool < 0){
8adf2390 54 // invalid tool
f4a68aec
L
55 if( return_error_on_unhandled_gcode ) {
56 char buf[32]; // should be big enough for any status
57 int n= snprintf(buf, sizeof(buf), "T%d invalid tool ", new_tool);
58 gcode->txt_after_ok.append(buf, n);
59 }
8adf2390 60 } else {
c974b296 61 if(new_tool != this->active_tool){
17c89e4d 62 // We must wait for an empty queue before we can disable the current extruder
f8e5ee9b 63 THEKERNEL->conveyor->wait_for_empty_queue();
18551e1f
L
64 this->tools[active_tool]->disable();
65 this->active_tool = new_tool;
66 this->current_tool_name = this->tools[active_tool]->get_name();
67 this->tools[active_tool]->enable();
68
69 //send new_tool_offsets to robot
17c89e4d 70 const float *new_tool_offset = tools[new_tool]->get_offset();
fae93525 71 THEKERNEL->robot->setToolOffset(new_tool_offset);
8adf2390
L
72 }
73 }
74 }
75}
14ecdbd7 76
38b9f24a
L
77void ToolManager::on_get_public_data(void* argument){
78 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
79
80 if(!pdr->starts_with(tool_manager_checksum)) return;
8e8b938e
JM
81 if(!pdr->second_element_is(is_active_tool_checksum)) return;
82
83 // check that we control the given tool
84 bool managed= false;
85 for(auto t : tools) {
86 uint16_t n= t->get_name();
87 if(pdr->third_element_is(n)){
88 managed= true;
89 break;
90 }
91 }
92
93 // we are not managing this tool so do not answer
94 if(!managed) return;
38b9f24a 95
86fa0b93 96 pdr->set_data_ptr(&this->current_tool_name);
38b9f24a
L
97 pdr->set_taken();
98}
99
100void ToolManager::on_set_public_data(void* argument){
101 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
102
103 if(!pdr->starts_with(tool_manager_checksum)) return;
104
105 // ok this is targeted at us, so change tools
17c89e4d 106 //uint16_t tool_name= *static_cast<float*>(pdr->get_data_ptr());
38b9f24a 107 // TODO: fire a tool change gcode
8e8b938e 108 //pdr->set_taken();
38b9f24a
L
109}
110
14ecdbd7 111// Add a tool to the tool list
92031952 112void ToolManager::add_tool(Tool* tool_to_add){
c974b296
L
113 if(this->tools.size() == 0){
114 tool_to_add->enable();
b379d27a 115 this->current_tool_name = tool_to_add->get_name();
b4ba8089 116 //send new_tool_offsets to robot
17c89e4d 117 const float *new_tool_offset = tool_to_add->get_offset();
fae93525 118 THEKERNEL->robot->setToolOffset(new_tool_offset);
c974b296
L
119 } else {
120 tool_to_add->disable();
121 }
14ecdbd7
AW
122 this->tools.push_back( tool_to_add );
123}
124
125
126