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