smoothie port to mbed online compiler (smoothieware.org)

Dependencies:   mbed

For documentation, license, ..., please check http://smoothieware.org/

This version has been tested with a 3 axis machine

Committer:
scachat
Date:
Tue Jul 31 21:11:18 2012 +0000
Revision:
0:31e91bb0ef3c
smoothie port to mbed online compiler

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scachat 0:31e91bb0ef3c 1 /*
scachat 0:31e91bb0ef3c 2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
scachat 0:31e91bb0ef3c 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.
scachat 0:31e91bb0ef3c 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.
scachat 0:31e91bb0ef3c 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
scachat 0:31e91bb0ef3c 6 */
scachat 0:31e91bb0ef3c 7
scachat 0:31e91bb0ef3c 8 #include "libs/Module.h"
scachat 0:31e91bb0ef3c 9 #include "libs/Kernel.h"
scachat 0:31e91bb0ef3c 10 #include "modules/communication/utils/Gcode.h"
scachat 0:31e91bb0ef3c 11 #include "modules/robot/Stepper.h"
scachat 0:31e91bb0ef3c 12 #include "Laser.h"
scachat 0:31e91bb0ef3c 13 #include "libs/nuts_bolts.h"
scachat 0:31e91bb0ef3c 14
scachat 0:31e91bb0ef3c 15 Laser::Laser(PinName pin) : laser_pin(pin){
scachat 0:31e91bb0ef3c 16 this->laser_pin.period_us(10);
scachat 0:31e91bb0ef3c 17 }
scachat 0:31e91bb0ef3c 18
scachat 0:31e91bb0ef3c 19 void Laser::on_module_loaded() {
scachat 0:31e91bb0ef3c 20 if( !this->kernel->config->value( laser_module_enable_checksum )->by_default(false)->as_bool() ){ return; }
scachat 0:31e91bb0ef3c 21 this->register_for_event(ON_GCODE_EXECUTE);
scachat 0:31e91bb0ef3c 22 this->register_for_event(ON_SPEED_CHANGE);
scachat 0:31e91bb0ef3c 23 this->register_for_event(ON_PLAY);
scachat 0:31e91bb0ef3c 24 this->register_for_event(ON_PAUSE);
scachat 0:31e91bb0ef3c 25 this->register_for_event(ON_BLOCK_BEGIN);
scachat 0:31e91bb0ef3c 26 this->register_for_event(ON_BLOCK_END);
scachat 0:31e91bb0ef3c 27 }
scachat 0:31e91bb0ef3c 28
scachat 0:31e91bb0ef3c 29 // Turn laser off laser at the end of a move
scachat 0:31e91bb0ef3c 30 void Laser::on_block_end(void* argument){
scachat 0:31e91bb0ef3c 31 this->laser_pin = 0;
scachat 0:31e91bb0ef3c 32 }
scachat 0:31e91bb0ef3c 33
scachat 0:31e91bb0ef3c 34 // Set laser power at the beginning of a block
scachat 0:31e91bb0ef3c 35 void Laser::on_block_begin(void* argument){
scachat 0:31e91bb0ef3c 36 this->set_proportional_power();
scachat 0:31e91bb0ef3c 37 }
scachat 0:31e91bb0ef3c 38
scachat 0:31e91bb0ef3c 39 // When the play/pause button is set to pause, or a module calls the ON_PAUSE event
scachat 0:31e91bb0ef3c 40 void Laser::on_pause(void* argument){
scachat 0:31e91bb0ef3c 41 this->laser_pin = 0;
scachat 0:31e91bb0ef3c 42 }
scachat 0:31e91bb0ef3c 43
scachat 0:31e91bb0ef3c 44 // When the play/pause button is set to play, or a module calls the ON_PLAY event
scachat 0:31e91bb0ef3c 45 void Laser::on_play(void* argument){
scachat 0:31e91bb0ef3c 46 this->set_proportional_power();
scachat 0:31e91bb0ef3c 47 }
scachat 0:31e91bb0ef3c 48
scachat 0:31e91bb0ef3c 49 // Turn laser on/off depending on received GCodes
scachat 0:31e91bb0ef3c 50 void Laser::on_gcode_execute(void* argument){
scachat 0:31e91bb0ef3c 51 Gcode* gcode = static_cast<Gcode*>(argument);
scachat 0:31e91bb0ef3c 52 this->laser_on = false;
scachat 0:31e91bb0ef3c 53 if( gcode->has_letter('G' )){
scachat 0:31e91bb0ef3c 54 int code = gcode->get_value('G');
scachat 0:31e91bb0ef3c 55 if( code == 0 ){ // G0
scachat 0:31e91bb0ef3c 56 this->laser_pin = 0;
scachat 0:31e91bb0ef3c 57 this->laser_on = false;
scachat 0:31e91bb0ef3c 58 }else if( code >= 1 && code <= 3 ){ // G1, G2, G3
scachat 0:31e91bb0ef3c 59 this->laser_on = true;
scachat 0:31e91bb0ef3c 60 }
scachat 0:31e91bb0ef3c 61 }
scachat 0:31e91bb0ef3c 62 }
scachat 0:31e91bb0ef3c 63
scachat 0:31e91bb0ef3c 64 // We follow the stepper module here, so speed must be proportional
scachat 0:31e91bb0ef3c 65 void Laser::on_speed_change(void* argument){
scachat 0:31e91bb0ef3c 66 this->set_proportional_power();
scachat 0:31e91bb0ef3c 67 }
scachat 0:31e91bb0ef3c 68
scachat 0:31e91bb0ef3c 69 void Laser::set_proportional_power(){
scachat 0:31e91bb0ef3c 70 if( this->laser_on && this->kernel->stepper->current_block ){
scachat 0:31e91bb0ef3c 71 this->laser_pin = double(this->kernel->stepper->trapezoid_adjusted_rate)/double(this->kernel->stepper->current_block->nominal_rate);
scachat 0:31e91bb0ef3c 72 }
scachat 0:31e91bb0ef3c 73 }