<commit_before>from numpy import array

from opentrons.trackers.pose_tracker import (
    update, Point, change_base
)
from opentrons.robot import robot_configs
from opentrons.data_storage import database
from opentrons.trackers.pose_tracker import absolute


X_SWITCH_OFFSET_MM = 5.0
Y_SWITCH_OFFSET_MM = 2.0
Z_SWITCH_OFFSET_MM = 5.0

Z_PROBE_CLEARANCE_MM = 5.0

BOUNCE_DISTANCE_MM = 5.0
XY_CLEARANCE = 7.5
Z_CROSSOVER_CLEARANCE = 80  # How much of z height to be added to nozzle to clear the top


def calibrate_container_with_delta(
        pose_tree, container, delta_x,
        delta_y, delta_z, save, new_container_name=None
):
    delta = Point(delta_x, delta_y, delta_z)
    new_coordinates = change_base(
        pose_tree,
        src=container,
        dst=container.parent) + delta
    pose_tree = update(pose_tree, container, new_coordinates)
    container._coordinates = container._coordinates + delta
    if save and new_container_name:
        database.save_new_container(container, new_container_name)
    elif save:
        database.overwrite_container(container)
    return pose_tree


def probe_instrument(instrument, robot) -> Point:
    from statistics import mean

    size_x, size_y, size_z = robot.config.probe_dimensions
    center = Point(*robot.config.probe_center)

    rel_x_start = (size_x / 2) + XY_CLEARANCE
    rel_y_start = (size_y / 2) + XY_CLEARANCE
    rel_z_start = Z_CROSSOVER_CLEARANCE

    # Each list item defines axis we are probing for, starting position vector
    # relative to probe top center and travel distance
    hot_spots = [
        ('x',       -rel_x_start, X_SWITCH_OFFSET_MM, Z_PROBE_CLEARANCE_MM,                 size_x),  # NOQA
        ('x',        rel_x_start, X_SWITCH_OFFSET_MM, Z_PROBE_CLEARANCE_MM,                -size_x),  # NOQA
        ('y', Y_SWITCH_OFFSET_MM,       -rel_y_start, Z_PROBE_CLEARANCE_MM,                 size_y),  # NOQA
        ('y', Y_SWITCH_OFFSET_MM,        rel_y_start, Z_PROBE_CLEARANCE_MM,                -size_y),  # NOQA
        ('z',                0.0, Z_SWITCH_OFFSET_MM,          rel_z_start, -Z_CROSSOVER_CLEARANCE)   # NOQA
    ]

    tip_length = robot.config.tip_length[instrument.mount][instrument.type]

    acc = []

    res = {
        'x': [], 'y': [], 'z': []
    }

    robot.home()

    for axis, *probing_vector, distance in hot_spots:
        x, y, z = array(probing_vector) + center

        safe_height = center.z + Z_CROSSOVER_CLEARANCE

        robot.poses = instrument._move(robot.poses, z=safe_height)
        robot.poses = instrument._move(robot.poses, x=x, y=y)
        robot.poses = instrument._move(robot.poses, z=z)

        axis_index = 'xyz'.index(axis)
        robot.poses = instrument._probe(robot.poses, axis, distance)

        # Tip position is stored in accumulator and averaged for each axis
        # to be used for more accurate positioning for the next axis
        value = absolute(robot.poses, instrument)[axis_index]
        acc.append(value)

        # Since we are measuring to update instrument offset and tip length
        # store mover position for XY and tip's Z
        node = instrument if axis == 'z' else instrument.instrument_mover
        res[axis].append(
            absolute(robot.poses, node)[axis_index]
        )

        # after probing two points along the same axis
        # average them out, update center and clear accumulator
        if len(acc) == 2:
            center = center._replace(**{axis: (acc[0] + acc[1]) / 2.0})
            acc.clear()

        # Bounce back to release end stop<commit_msg>move to safe probe height before looping through switches<commit_after>from numpy import array

