Ejemplo n.º 1
0
def update_statistics():
    def chunker(iterable, chunksize, filler):
        return izip_longest(*[iter(iterable)] * chunksize, fillvalue=filler)

    stats = Statistics()
    ids = [
        i['session_key'] for i in models.SessionData.objects.filter(
            level__gte=0).values('session_key')
    ]

    for chunk in chunker(ids, 100, None):
        chunk = filter(None, chunk)
        for u in models.SessionData.objects.filter(pk__in=chunk):
            stats.add(u.json())
    Settings.set('statistics', stats)
Ejemplo n.º 2
0
class NBackApp(App):
    SETTINGS_FILENAME = "config.ini"

    config = ObjectProperty(None)

    def build(self):
        self._load_settings()
        # TODO string should be accessed as constant
        db_dir = self.config.get('internal', 'db_path')
        db_location = os.path.join(db_dir, Statistics.DB_NAME)
        self.stats = Statistics(db_location)
        self.sm = ScreenHistoryManager()
        self.sm.add_widget(MainScreen(name='menu'))
        self.sm.add_widget(game.GameScreen(name='game'))
        self.sm.add_widget(settings.SettingScreen(name='settings'))
        self.sm.add_widget(statistics.StatisticsScreen(name='statistics'))
        self.sm.add_widget(about.AboutScreen(name='about'))
        Window.bind(on_keyboard=self._hook_keyboard)
        # return game
        return self.sm

    def add_result(self, level, position, shape, success, items_count):
        self.stats.add(level, position, shape, success, items_count)

    def _hook_keyboard(self, window, key, *args):
        if key in BACK_KEY_CODES:
            try:
                self.sm.move_to_previous_screen()
            except IndexError:
                self.stop()

    def _load_settings(self):
        self.config = ConfigParser()
        if not os.path.exists(self.SETTINGS_FILENAME):
            raise IOError("Cannot locate configuration file.")
        self.config.read(self.SETTINGS_FILENAME)
