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/>.
9 #include "libs/Module.h"
10 #include "libs/Kernel.h"
11 #include "libs/nuts_bolts.h"
13 #include "StreamOutputPool.h"
14 #include "checksumm.h"
15 #include "ConfigValue.h"
16 #include "StepTicker.h"
18 #include "SlowTicker.h"
22 #include "PwmOut.h" // mbed.h lib
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")
42 void Laser::on_module_loaded()
44 if( !THEKERNEL
->config
->value( laser_module_enable_checksum
)->by_default(false)->as_bool() ) {
45 // as not needed free up resource
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();
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();
58 pwm_pin
= dummy_pin
->hardware_pwm();
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
);
68 this->pwm_inverting
= dummy_pin
->is_inverting();
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();
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() ;
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() ;
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() ;
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() ;
103 //register for events
104 this->register_for_event(ON_HALT
);
106 THEKERNEL
->slow_ticker
->attach(1000, this, &Laser::set_proportional_power
);
110 // Turn laser off laser at the end of a move
111 void Laser::on_block_end(void* argument
)
113 this->pwm_pin
->write(this->pwm_inverting
? 1 : 0);
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);
123 // Set laser power at the beginning of a block
124 void Laser::on_block_begin(void* argument
)
126 this->set_proportional_power();
130 // Turn laser on/off depending on received GCodes
131 void Laser::on_gcode_execute(void* argument
)
133 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
134 this->laser_on
= false;
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;
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)
151 this->laser_power
= requested_power
;
155 this->ttl_pin
->set(this->laser_on
);
159 // We follow the stepper module here, so speed must be proportional
160 void Laser::on_speed_change(void* argument
)
162 if( this->laser_on
) {
163 this->set_proportional_power();
168 void Laser::turn_laser_off()
170 this->pwm_pin
->write(this->pwm_inverting
? 1 : 0);
171 if (this->ttl_used
) this->ttl_pin
->set(false);
175 // calculates the current speed ratio from the currently executing block
176 float Laser::current_speed_ratio(const Block
*block
) const
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
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
186 const Block
*block
= StepTicker::getInstance()->get_current_block();
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)
194 float ratio
= current_speed_ratio(block
);
195 power
= requested_power
* ratio
;
203 // called every millisecond from timer ISR
204 uint32_t Laser::set_proportional_power(uint32_t dummy
)
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);
214 } else if(laser_on
) {
221 void Laser::on_halt(void *argument
)
223 if(argument
== nullptr) {