from opentrons.trackers.pose_tracker import (
    update, Point, change_base
)
from opentrons.robot import robot_configs
from opentrons.data_storage import database
from opentrons.trackers.pose_tracker import absolute


X_SWITCH_OFFSET_MM = 5.0
Y_SWITCH_OFFSET_MM = 2.0
Z_SWITCH_OFFSET_MM = 5.0

Z_PROBE_CLEARANCE_MM = 5.0

BOUNCE_DISTANCE_MM = 5.0
XY_CLEARANCE = 7.5
Z_CROSSOVER_CLEARANCE = 80  # How much of z height to be added to nozzle to clear the top


def calibrate_container_with_delta(
        pose_tree, container, delta_x,
        delta_y, delta_z, save, new_container_name=None
):
    delta = Point(delta_x, delta_y, delta_z)
    new_coordinates = change_base(
        pose_tree,
        src=container,
        dst=container.parent) + delta
    pose_tree = update(pose_tree, container, new_coordinates)
    container._coordinates = container._coordinates + delta
    if save and new_container_name:
        database.save_new_container(container, new_container_name)
    elif save:
        database.overwrite_container(container)
    return pose_tree


def probe_instrument(instrument, robot) -> Point:
    from statistics import mean

    size_x, size_y, size_z = robot.config.probe_dimensions
    center = Point(*robot.config.probe_center)

    rel_x_start = (size_x / 2) + XY_CLEARANCE
    rel_y_start = (size_y / 2) + XY_CLEARANCE
    rel_z_start = Z_CROSSOVER_CLEARANCE

    # Each list item defines axis we are probing for, starting position vector
    # relative to probe top center and travel distance
    hot_spots = [
        ('x',       -rel_x_start, X_SWITCH_OFFSET_MM, Z_PROBE_CLEARANCE_MM,                 size_x),  # NOQA
        ('x',        rel_x_start, X_SWITCH_OFFSET_MM, Z_PROBE_CLEARANCE_MM,                -size_x),  # NOQA
        ('y', Y_SWITCH_OFFSET_MM,       -rel_y_start, Z_PROBE_CLEARANCE_MM,                 size_y),  # NOQA
        ('y', Y_SWITCH_OFFSET_MM,        rel_y_start, Z_PROBE_CLEARANCE_MM,                -size_y),  # NOQA
        ('z',                0.0, Z_SWITCH_OFFSET_MM,          rel_z_start, -Z_CROSSOVER_CLEARANCE)   # NOQA
    ]

    tip_length = robot.config.tip_length[instrument.mount][instrument.type]

    acc = []

    res = {
        'x': [], 'y': [], 'z': []
    }

    safe_height = center.z + Z_CROSSOVER_CLEARANCE

    robot.home()
    robot.poses = instrument._move(robot.poses, z=safe_height)

    for axis, *probing_vector, distance in hot_spots:
        x, y, z = array(probing_vector) + center

        robot.poses = instrument._move(robot.poses, x=x, y=y)
        robot.poses = instrument._move(robot.poses, z=z)

        axis_index = 'xyz'.index(axis)
        robot.poses = instrument._probe(robot.poses, axis, distance)

        # Tip position is stored in accumulator and averaged for each axis
        # to be used for more accurate positioning for the next axis
        value = absolute(robot.poses, instrument)[axis_index]
        acc.append(value)

        # Since we are measuring to update instrument offset and tip length
        # store mover position for XY and tip's Z
        node = instrument if axis == 'z' else instrument.instrument_mover
        res[axis].append(
            absolute(robot.poses, node)[axis_index]
        )

        # after probing two points along the same axis
        # average them out, update center and clear accumulator
        if len(acc) == 2:
            center = center._replace(**{axis: (acc[0] + acc[1]) / 2.0})
            acc.clear()

        # Bounce back to release end stop
{'fraction_non_alphanumeric': 0.08591352859135286, 'fraction_numerical': 0.006415620641562064, 'mean_word_length': 2.7643044619422574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5900826', 'n_tokens_mistral': 2421, 'n_tokens_neox': 2291, 'n_words': 579}