Ejemplo n.º 3
0
class Engrave:
    _APPROACH = 100  # Approach distance in mm
    IMAGE_SIZE_NONE = svg.Point(104, 50)  # size of the image in MM not including edge
    IMAGE_SIZE_EDGE = svg.Point(100, 36)  # size of image in MM not including edge
    IMAGE_SIZE_CURVED = svg.Point(108, 56)  # size of image in MM not including edge
    stock = Stock()
    _PIXEL_SIZE = 1.2  # For calculating the arc length of the images

    def __init__(self, rdk):
        """
        Constructor
        Checks whether an .svg file exist at the given location, if not loads a default .svg file
        and collects information from the config file
        """

        # Check whether the customer has given a valid filepath, if not take the default .svg file
        if os.path.isfile(CaseConfig.file()):
            self.svg = svg.svg_load(CaseConfig.file())
        else:
            self.svg = svg.svg_load(CaseConfig.file('DEFAULT'))

        # Initialize fields of the cover, robodk and statistics
        self.curve = CaseConfig.curve_style()
        self.color = CaseConfig.colour()
        self.RDK = rdk
        self.stat = Statistics()

    @staticmethod
    def point2d_2_pose(point, tangent):
        """
        Translates a point to a point in 3D space
        :param point: Point object containing members x and y
        :param tangent: Tangent object containing method angle
        :return: Returns a 4 x 4 matrix of the point
        """
        return transl(point.x, point.y, 0) * rotz(tangent.angle())

    @staticmethod
    def curved_add_z(x):
        """
        Calculates the z value depending on the x parameter
        Based on the function of the curvature on the curved cover
        :param x: x coordinate
        :return: the corresponding z coordinate
        """
        x = fabs(x+23.3)
        y = pow(57, 2)-pow(x-57, 2)
        z = (8/57)*sqrt(y)
        return z

    def begin_flat(self, time: Timer):
        """
        Engraving for flat covers
        Instructions sent to the robot in RoboDK for engraving the flat cover depending on the file given
        """

        # Initialization of the robot, reference frame, tool and the pixel object and set the speed
        start = datetime.now()
        robot = self.RDK.Item('engraver', 2)
        robot.setSpeed(250)
        robot.setAcceleration(2000)
        item_frame = self.RDK.Item('engrave_flat', 3)
        tool_frame = self.RDK.Item('laser_tool', 4)
        pix_ref = self.RDK.Item('pixel', 5)

        # Move the phone into the environment
        self.move_to_environment()

        # Initialize the cover to engrave upon
        item = self.RDK.Item(f'cover_{self.color}_none_{self.stock.get(f"{self.color}_{self.curve}")+1}', 5)

        # Resize the svg to the engraveable surface on the cover
        self.svg.calc_polygon_fit(self.IMAGE_SIZE_NONE, self._PIXEL_SIZE)

        # Place the image centered on the cover based on the image size
        size_img = self.svg.size_poly()
        placement_var = -19+self.IMAGE_SIZE_NONE.x/2-size_img.x/2

        # For each path in the svg
        for path in self.svg:

            # Align the image with the cover
            path.polygon_move(-25.5, placement_var)

            # Recolour the pixel to black and copy it for simulating engraving
            pix_ref.Recolor([0, 0, 0, 1])
            pix_ref.Copy()

            # Get the first point on the image and switch it coordinates to get the correct rotation
            point_quantity = path.nPoints()
            point_0 = path.getPoint(0)
            point_0.switchXY()

            # Calculate a matrix for orienting the pose to the tool
            orient_frame2tool = invH(item_frame.Pose()) * robot.SolveFK(robot.JointsHome()) * tool_frame.Pose()
            orient_frame2tool[0:3, 3] = Mat([0, 0, 0])

            # Get the first target, and align it with the tool and move the robot there
            target0 = transl(-point_0.x, point_0.y, -299) * orient_frame2tool * Pose(0, 0, 0, -180, 0, 270)
            robot.MoveL(target0)

            # Set the color in RoboDK
            self.RDK.RunProgram('SetColorRGB(%.3f,%.3f,%.3f)' % (
                0, 0, 0))

            # For each point in the given path
            for point in range(point_quantity):

                # Get the point and switch the coordinates for correct rotation
                path_point = path.getPoint(point)
                path_point.switchXY()

                # Calculate the target based on the points, a height and orientation of the tool and move the engraver
                path_target = transl(-path_point.x, path_point.y, -299) * orient_frame2tool * Pose(0, 0, 0, -180, 0, 270)
                robot.MoveL(path_target)

                # create a new pixel object with the calculated pixel pose and add it to the cover
                point_pose = transl(-path_point.x + 5.2, 12, -path_point.y+67.55) * orient_frame2tool * Pose(0, 0, 0, 90, 0, 0)
                item.AddGeometry(pix_ref, point_pose)

            # Move to the end point
            robot.MoveL(path_target)

        # When done engraving move the robot home and move the phone out of the engraving environment
        # Note: the real home position of the engraver is very error prone as is therefore omitted from here
        robot.MoveJ(Pose(0, -23.3, -259, 0, 0, 90))
        self.move_from_environment()

        # Add the engraving to the statistics
        stat_str = self.color + '_' + self.curve + '_engraved'
        self.stat.add(stat_str, 1)
        time.end('average_engraving_time', start)

    def begin_edge(self, time: Timer):
        """
        Engraving for curved edges covers
        Instructions sent to the robot in RoboDK for engraving the curved edges cover depending on the file given
        """

        # Initialization of the robot, reference frame, tool and the pixel object and start the timer
        start = datetime.now()
        robot = self.RDK.Item('engraver', 2)
        robot.setSpeed(250)
        robot.setAcceleration(2000)
        item_frame = self.RDK.Item('engrave_flat', 3)
        tool_frame = self.RDK.Item('laser_tool', 4)
        pix_ref = self.RDK.Item('pixel', 5)

        # Move the phone into the environment
        self.move_to_environment()

        # Initialize the cover to engrave upon
        item = self.RDK.Item(f'cover_{self.color}_edge_{self.stock.get(f"{self.color}_{self.curve}") + 1}', 5)

        # Resize the svg to the engraveable surface on the cover
        self.svg.calc_polygon_fit(self.IMAGE_SIZE_EDGE, self._PIXEL_SIZE)

        # Place the image centered on the cover based on the image size
        size_img = self.svg.size_poly()
        placement_var = -15+self.IMAGE_SIZE_EDGE.x/2-size_img.x/2

        # For each path in the svg
        for path in self.svg:

            # Align the image with the cover
            path.polygon_move(-17, placement_var)

            # Recolour the pixel to black and copy it for simulating engraving
            pix_ref.Recolor([0, 0, 0, 1])
            pix_ref.Copy()

            # Get the first point on the image and switch it coordinates to get the correct rotation
            point_quantity = path.nPoints()
            point_0 = path.getPoint(0)
            point_0.switchXY()

            # Calculate a matrix for orienting the pose to the tool
            orient_frame2tool = invH(item_frame.Pose()) * robot.SolveFK(robot.JointsHome()) * tool_frame.Pose()
            orient_frame2tool[0:3, 3] = Mat([0, 0, 0])

            # Get the first target, and align it with the tool and move the robot there
            target0 = transl(-point_0.x, point_0.y, -296.38) * orient_frame2tool * Pose(0, 0, 0, -180, 0, 270)
            target0_app = target0
            robot.MoveL(target0_app)

            # Set the color in RoboDK
            self.RDK.RunProgram('SetColorRGB(%.3f,%.3f,%.3f)' % (
                0, 0, 0))

            # For each point in the given path
            for point in range(point_quantity):

                # Get the point and switch the coordinates for correct rotation
                path_point = path.getPoint(point)
                path_point.switchXY()

                # Calculate the target based on the points, a height and orientation of the tool and move the engraver
                path_target = transl(-path_point.x, path_point.y, -296.38) * orient_frame2tool * Pose(0, 0, 0, -180, 0, 270)
                robot.MoveL(path_target)

                # create a new pixel object with the calculated pixel pose and add it to the cover
                point_pose = transl(-path_point.x + 5.2, 14.75, -path_point.y + 67.55) * orient_frame2tool * Pose(0, 0, 0, 90, 0, 0)
                item.AddGeometry(pix_ref, point_pose)

            # Move to the end point
            robot.MoveL(path_target)

        # When done engraving move the robot home and move the phone out of the engraving environment
        # Note: the real home position of the engraver is very error prone as is therefore omitted from here
        robot.MoveJ(Pose(0, -23.3, -259, 0, 0, 90))
        self.move_from_environment()

        # Add the engraving to the statistics
        stat_str = self.color + '_' + self.curve + '_engraved'
        self.stat.add(stat_str, 1)

        # End the timer with the engraving time and add it to the average
        time.end('average_engraving_time', start)

    def begin_curved(self, time: Timer):
        """
        Engraving for curved covers
        Instructions sent to the robot in RoboDK for engraving the curved cover depending on the file given
        """

        # Initialization of the robot, reference frame, tool and the pixel object and start the timer
        start = datetime.now()
        robot = self.RDK.Item('engraver', 2)
        robot.setSpeed(250)
        robot.setAcceleration(2000)
        item_frame = self.RDK.Item('engrave_flat', 3)
        tool_frame = self.RDK.Item('laser_tool', 4)
        pix_ref = self.RDK.Item('pixel', 5)

        # Move the phone into the environment
        self.move_to_environment()

        # Initialize the cover to engrave upon
        item = self.RDK.Item(f'cover_{self.color}_curved_{self.stock.get(f"{self.color}_{self.curve}")+1}', 5)

        # Resize the svg to the engraveable surface on the cover
        self.svg.calc_polygon_fit(self.IMAGE_SIZE_CURVED, self._PIXEL_SIZE)

        # Place the image centered on the cover based on the image size
        size_img = self.svg.size_poly()
        placement_var = -21.3+self.IMAGE_SIZE_CURVED.x/2-size_img.x/2

        # For each path in the svg
        for path in self.svg:

            # Align the image with the cover
            path.polygon_move(-27.5, placement_var)

            # Recolour the pixel to black and copy it for simulating engraving
            pix_ref.Recolor([0, 0, 0, 1])
            pix_ref.Copy()

            # Get the first point on the image and switch it coordinates to get the correct rotation
            point_quantity = path.nPoints()
            point_0 = path.getPoint(0)
            point_0.switchXY()

            # Calculate a matrix for orienting the pose to the tool
            orient_frame2tool = invH(item_frame.Pose()) * robot.SolveFK(robot.JointsHome()) * tool_frame.Pose()
            orient_frame2tool[0:3, 3] = Mat([0, 0, 0])

            # Get the first target, and align it with the tool and move the robot there
            target0 = transl(-point_0.x, point_0.y, -302.1+self.curved_add_z(point_0.y)) * orient_frame2tool * Pose(0, 0, 0, -180, 0, 270)
            target0_app = target0
            robot.MoveL(target0_app)

            # Set the color in RoboDK
            self.RDK.RunProgram('SetColorRGB(%.3f,%.3f,%.3f)' % (
                0, 0, 0))

            # For each point in the given path
            for point in range(point_quantity):

                # Get the point and switch the coordinates for correct rotation
                path_point = path.getPoint(point)
                path_point.switchXY()

                # Calculate the target based on the points, a height and orientation of the tool and move the engraver
                path_target = transl(-path_point.x, path_point.y, -302.1 + self.curved_add_z(path_point.y)) * orient_frame2tool * Pose(0, 0, 0, -180, 0, 270)
                robot.MoveL(path_target)

                # create a new pixel object with the calculated pixel pose 20.67
                point_pose = transl(-path_point.x + 5.2, 8.8+self.curved_add_z(path_point.y), -path_point.y +67.55) * orient_frame2tool * Pose(0, 0, 0, 90, 0, 0)

                # create a new pixel object with the calculated pixel pose and add it to the cover
                item.AddGeometry(pix_ref, point_pose)

            # Move to the end point
            robot.MoveL(path_target)

        # When done engraving move the robot home and move the phone out of the engraving environment
        # Note: the real home position of the engraver is very error prone as is therefore omitted from here
        robot.MoveJ(Pose(0, -23.3, -259, 0, 0, 90))
        self.move_from_environment()

        # Add the engraving to the statistics
        stat_str = self.color + '_' + self.curve + '_engraved'
        self.stat.add(stat_str, 1)

        # End the timer with the engraving time and add it to the average
        time.end('average_engraving_time', start)

    def move_to_environment(self):
        """
        Moves the cover into the engraving environment with the engraving plate
        """

        # Initialize the moving_plate and its reference frame and set the speed
        robot = self.RDK.Item('moving_plate')
        plate_ref = self.RDK.Item('engraving_plate_ref', 3)
        robot.setSpeed(300)

        # Set the position of the engraving plate, set the reference frame of the plate and move it to the position
        position_engraving = Pose(318, 0, 16.3, 0, 0, 0)
        robot.setPoseFrame(plate_ref)
        robot.MoveL(position_engraving)

    def move_from_environment(self):
        """
        Moves the cover from the engraving environment with the engraving plate
        """

        # Initialize the moving_plate and its reference frame and set the speed
        robot = self.RDK.Item('moving_plate')
        plate_ref = self.RDK.Item('engraving_plate_ref', 3)
        robot.setSpeed(300)

        # Set the position of the engraving plate, set the reference frame of the plate and move it to the position
        position_engraving_out = Pose(145, 0, 16.3, 0, 0, 0)
        robot.setPoseFrame(plate_ref)
        robot.MoveL(position_engraving_out)
