Skip to content

Commit

Permalink
making mbed pwm pins for laser configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurwolf committed Oct 11, 2013
1 parent ac0a199 commit dad4ecf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
28 changes: 20 additions & 8 deletions src/modules/tools/laser/Laser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,22 @@ void Laser::on_module_loaded() {
return;
}

//this->laser_pin = new mbed::PwmOut(p21);
this->laser_pin.from_string(this->kernel->config->value(laser_module_pin_checksum)->by_default("nc")->as_string())->as_output();
// this->laser_pin.period_us(20);
this->laser_pin.set(0);
// Get smoothie-style pin from config
Pin* dummy_pin = new Pin();
dummy_pin->from_string(this->kernel->config->value(laser_module_pin_checksum)->by_default("nc")->as_string())->as_output();

// Get mBed-style pin from smoothie-style pin
if( dummy_pin->port_number == 2 ){
if( dummy_pin->pin == 0 ){ this->laser_pin = new mbed::PwmOut(p26); }
if( dummy_pin->pin == 1 ){ this->laser_pin = new mbed::PwmOut(p25); }
if( dummy_pin->pin == 2 ){ this->laser_pin = new mbed::PwmOut(p24); }
if( dummy_pin->pin == 3 ){ this->laser_pin = new mbed::PwmOut(p23); }
if( dummy_pin->pin == 4 ){ this->laser_pin = new mbed::PwmOut(p22); }
if( dummy_pin->pin == 5 ){ this->laser_pin = new mbed::PwmOut(p21); }
}

this->laser_pin->period_us(20);
this->laser_pin->write(0);

this->laser_max_power = this->kernel->config->value(laser_module_max_power_checksum)->by_default(0.8)->as_number() ;
this->laser_tickle_power = this->kernel->config->value(laser_module_tickle_power_checksum)->by_default(0)->as_number() ;
Expand All @@ -41,7 +53,7 @@ void Laser::on_module_loaded() {

// Turn laser off laser at the end of a move
void Laser::on_block_end(void* argument){
this->laser_pin.set(0);
this->laser_pin->write(0);
}

// Set laser power at the beginning of a block
Expand All @@ -51,7 +63,7 @@ void Laser::on_block_begin(void* argument){

// When the play/pause button is set to pause, or a module calls the ON_PAUSE event
void Laser::on_pause(void* argument){
this->laser_pin.set(0);
this->laser_pin->write(0);
}

// When the play/pause button is set to play, or a module calls the ON_PLAY event
Expand All @@ -66,7 +78,7 @@ void Laser::on_gcode_execute(void* argument){
if( gcode->has_g){
int code = gcode->g;
if( code == 0 ){ // G0
this->laser_pin.set(this->laser_tickle_power);
this->laser_pin->write(this->laser_tickle_power);
this->laser_on = false;
}else if( code >= 1 && code <= 3 ){ // G1, G2, G3
this->laser_on = true;
Expand All @@ -89,6 +101,6 @@ void Laser::on_speed_change(void* argument){
void Laser::set_proportional_power(){
if( this->laser_on && this->kernel->stepper->current_block ){
// adjust power to maximum power and actual velocity
this->laser_pin.set(float(double(this->laser_max_power) * double(this->kernel->stepper->trapezoid_adjusted_rate) / double(this->kernel->stepper->current_block->nominal_rate)));
this->laser_pin->write(float(double(this->laser_max_power) * double(this->kernel->stepper->trapezoid_adjusted_rate) / double(this->kernel->stepper->current_block->nominal_rate)));
}
}
9 changes: 5 additions & 4 deletions src/modules/tools/laser/Laser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "libs/Pin.h"
#include "libs/Kernel.h"
#include "modules/communication/utils/Gcode.h"
#include "PwmOut.h" // mbed.h lib


#define laser_module_enable_checksum CHECKSUM("laser_module_enable")
Expand All @@ -32,10 +33,10 @@ class Laser : public Module{
void on_speed_change(void* argument);
void set_proportional_power();

Pwm laser_pin; // PWM output to regulate the laser power
bool laser_on; // Laser status
float laser_max_power; // maximum allowed laser power to be output on the pwm pin
float laser_tickle_power; // value used to tickle the laser on moves
mbed::PwmOut* laser_pin; // PWM output to regulate the laser power
bool laser_on; // Laser status
float laser_max_power; // maximum allowed laser power to be output on the pwm pin
float laser_tickle_power; // value used to tickle the laser on moves
};

#endif

0 comments on commit dad4ecf

Please sign in to comment.