Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Laser.cpp Source File

Laser.cpp

00001 /*
00002     This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
00003     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.
00004     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.
00005     You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
00006 */
00007 
00008 #include "libs/Module.h"
00009 #include "libs/Kernel.h"
00010 #include "modules/communication/utils/Gcode.h"
00011 #include "modules/robot/Stepper.h"
00012 #include "Laser.h"
00013 #include "libs/nuts_bolts.h"
00014 
00015 Laser::Laser(){
00016 }
00017 
00018 void Laser::on_module_loaded() {
00019     if( !THEKERNEL->config->value( laser_module_enable_checksum )->by_default(false)->as_bool() ){
00020         // as not needed free up resource
00021         delete this;
00022         return;
00023     }
00024 
00025     // Get smoothie-style pin from config
00026     Pin* dummy_pin = new Pin();
00027     dummy_pin->from_string(THEKERNEL->config->value(laser_module_pin_checksum)->by_default("nc")->as_string())->as_output();
00028     
00029     // Get mBed-style pin from smoothie-style pin
00030     if( dummy_pin->port_number == 2 ){
00031         if( dummy_pin->pin == 0 ){ this->laser_pin = new mbed::PwmOut(p26); }
00032         if( dummy_pin->pin == 1 ){ this->laser_pin = new mbed::PwmOut(p25); }
00033         if( dummy_pin->pin == 2 ){ this->laser_pin = new mbed::PwmOut(p24); }
00034         if( dummy_pin->pin == 3 ){ this->laser_pin = new mbed::PwmOut(p23); }
00035         if( dummy_pin->pin == 4 ){ this->laser_pin = new mbed::PwmOut(p22); }
00036         if( dummy_pin->pin == 5 ){ this->laser_pin = new mbed::PwmOut(p21); }
00037     }
00038     this->laser_inverting = dummy_pin->inverting;
00039 
00040     this->laser_pin->period_us(THEKERNEL->config->value(laser_module_pwm_period_checksum)->by_default(20)->as_number());
00041     this->laser_pin->write(this->laser_inverting ? 1 : 0);
00042 
00043     this->laser_max_power =    THEKERNEL->config->value(laser_module_max_power_checksum   )->by_default(0.8f)->as_number() ;
00044     this->laser_tickle_power = THEKERNEL->config->value(laser_module_tickle_power_checksum)->by_default(0   )->as_number() ;
00045 
00046     //register for events
00047     this->register_for_event(ON_GCODE_EXECUTE);
00048     this->register_for_event(ON_SPEED_CHANGE);
00049     this->register_for_event(ON_PLAY);
00050     this->register_for_event(ON_PAUSE);
00051     this->register_for_event(ON_BLOCK_BEGIN);
00052     this->register_for_event(ON_BLOCK_END);
00053 }
00054 
00055 // Turn laser off laser at the end of a move
00056 void  Laser::on_block_end(void* argument){
00057     this->laser_pin->write(this->laser_inverting ? 1 : 0);
00058 }
00059 
00060 // Set laser power at the beginning of a block
00061 void Laser::on_block_begin(void* argument){
00062     this->set_proportional_power();
00063 }
00064 
00065 // When the play/pause button is set to pause, or a module calls the ON_PAUSE event
00066 void Laser::on_pause(void* argument){
00067     this->laser_pin->write(this->laser_inverting ? 1 : 0);
00068 }
00069 
00070 // When the play/pause button is set to play, or a module calls the ON_PLAY event
00071 void Laser::on_play(void* argument){
00072     this->set_proportional_power();
00073 }
00074 
00075 // Turn laser on/off depending on received GCodes
00076 void Laser::on_gcode_execute(void* argument){
00077     Gcode* gcode = static_cast<Gcode*>(argument);
00078     this->laser_on = false;
00079     if( gcode->has_g){
00080         int code = gcode->g;
00081         if( code == 0 ){                    // G0
00082             this->laser_pin->write(this->laser_inverting ? 1 - this->laser_tickle_power : this->laser_tickle_power);
00083             this->laser_on =  false;
00084         }else if( code >= 1 && code <= 3 ){ // G1, G2, G3
00085             this->laser_on =  true;
00086         }
00087     }
00088     if ( gcode->has_letter('S' )){
00089         this->laser_max_power = gcode->get_value('S');
00090 //         THEKERNEL->streams->printf("Adjusted laser power to %d/100\r\n",(int)(this->laser_max_power*100.0+0.5));
00091     }
00092 
00093 }
00094 
00095 // We follow the stepper module here, so speed must be proportional
00096 void Laser::on_speed_change(void* argument){
00097     if( this->laser_on ){
00098         this->set_proportional_power();
00099     }
00100 }
00101 
00102 void Laser::set_proportional_power(){
00103     if( this->laser_on && THEKERNEL->stepper->current_block ){
00104         // adjust power to maximum power and actual velocity
00105         float proportional_power = float(float(this->laser_max_power) * float(THEKERNEL->stepper->trapezoid_adjusted_rate) / float(THEKERNEL->stepper->current_block->nominal_rate));
00106         this->laser_pin->write(this->laser_inverting ? 1 - proportional_power : proportional_power);
00107     }
00108 }