Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Marlin/src/gcode/calibrate/G28.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "../snapmaker/src/module/toolhead_3dp.h"
#include "../snapmaker/src/service/bed_level.h"
#include "../snapmaker/src/module/linear.h"
#include "../snapmaker/src/module/toolhead_laser.h"
#endif

#if ENABLED(QUICK_HOME)
Expand Down Expand Up @@ -193,6 +194,10 @@ void GcodeSuite::G28(const bool always_home_all) {
DEBUG_ECHOLNPGM(">>> G28");
log_machine_info();
}
if (laser->IsOnline()) {
laser->InlineDisable();
laser->TurnOff();
}

#if ENABLED(DUAL_X_CARRIAGE)
bool IDEX_saved_duplication_state = extruder_duplication_enabled;
Expand Down
17 changes: 17 additions & 0 deletions Marlin/src/gcode/gcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ GcodeSuite gcode;

#if (MOTHERBOARD == BOARD_SNAPMAKER_2_0)
#include "../../../snapmaker/src/module/module_base.h"
#include "../snapmaker/src/module/toolhead_laser.h"
#endif

#include "../Marlin.h" // for idle() and suspend_auto_report
Expand Down Expand Up @@ -125,6 +126,17 @@ void GcodeSuite::get_destination_from_command() {
#if BOTH(MIXING_EXTRUDER, DIRECT_MIXING_IN_G1)
M165();
#endif

// Set the laser power in the planner to configure this move
float power = NAN; // nullable power
if (parser.seen('P'))
power = parser.value_float();
else if (parser.seen('S'))
power = parser.value_float() * 100.0f / 255.0f;

// If no power given treat as non-inline
if (laser->IsOnline() && !isnan(power))
laser->SetOutputInline(power);
}

/**
Expand Down Expand Up @@ -316,6 +328,11 @@ void GcodeSuite::execute_command(void) {
case 5: M5(); break; // M5 - turn spindle/laser off
#endif

// Coolant controls: Ignore silently
// case 7:
// case 8:
// case 9: break;

#if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
case 12: M12(); break; // M12: Synchronize and optionally force a CLC set
#endif
Expand Down
10 changes: 10 additions & 0 deletions Marlin/src/module/planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ uint8_t Planner::delay_before_delivering; // This counter delays delivery

planner_settings_t Planner::settings; // Initialized by settings.load()

laser_state_t Planner::laser_inline; // Planner laser power for blocks

uint32_t Planner::max_acceleration_steps_per_s2[X_TO_EN]; // (steps/s^2) Derived from mm_per_s2

float Planner::steps_to_mm[X_TO_EN]; // (mm) Millimeters per step
Expand Down Expand Up @@ -765,6 +767,11 @@ void Planner::calculate_trapezoid_for_block(block_t* const block, const float &e
block->cruise_rate = cruise_rate;
#endif
block->final_rate = final_rate;

/**
* Laser trapezoid: set entry power
*/
block->laser.power_entry = block->laser.power * entry_factor;
}

/* PLANNER SPEED DEFINITION
Expand Down Expand Up @@ -1831,6 +1838,9 @@ bool Planner::_populate_block(block_t * const block, bool split_move,
// Clear all flags, including the "busy" bit
block->flag = 0x00;

block->laser.status = laser_inline.status;
block->laser.power = laser_inline.power;

// Set direction bits
block->direction_bits = dm;

Expand Down
26 changes: 26 additions & 0 deletions Marlin/src/module/planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ enum BlockFlag : char {
BLOCK_FLAG_SYNC_POSITION = _BV(BLOCK_BIT_SYNC_POSITION)
};

typedef struct {
bool isEnabled:1;
} laser_power_status_t;

typedef struct {
laser_power_status_t status;
uint16_t power; // When in trapezoid mode this is nominal power
uint16_t power_entry; // Entry power for the laser
} block_inline_laser_t;

/**
* struct block_t
*
Expand Down Expand Up @@ -159,12 +169,26 @@ typedef struct block_t {

uint32_t filePos; // position of gcode of this block in the file

block_inline_laser_t laser;

} block_t;

#define HAS_POSITION_FLOAT ANY(LIN_ADVANCE, SCARA_FEEDRATE_SCALING, GRADIENT_MIX)

#define BLOCK_MOD(n) ((n)&(BLOCK_BUFFER_SIZE-1))

typedef struct {
/**
uint32_t max_acceleration_mm_per_s2[X_TO_EN], // (mm/s^2) M201 XYZE
* Laser status flags
*/
laser_power_status_t status;
/**
* Laser power: 0 to 255;
*/
uint16_t power;
} laser_state_t;

