1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003 |
-
-
- #include "Marlin.h"
- #include "planner.h"
- #include "stepper.h"
- #include "temperature.h"
- #include "ultralcd.h"
- #include "language.h"
-
- #ifdef MESH_BED_LEVELING
- #include "mesh_bed_leveling.h"
- #endif
-
-
-
-
-
- millis_t minsegmenttime;
- float max_feedrate[NUM_AXIS];
- float axis_steps_per_unit[NUM_AXIS];
- unsigned long max_acceleration_units_per_sq_second[NUM_AXIS];
- float minimumfeedrate;
- float acceleration;
- float retract_acceleration;
- float travel_acceleration;
- float max_xy_jerk;
- float max_z_jerk;
- float max_e_jerk;
- float mintravelfeedrate;
- unsigned long axis_steps_per_sqr_second[NUM_AXIS];
-
- #ifdef ENABLE_AUTO_BED_LEVELING
-
- matrix_3x3 plan_bed_level_matrix = {
- 1.0, 0.0, 0.0,
- 0.0, 1.0, 0.0,
- 0.0, 0.0, 1.0
- };
- #endif
-
- #ifdef AUTOTEMP
- float autotemp_max = 250;
- float autotemp_min = 210;
- float autotemp_factor = 0.1;
- bool autotemp_enabled = false;
- #endif
-
-
-
-
-
- block_t block_buffer[BLOCK_BUFFER_SIZE];
- volatile unsigned char block_buffer_head;
- volatile unsigned char block_buffer_tail;
-
-
-
-
-
-
- long position[NUM_AXIS];
- static float previous_speed[NUM_AXIS];
- static float previous_nominal_speed;
-
- unsigned char g_uc_extruder_last_move[4] = {0,0,0,0};
-
- #ifdef XY_FREQUENCY_LIMIT
-
- #define MAX_FREQ_TIME (1000000.0/XY_FREQUENCY_LIMIT)
-
- static unsigned char old_direction_bits = 0;
-
- static long axis_segment_time[2][3] = { {MAX_FREQ_TIME+1,0,0}, {MAX_FREQ_TIME+1,0,0} };
- #endif
-
- #ifdef FILAMENT_SENSOR
- static char meas_sample;
- #endif
-
-
-
-
-
-
-
- FORCE_INLINE int8_t next_block_index(int8_t block_index) { return BLOCK_MOD(block_index + 1); }
- FORCE_INLINE int8_t prev_block_index(int8_t block_index) { return BLOCK_MOD(block_index - 1); }
-
-
-
- FORCE_INLINE float estimate_acceleration_distance(float initial_rate, float target_rate, float acceleration) {
- if (acceleration == 0) return 0;
- return (target_rate * target_rate - initial_rate * initial_rate) / (acceleration * 2);
- }
-
-
-
-
-
-
- FORCE_INLINE float intersection_distance(float initial_rate, float final_rate, float acceleration, float distance) {
- if (acceleration == 0) return 0;
- return (acceleration * 2 * distance - initial_rate * initial_rate + final_rate * final_rate) / (acceleration * 4);
- }
-
-
-
- void calculate_trapezoid_for_block(block_t *block, float entry_factor, float exit_factor) {
- unsigned long initial_rate = ceil(block->nominal_rate * entry_factor);
- unsigned long final_rate = ceil(block->nominal_rate * exit_factor);
-
-
- NOLESS(initial_rate, 120);
- NOLESS(final_rate, 120);
-
- long acceleration = block->acceleration_st;
- int32_t accelerate_steps = ceil(estimate_acceleration_distance(initial_rate, block->nominal_rate, acceleration));
- int32_t decelerate_steps = floor(estimate_acceleration_distance(block->nominal_rate, final_rate, -acceleration));
-
-
- int32_t plateau_steps = block->step_event_count - accelerate_steps - decelerate_steps;
-
-
-
-
- if (plateau_steps < 0) {
- accelerate_steps = ceil(intersection_distance(initial_rate, final_rate, acceleration, block->step_event_count));
- accelerate_steps = max(accelerate_steps, 0);
- accelerate_steps = min((uint32_t)accelerate_steps, block->step_event_count);
- plateau_steps = 0;
- }
-
- #ifdef ADVANCE
- volatile long initial_advance = block->advance * entry_factor * entry_factor;
- volatile long final_advance = block->advance * exit_factor * exit_factor;
- #endif
-
-
-
- CRITICAL_SECTION_START;
- if (!block->busy) {
- block->accelerate_until = accelerate_steps;
- block->decelerate_after = accelerate_steps+plateau_steps;
- block->initial_rate = initial_rate;
- block->final_rate = final_rate;
- #ifdef ADVANCE
- block->initial_advance = initial_advance;
- block->final_advance = final_advance;
- #endif
- }
- CRITICAL_SECTION_END;
- }
-
-
-
- FORCE_INLINE float max_allowable_speed(float acceleration, float target_velocity, float distance) {
- return sqrt(target_velocity * target_velocity - 2 * acceleration * distance);
- }
-
-
-
-
-
-
-
-
-
-
-
- void planner_reverse_pass_kernel(block_t *previous, block_t *current, block_t *next) {
- if (!current) return;
-
- if (next) {
-
-
-
- if (current->entry_speed != current->max_entry_speed) {
-
-
-
- if (!current->nominal_length_flag && current->max_entry_speed > next->entry_speed) {
- current->entry_speed = min(current->max_entry_speed,
- max_allowable_speed(-current->acceleration, next->entry_speed, current->millimeters));
- }
- else {
- current->entry_speed = current->max_entry_speed;
- }
- current->recalculate_flag = true;
-
- }
- }
- }
-
-
-
- void planner_reverse_pass() {
- uint8_t block_index = block_buffer_head;
-
-
- CRITICAL_SECTION_START;
- unsigned char tail = block_buffer_tail;
- CRITICAL_SECTION_END
-
- if (BLOCK_MOD(block_buffer_head - tail + BLOCK_BUFFER_SIZE) > 3) {
- block_index = BLOCK_MOD(block_buffer_head - 3);
- block_t *block[3] = { NULL, NULL, NULL };
- while (block_index != tail) {
- block_index = prev_block_index(block_index);
- block[2]= block[1];
- block[1]= block[0];
- block[0] = &block_buffer[block_index];
- planner_reverse_pass_kernel(block[0], block[1], block[2]);
- }
- }
- }
-
-
- void planner_forward_pass_kernel(block_t *previous, block_t *current, block_t *next) {
- if (!previous) return;
-
-
-
-
-
- if (!previous->nominal_length_flag) {
- if (previous->entry_speed < current->entry_speed) {
- double entry_speed = min(current->entry_speed,
- max_allowable_speed(-previous->acceleration, previous->entry_speed, previous->millimeters));
-
-
- if (current->entry_speed != entry_speed) {
- current->entry_speed = entry_speed;
- current->recalculate_flag = true;
- }
- }
- }
- }
-
-
-
- void planner_forward_pass() {
- uint8_t block_index = block_buffer_tail;
- block_t *block[3] = { NULL, NULL, NULL };
-
- while (block_index != block_buffer_head) {
- block[0] = block[1];
- block[1] = block[2];
- block[2] = &block_buffer[block_index];
- planner_forward_pass_kernel(block[0], block[1], block[2]);
- block_index = next_block_index(block_index);
- }
- planner_forward_pass_kernel(block[1], block[2], NULL);
- }
-
-
-
-
- void planner_recalculate_trapezoids() {
- int8_t block_index = block_buffer_tail;
- block_t *current;
- block_t *next = NULL;
-
- while (block_index != block_buffer_head) {
- current = next;
- next = &block_buffer[block_index];
- if (current) {
-
- if (current->recalculate_flag || next->recalculate_flag) {
-
- float nom = current->nominal_speed;
- calculate_trapezoid_for_block(current, current->entry_speed / nom, next->entry_speed / nom);
- current->recalculate_flag = false;
- }
- }
- block_index = next_block_index( block_index );
- }
-
- if (next) {
- float nom = next->nominal_speed;
- calculate_trapezoid_for_block(next, next->entry_speed / nom, MINIMUM_PLANNER_SPEED / nom);
- next->recalculate_flag = false;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void planner_recalculate() {
- planner_reverse_pass();
- planner_forward_pass();
- planner_recalculate_trapezoids();
- }
-
- void plan_init() {
- block_buffer_head = block_buffer_tail = 0;
- memset(position, 0, sizeof(position));
- for (int i=0; i<NUM_AXIS; i++) previous_speed[i] = 0.0;
- previous_nominal_speed = 0.0;
- }
-
-
- #ifdef AUTOTEMP
- void getHighESpeed() {
- static float oldt = 0;
-
- if (!autotemp_enabled) return;
- if (degTargetHotend0() + 2 < autotemp_min) return;
-
- float high = 0.0;
- uint8_t block_index = block_buffer_tail;
-
- while (block_index != block_buffer_head) {
- block_t *block = &block_buffer[block_index];
- if (block->steps[X_AXIS] || block->steps[Y_AXIS] || block->steps[Z_AXIS]) {
- float se = (float)block->steps[E_AXIS] / block->step_event_count * block->nominal_speed;
- if (se > high) high = se;
- }
- block_index = next_block_index(block_index);
- }
-
- float t = autotemp_min + high * autotemp_factor;
- t = constrain(t, autotemp_min, autotemp_max);
- if (oldt > t) {
- t *= (1 - AUTOTEMP_OLDWEIGHT);
- t += AUTOTEMP_OLDWEIGHT * oldt;
- }
- oldt = t;
- setTargetHotend0(t);
- }
- #endif
-
- void check_axes_activity() {
- unsigned char axis_active[NUM_AXIS] = { 0 },
- tail_fan_speed = fanSpeed;
- #ifdef BARICUDA
- unsigned char tail_valve_pressure = ValvePressure,
- tail_e_to_p_pressure = EtoPPressure;
- #endif
-
- block_t *block;
-
- if (blocks_queued()) {
- uint8_t block_index = block_buffer_tail;
- tail_fan_speed = block_buffer[block_index].fan_speed;
- #ifdef BARICUDA
- block = &block_buffer[block_index];
- tail_valve_pressure = block->valve_pressure;
- tail_e_to_p_pressure = block->e_to_p_pressure;
- #endif
- while (block_index != block_buffer_head) {
- block = &block_buffer[block_index];
- for (int i=0; i<NUM_AXIS; i++) if (block->steps[i]) axis_active[i]++;
- block_index = next_block_index(block_index);
- }
- }
- if (DISABLE_X && !axis_active[X_AXIS]) disable_x();
- if (DISABLE_Y && !axis_active[Y_AXIS]) disable_y();
- if (DISABLE_Z && !axis_active[Z_AXIS]) disable_z();
- if (DISABLE_E && !axis_active[E_AXIS]) {
- disable_e0();
- disable_e1();
- disable_e2();
- disable_e3();
- }
-
- #if HAS_FAN
- #ifdef FAN_KICKSTART_TIME
- static millis_t fan_kick_end;
- if (tail_fan_speed) {
- if (fan_kick_end == 0) {
-
- fan_kick_end = millis() + FAN_KICKSTART_TIME;
- tail_fan_speed = 255;
- } else if (fan_kick_end > millis())
-
- tail_fan_speed = 255;
- } else {
- fan_kick_end = 0;
- }
- #endif
- #ifdef FAN_SOFT_PWM
- fanSpeedSoftPwm = tail_fan_speed;
- #else
- analogWrite(FAN_PIN, tail_fan_speed);
- #endif
- #endif
-
- #ifdef AUTOTEMP
- getHighESpeed();
- #endif
-
- #ifdef BARICUDA
- #if HAS_HEATER_1
- analogWrite(HEATER_1_PIN,tail_valve_pressure);
- #endif
- #if HAS_HEATER_2
- analogWrite(HEATER_2_PIN,tail_e_to_p_pressure);
- #endif
- #endif
- }
-
-
- float junction_deviation = 0.1;
-
-
-
- #if defined(ENABLE_AUTO_BED_LEVELING) || defined(MESH_BED_LEVELING)
- void plan_buffer_line(float x, float y, float z, const float &e, float feed_rate, const uint8_t &extruder)
- #else
- void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate, const uint8_t &extruder)
- #endif
- {
-
- int next_buffer_head = next_block_index(block_buffer_head);
-
-
-
- while(block_buffer_tail == next_buffer_head) {
- manage_heater();
- manage_inactivity();
- lcd_update();
- }
-
- #ifdef MESH_BED_LEVELING
- if (mbl.active) z += mbl.get_z(x, y);
- #elif defined(ENABLE_AUTO_BED_LEVELING)
- apply_rotation_xyz(plan_bed_level_matrix, x, y, z);
- #endif
-
-
-
-
- long target[NUM_AXIS];
- target[X_AXIS] = lround(x * axis_steps_per_unit[X_AXIS]);
- target[Y_AXIS] = lround(y * axis_steps_per_unit[Y_AXIS]);
- target[Z_AXIS] = lround(z * axis_steps_per_unit[Z_AXIS]);
- target[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
-
- float dx = target[X_AXIS] - position[X_AXIS],
- dy = target[Y_AXIS] - position[Y_AXIS],
- dz = target[Z_AXIS] - position[Z_AXIS],
- de = target[E_AXIS] - position[E_AXIS];
-
- #ifdef PREVENT_DANGEROUS_EXTRUDE
- if (de) {
- if (degHotend(extruder) < extrude_min_temp) {
- position[E_AXIS] = target[E_AXIS];
- de = 0;
- SERIAL_ECHO_START;
- SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
- }
- #ifdef PREVENT_LENGTHY_EXTRUDE
- if (labs(de) > axis_steps_per_unit[E_AXIS] * EXTRUDE_MAXLENGTH) {
- position[E_AXIS] = target[E_AXIS];
- de = 0;
- SERIAL_ECHO_START;
- SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP);
- }
- #endif
- }
- #endif
-
-
- block_t *block = &block_buffer[block_buffer_head];
-
-
- block->busy = false;
-
-
- #ifdef COREXY
-
-
- block->steps[A_AXIS] = labs(dx + dy);
- block->steps[B_AXIS] = labs(dx - dy);
- #else
-
- block->steps[X_AXIS] = labs(dx);
- block->steps[Y_AXIS] = labs(dy);
- #endif
-
- block->steps[Z_AXIS] = labs(dz);
- block->steps[E_AXIS] = labs(de);
- block->steps[E_AXIS] *= volumetric_multiplier[extruder];
- block->steps[E_AXIS] *= extruder_multiply[extruder];
- block->steps[E_AXIS] /= 100;
- block->step_event_count = max(block->steps[X_AXIS], max(block->steps[Y_AXIS], max(block->steps[Z_AXIS], block->steps[E_AXIS])));
-
-
- if (block->step_event_count <= dropsegments) return;
-
- block->fan_speed = fanSpeed;
- #ifdef BARICUDA
- block->valve_pressure = ValvePressure;
- block->e_to_p_pressure = EtoPPressure;
- #endif
-
-
- uint8_t db = 0;
- #ifdef COREXY
- if (dx < 0) db |= BIT(X_HEAD);
- if (dy < 0) db |= BIT(Y_HEAD);
- if (dx + dy < 0) db |= BIT(A_AXIS);
- if (dx - dy < 0) db |= BIT(B_AXIS);
- #else
- if (dx < 0) db |= BIT(X_AXIS);
- if (dy < 0) db |= BIT(Y_AXIS);
- #endif
- if (dz < 0) db |= BIT(Z_AXIS);
- if (de < 0) db |= BIT(E_AXIS);
- block->direction_bits = db;
-
- block->active_extruder = extruder;
-
-
- #ifdef COREXY
- if (block->steps[A_AXIS] || block->steps[B_AXIS]) {
- enable_x();
- enable_y();
- }
- #else
- if (block->steps[X_AXIS]) enable_x();
- if (block->steps[Y_AXIS]) enable_y();
- #endif
-
- #ifndef Z_LATE_ENABLE
- if (block->steps[Z_AXIS]) enable_z();
- #endif
-
-
- if (block->steps[E_AXIS]) {
- if (DISABLE_INACTIVE_EXTRUDER) {
-
- for (int i=0; i<EXTRUDERS; i++)
- if (g_uc_extruder_last_move[i] > 0) g_uc_extruder_last_move[i]--;
-
- switch(extruder) {
- case 0:
- enable_e0();
- g_uc_extruder_last_move[0] = BLOCK_BUFFER_SIZE * 2;
- #if EXTRUDERS > 1
- if (g_uc_extruder_last_move[1] == 0) disable_e1();
- #if EXTRUDERS > 2
- if (g_uc_extruder_last_move[2] == 0) disable_e2();
- #if EXTRUDERS > 3
- if (g_uc_extruder_last_move[3] == 0) disable_e3();
- #endif
- #endif
- #endif
- break;
- #if EXTRUDERS > 1
- case 1:
- enable_e1();
- g_uc_extruder_last_move[1] = BLOCK_BUFFER_SIZE * 2;
- if (g_uc_extruder_last_move[0] == 0) disable_e0();
- #if EXTRUDERS > 2
- if (g_uc_extruder_last_move[2] == 0) disable_e2();
- #if EXTRUDERS > 3
- if (g_uc_extruder_last_move[3] == 0) disable_e3();
- #endif
- #endif
- break;
- #if EXTRUDERS > 2
- case 2:
- enable_e2();
- g_uc_extruder_last_move[2] = BLOCK_BUFFER_SIZE * 2;
- if (g_uc_extruder_last_move[0] == 0) disable_e0();
- if (g_uc_extruder_last_move[1] == 0) disable_e1();
- #if EXTRUDERS > 3
- if (g_uc_extruder_last_move[3] == 0) disable_e3();
- #endif
- break;
- #if EXTRUDERS > 3
- case 3:
- enable_e3();
- g_uc_extruder_last_move[3] = BLOCK_BUFFER_SIZE * 2;
- if (g_uc_extruder_last_move[0] == 0) disable_e0();
- if (g_uc_extruder_last_move[1] == 0) disable_e1();
- if (g_uc_extruder_last_move[2] == 0) disable_e2();
- break;
- #endif
- #endif
- #endif
- }
- }
- else {
- enable_e0();
- enable_e1();
- enable_e2();
- enable_e3();
- }
- }
-
- if (block->steps[E_AXIS])
- NOLESS(feed_rate, minimumfeedrate);
- else
- NOLESS(feed_rate, mintravelfeedrate);
-
-
-
- #ifdef COREXY
- float delta_mm[6];
- delta_mm[X_HEAD] = dx / axis_steps_per_unit[A_AXIS];
- delta_mm[Y_HEAD] = dy / axis_steps_per_unit[B_AXIS];
- delta_mm[A_AXIS] = (dx + dy) / axis_steps_per_unit[A_AXIS];
- delta_mm[B_AXIS] = (dx - dy) / axis_steps_per_unit[B_AXIS];
- #else
- float delta_mm[4];
- delta_mm[X_AXIS] = dx / axis_steps_per_unit[X_AXIS];
- delta_mm[Y_AXIS] = dy / axis_steps_per_unit[Y_AXIS];
- #endif
- delta_mm[Z_AXIS] = dz / axis_steps_per_unit[Z_AXIS];
- delta_mm[E_AXIS] = (de / axis_steps_per_unit[E_AXIS]) * volumetric_multiplier[extruder] * extruder_multiply[extruder] / 100.0;
-
- if (block->steps[X_AXIS] <= dropsegments && block->steps[Y_AXIS] <= dropsegments && block->steps[Z_AXIS] <= dropsegments) {
- block->millimeters = fabs(delta_mm[E_AXIS]);
- }
- else {
- block->millimeters = sqrt(
- #ifdef COREXY
- square(delta_mm[X_HEAD]) + square(delta_mm[Y_HEAD])
- #else
- square(delta_mm[X_AXIS]) + square(delta_mm[Y_AXIS])
- #endif
- + square(delta_mm[Z_AXIS])
- );
- }
- float inverse_millimeters = 1.0 / block->millimeters;
-
-
- float inverse_second = feed_rate * inverse_millimeters;
-
- int moves_queued = movesplanned();
-
-
- #if defined(OLD_SLOWDOWN) || defined(SLOWDOWN)
- bool mq = moves_queued > 1 && moves_queued < BLOCK_BUFFER_SIZE / 2;
- #ifdef OLD_SLOWDOWN
- if (mq) feed_rate *= 2.0 * moves_queued / BLOCK_BUFFER_SIZE;
- #endif
- #ifdef SLOWDOWN
-
- unsigned long segment_time = lround(1000000.0/inverse_second);
- if (mq) {
- if (segment_time < minsegmenttime) {
-
- inverse_second = 1000000.0 / (segment_time + lround(2 * (minsegmenttime - segment_time) / moves_queued));
- #ifdef XY_FREQUENCY_LIMIT
- segment_time = lround(1000000.0 / inverse_second);
- #endif
- }
- }
- #endif
- #endif
-
- block->nominal_speed = block->millimeters * inverse_second;
- block->nominal_rate = ceil(block->step_event_count * inverse_second);
-
- #ifdef FILAMENT_SENSOR
-
-
- if (extruder == FILAMENT_SENSOR_EXTRUDER_NUM && delay_index2 > -1) {
-
- const int MMD = MAX_MEASUREMENT_DELAY + 1, MMD10 = MMD * 10;
-
- delay_dist += delta_mm[E_AXIS];
- while (delay_dist >= MMD10) delay_dist -= MMD10;
- while (delay_dist < 0) delay_dist += MMD10;
-
- delay_index1 = delay_dist / 10.0;
- delay_index1 = constrain(delay_index1, 0, MAX_MEASUREMENT_DELAY);
-
- if (delay_index1 != delay_index2) {
- meas_sample = widthFil_to_size_ratio() - 100;
- while (delay_index1 != delay_index2) {
-
- if (++delay_index2 >= MMD) delay_index2 -= MMD;
- delay_index2 = constrain(delay_index2, 0, MAX_MEASUREMENT_DELAY);
- measurement_delay[delay_index2] = meas_sample;
- }
- }
- }
- #endif
-
-
- float current_speed[NUM_AXIS];
- float speed_factor = 1.0;
- for (int i = 0; i < NUM_AXIS; i++) {
- current_speed[i] = delta_mm[i] * inverse_second;
- float cs = fabs(current_speed[i]), mf = max_feedrate[i];
- if (cs > mf) speed_factor = min(speed_factor, mf / cs);
- }
-
-
- #ifdef XY_FREQUENCY_LIMIT
- #define MAX_FREQ_TIME (1000000.0 / XY_FREQUENCY_LIMIT)
-
-
- unsigned char direction_change = block->direction_bits ^ old_direction_bits;
- old_direction_bits = block->direction_bits;
- segment_time = lround((float)segment_time / speed_factor);
-
- long xs0 = axis_segment_time[X_AXIS][0],
- xs1 = axis_segment_time[X_AXIS][1],
- xs2 = axis_segment_time[X_AXIS][2],
- ys0 = axis_segment_time[Y_AXIS][0],
- ys1 = axis_segment_time[Y_AXIS][1],
- ys2 = axis_segment_time[Y_AXIS][2];
-
- if ((direction_change & BIT(X_AXIS)) != 0) {
- xs2 = axis_segment_time[X_AXIS][2] = xs1;
- xs1 = axis_segment_time[X_AXIS][1] = xs0;
- xs0 = 0;
- }
- xs0 = axis_segment_time[X_AXIS][0] = xs0 + segment_time;
-
- if ((direction_change & BIT(Y_AXIS)) != 0) {
- ys2 = axis_segment_time[Y_AXIS][2] = axis_segment_time[Y_AXIS][1];
- ys1 = axis_segment_time[Y_AXIS][1] = axis_segment_time[Y_AXIS][0];
- ys0 = 0;
- }
- ys0 = axis_segment_time[Y_AXIS][0] = ys0 + segment_time;
-
- long max_x_segment_time = max(xs0, max(xs1, xs2)),
- max_y_segment_time = max(ys0, max(ys1, ys2)),
- min_xy_segment_time = min(max_x_segment_time, max_y_segment_time);
- if (min_xy_segment_time < MAX_FREQ_TIME) {
- float low_sf = speed_factor * min_xy_segment_time / MAX_FREQ_TIME;
- speed_factor = min(speed_factor, low_sf);
- }
- #endif
-
-
- if (speed_factor < 1.0) {
- for (unsigned char i = 0; i < NUM_AXIS; i++) current_speed[i] *= speed_factor;
- block->nominal_speed *= speed_factor;
- block->nominal_rate *= speed_factor;
- }
-
-
- float steps_per_mm = block->step_event_count / block->millimeters;
- long bsx = block->steps[X_AXIS], bsy = block->steps[Y_AXIS], bsz = block->steps[Z_AXIS], bse = block->steps[E_AXIS];
- if (bsx == 0 && bsy == 0 && bsz == 0) {
- block->acceleration_st = ceil(retract_acceleration * steps_per_mm);
- }
- else if (bse == 0) {
- block->acceleration_st = ceil(travel_acceleration * steps_per_mm);
- }
- else {
- block->acceleration_st = ceil(acceleration * steps_per_mm);
- }
-
- unsigned long acc_st = block->acceleration_st,
- xsteps = axis_steps_per_sqr_second[X_AXIS],
- ysteps = axis_steps_per_sqr_second[Y_AXIS],
- zsteps = axis_steps_per_sqr_second[Z_AXIS],
- esteps = axis_steps_per_sqr_second[E_AXIS];
- if ((float)acc_st * bsx / block->step_event_count > xsteps) acc_st = xsteps;
- if ((float)acc_st * bsy / block->step_event_count > ysteps) acc_st = ysteps;
- if ((float)acc_st * bsz / block->step_event_count > zsteps) acc_st = zsteps;
- if ((float)acc_st * bse / block->step_event_count > esteps) acc_st = esteps;
-
- block->acceleration_st = acc_st;
- block->acceleration = acc_st / steps_per_mm;
- block->acceleration_rate = (long)(acc_st * 16777216.0 / (F_CPU / 8.0));
-
- #if 0
-
- double unit_vec[3];
-
- unit_vec[X_AXIS] = delta_mm[X_AXIS]*inverse_millimeters;
- unit_vec[Y_AXIS] = delta_mm[Y_AXIS]*inverse_millimeters;
- unit_vec[Z_AXIS] = delta_mm[Z_AXIS]*inverse_millimeters;
-
-
-
-
-
-
-
-
-
-
- double vmax_junction = MINIMUM_PLANNER_SPEED;
-
-
- if ((block_buffer_head != block_buffer_tail) && (previous_nominal_speed > 0.0)) {
-
-
- double cos_theta = - previous_unit_vec[X_AXIS] * unit_vec[X_AXIS]
- - previous_unit_vec[Y_AXIS] * unit_vec[Y_AXIS]
- - previous_unit_vec[Z_AXIS] * unit_vec[Z_AXIS] ;
-
-
- if (cos_theta < 0.95) {
- vmax_junction = min(previous_nominal_speed,block->nominal_speed);
-
- if (cos_theta > -0.95) {
-
- double sin_theta_d2 = sqrt(0.5*(1.0-cos_theta));
- vmax_junction = min(vmax_junction,
- sqrt(block->acceleration * junction_deviation * sin_theta_d2/(1.0-sin_theta_d2)) );
- }
- }
- }
- #endif
-
-
- float vmax_junction = max_xy_jerk / 2;
- float vmax_junction_factor = 1.0;
- float mz2 = max_z_jerk / 2, me2 = max_e_jerk / 2;
- float csz = current_speed[Z_AXIS], cse = current_speed[E_AXIS];
- if (fabs(csz) > mz2) vmax_junction = min(vmax_junction, mz2);
- if (fabs(cse) > me2) vmax_junction = min(vmax_junction, me2);
- vmax_junction = min(vmax_junction, block->nominal_speed);
- float safe_speed = vmax_junction;
-
- if ((moves_queued > 1) && (previous_nominal_speed > 0.0001)) {
- float dx = current_speed[X_AXIS] - previous_speed[X_AXIS],
- dy = current_speed[Y_AXIS] - previous_speed[Y_AXIS],
- dz = fabs(csz - previous_speed[Z_AXIS]),
- de = fabs(cse - previous_speed[E_AXIS]),
- jerk = sqrt(dx * dx + dy * dy);
-
-
- vmax_junction = block->nominal_speed;
-
- if (jerk > max_xy_jerk) vmax_junction_factor = max_xy_jerk / jerk;
- if (dz > max_z_jerk) vmax_junction_factor = min(vmax_junction_factor, max_z_jerk / dz);
- if (de > max_e_jerk) vmax_junction_factor = min(vmax_junction_factor, max_e_jerk / de);
-
- vmax_junction = min(previous_nominal_speed, vmax_junction * vmax_junction_factor);
- }
- block->max_entry_speed = vmax_junction;
-
-
- double v_allowable = max_allowable_speed(-block->acceleration, MINIMUM_PLANNER_SPEED, block->millimeters);
- block->entry_speed = min(vmax_junction, v_allowable);
-
-
-
-
-
-
-
-
-
- block->nominal_length_flag = (block->nominal_speed <= v_allowable);
- block->recalculate_flag = true;
-
-
- for (int i = 0; i < NUM_AXIS; i++) previous_speed[i] = current_speed[i];
- previous_nominal_speed = block->nominal_speed;
-
- #ifdef ADVANCE
-
- if (!bse || (!bsx && !bsy && !bsz)) {
- block->advance_rate = 0;
- block->advance = 0;
- }
- else {
- long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_st);
- float advance = (STEPS_PER_CUBIC_MM_E * EXTRUDER_ADVANCE_K) * (cse * cse * EXTRUSION_AREA * EXTRUSION_AREA) * 256;
- block->advance = advance;
- block->advance_rate = acc_dist ? advance / (float)acc_dist : 0;
- }
-
-
- #endif
-
- calculate_trapezoid_for_block(block, block->entry_speed / block->nominal_speed, safe_speed / block->nominal_speed);
-
-
- block_buffer_head = next_buffer_head;
-
-
- for (int i = 0; i < NUM_AXIS; i++) position[i] = target[i];
-
- planner_recalculate();
-
- st_wake_up();
-
- }
-
- #if defined(ENABLE_AUTO_BED_LEVELING) && !defined(DELTA)
- vector_3 plan_get_position() {
- vector_3 position = vector_3(st_get_position_mm(X_AXIS), st_get_position_mm(Y_AXIS), st_get_position_mm(Z_AXIS));
-
-
-
- matrix_3x3 inverse = matrix_3x3::transpose(plan_bed_level_matrix);
-
- position.apply_rotation(inverse);
-
-
- return position;
- }
- #endif
-
- #if defined(ENABLE_AUTO_BED_LEVELING) || defined(MESH_BED_LEVELING)
- void plan_set_position(float x, float y, float z, const float &e)
- #else
- void plan_set_position(const float &x, const float &y, const float &z, const float &e)
- #endif
- {
- #ifdef MESH_BED_LEVELING
- if (mbl.active) z += mbl.get_z(x, y);
- #elif defined(ENABLE_AUTO_BED_LEVELING)
- apply_rotation_xyz(plan_bed_level_matrix, x, y, z);
- #endif
-
- float nx = position[X_AXIS] = lround(x * axis_steps_per_unit[X_AXIS]),
- ny = position[Y_AXIS] = lround(y * axis_steps_per_unit[Y_AXIS]),
- nz = position[Z_AXIS] = lround(z * axis_steps_per_unit[Z_AXIS]),
- ne = position[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
- st_set_position(nx, ny, nz, ne);
- previous_nominal_speed = 0.0;
-
- for (int i=0; i<NUM_AXIS; i++) previous_speed[i] = 0.0;
- }
-
- void plan_set_e_position(const float &e) {
- position[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
- st_set_e_position(position[E_AXIS]);
- }
-
-
- void reset_acceleration_rates() {
- for (int i = 0; i < NUM_AXIS; i++)
- axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
- }
|