Ejemplo n.º 4
0
class Cover:
    # Global class variables
    # Distances in mm

    # Offsets for positions of covers in the storage container
    _OFFSETZ_NONE = 50.25
    _OFFSETZ_EDGE = 25
    _OFFSETZ_CURVED = 4.4
    _OFFSETX_COVER_DIST = 70
    _OFFSETY_COVER_DIST = 73
    _APPROACH = 100

    # Cover related offsets for calculations
    _OFFSETZ_COVER_FLAT_DIST = 11.7
    _OFFSETZ_COVER_EDGE_DIST = 14.7
    _OFFSETZ_COVER_CURVED_DIST = 16.7
    _TOP_COVER_INDENT_OFFSET = 5
    _BOTTOM_COVER_HEIGHT = 13

    # Colours for recolouring the bottom cover
    _COLOR_BLACK = [0.1019607843, 0.1019607843, 0.1019607843]
    _COLOR_WHITE = [1, 1, 1]
    _COLOR_BLUE = [0, 0, 0.6078431372]

    def __init__(self, color, curve_type, stock: Stock):
        """
        Constructor:
        Assigns the parameters to the given fields and corrects the position of the cover based on the parameters
        :param color: The color given in the config file
        :param curve_type: The curve type given in the config file
        :param stock: Stock Stock controlling object with type hint Stock
        """

        # Initialization of important object fields
        self.RDK = Robolink()
        self.color = color
        self.curve = curve_type
        self.stock = stock
        self.stat = Statistics()

        # Initial position of the first set of covers
        self.position = Pose(45.5, self._OFFSETY_COVER_DIST, 0, -180, 0, 0)

        # Correct the position of the cover based on colour and curvature
        self.correct_pos(self.stock)

        # Initialize bottom cover as RoboDK Item for recolouring
        bottom = self.RDK.Item('bottom', 5)

        # Based on selection of colour, recolour bottom cover
        if CaseConfig.bottom_colour() == 'black':
            bottom.Recolor(self._COLOR_BLACK)
        if CaseConfig.bottom_colour() == 'white':
            bottom.Recolor(self._COLOR_WHITE)
        if CaseConfig.bottom_colour() == 'blue':
            bottom.Recolor(self._COLOR_BLUE)

    def correct_pos(self, stock: Stock):
        """
        Corrects the position of the cover relative to the stock container, depending on the type of cover.
        Instead of giving each cover a static position
        :param stock: Stock controlling object with type hint Stock
        :return: The position of the specific cover
        """

        # Dicts with identifiers for easier calculation of position
        curve_types = {
            'black_none': 0,
            'black_edge': 3,
            'black_curved': 6,
            'white_none': 1,
            'white_edge': 4,
            'white_curved': 7,
            'blue_none': 2,
            'blue_edge': 5,
            'blue_curved': 8
        }

        case_types = {
            'black_none': 0,
            'black_edge': 1,
            'black_curved': 2,
            'white_none': 3,
            'white_edge': 4,
            'white_curved': 5,
            'blue_none': 6,
            'blue_edge': 7,
            'blue_curved': 8
        }

        case_curve = {'curved': 0, 'edge': 1, 'none': 2}

        # Apply the offset from the bottom of the container to the first cover in the pile
        curvature = case_curve[f'{self.curve}']
        if curvature == 1:
            self.position[2, 3] = self.position[2, 3] + self._OFFSETZ_EDGE
        if curvature == 2:
            self.position[2, 3] = self.position[2, 3] + self._OFFSETZ_NONE
        if curvature == 0:
            self.position[2, 3] = self.position[2, 3] + self._OFFSETZ_CURVED

        # Apply the x-offset based on the type and the colour of the cover
        identifier = case_types[f'{self.color}_{self.curve}']
        self.position[
            0, 3] = self.position[0, 3] + identifier * self._OFFSETX_COVER_DIST

        # Depending on the remaining stock and type of curve, calculate the Z-offset to compensate for the empty space
        identifier_curve = curve_types[f'{self.color}_{self.curve}']
        if identifier_curve in range(0, 3):
            self.position[2, 3] = self.position[2, 3] + stock.get(
                f'{self.color}_none') * self._OFFSETZ_COVER_FLAT_DIST
        if identifier_curve in range(3, 6):
            self.position[2, 3] = self.position[2, 3] + stock.get(
                f'{self.color}_edge') * self._OFFSETZ_COVER_EDGE_DIST
        if identifier_curve in range(6, 9):
            self.position[2, 3] = self.position[2, 3] + stock.get(
                f'{self.color}_curved') * self._OFFSETZ_COVER_CURVED_DIST

    def __str__(self):
        """
        String representation of the class
        :return: Prints the color, curve type and position of the cover
        """
        print(f'{self.color} curve_{self.curve} cover at {self.position}')

    def get_pos(self):
        """
        Debugging method
        :return:  Returns the position of the cover
        """
        return self.position

    def grab(self, robot):
        """
        Method that sends instructions to the robot in RoboDK to grab the cover from the stock container
        :param robot: Robot object representing the robot in RoboDK
        """

        # Initialize the reference frame as an Item to use in positioning
        frame = self.RDK.Item('storage', 3)

        # Set the reference frame to the newly initialized frame
        robot.setPoseFrame(frame)
        robot.setSpeed(900)

        # Copy the initial position and apply an offset to ensure the same approach each time and move the robot there
        position_copy = self.position
        if self.curve == 'none':
            position_copy[2, 3] = position_copy[
                2, 3] + self._APPROACH + self.stock.get_init(
                ) * self._OFFSETZ_COVER_FLAT_DIST
        elif self.curve == 'edge':
            position_copy[2, 3] = position_copy[
                2, 3] + self._APPROACH + self.stock.get_init(
                ) * self._OFFSETZ_COVER_EDGE_DIST
        elif self.curve == 'curved':
            position_copy[2, 3] = position_copy[
                2, 3] + self._APPROACH + self.stock.get_init(
                ) * self._OFFSETZ_COVER_CURVED_DIST
        robot.MoveJ(position_copy)

        # Reduce the speed and subtract all the height offsets to approach cover and move robot to the given position
        robot.setSpeed(300)
        if self.curve == 'none':
            position_copy[2, 3] = position_copy[
                2, 3] - self._APPROACH - self.stock.get_init(
                ) * self._OFFSETZ_COVER_FLAT_DIST
        elif self.curve == 'edge':
            position_copy[2, 3] = position_copy[
                2, 3] - self._APPROACH - self.stock.get_init(
                ) * self._OFFSETZ_COVER_EDGE_DIST
        elif self.curve == 'curved':
            position_copy[2, 3] = position_copy[
                2, 3] - self._APPROACH - self.stock.get_init(
                ) * self._OFFSETZ_COVER_CURVED_DIST
        robot.MoveL(position_copy)

        # Run RoboDk program to attach cover to tool
        self.RDK.RunProgram('Prog6')

        # Return to the approach position to ensure that the robot doesn't damage anything else and move there
        if self.curve == 'none':
            position_copy[2, 3] = position_copy[
                2, 3] + self._APPROACH + self.stock.get_init(
                ) * self._OFFSETZ_COVER_FLAT_DIST
        elif self.curve == 'edge':
            position_copy[2, 3] = position_copy[
                2, 3] + self._APPROACH + self.stock.get_init(
                ) * self._OFFSETZ_COVER_EDGE_DIST
        elif self.curve == 'curved':
            position_copy[2, 3] = position_copy[
                2, 3] + self._APPROACH + self.stock.get_init(
                ) * self._OFFSETZ_COVER_CURVED_DIST
        robot.MoveL(position_copy)

        # Add the used cover to statistics for later collection of data
        self.stock.sub(f'{self.color}_{self.curve}', 1)

    def give_top(self, time: Timer):
        """
        Sends instructions to RoboDK to grab a cover, place it on top of the bottom cover
        if the cover needs to be engraved, places it on the engraving plate.
        """
        # Start the timer
        start = datetime.now()

        # Carrier positions in relation to it's reference frame
        carrier_offsetx = 88.7  # (Carrier length / 2)
        carrier_offsety = 55.3  # (Carrier width / 2)
        carrier_offsetz = 42.3  # (Carrier depth)

        # Initialization of position of carrier and adding approach distance
        carrier_position_app = Pose(carrier_offsetx, carrier_offsety,
                                    carrier_offsetz, -180, 0, -90)
        carrier_position_app[2,
                             3] = carrier_position_app[2, 3] + self._APPROACH

        # Ensure connections to RoboDK
        self.RDK.Connect()

        # Connect to the robot and initialize pickup procedure
        robot = self.RDK.Item('fanuc', 2)
        self.grab(robot)
        robot.setSpeed(900)

        # Create object frame for usage as reference frame
        carrier_ = self.RDK.Item('carrier', 3)

        # Assign reference frame to robot and move to position
        robot.setPoseFrame(carrier_)
        robot.MoveJ(carrier_position_app)

        # Initialize new position variable with previous position
        position_withoffset = carrier_position_app

        # Check the type of cover again and subtract the approach offset and detail offset,
        # while considering that a cover is attached to the tool
        if self.curve == 'none':
            position_withoffset[2, 3] = position_withoffset[
                2, 3] - self._APPROACH + self._OFFSETZ_COVER_FLAT_DIST - 1.5
        if self.curve == 'edge':
            position_withoffset[2, 3] = position_withoffset[
                2, 3] - self._APPROACH + self._OFFSETZ_COVER_EDGE_DIST - 1.77
        if self.curve == 'curved':
            position_withoffset[2, 3] = position_withoffset[
                2, 3] - self._APPROACH + self._OFFSETZ_COVER_CURVED_DIST - 2.4

        # Take into account that it is now a whole phone
        position_withoffset[2, 3] = position_withoffset[
            2, 3] - self._TOP_COVER_INDENT_OFFSET + self._BOTTOM_COVER_HEIGHT

        # Reduce speed for safety and move to position
        robot.setSpeed(300)
        robot.MoveL(position_withoffset)

        # Run program named Prog7 to attach bottom cover to tool
        self.RDK.RunProgram('Prog7')

        # Add the creation of the phone to statistics
        self.stat.add(f'{self.color}_{self.curve}', 1)

        # If the cover needs engraving
        if CaseConfig.engrave():

            # End the timer and add it to the production time average
            time.end('average_production_time', start)

            # Add approach distance to the position and move the robot there and reset the speed
            carrier_position_app[2,
                                 3] = carrier_position_app[2,
                                                           3] + self._APPROACH
            robot.MoveL(carrier_position_app)
            robot.setSpeed(900)

            # Create frame object for reference frame and assign it to the robot
            engrave_plate_ref = self.RDK.Item('engraving_plate_ref', 3)
            robot.setPoseFrame(engrave_plate_ref)

            # Initialize the position of the engraving plate
            engrave_plate_pos_app = Pose(145.29, 0.05, 42.6, -180, 0, 90)

            # Check for type of cover an apply an approach to the z value and move the robot there
            if self.curve == 'none':
                engrave_plate_pos_app[
                    2, 3] = engrave_plate_pos_app[2, 3] + self._APPROACH
            if self.curve == 'edge':
                engrave_plate_pos_app[
                    2, 3] = engrave_plate_pos_app[2, 3] + self._APPROACH + 3
            if self.curve == 'curved':
                engrave_plate_pos_app[
                    2, 3] = engrave_plate_pos_app[2, 3] + self._APPROACH + 5
            robot.MoveJ(engrave_plate_pos_app)

            # Initialize new position variable and check for cover type and subtract the approach while taking into account carrying a phone
            engrave_plate_pos = engrave_plate_pos_app
            robot.setSpeed(300)
            if self.curve == 'none':
                engrave_plate_pos[2,
                                  3] = engrave_plate_pos[2, 3] - self._APPROACH
            elif self.curve == 'edge':
                engrave_plate_pos[
                    2, 3] = engrave_plate_pos[2, 3] - self._APPROACH - 2
            else:
                engrave_plate_pos[
                    2, 3] = engrave_plate_pos[2, 3] - self._APPROACH - 2.5
            robot.MoveL(engrave_plate_pos)

            # Create tool object and detach all children of this object
            tool_suction_ = self.RDK.Item('tool_suction', 4)
            tool_suction_.DetachAll()  # Detach all objects from the robot

            # Check the type of cover and apply approach distance and move the robot there
            if self.curve == 'none':
                engrave_plate_pos[2,
                                  3] = engrave_plate_pos[2, 3] + self._APPROACH
            elif self.curve == 'edge':
                engrave_plate_pos[
                    2, 3] = engrave_plate_pos[2, 3] + self._APPROACH - 2
            else:
                engrave_plate_pos[
                    2, 3] = engrave_plate_pos[2, 3] + self._APPROACH - 2.5
            robot.MoveL(engrave_plate_pos)

            # Move the robot home and attach the phone to the engraving plate by running the program "prog8"
            robot.setSpeed(900)
            robot.MoveJ(robot.JointsHome())
            self.RDK.RunProgram('Prog8')

        # If the cover doesn't need engraving
        else:

            # End the timer and add it to the production time average
            time.end('average_production_time', start)

            # Create a tool object and Detach all children from it
            tool_suction_ = self.RDK.Item('tool_suction', 4)
            tool_suction_.DetachAll()

            # Apply approach distance and move the robot there
            carrier_position_app[2,
                                 3] = carrier_position_app[2,
                                                           3] + self._APPROACH
            robot.MoveL(carrier_position_app)

            # Reset speed and move the robot to it's home position
            robot.setSpeed(900)
            robot.MoveJ(robot.JointsHome())

    def retrieve(self):
        """
        Retrieves the cover from the engraving plate and places it on the pallette
        Thereby finishing the production of an engraved cover
        """

        # Initialize moving plate tool object and detach all children of the object.
        move_plate_tool = self.RDK.Item('engraving_plate', 4)
        move_plate_tool.DetachAll()

        # Initialize the reference frame of the engraving plate, the robot and set the robot reference frame to the
        # frame of the engraving plate and set the speed
        engrave_plate_ref = self.RDK.Item('engraving_plate_ref', 3)
        robot = self.RDK.Item('fanuc', 3)
        robot.setPoseFrame(engrave_plate_ref)
        robot.setSpeed(900)

        # Initialize the position of the engraving plate
        engrave_plate_pos_app = Pose(145.29, 0.05, 42.5, -180, 0, 90)

        # Apply approach distance to the position and move the robot there
        if self.curve == 'none':
            engrave_plate_pos_app[
                2, 3] = engrave_plate_pos_app[2, 3] + self._APPROACH
        elif self.curve == 'edge':
            engrave_plate_pos_app[
                2, 3] = engrave_plate_pos_app[2, 3] + self._APPROACH
        else:
            engrave_plate_pos_app[
                2, 3] = engrave_plate_pos_app[2, 3] + self._APPROACH
        robot.MoveJ(engrave_plate_pos_app)

        # Initialize new position variable with the old and subtract the approach distance along with an offset
        robot.setSpeed(300)
        engrave_plate_pos = engrave_plate_pos_app
        if self.curve == 'none':
            engrave_plate_pos[2,
                              3] = engrave_plate_pos[2,
                                                     3] - self._APPROACH - 0.25
        elif self.curve == 'edge':
            engrave_plate_pos[2,
                              3] = engrave_plate_pos[2,
                                                     3] - self._APPROACH + 1.6
        else:
            engrave_plate_pos[2,
                              3] = engrave_plate_pos[2,
                                                     3] - self._APPROACH + 3.0

        # Move the robot to the new position and attach the phone to it
        robot.MoveL(engrave_plate_pos)
        self.RDK.RunProgram('Prog6')
        self.RDK.RunProgram('Prog7')

        # Apply approach distance to the position and move the robot there
        if self.curve == 'none':
            engrave_plate_pos_app[
                2, 3] = engrave_plate_pos_app[2, 3] + self._APPROACH
        elif self.curve == 'edge':
            engrave_plate_pos_app[
                2, 3] = engrave_plate_pos_app[2, 3] + self._APPROACH
        else:
            engrave_plate_pos_app[
                2, 3] = engrave_plate_pos_app[2, 3] + self._APPROACH
        robot.MoveJ(engrave_plate_pos_app)

        # Cover position coordinates relative to the carrier reference frame reference frame
        carrier_offsetx = 88.7  # (Carrier length / 2)
        carrier_offsety = 55.3  # (Carrier width / 2)
        carrier_offsetz = 42.3  # (Carrier depth)

        # Initialize position field with position of the carrier
        carrier_position_app = Pose(carrier_offsetx, carrier_offsety,
                                    carrier_offsetz, -180, 0, -90)

        # Initialize frame object of the carrier and set the reference frame of the robot to the given frame
        carrier_ = self.RDK.Item('carrier', 3)
        robot.setPoseFrame(carrier_)
        robot.setSpeed(900)

        # Apply the approach distance to the position and move the robot there
        if self.curve == 'none':
            carrier_position_app[2,
                                 3] = carrier_position_app[2,
                                                           3] + self._APPROACH
        if self.curve == 'edge':
            carrier_position_app[2,
                                 3] = carrier_position_app[2,
                                                           3] + self._APPROACH
        if self.curve == 'curved':
            carrier_position_app[2,
                                 3] = carrier_position_app[2,
                                                           3] + self._APPROACH
        robot.MoveJ(carrier_position_app)

        # Initialize new position field
        position_withoffset = carrier_position_app

        # Check the type of cover again and subtract the approach offset,
        # while considering that a cover is attached to the tool
        robot.setSpeed(300)
        if self.curve == 'none':
            position_withoffset[2, 3] = position_withoffset[
                2, 3] - self._APPROACH + self._OFFSETZ_COVER_FLAT_DIST - 1.85
        if self.curve == 'edge':
            position_withoffset[2, 3] = position_withoffset[
                2, 3] - self._APPROACH + self._OFFSETZ_COVER_EDGE_DIST - 1.27
        if self.curve == 'curved':
            position_withoffset[2, 3] = position_withoffset[
                2, 3] - self._APPROACH + self._OFFSETZ_COVER_CURVED_DIST - 2

        # Take into account that it is now a whole phone and move the robot to the position
        position_withoffset[2, 3] = position_withoffset[
            2, 3] - self._TOP_COVER_INDENT_OFFSET + self._BOTTOM_COVER_HEIGHT
        robot.MoveL(position_withoffset)

        # Initialize the tool and detach all children of the tool and move the robot home
        tool_suction_ = self.RDK.Item('tool_suction', 4)
        tool_suction_.DetachAll()
        robot.setSpeed(900)
        robot.MoveJ(robot.JointsHome())

    def new_cover_check(self):
        """
        Checks if any cover is remaining from earlier run-throughs and deletes them
        :return: Deletion of previous covers
        """

        # Dict containing all cover types
        cover_types = {
            0: 'black_none',
            1: 'black_edge',
            2: 'black_curved',
            3: 'white_none',
            4: 'white_edge',
            5: 'white_curved',
            6: 'blue_none',
            7: 'blue_edge',
            8: 'blue_curved'
        }

        # Check all types for previous covers by checking stock
        for key in cover_types:
            item = self.RDK.Item(
                f'cover_{cover_types[key]}_{self.stock.get(f"{cover_types[key]}") + 1}'
            )
            if item.Valid():
                item.Delete()
Ejemplo n.º 5
0
                agent.save()
                break
        print(time.time() - t0)
    else:
        envs_to_test = config['test']['envs']
        for env in envs_to_test:

            methods = config['test']['methods']
            assert len(methods) > 0
            for method in methods:

                number_of_tests = config['test']['number']
                for i in range(number_of_tests):

                    tf.reset_default_graph()
                    current_agent = get_agent(method, config, env)

                    for _ in range(current_agent.max_episodes):

                        step(current_agent)
                        logger.log(current_agent.eps_reward)
                        if current_agent.episode % 100 == 0:
                            current_agent.save()

                    stats.add(current_agent, env)
                    # pickle.dump(current_agent.q_vals, open('q_vals_dqn.pkl', 'wb'))
                    logger.reset()

        stats.save()
        stats.visualise()