typedef struct {
uint32_t max_acceleration_mm_per_s2[X_TO_EN], // (mm/s^2) M201 XYZE
min_segment_time_us; // (µs) M205 B
Expand Down Expand Up @@ -240,6 +264,8 @@ class Planner {
#endif
static bool is_user_set_lead; // M92 Specifies whether to use a user-defined value
static planner_settings_t settings;

static laser_state_t laser_inline;

static uint32_t max_acceleration_steps_per_s2[X_TO_EN]; // (steps/s^2) Derived from mm_per_s2
static float steps_to_mm[X_TO_EN]; // Millimeters per step
Expand Down
35 changes: 35 additions & 0 deletions Marlin/src/module/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Stepper stepper; // Singleton
#include "../HAL/shared/Delay.h"
#include "../../../snapmaker/src/module/emergency_stop.h"
#include "../../../snapmaker/src/snapmaker.h"
#include "../../../snapmaker/src/module/toolhead_laser.h"

#if MB(ALLIGATOR)
#include "../feature/dac/dac_dac084s085.h"
Expand Down Expand Up @@ -223,6 +224,12 @@ volatile int32_t Stepper::endstops_trigsteps[XYZ];
volatile int32_t Stepper::count_position[NUM_AXIS] = { 0 };
int8_t Stepper::count_direction[NUM_AXIS] = { 0, 0, 0, 0, 0 };

Stepper::stepper_laser_t Stepper::laser_trap = {
.enabled = false,
.cur_power = 0,
.cruise_set = false
};

#define DUAL_ENDSTOP_APPLY_STEP(A,V) \
if (separate_multi_axis) { \
if (A##_HOME_DIR < 0) { \
Expand Down Expand Up @@ -1643,6 +1650,12 @@ uint32_t Stepper::stepper_block_phase_isr() {
}
else if (LA_steps) nextAdvanceISR = 0;
#endif // LIN_ADVANCE

// Update laser - Accelerating
if (laser_trap.enabled) {
laser_trap.cur_power = (current_block->laser.power * acc_step_rate) / current_block->nominal_rate;
laser->TurnOn_ISR(laser_trap.cur_power);
}
}
// Are we in Deceleration phase ?
else if (step_events_completed > decelerate_after) {
Expand Down Expand Up @@ -1691,6 +1704,12 @@ uint32_t Stepper::stepper_block_phase_isr() {
}
else if (LA_steps) nextAdvanceISR = 0;
#endif // LIN_ADVANCE

// Update laser - Decelerating
if (laser_trap.enabled) {
laser_trap.cur_power = (current_block->laser.power * step_rate) / current_block->nominal_rate;
laser->TurnOn_ISR(laser_trap.cur_power);
}
}
// We must be in cruise phase otherwise
else {
Expand All @@ -1708,6 +1727,15 @@ uint32_t Stepper::stepper_block_phase_isr() {

// The timer interval is just the nominal value for the nominal speed
interval = ticks_nominal;

// Update laser - Cruising
if (laser_trap.enabled) {
if (!laser_trap.cruise_set) {
laser_trap.cur_power = current_block->laser.power;
laser->TurnOn_ISR(laser_trap.cur_power);
laser_trap.cruise_set = true;
}
}
}
}
}
Expand Down Expand Up @@ -1889,6 +1917,13 @@ uint32_t Stepper::stepper_block_phase_isr() {
set_directions();
}

// Set up inline laser power
laser_trap.enabled = current_block->laser.status.isEnabled;
laser_trap.cur_power = current_block->laser.power_entry; // RESET STATE
laser_trap.cruise_set = false;
if (laser_trap.enabled)
laser->TurnOn_ISR(laser_trap.cur_power);

// At this point, we must ensure the movement about to execute isn't
// trying to force the head against a limit switch. If using interrupt-
// driven change detection, and already against a limit then no call to
Expand Down
11 changes: 11 additions & 0 deletions Marlin/src/module/stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,17 @@ class Stepper {
//
static int8_t count_direction[NUM_AXIS];

//
// Laser Inline Power Trapezoids
//
typedef struct {
bool enabled; // Trapezoid needed flag (i.e., laser on, planner in control)
uint16_t cur_power; // Current laser power
bool cruise_set; // Power set up for cruising?
} stepper_laser_t;

static stepper_laser_t laser_trap;

public:

//
Expand Down
32 changes: 28 additions & 4 deletions snapmaker/src/gcode/M3-M5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@
*/

void GcodeSuite::M3_M4(const bool is_M4) {
float power = NAN; // nullable power
if (parser.seen('P'))
power = parser.value_float();
else if (parser.seen('S'))
power = parser.value_float() * 100.0f / 255.0f;

if (laser->IsOnline()) {
// If no power given treat as non-inline
if (parser.seen('I') && !isnan(power)) {
laser->SetOutputInline(power);
return;
}

laser->InlineDisable(); // Disable planner laser control
}

planner.synchronize(); // wait until previous movement commands (G0/G0/G2/G3) have completed before playing with the spindle

Expand All @@ -72,16 +87,16 @@ void GcodeSuite::M3_M4(const bool is_M4) {
*/

if(laser->IsOnline()) {
if(parser.seen('P')) {
laser->SetOutput(parser.value_float());
if(!isnan(power)) {
laser->SetOutput(power);
}
else {
laser->TurnOn();
}
}
else if(cnc.IsOnline()) {
if(parser.seen('P'))
cnc.SetOutput(parser.value_float());
if(!isnan(power))
cnc.SetOutput(power);
else
cnc.TurnOn();
}
Expand All @@ -91,6 +106,15 @@ void GcodeSuite::M3_M4(const bool is_M4) {
* M5 turn off spindle
*/
void GcodeSuite::M5() {
if (laser->IsOnline()) {
if (parser.seen('I')) {
laser->SetOutputInline(0);
return;
}

laser->InlineDisable(); // Disable planner laser control
}

planner.synchronize();
//set_spindle_laser_enabled(false);
if(laser->IsOnline()) {
Expand Down
36 changes: 30 additions & 6 deletions snapmaker/src/module/toolhead_laser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,13 @@ void ToolHeadLaser::SetPower(float power) {

power_val_ = power;

if (power > power_limit_)
power = power_limit_;

integer = (int)power;
decimal = power - integer;

power_pwm_ = (uint16_t)(power_table_[integer] + (power_table_[integer + 1] - power_table_[integer]) * decimal);

if (power_pwm_ > power_limit_pwm_)
power_pwm_ = power_limit_pwm_;
}


Expand All @@ -271,10 +271,12 @@ void ToolHeadLaser::SetPowerLimit(float limit) {
if (limit > TOOLHEAD_LASER_POWER_NORMAL_LIMIT)
limit = TOOLHEAD_LASER_POWER_NORMAL_LIMIT;

power_limit_ = limit;
// Compute limit PWM value
power_limit_pwm_ = 255;
SetPower(limit);
power_limit_pwm_ = power_pwm_;

SetPower(power_val_);
power_val_ = cur_power;
SetPower(cur_power);

// check if we need to change current output
if (state_ == TOOLHEAD_LASER_STATE_ON)
Expand Down Expand Up @@ -1010,3 +1012,25 @@ void ToolHeadLaser::Process() {

TryCloseFan();
}


void ToolHeadLaser::InlineDisable() {
planner.laser_inline.status.isEnabled = false;
}


void ToolHeadLaser::SetOutputInline(float power) {
CheckFan(power);
planner.laser_inline.status.isEnabled = true;

SetPower(power);
planner.laser_inline.power = power_pwm_;
}


void ToolHeadLaser::TurnOn_ISR(uint16_t power_pwm) {
if (power_pwm > power_limit_pwm_)
power_pwm = power_limit_pwm_;

TimSetPwm(power_pwm);
}
Loading