123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083 |
-
-
-
-
- #include "Marlin.h"
- #include "planner.h"
- #include "stepper.h"
- #include "temperature.h"
- #include "ultralcd.h"
- #include "language.h"
-
- #if ENABLED(MESH_BED_LEVELING)
- #include "mesh_bed_leveling.h"
- #endif
-
- Planner planner;
-
- Planner::Planner() {
- #if ENABLED(AUTO_BED_LEVELING_FEATURE)
- bed_level_matrix.set_to_identity();
- #endif
- init();
- }
-
- void Planner::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;
- }
-
-
- void Planner::calculate_trapezoid_for_block(block_t* block, float entry_factor, float exit_factor) {
- unsigned long initial_rate = ceil(block->nominal_rate * entry_factor),
- 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;
- }
-
- #if ENABLED(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;
- #if ENABLED(ADVANCE)
- block->initial_advance = initial_advance;
- block->final_advance = final_advance;
- #endif
- }
- CRITICAL_SECTION_END;
- }
-
-
-
-
-
-
-
-
-
-
-
- void Planner::reverse_pass_kernel(block_t* previous, block_t* current, block_t* next) {
- if (!current) return;
- UNUSED(previous);
-
- if (next) {
-
-
-
- float max_entry_speed = current->max_entry_speed;
- if (current->entry_speed != max_entry_speed) {
-
-
-
- if (!current->nominal_length_flag && max_entry_speed > next->entry_speed) {
- current->entry_speed = min(max_entry_speed,
- max_allowable_speed(-current->acceleration, next->entry_speed, current->millimeters));
- }
- else {
- current->entry_speed = max_entry_speed;
- }
- current->recalculate_flag = true;
-
- }
- }
- }
-
-
- void Planner::reverse_pass() {
-
- if (movesplanned() > 3) {
-
- block_t* block[3] = { NULL, NULL, NULL };
-
-
- CRITICAL_SECTION_START;
- uint8_t tail = block_buffer_tail;
- CRITICAL_SECTION_END
-
- uint8_t b = BLOCK_MOD(block_buffer_head - 3);
- while (b != tail) {
- b = prev_block_index(b);
- block[2] = block[1];
- block[1] = block[0];
- block[0] = &block_buffer[b];
- 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;
- UNUSED(next);
-
-
-
-
-
- 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() {
- block_t* block[3] = { NULL, NULL, NULL };
-
- for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
- block[0] = block[1];
- block[1] = block[2];
- block[2] = &block_buffer[b];
- forward_pass_kernel(block[0], block[1], block[2]);
- }
- 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() {
- reverse_pass();
- forward_pass();
- recalculate_trapezoids();
- }
-
-
- #if ENABLED(AUTOTEMP)
-
- void Planner::getHighESpeed() {
- static float oldt = 0;
-
- if (!autotemp_enabled) return;
- if (thermalManager.degTargetHotend(0) + 2 < autotemp_min) return;
-
- float high = 0.0;
- for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
- block_t* block = &block_buffer[b];
- 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;
- NOLESS(high, se);
- }
- }
-
- 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;
- thermalManager.setTargetHotend(t, 0);
- }
-
- #endif
-
-
- void Planner::check_axes_activity() {
- unsigned char axis_active[NUM_AXIS] = { 0 },
- tail_fan_speed[FAN_COUNT];
-
- #if FAN_COUNT > 0
- for (uint8_t i = 0; i < FAN_COUNT; i++) tail_fan_speed[i] = fanSpeeds[i];
- #endif
-
- #if ENABLED(BARICUDA)
- unsigned char tail_valve_pressure = baricuda_valve_pressure,
- tail_e_to_p_pressure = baricuda_e_to_p_pressure;
- #endif
-
- if (blocks_queued()) {
-
- #if FAN_COUNT > 0
- for (uint8_t i = 0; i < FAN_COUNT; i++) tail_fan_speed[i] = block_buffer[block_buffer_tail].fan_speed[i];
- #endif
-
- block_t* block;
-
- #if ENABLED(BARICUDA)
- block = &block_buffer[block_buffer_tail];
- tail_valve_pressure = block->valve_pressure;
- tail_e_to_p_pressure = block->e_to_p_pressure;
- #endif
-
- for (uint8_t b = block_buffer_tail; b != block_buffer_head; b = next_block_index(b)) {
- block = &block_buffer[b];
- for (int i = 0; i < NUM_AXIS; i++) if (block->steps[i]) axis_active[i]++;
- }
- }
- #if ENABLED(DISABLE_X)
- if (!axis_active[X_AXIS]) disable_x();
- #endif
- #if ENABLED(DISABLE_Y)
- if (!axis_active[Y_AXIS]) disable_y();
- #endif
- #if ENABLED(DISABLE_Z)
- if (!axis_active[Z_AXIS]) disable_z();
- #endif
- #if ENABLED(DISABLE_E)
- if (!axis_active[E_AXIS]) {
- disable_e0();
- disable_e1();
- disable_e2();
- disable_e3();
- }
- #endif
-
- #if FAN_COUNT > 0
-
- #if defined(FAN_MIN_PWM)
- #define CALC_FAN_SPEED(f) (tail_fan_speed[f] ? ( FAN_MIN_PWM + (tail_fan_speed[f] * (255 - FAN_MIN_PWM)) / 255 ) : 0)
- #else
- #define CALC_FAN_SPEED(f) tail_fan_speed[f]
- #endif
-
- #ifdef FAN_KICKSTART_TIME
-
- static millis_t fan_kick_end[FAN_COUNT] = { 0 };
-
- #define KICKSTART_FAN(f) \
- if (tail_fan_speed[f]) { \
- millis_t ms = millis(); \
- if (fan_kick_end[f] == 0) { \
- fan_kick_end[f] = ms + FAN_KICKSTART_TIME; \
- tail_fan_speed[f] = 255; \
- } else { \
- if (PENDING(ms, fan_kick_end[f])) { \
- tail_fan_speed[f] = 255; \
- } \
- } \
- } else { \
- fan_kick_end[f] = 0; \
- }
-
- #if HAS_FAN0
- KICKSTART_FAN(0);
- #endif
- #if HAS_FAN1
- KICKSTART_FAN(1);
- #endif
- #if HAS_FAN2
- KICKSTART_FAN(2);
- #endif
-
- #endif
-
- #if ENABLED(FAN_SOFT_PWM)
- #if HAS_FAN0
- thermalManager.fanSpeedSoftPwm[0] = CALC_FAN_SPEED(0);
- #endif
- #if HAS_FAN1
- thermalManager.fanSpeedSoftPwm[1] = CALC_FAN_SPEED(1);
- #endif
- #if HAS_FAN2
- thermalManager.fanSpeedSoftPwm[2] = CALC_FAN_SPEED(2);
- #endif
- #else
- #if HAS_FAN0
- analogWrite(FAN_PIN, CALC_FAN_SPEED(0));
- #endif
- #if HAS_FAN1
- analogWrite(FAN1_PIN, CALC_FAN_SPEED(1));
- #endif
- #if HAS_FAN2
- analogWrite(FAN2_PIN, CALC_FAN_SPEED(2));
- #endif
- #endif
-
- #endif
-
- #if ENABLED(AUTOTEMP)
- getHighESpeed();
- #endif
-
- #if ENABLED(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
- }
-
-
-
- #if ENABLED(AUTO_BED_LEVELING_FEATURE) || ENABLED(MESH_BED_LEVELING)
- void Planner::buffer_line(float x, float y, float z, const float& e, float feed_rate, const uint8_t extruder)
- #else
- void Planner::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) idle();
-
- #if ENABLED(MESH_BED_LEVELING)
- if (mbl.active) z += mbl.get_z(x - home_offset[X_AXIS], y - home_offset[Y_AXIS]);
- #elif ENABLED(AUTO_BED_LEVELING_FEATURE)
- apply_rotation_xyz(bed_level_matrix, x, y, z);
- #endif
-
-
-
-
- long target[NUM_AXIS] = {
- lround(x * axis_steps_per_unit[X_AXIS]),
- lround(y * axis_steps_per_unit[Y_AXIS]),
- lround(z * axis_steps_per_unit[Z_AXIS]),
- lround(e * axis_steps_per_unit[E_AXIS])
- };
-
- long dx = target[X_AXIS] - position[X_AXIS],
- dy = target[Y_AXIS] - position[Y_AXIS],
- dz = target[Z_AXIS] - position[Z_AXIS];
-
-
- if (DEBUGGING(DRYRUN))
- position[E_AXIS] = target[E_AXIS];
-
- long de = target[E_AXIS] - position[E_AXIS];
-
- #if ENABLED(PREVENT_DANGEROUS_EXTRUDE)
- if (de) {
- if (thermalManager.tooColdToExtrude(extruder)) {
- position[E_AXIS] = target[E_AXIS];
- de = 0;
- SERIAL_ECHO_START;
- SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
- }
- #if ENABLED(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;
-
-
- #if ENABLED(COREXY)
-
-
- block->steps[A_AXIS] = labs(dx + dy);
- block->steps[B_AXIS] = labs(dx - dy);
- block->steps[Z_AXIS] = labs(dz);
- #elif ENABLED(COREXZ)
-
- block->steps[A_AXIS] = labs(dx + dz);
- block->steps[Y_AXIS] = labs(dy);
- block->steps[C_AXIS] = labs(dx - dz);
- #else
-
- block->steps[X_AXIS] = labs(dx);
- block->steps[Y_AXIS] = labs(dy);
- block->steps[Z_AXIS] = labs(dz);
- #endif
-
- block->steps[E_AXIS] = labs(de);
- block->steps[E_AXIS] *= volumetric_multiplier[extruder];
- block->steps[E_AXIS] *= extruder_multiplier[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;
-
- #if FAN_COUNT > 0
- for (uint8_t i = 0; i < FAN_COUNT; i++) block->fan_speed[i] = fanSpeeds[i];
- #endif
-
- #if ENABLED(BARICUDA)
- block->valve_pressure = baricuda_valve_pressure;
- block->e_to_p_pressure = baricuda_e_to_p_pressure;
- #endif
-
-
- uint8_t db = 0;
- #if ENABLED(COREXY)
- if (dx < 0) SBI(db, X_HEAD);
- if (dy < 0) SBI(db, Y_HEAD);
- if (dz < 0) SBI(db, Z_AXIS);
- if (dx + dy < 0) SBI(db, A_AXIS);
- if (dx - dy < 0) SBI(db, B_AXIS);
- #elif ENABLED(COREXZ)
- if (dx < 0) SBI(db, X_HEAD);
- if (dy < 0) SBI(db, Y_AXIS);
- if (dz < 0) SBI(db, Z_HEAD);
- if (dx + dz < 0) SBI(db, A_AXIS);
- if (dx - dz < 0) SBI(db, C_AXIS);
- #else
- if (dx < 0) SBI(db, X_AXIS);
- if (dy < 0) SBI(db, Y_AXIS);
- if (dz < 0) SBI(db, Z_AXIS);
- #endif
- if (de < 0) SBI(db, E_AXIS);
- block->direction_bits = db;
-
- block->active_extruder = extruder;
-
-
- #if ENABLED(COREXY)
- if (block->steps[A_AXIS] || block->steps[B_AXIS]) {
- enable_x();
- enable_y();
- }
- #if DISABLED(Z_LATE_ENABLE)
- if (block->steps[Z_AXIS]) enable_z();
- #endif
- #elif ENABLED(COREXZ)
- if (block->steps[A_AXIS] || block->steps[C_AXIS]) {
- enable_x();
- enable_z();
- }
- if (block->steps[Y_AXIS]) enable_y();
- #else
- if (block->steps[X_AXIS]) enable_x();
- if (block->steps[Y_AXIS]) enable_y();
- #if DISABLED(Z_LATE_ENABLE)
- if (block->steps[Z_AXIS]) enable_z();
- #endif
- #endif
-
-
- if (block->steps[E_AXIS]) {
-
- #if ENABLED(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();
- #if ENABLED(DUAL_X_CARRIAGE)
- if (extruder_duplication_enabled) {
- enable_e1();
- g_uc_extruder_last_move[1] = (BLOCK_BUFFER_SIZE) * 2;
- }
- #endif
- 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();
- #endif
- }
-
- if (block->steps[E_AXIS])
- NOLESS(feed_rate, min_feedrate);
- else
- NOLESS(feed_rate, min_travel_feedrate);
-
-
-
- #if ENABLED(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[Z_AXIS] = dz / axis_steps_per_unit[Z_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];
- #elif ENABLED(COREXZ)
- float delta_mm[6];
- delta_mm[X_HEAD] = dx / axis_steps_per_unit[A_AXIS];
- delta_mm[Y_AXIS] = dy / axis_steps_per_unit[Y_AXIS];
- delta_mm[Z_HEAD] = dz / axis_steps_per_unit[C_AXIS];
- delta_mm[A_AXIS] = (dx + dz) / axis_steps_per_unit[A_AXIS];
- delta_mm[C_AXIS] = (dx - dz) / axis_steps_per_unit[C_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];
- delta_mm[Z_AXIS] = dz / axis_steps_per_unit[Z_AXIS];
- #endif
- delta_mm[E_AXIS] = (de / axis_steps_per_unit[E_AXIS]) * volumetric_multiplier[extruder] * extruder_multiplier[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(
- #if ENABLED(COREXY)
- square(delta_mm[X_HEAD]) + square(delta_mm[Y_HEAD]) + square(delta_mm[Z_AXIS])
- #elif ENABLED(COREXZ)
- square(delta_mm[X_HEAD]) + square(delta_mm[Y_AXIS]) + square(delta_mm[Z_HEAD])
- #else
- square(delta_mm[X_AXIS]) + square(delta_mm[Y_AXIS]) + square(delta_mm[Z_AXIS])
- #endif
- );
- }
- float inverse_millimeters = 1.0 / block->millimeters;
-
-
- float inverse_second = feed_rate * inverse_millimeters;
-
- int moves_queued = movesplanned();
-
-
- #if ENABLED(OLD_SLOWDOWN) || ENABLED(SLOWDOWN)
- bool mq = moves_queued > 1 && moves_queued < (BLOCK_BUFFER_SIZE) / 2;
- #if ENABLED(OLD_SLOWDOWN)
- if (mq) feed_rate *= 2.0 * moves_queued / (BLOCK_BUFFER_SIZE);
- #endif
- #if ENABLED(SLOWDOWN)
-
- unsigned long segment_time = lround(1000000.0/inverse_second);
- if (mq) {
- if (segment_time < min_segment_time) {
-
- inverse_second = 1000000.0 / (segment_time + lround(2 * (min_segment_time - 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);
-
- #if ENABLED(FILAMENT_WIDTH_SENSOR)
- static float filwidth_e_count = 0, filwidth_delay_dist = 0;
-
-
- if (extruder == FILAMENT_SENSOR_EXTRUDER_NUM && filwidth_delay_index2 >= 0) {
-
- const int MMD_CM = MAX_MEASUREMENT_DELAY + 1, MMD_MM = MMD_CM * 10;
-
-
- filwidth_e_count += delta_mm[E_AXIS];
- filwidth_delay_dist += delta_mm[E_AXIS];
-
-
- if (filwidth_e_count > 0.0001) {
-
-
- while (filwidth_delay_dist >= MMD_MM) filwidth_delay_dist -= MMD_MM;
-
-
- filwidth_delay_index1 = (int)(filwidth_delay_dist / 10.0 + 0.0001);
-
-
- if (filwidth_delay_index1 != filwidth_delay_index2) {
- filwidth_e_count = 0;
- int8_t meas_sample = thermalManager.widthFil_to_size_ratio() - 100;
- do {
- filwidth_delay_index2 = (filwidth_delay_index2 + 1) % MMD_CM;
- measurement_delay[filwidth_delay_index2] = meas_sample;
- } while (filwidth_delay_index1 != filwidth_delay_index2);
- }
- }
- }
- #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
-
-
- 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 (TEST(direction_change, X_AXIS)) {
- 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 (TEST(direction_change, Y_AXIS)) {
- 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],
- allsteps = block->step_event_count;
- if (xsteps < (acc_st * bsx) / allsteps) acc_st = (xsteps * allsteps) / bsx;
- if (ysteps < (acc_st * bsy) / allsteps) acc_st = (ysteps * allsteps) / bsy;
- if (zsteps < (acc_st * bsz) / allsteps) acc_st = (zsteps * allsteps) / bsz;
- if (esteps < (acc_st * bse) / allsteps) acc_st = (esteps * allsteps) / bse;
-
- 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
-
- float junction_deviation = 0.1;
-
-
- 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 dsx = current_speed[X_AXIS] - previous_speed[X_AXIS],
- dsy = current_speed[Y_AXIS] - previous_speed[Y_AXIS],
- dsz = fabs(csz - previous_speed[Z_AXIS]),
- dse = fabs(cse - previous_speed[E_AXIS]),
- jerk = sqrt(dsx * dsx + dsy * dsy);
-
-
- vmax_junction = block->nominal_speed;
-
- if (jerk > max_xy_jerk) vmax_junction_factor = max_xy_jerk / jerk;
- if (dsz > max_z_jerk) vmax_junction_factor = min(vmax_junction_factor, max_z_jerk / dsz);
- if (dse > max_e_jerk) vmax_junction_factor = min(vmax_junction_factor, max_e_jerk / dse);
-
- 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;
-
- #if ENABLED(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];
-
- recalculate();
-
- stepper.wake_up();
-
- }
-
- #if ENABLED(AUTO_BED_LEVELING_FEATURE) && DISABLED(DELTA)
-
-
-
- vector_3 Planner::adjusted_position() {
- vector_3 pos = vector_3(stepper.get_axis_position_mm(X_AXIS), stepper.get_axis_position_mm(Y_AXIS), stepper.get_axis_position_mm(Z_AXIS));
-
-
-
-
- matrix_3x3 inverse = matrix_3x3::transpose(bed_level_matrix);
-
-
- pos.apply_rotation(inverse);
-
-
- return pos;
- }
-
- #endif
-
-
- #if ENABLED(AUTO_BED_LEVELING_FEATURE) || ENABLED(MESH_BED_LEVELING)
- void Planner::set_position(float x, float y, float z, const float& e)
- #else
- void Planner::set_position(const float& x, const float& y, const float& z, const float& e)
- #endif
- {
- #if ENABLED(MESH_BED_LEVELING)
- if (mbl.active) z += mbl.get_z(x - home_offset[X_AXIS], y - home_offset[Y_AXIS]);
- #elif ENABLED(AUTO_BED_LEVELING_FEATURE)
- apply_rotation_xyz(bed_level_matrix, x, y, z);
- #endif
-
- long 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]);
- stepper.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 Planner::set_e_position(const float& e) {
- position[E_AXIS] = lround(e * axis_steps_per_unit[E_AXIS]);
- stepper.set_e_position(position[E_AXIS]);
- }
-
-
- void Planner::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];
- }
-
- #if ENABLED(AUTOTEMP)
-
- void Planner::autotemp_M109() {
- autotemp_enabled = code_seen('F');
- if (autotemp_enabled) autotemp_factor = code_value();
- if (code_seen('S')) autotemp_min = code_value();
- if (code_seen('B')) autotemp_max = code_value();
- }
-
- #endif
|