added wait for empty queue back to toolchange
[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
1440c258 29#include "modules/robot/RobotPublicAccess.h"
14ecdbd7 30
f4a68aec
L
31#define return_error_on_unhandled_gcode_checksum CHECKSUM("return_error_on_unhandled_gcode")
32
1440c258
L
33#define X_AXIS 0
34#define Y_AXIS 1
35#define Z_AXIS 2
36
92031952 37ToolManager::ToolManager(){
8adf2390 38 active_tool = 0;
38b9f24a 39 current_tool_name = CHECKSUM("hotend");
8adf2390 40}
14ecdbd7 41
92031952 42void ToolManager::on_module_loaded(){
f4a68aec
L
43 this->on_config_reload(this);
44
45 this->register_for_event(ON_CONFIG_RELOAD);
8adf2390 46 this->register_for_event(ON_GCODE_RECEIVED);
38b9f24a
L
47 this->register_for_event(ON_GET_PUBLIC_DATA);
48 this->register_for_event(ON_SET_PUBLIC_DATA);
8adf2390
L
49}
50
f4a68aec
L
51void ToolManager::on_config_reload(void *argument){
52 return_error_on_unhandled_gcode = THEKERNEL->config->value( return_error_on_unhandled_gcode_checksum )->by_default(false)->as_bool();
53}
54
92031952 55void ToolManager::on_gcode_received(void *argument){
8adf2390
L
56 Gcode *gcode = static_cast<Gcode*>(argument);
57
58 if( gcode->has_letter('T') ){
59 int new_tool = gcode->get_value('T');
60 gcode->mark_as_taken();
c974b296 61 if(new_tool >= (int)this->tools.size() || new_tool < 0){
8adf2390 62 // invalid tool
f4a68aec
L
63 if( return_error_on_unhandled_gcode ) {
64 char buf[32]; // should be big enough for any status
65 int n= snprintf(buf, sizeof(buf), "T%d invalid tool ", new_tool);
66 gcode->txt_after_ok.append(buf, n);
67 }
8adf2390 68 } else {
c974b296 69 if(new_tool != this->active_tool){
f8e5ee9b 70 THEKERNEL->conveyor->wait_for_empty_queue();
18551e1f
L
71 this->tools[active_tool]->disable();
72 this->active_tool = new_tool;
73 this->current_tool_name = this->tools[active_tool]->get_name();
74 this->tools[active_tool]->enable();
75
76 //send new_tool_offsets to robot
18551e1f
L
77 float *new_tool_offset = tools[new_tool]->get_offset();
78 THEKERNEL->robot->setToolOffset(new_tool_offset[0], new_tool_offset[1], new_tool_offset[2]);
8adf2390
L
79 }
80 }
81 }
82}
14ecdbd7 83
38b9f24a
L
84void ToolManager::on_get_public_data(void* argument){
85 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
86
87 if(!pdr->starts_with(tool_manager_checksum)) return;
88
89 // ok this is targeted at us, so send back the requested data
90 // this must be static as it will be accessed long after we have returned
91 static struct pad_toolmanager tool_return;
92 tool_return.current_tool_name= this->current_tool_name;
93
94 pdr->set_data_ptr(&tool_return);
95 pdr->set_taken();
96}
97
98void ToolManager::on_set_public_data(void* argument){
99 PublicDataRequest* pdr = static_cast<PublicDataRequest*>(argument);
100
101 if(!pdr->starts_with(tool_manager_checksum)) return;
102
103 // ok this is targeted at us, so change tools
104 uint16_t tool_name= *static_cast<float*>(pdr->get_data_ptr());
105 // TODO: fire a tool change gcode
106 pdr->set_taken();
107}
108
14ecdbd7 109// Add a tool to the tool list
92031952 110void ToolManager::add_tool(Tool* tool_to_add){
c974b296
L
111 if(this->tools.size() == 0){
112 tool_to_add->enable();
b379d27a 113 this->current_tool_name = tool_to_add->get_name();
b4ba8089
L
114 //send new_tool_offsets to robot
115 float *new_tool_offset = tool_to_add->get_offset();
116 THEKERNEL->robot->setToolOffset(new_tool_offset[0], new_tool_offset[1], new_tool_offset[2]);
c974b296
L
117 } else {
118 tool_to_add->disable();
119 }
14ecdbd7
AW
120 this->tools.push_back( tool_to_add );
121}
122
123
124