Restart scanning

This commit is contained in:
espr14 2020-12-21 19:33:36 +01:00
parent 1610c96fc4
commit c397e9249a
1 changed files with 131 additions and 86 deletions

View File

@ -426,8 +426,41 @@ void go_and_stop(uint8_t axis, int16_t dec, uint16_t &delay_us, uint16_t &steps)
--steps; --steps;
} }
/// Count number of zeros on the 32 byte array
/// If the number is less than 16, it changes @min_z so it will be next time
bool more_zeros(uint8_t* pixels, int16_t &min_z){
uint8_t hist[256];
uint8_t i = 0;
/// clear
do {
hist[i] = 0;
} while (i++ < 255);
/// fill
for (i = 0; i < 32; ++i){
++hist[pixels[i]];
}
/// already more zeros on the line
if (hist[0] >= 16)
return true;
/// find threshold
uint8_t sum = 0;
i = 0;
do {
sum += hist[i];
if (sum >= 16)
break;
} while (i++ < 255);
min_z += i;
return false;
}
void xyzcal_scan_pixels_32x32_Zhop(int16_t cx, int16_t cy, int16_t min_z, int16_t max_z, uint16_t delay_us, uint8_t* pixels){ void xyzcal_scan_pixels_32x32_Zhop(int16_t cx, int16_t cy, int16_t min_z, int16_t max_z, uint16_t delay_us, uint8_t* pixels){
if(!pixels) if (!pixels)
return; return;
int16_t z = _Z; int16_t z = _Z;
int16_t z_trig; int16_t z_trig;
@ -436,109 +469,121 @@ void xyzcal_scan_pixels_32x32_Zhop(int16_t cx, int16_t cy, int16_t min_z, int16_
xyzcal_lineXYZ_to(cx - 1024, cy - 1024, min_z, delay_us, 0); xyzcal_lineXYZ_to(cx - 1024, cy - 1024, min_z, delay_us, 0);
int16_t start_z; int16_t start_z;
uint16_t steps_to_go; uint16_t steps_to_go;
uint8_t restart = 0; ///< restart if needed but just once
int16_t corr_min_z = min_z; ///< shifted min_z if it's too low
for (uint8_t r = 0; r < 32; r++){ ///< Y axis do {
xyzcal_lineXYZ_to(_X, cy - 1024 + r * 64, z, delay_us, 0); if (restart == 1)
for (int8_t d = 0; d < 2; ++d){ ///< direction restart = 2;
xyzcal_lineXYZ_to((d & 1) ? (cx + 1024) : (cx - 1024), _Y, min_z, delay_us, 0); for (uint8_t r = 0; r < 32; r++){ ///< Y axis
xyzcal_lineXYZ_to(_X, cy - 1024 + r * 64, z, delay_us, 0);
for (int8_t d = 0; d < 2; ++d){ ///< direction
xyzcal_lineXYZ_to((d & 1) ? (cx + 1024) : (cx - 1024), _Y, corr_min_z, delay_us, 0);
z = _Z; z = _Z;
sm4_set_dir(X_AXIS, d); sm4_set_dir(X_AXIS, d);
for (uint8_t c = 0; c < 32; c++){ ///< X axis for (uint8_t c = 0; c < 32; c++){ ///< X axis
z_trig = min_z; z_trig = corr_min_z;
/// move up to un-trigger (surpress hysteresis) /// move up to un-trigger (surpress hysteresis)
sm4_set_dir(Z_AXIS, Z_PLUS); sm4_set_dir(Z_AXIS, Z_PLUS);
/// speed up from stop, go half the way /// speed up from stop, go half the way
current_delay_us = MAX_DELAY; current_delay_us = MAX_DELAY;
for (start_z = z; z < (max_z + start_z) / 2; ++z){ for (start_z = z; z < (max_z + start_z) / 2; ++z){
if (!_PINDA){ if (!_PINDA){
break; break;
}
accelerate(Z_AXIS_MASK, Z_ACCEL, current_delay_us, Z_MIN_DELAY);
} }
accelerate(Z_AXIS_MASK, Z_ACCEL, current_delay_us, Z_MIN_DELAY);
}
if(_PINDA){ if(_PINDA){
uint16_t steps_to_go = MAX(0, max_z - z); uint16_t steps_to_go = MAX(0, max_z - z);
while (_PINDA && z < max_z){ while (_PINDA && z < max_z){
go_and_stop(Z_AXIS_MASK, Z_ACCEL, current_delay_us, steps_to_go); go_and_stop(Z_AXIS_MASK, Z_ACCEL, current_delay_us, steps_to_go);
++z;
}
}
/// slow down to stop
while (current_delay_us < MAX_DELAY){
accelerate(Z_AXIS_MASK, -Z_ACCEL, current_delay_us, Z_MIN_DELAY);
++z; ++z;
} }
}
/// slow down to stop
while (current_delay_us < MAX_DELAY){
accelerate(Z_AXIS_MASK, -Z_ACCEL, current_delay_us, Z_MIN_DELAY);
++z;
}
/// move down to trigger /// move down to trigger
sm4_set_dir(Z_AXIS, Z_MINUS); sm4_set_dir(Z_AXIS, Z_MINUS);
/// speed up /// speed up
current_delay_us = MAX_DELAY; current_delay_us = MAX_DELAY;
for (start_z = z; z > (min_z + start_z) / 2; --z){ for (start_z = z; z > (corr_min_z + start_z) / 2; --z){
if (_PINDA){ if (_PINDA){
z_trig = z; z_trig = z;
break; break;
}
accelerate(Z_AXIS_MASK, Z_ACCEL, current_delay_us, Z_MIN_DELAY);
} }
accelerate(Z_AXIS_MASK, Z_ACCEL, current_delay_us, Z_MIN_DELAY); /// slow down
} if(!_PINDA){
/// slow down steps_to_go = MAX(0, z - corr_min_z);
if(!_PINDA){ while (!_PINDA && z > corr_min_z){
steps_to_go = MAX(0, z - min_z); go_and_stop(Z_AXIS_MASK, Z_ACCEL, current_delay_us, steps_to_go);
while (!_PINDA && z > min_z){ --z;
go_and_stop(Z_AXIS_MASK, Z_ACCEL, current_delay_us, steps_to_go); }
z_trig = z;
}
/// slow down to stop
while (z > corr_min_z && current_delay_us < MAX_DELAY){
accelerate(Z_AXIS_MASK, -Z_ACCEL, current_delay_us, Z_MIN_DELAY);
--z; --z;
} }
z_trig = z;
}
/// slow down to stop
while (z > min_z && current_delay_us < MAX_DELAY){
accelerate(Z_AXIS_MASK, -Z_ACCEL, current_delay_us, Z_MIN_DELAY);
--z;
}
count_position[2] = z; count_position[2] = z;
if (d == 0){ if (d == 0){
line_buffer[c] = (uint16_t)(z_trig - min_z); line_buffer[c] = (uint16_t)(z_trig - corr_min_z);
} else { } else {
/// data reversed in X /// data reversed in X
// DBG(_n("%04x"), (line_buffer[31 - c] + (z - min_z)) / 2); // DBG(_n("%04x"), (line_buffer[31 - c] + (z - corr_min_z)) / 2);
/// save average of both directions /// save average of both directions
pixels[(uint16_t)r * 32 + (31 - c)] = (uint8_t)MIN((uint32_t)255, ((uint32_t)line_buffer[31 - c] + (z_trig - min_z)) / 2); pixels[(uint16_t)r * 32 + (31 - c)] = (uint8_t)MIN((uint32_t)255, ((uint32_t)line_buffer[31 - c] + (z_trig - corr_min_z)) / 2);
} }
/// move to the next point and move Z up diagonally (if needed) /// move to the next point and move Z up diagonally (if needed)
current_delay_us = MAX_DELAY; current_delay_us = MAX_DELAY;
// const int8_t dir = (d & 1) ? -1 : 1; // const int8_t dir = (d & 1) ? -1 : 1;
const int16_t end_x = ((d & 1) ? 1 : -1) * (64 * (16 - c) - 32) + cx; const int16_t end_x = ((d & 1) ? 1 : -1) * (64 * (16 - c) - 32) + cx;
const int16_t length_x = ABS(end_x - _X); const int16_t length_x = ABS(end_x - _X);
const int16_t half_x = length_x / 2; const int16_t half_x = length_x / 2;
int16_t x = 0; int16_t x = 0;
/// don't go up if PINDA not triggered /// don't go up if PINDA not triggered
const bool up = _PINDA; const bool up = _PINDA;
int8_t axis = up ? X_AXIS_MASK | Z_AXIS_MASK : X_AXIS_MASK; int8_t axis = up ? X_AXIS_MASK | Z_AXIS_MASK : X_AXIS_MASK;
sm4_set_dir(Z_AXIS, Z_PLUS); sm4_set_dir(Z_AXIS, Z_PLUS);
/// speed up /// speed up
for (x = 0; x <= half_x; ++x){ for (x = 0; x <= half_x; ++x){
accelerate(axis, Z_ACCEL, current_delay_us, Z_MIN_DELAY); accelerate(axis, Z_ACCEL, current_delay_us, Z_MIN_DELAY);
if (up) if (up)
++z; ++z;
}
/// slow down
steps_to_go = length_x - x;
for (; x < length_x; ++x){
go_and_stop(axis, Z_ACCEL, current_delay_us, steps_to_go);
if (up)
++z;
}
count_position[0] = end_x;
count_position[2] = z;
} }
/// slow down
steps_to_go = length_x - x;
for (; x < length_x; ++x){
go_and_stop(axis, Z_ACCEL, current_delay_us, steps_to_go);
if (up)
++z;
}
count_position[0] = end_x;
count_position[2] = z;
} }
if (r == 0 && restart == 0){
if (!more_zeros(pixels, corr_min_z))
restart = 1;
}
if (restart == 1)
break;
// DBG(_n("\n\n"));
} }
// DBG(_n("\n\n")); } while (restart == 1);
}
} }
/// Returns rate of match /// Returns rate of match