Distance checking and digital output configuration

Hi,
I use an ur10e robot to follow a curvilinear trajectory with a variable radius of curvature. The robot is equipped with a tool which must scan along the curvilinear profile. We would like to scan every 2 mm so the waypoints are located 2 mm from each other. I need to notify the scanner at each waypoint so it can scan. By sending at least one pulse on two different channels (in fact, the scanner receives a signal similar to that of an encoder). These pulses must be sent without the robot stopping at the waypoint which is why I use a thread that monitors the distance and sends the pulses. Without a smoothing radius, the robot slows down at all points and vibration results. This is why I added a smoothing radius because we want to limit vibrations as much as possible. At the end the scanner gives the total distance traveled based on the total number of pulses sent by the robot. The problem is that on the same route repeated several times, the robot does not always send the same number of pulses. The number of pulses differs approximately 3 times out of 10. This results in differences in the total distance measured by the scanner and we would not like that at all. I don’t know if this is linked to the fact that the ur10e robot has a repeatability defect of around 0.05mm. Here is the code I am using below. Does anyone know why I have this inconsistency in the total number of impulses on the same route?
Thank you !

def my_program():
    DISTANCE_THRESHOLD = 0.002 
    checking = True
    
    
    set_standard_digital_out(0, False)
    set_standard_digital_out(1, False)
    set_standard_digital_out(2, False)
    set_standard_digital_out(3, False)
    
    thread check_distance():
        pose1 = get_actual_tcp_pose()
        while checking:
            pose2 = get_actual_tcp_pose()
            if point_dist(pose1, pose2) >= DISTANCE_THRESHOLD :
                set_standard_digital_out(1, True)
                sleep(0.002)
                set_standard_digital_out(0, True)
                sleep(0.002)
                set_standard_digital_out(1, False)
                sleep(0.002)
                set_standard_digital_out(0, False)
                sleep(0.002)
                pose1 = pose2
                sync()
            end
            sync()
        end
    end
    sync()
    movel((p[-0.0148, 0.6243, -0.2148, -2.9926, 0.0008, 0.0113]), a=1, v=0.1, t=0, r=0)
    sync()
    thrd = run check_distance()
    sync()
    movel((p[-0.0148, 0.6264, -0.2145, -2.9978, 0.0008, 0.0113]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6285, -0.2142, -3.0028, 0.0008, 0.0113]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6306, -0.2139, -3.0079, 0.0008, 0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6327, -0.2136, -3.0129, 0.0007, 0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6348, -0.2134, -3.0179, 0.0007, 0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6369, -0.2131, -3.0228, 0.0007, 0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.639, -0.2129, -3.0278, 0.0007, 0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.641, -0.2126, -3.0326, 0.0006, 0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6431, -0.2124, -3.0375, 0.0006, 0.0115]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6452, -0.2122, -3.0423, 0.0006, 0.0115]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6473, -0.212, -3.047, 0.0005, 0.0115]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6494, -0.2118, -3.0518, 0.0005, 0.0115]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6515, -0.2116, -3.0565, 0.0005, 0.0115]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6536, -0.2114, -3.0612, 0.0005, 0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6557, -0.2113, -3.0659, 0.0004, 0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6578, -0.2111, -3.0705, 0.0004, 0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6599, -0.211, -3.0751, 0.0004, 0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.662, -0.2108, -3.0796, 0.0004, 0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6641, -0.2107, -3.0841, 0.0003, 0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6662, -0.2106, -3.0886, 0.0003, 0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6683, -0.2105, -3.093, 0.0003, 0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6704, -0.2104, -3.0974, 0.0003, 0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6725, -0.2103, -3.1018, 0.0002, 0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6746, -0.2102, -3.1061, 0.0002, 0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6767, -0.2102, -3.1104, 0.0002, 0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6788, -0.2101, -3.1147, 0.0002, 0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6809, -0.21, -3.119, 0.0001, 0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.683, -0.21, -3.1232, 0.0001, 0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6851, -0.21, -3.1274, 0.0001, 0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6872, -0.2099, -3.1316, 0.0001, 0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6893, -0.2099, -3.1357, 0.0, 0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6914, -0.2099, 3.14, 0.0027, -0.0121]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6935, -0.2099, 3.1391, 0.0, -0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6956, -0.2099, 3.135, 0.0, -0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6976, -0.2099, 3.1309, 0.0001, -0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.6997, -0.21, 3.1268, 0.0001, -0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7018, -0.21, 3.1227, 0.0001, -0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7039, -0.21, 3.1186, 0.0001, -0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.706, -0.2101, 3.1145, 0.0002, -0.0118]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7081, -0.2102, 3.1104, 0.0002, -0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7102, -0.2102, 3.1063, 0.0002, -0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7123, -0.2103, 3.1022, 0.0002, -0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7144, -0.2104, 3.0981, 0.0003, -0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7165, -0.2105, 3.094, 0.0003, -0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7185, -0.2106, 3.0898, 0.0003, -0.0117]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7206, -0.2107, 3.0856, 0.0003, -0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7227, -0.2108, 3.0814, 0.0003, -0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7248, -0.211, 3.0772, 0.0004, -0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7269, -0.2111, 3.0729, 0.0004, -0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.729, -0.2112, 3.0686, 0.0004, -0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7311, -0.2114, 3.0642, 0.0004, -0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7332, -0.2116, 3.0598, 0.0005, -0.0116]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7353, -0.2117, 3.0554, 0.0005, -0.0115]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7374, -0.2119, 3.0509, 0.0005, -0.0115]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7395, -0.2121, 3.0463, 0.0005, -0.0115]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7416, -0.2123, 3.0417, 0.0006, -0.0115]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7437, -0.2126, 3.037, 0.0006, -0.0115]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7458, -0.2128, 3.0323, 0.0006, -0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7479, -0.213, 3.0274, 0.0007, -0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.75, -0.2133, 3.0225, 0.0007, -0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7521, -0.2135, 3.0175, 0.0007, -0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7543, -0.2138, 3.0124, 0.0007, -0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7564, -0.2141, 3.0072, 0.0008, -0.0114]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7585, -0.2144, 3.0019, 0.0008, -0.0113]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7606, -0.2147, 2.9965, 0.0008, -0.0113]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7627, -0.215, 2.991, 0.0009, -0.0113]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7649, -0.2153, 2.9854, 0.0009, -0.0113]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.767, -0.2156, 2.9797, 0.0009, -0.0112]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7691, -0.216, 2.9738, 0.0009, -0.0112]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7713, -0.2164, 2.9679, 0.001, -0.0112]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7734, -0.2168, 2.9617, 0.001, -0.0112]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7755, -0.2171, 2.9555, 0.001, -0.0112]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7776, -0.2176, 2.9492, 0.0011, -0.0111]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7798, -0.218, 2.9427, 0.0011, -0.0111]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7819, -0.2184, 2.9361, 0.0011, -0.0111]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.784, -0.2189, 2.9293, 0.0012, -0.0111]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7862, -0.2193, 2.9223, 0.0012, -0.011]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7883, -0.2198, 2.9153, 0.0013, -0.011]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7904, -0.2203, 2.9081, 0.0013, -0.011]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7926, -0.2208, 2.9007, 0.0013, -0.0109]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7947, -0.2214, 2.8931, 0.0014, -0.0109]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.7969, -0.2219, 2.8854, 0.0014, -0.0109]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.799, -0.2225, 2.8776, 0.0014, -0.0109]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8011, -0.2231, 2.8696, 0.0015, -0.0108]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8032, -0.2237, 2.8614, 0.0015, -0.0108]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8054, -0.2243, 2.853, 0.0016, -0.0108]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8075, -0.2249, 2.8445, 0.0016, -0.0107]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8096, -0.2256, 2.8358, 0.0016, -0.0107]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8118, -0.2263, 2.827, 0.0017, -0.0107]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8139, -0.227, 2.818, 0.0017, -0.0106]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.816, -0.2277, 2.8089, 0.0018, -0.0106]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8181, -0.2284, 2.7995, 0.0018, -0.0106]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8202, -0.2292, 2.7901, 0.0019, -0.0105]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8223, -0.23, 2.7804, 0.0019, -0.0105]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8244, -0.2308, 2.7706, 0.002, -0.0105]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8265, -0.2316, 2.7607, 0.002, -0.0104]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8286, -0.2325, 2.7507, 0.0021, -0.0104]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8307, -0.2333, 2.7404, 0.0021, -0.0103]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8328, -0.2342, 2.7301, 0.0022, -0.0103]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8349, -0.2352, 2.7197, 0.0022, -0.0103]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.837, -0.2361, 2.7091, 0.0022, -0.0102]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.839, -0.2371, 2.6984, 0.0023, -0.0102]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8411, -0.238, 2.6877, 0.0023, -0.0101]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8431, -0.2391, 2.6768, 0.0024, -0.0101]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8451, -0.2401, 2.6659, 0.0024, -0.0101]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8472, -0.2411, 2.6548, 0.0025, -0.01]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8492, -0.2422, 2.6437, 0.0025, -0.01]), a=1, v=0.1, t=0, r=0.0099)
    movel((p[-0.0148, 0.8512, -0.2433, 2.6325, 0.0026, -0.0099]), a=1, v=0.1, t=0, r=0)
    sync()
    kill thrd
    set_standard_digital_out(0, False)
    set_standard_digital_out(1, False)
    set_standard_digital_out(2, False)
    set_standard_digital_out(3, False)
    sync()
end
my_program()