Allow TABS in config
[clinton/Smoothieware.git] / src / modules / tools / laser / Laser.cpp
CommitLineData
de91760a 1/*
58baeec1
MM
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/>.
4cff3ded
AW
6*/
7
4cff3ded
AW
8#include "libs/Module.h"
9#include "libs/Kernel.h"
10#include "modules/communication/utils/Gcode.h"
11#include "modules/robot/Stepper.h"
12#include "Laser.h"
a2cd92c0 13#include "libs/nuts_bolts.h"
61134a65
JM
14#include "Config.h"
15#include "StreamOutputPool.h"
16#include "Block.h"
4cff3ded 17
e605a0d6 18Laser::Laser(){
4cff3ded
AW
19}
20
21void Laser::on_module_loaded() {
314ab8f7 22 if( !THEKERNEL->config->value( laser_module_enable_checksum )->by_default(false)->as_bool() ){
0b1e8fd1
BG
23 // as not needed free up resource
24 delete this;
25 return;
26 }
e605a0d6 27
dad4ecfc
AW
28 // Get smoothie-style pin from config
29 Pin* dummy_pin = new Pin();
314ab8f7 30 dummy_pin->from_string(THEKERNEL->config->value(laser_module_pin_checksum)->by_default("nc")->as_string())->as_output();
61134a65 31
820ee962
MM
32 laser_pin = NULL;
33
dad4ecfc
AW
34 // Get mBed-style pin from smoothie-style pin
35 if( dummy_pin->port_number == 2 ){
36 if( dummy_pin->pin == 0 ){ this->laser_pin = new mbed::PwmOut(p26); }
37 if( dummy_pin->pin == 1 ){ this->laser_pin = new mbed::PwmOut(p25); }
38 if( dummy_pin->pin == 2 ){ this->laser_pin = new mbed::PwmOut(p24); }
39 if( dummy_pin->pin == 3 ){ this->laser_pin = new mbed::PwmOut(p23); }
40 if( dummy_pin->pin == 4 ){ this->laser_pin = new mbed::PwmOut(p22); }
41 if( dummy_pin->pin == 5 ){ this->laser_pin = new mbed::PwmOut(p21); }
42 }
820ee962
MM
43
44 if (laser_pin == NULL)
45 {
46 THEKERNEL->streams->printf("Error: Laser cannot use P%d.%d (P2.0 - P2.5 only). Laser module disabled.\n", dummy_pin->port_number, dummy_pin->pin);
47 delete dummy_pin;
48 delete this;
49 return;
50 }
51
9f9d7939 52 this->laser_inverting = dummy_pin->inverting;
dad4ecfc 53
820ee962
MM
54 delete dummy_pin;
55 dummy_pin = NULL;
56
9548623d 57 this->laser_pin->period_us(THEKERNEL->config->value(laser_module_pwm_period_checksum)->by_default(20)->as_number());
9f9d7939 58 this->laser_pin->write(this->laser_inverting ? 1 : 0);
e605a0d6 59
1ad23cd3
MM
60 this->laser_max_power = THEKERNEL->config->value(laser_module_max_power_checksum )->by_default(0.8f)->as_number() ;
61 this->laser_tickle_power = THEKERNEL->config->value(laser_module_tickle_power_checksum)->by_default(0 )->as_number() ;
0b1e8fd1 62
e605a0d6 63 //register for events
4cff3ded
AW
64 this->register_for_event(ON_GCODE_EXECUTE);
65 this->register_for_event(ON_SPEED_CHANGE);
befcf5cc
AW
66 this->register_for_event(ON_PLAY);
67 this->register_for_event(ON_PAUSE);
a2cd92c0
AW
68 this->register_for_event(ON_BLOCK_BEGIN);
69 this->register_for_event(ON_BLOCK_END);
4cff3ded
AW
70}
71
a2cd92c0
AW
72// Turn laser off laser at the end of a move
73void Laser::on_block_end(void* argument){
9f9d7939 74 this->laser_pin->write(this->laser_inverting ? 1 : 0);
a2cd92c0
AW
75}
76
77// Set laser power at the beginning of a block
78void Laser::on_block_begin(void* argument){
79 this->set_proportional_power();
80}
befcf5cc
AW
81
82// When the play/pause button is set to pause, or a module calls the ON_PAUSE event
83void Laser::on_pause(void* argument){
9f9d7939 84 this->laser_pin->write(this->laser_inverting ? 1 : 0);
befcf5cc
AW
85}
86
87// When the play/pause button is set to play, or a module calls the ON_PLAY event
88void Laser::on_play(void* argument){
a2cd92c0 89 this->set_proportional_power();
befcf5cc
AW
90}
91
4cff3ded
AW
92// Turn laser on/off depending on received GCodes
93void Laser::on_gcode_execute(void* argument){
94 Gcode* gcode = static_cast<Gcode*>(argument);
b66fb830 95 this->laser_on = false;
0fe213e1
AW
96 if( gcode->has_g){
97 int code = gcode->g;
a2cd92c0 98 if( code == 0 ){ // G0
9f9d7939 99 this->laser_pin->write(this->laser_inverting ? 1 - this->laser_tickle_power : this->laser_tickle_power);
4cff3ded 100 this->laser_on = false;
a2cd92c0 101 }else if( code >= 1 && code <= 3 ){ // G1, G2, G3
4cff3ded
AW
102 this->laser_on = true;
103 }
104 }
de91760a 105 if ( gcode->has_letter('S' )){
58baeec1 106 this->laser_max_power = gcode->get_value('S');
314ab8f7 107// THEKERNEL->streams->printf("Adjusted laser power to %d/100\r\n",(int)(this->laser_max_power*100.0+0.5));
de91760a
GM
108 }
109
4cff3ded
AW
110}
111
6f57fb56 112// We follow the stepper module here, so speed must be proportional
4cff3ded 113void Laser::on_speed_change(void* argument){
58ecb87d
MC
114 if( this->laser_on ){
115 this->set_proportional_power();
116 }
a2cd92c0
AW
117}
118
119void Laser::set_proportional_power(){
314ab8f7 120 if( this->laser_on && THEKERNEL->stepper->current_block ){
58baeec1 121 // adjust power to maximum power and actual velocity
9f9d7939
L
122 float proportional_power = float(float(this->laser_max_power) * float(THEKERNEL->stepper->trapezoid_adjusted_rate) / float(THEKERNEL->stepper->current_block->nominal_rate));
123 this->laser_pin->write(this->laser_inverting ? 1 - proportional_power : proportional_power);
4cff3ded 124 }
2f37949e 125}