initial implementation of new laser module
[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 "Laser.h"
9 #include "libs/Module.h"
10 #include "libs/Kernel.h"
11 #include "libs/nuts_bolts.h"
12 #include "Config.h"
13 #include "StreamOutputPool.h"
14 #include "checksumm.h"
15 #include "ConfigValue.h"
16 #include "StepTicker.h"
17 #include "Block.h"
18 #include "SlowTicker.h"
19
20 #include "libs/Pin.h"
21 #include "Gcode.h"
22 #include "PwmOut.h" // mbed.h lib
23
24 #define laser_module_enable_checksum CHECKSUM("laser_module_enable")
25 #define laser_module_pin_checksum CHECKSUM("laser_module_pin")
26 #define laser_module_pwm_pin_checksum CHECKSUM("laser_module_pwm_pin")
27 #define laser_module_ttl_pin_checksum CHECKSUM("laser_module_ttl_pin")
28 #define laser_module_pwm_period_checksum CHECKSUM("laser_module_pwm_period")
29 #define laser_module_maximum_power_checksum CHECKSUM("laser_module_maximum_power")
30 #define laser_module_minimum_power_checksum CHECKSUM("laser_module_minimum_power")
31 #define laser_module_default_power_checksum CHECKSUM("laser_module_default_power")
32 #define laser_module_tickle_power_checksum CHECKSUM("laser_module_tickle_power")
33 #define laser_module_max_power_checksum CHECKSUM("laser_module_max_power")
34 #define laser_module_maximum_s_value_checksum CHECKSUM("laser_module_maximum_s_value")
35
36
37 Laser::Laser()
38 {
39 laser_on = false;
40 }
41
42 void Laser::on_module_loaded()
43 {
44 if( !THEKERNEL->config->value( laser_module_enable_checksum )->by_default(false)->as_bool() ) {
45 // as not needed free up resource
46 delete this;
47 return;
48 }
49
50 // Get smoothie-style pin from config
51 Pin* dummy_pin = new Pin();
52 dummy_pin->from_string(THEKERNEL->config->value(laser_module_pin_checksum)->by_default("nc")->as_string())->as_output();
53
54 // Alternative less ambiguous name for pwm_pin
55 if (!dummy_pin->connected())
56 dummy_pin->from_string(THEKERNEL->config->value(laser_module_pwm_pin_checksum)->by_default("nc")->as_string())->as_output();
57
58 pwm_pin = dummy_pin->hardware_pwm();
59
60 if (pwm_pin == NULL) {
61 THEKERNEL->streams->printf("Error: Laser cannot use P%d.%d (P2.0 - P2.5, P1.18, P1.20, P1.21, P1.23, P1.24, P1.26, P3.25, P3.26 only). Laser module disabled.\n", dummy_pin->port_number, dummy_pin->pin);
62 delete dummy_pin;
63 delete this;
64 return;
65 }
66
67
68 this->pwm_inverting = dummy_pin->is_inverting();
69
70 delete dummy_pin;
71 dummy_pin = NULL;
72
73 // TTL settings
74 this->ttl_pin = new Pin();
75 ttl_pin->from_string( THEKERNEL->config->value(laser_module_ttl_pin_checksum)->by_default("nc" )->as_string())->as_output();
76 this->ttl_used = ttl_pin->connected();
77 this->ttl_inverting = ttl_pin->is_inverting();
78 if (ttl_used) {
79 ttl_pin->set(0);
80 } else {
81 delete ttl_pin;
82 ttl_pin = NULL;
83 }
84
85
86 this->pwm_pin->period_us(THEKERNEL->config->value(laser_module_pwm_period_checksum)->by_default(20)->as_number());
87 this->pwm_pin->write(this->pwm_inverting ? 1 : 0);
88 this->laser_maximum_power = THEKERNEL->config->value(laser_module_maximum_power_checksum)->by_default(1.0f)->as_number() ;
89
90 // These config variables are deprecated, they have been replaced with laser_module_default_power and laser_module_minimum_power
91 this->laser_minimum_power = THEKERNEL->config->value(laser_module_tickle_power_checksum)->by_default(0)->as_number() ;
92 this->laser_power = THEKERNEL->config->value(laser_module_max_power_checksum)->by_default(0.8f)->as_number() ;
93
94 // Load in our preferred config variables
95 this->laser_minimum_power = THEKERNEL->config->value(laser_module_minimum_power_checksum)->by_default(this->laser_minimum_power)->as_number() ;
96 this->laser_power = THEKERNEL->config->value(laser_module_default_power_checksum)->by_default(this->laser_power)->as_number() ;
97
98 // S value that represents maximum (default 1)
99 this->laser_maximum_s_value = THEKERNEL->config->value(laser_module_maximum_s_value_checksum)->by_default(1.0f)->as_number() ;
100
101 turn_laser_off();
102
103 //register for events
104 this->register_for_event(ON_HALT);
105
106 THEKERNEL->slow_ticker->attach(1000, this, &Laser::set_proportional_power);
107 }
108
109 #if 0
110 // Turn laser off laser at the end of a move
111 void Laser::on_block_end(void* argument)
112 {
113 this->pwm_pin->write(this->pwm_inverting ? 1 : 0);
114
115 if (this->ttl_used) {
116 Block* block = static_cast<Block*>(argument);
117 // Only switch TTL off if this is the last block for this move - G2/3 are multiple blocks
118 if (block->final_rate == 0)
119 this->ttl_pin->set(0);
120 }
121 }
122
123 // Set laser power at the beginning of a block
124 void Laser::on_block_begin(void* argument)
125 {
126 this->set_proportional_power();
127
128 }
129
130 // Turn laser on/off depending on received GCodes
131 void Laser::on_gcode_execute(void* argument)
132 {
133 Gcode* gcode = static_cast<Gcode*>(argument);
134 this->laser_on = false;
135 if( gcode->has_g) {
136 int code = gcode->g;
137 if( code == 0 ) { // G0
138 this->pwm_pin->write(this->pwm_inverting ? 1 - this->laser_minimum_power : this->laser_minimum_power);
139 this->laser_on = false;
140 } else if( code >= 1 && code <= 3 ) { // G1, G2, G3
141 this->laser_on = true;
142 }
143 }
144
145 if ( gcode->has_letter('S' )) {
146 float requested_power = gcode->get_value('S') / this->laser_maximum_s_value;
147 // Ensure we can't exceed maximum power
148 if (requested_power > 1)
149 requested_power = 1;
150
151 this->laser_power = requested_power;
152 }
153
154 if (this->ttl_used)
155 this->ttl_pin->set(this->laser_on);
156
157 }
158
159 // We follow the stepper module here, so speed must be proportional
160 void Laser::on_speed_change(void* argument)
161 {
162 if( this->laser_on ) {
163 this->set_proportional_power();
164 }
165 }
166 #endif
167
168 void Laser::turn_laser_off()
169 {
170 this->pwm_pin->write(this->pwm_inverting ? 1 : 0);
171 if (this->ttl_used) this->ttl_pin->set(false);
172 laser_on = false;
173 }
174
175 // calculates the current speed ratio from the currently executing block
176 float Laser::current_speed_ratio(const Block *block) const
177 {
178 // TODO find the primarty moving actuator and figur eout the ration of its speed, from 0 to 1 based on where it is on the trapezoid
179
180 return 1.0F;
181 }
182
183 // get laser power for the currently executing block, returns false if nothing running or a G0
184 bool Laser::get_laser_power(float& power) const
185 {
186 const Block *block = StepTicker::getInstance()->get_current_block();
187
188 if(block != nullptr && block->is_g123) {
189 float requested_power = block->s_value / this->laser_maximum_s_value;
190 // Ensure we can't exceed maximum power
191 if (requested_power > 1)
192 requested_power = 1;
193
194 float ratio = current_speed_ratio(block);
195 power = requested_power * ratio;
196
197 return true;
198 }
199
200 return false;
201 }
202
203 // called every millisecond from timer ISR
204 uint32_t Laser::set_proportional_power(uint32_t dummy)
205 {
206 float power;
207 if(get_laser_power(power)) {
208 // adjust power to maximum power and actual velocity
209 float proportional_power = ( (this->laser_maximum_power - this->laser_minimum_power) * power ) + this->laser_minimum_power;
210 this->pwm_pin->write(this->pwm_inverting ? 1 - proportional_power : proportional_power);
211 if(!laser_on && this->ttl_used) this->ttl_pin->set(true);
212 laser_on = true;
213
214 } else if(laser_on) {
215 // turn laser off
216 turn_laser_off();
217 }
218 return 0;
219 }
220
221 void Laser::on_halt(void *argument)
222 {
223 if(argument == nullptr) {
224 turn_laser_off();
225 }
226 }