# -*- coding: UTF-8 -*-
# Copyright 2015-2024 - RoboDK Inc. - https://robodk.com/
#
# This is a compiled post processor. Please contact us at info@robodk.com if you need access to the source code of this post processor.
#
# This file loads the compiled version of the RoboDK post processor for:
#   Motoman robot controllers
#
# More information about RoboDK Post Processors and Offline Programming:
#     https://robodk.com/help#PostProcessor
#     https://robodk.com/doc/en/PythonAPI/postprocessor.html
# ----------------------------------------------------

import sys
import os

import math
from robodk import *

# Detect Python version and post processor
print("Using Python version: " + str(sys.version_info))
path_file = os.path.dirname(__file__).replace(os.sep, "/")
print("RoboDK Post Processor: " + path_file)

# Check if the post is compatible with the Python version
version_str = str(sys.version_info[0]) + str(sys.version_info[1])
path_library = path_file + '/v' + version_str
if not os.path.isdir(path_library):
    msg = "Invalid Python version or post processor not found. Make sure you are using a supported Python version: " + path_library
    msg += "\nSelect Tools-Options-Python and select a supported Python version"
    print(msg)
    raise Exception(msg)

# Load the post processor
exec("from v" + version_str + ".Motoman import RobotPost as BasePost")

class RobotPost(BasePost):

    # ------------------------ Customize your RoboDK post processor using the following variables ------------------------

    # Set the default maximum number of lines per program.
    # It will then generate multiple "pages (files)". This can be overriden by RoboDK settings.
    MAX_LINES_X_PROG = 2000

    # Set the maximum number of characters allowed for program names
    MAX_NAME_CHARS = 7

    # Set to True to use SETTOOL for setting the tool in the JBI program
    # USE_SETTOOL = True   # This requires the SETTOOL option from Motoman (paid option)
    USE_SETTOOL = False

    # Specify the default UTool Id to use (register).
    # You can also use Numbered tools in RoboDK (for example, a tool named "Tool 2" will use UTOOL number 2)
    ACTIVE_TOOL = 9

    # Set the linear speed in CM/MIN instead of MM/S
    # Some Motoman controllers require the speed in CM/S
    SPEED_CM_MIN = False

    # Set to False to always use pulses (otherwise, it may require a paid option)
    # USE_RELATIVE_JOB = True  # This requires the Relative Job option from Motoman
    USE_RELATIVE_JOB = False

    # Force joint movements to be in Cartesian
    MOVEJ_IN_CARTESIAN = False

    # Generate sub programs with each program
    INCLUDE_SUB_PROGRAMS = True

    # Specify a spare Position register for calculations (Tool, Reference, ...)
    SPARE_PR = 95

    # Set to True to use MFRAME for setting reference frames automatically within the program
    USE_MFRAME = False

    # Specify the default UFrame Id to use (register).
    # You can also use Numbered References in RoboDK (for example, a reference named "Reference 4" will use UFRAME number 4)
    ACTIVE_FRAME = 9

    # Specify if external axes must be moved according to a separate MOVJ command
    # EXTAXES_USE_MOVJ = False # Will output: MOVL C00008 EC00008 V=166.7
    EXTAXES_USE_MOVJ = True    # Will output: MOVL C00008 V=166.7  +MOVJ EC00008 VJ=100.00

    # Specify if we want synchronized motion for Linear moves:
    # if True (default): Will output: SMOVL C00008 BC00008 V=166.7  +MOVJ EC00008              -> Synchronized motion (for the turntable)
    # if False         : Will output: MOVL  C00008 BC00008 V=166.7  +MOVJ EC00008 VJ=100.00    -> Non synchronized motion (for the turntable)
    EXTAXES_USE_SMOVL = False

    # Specify the pulses/degree ratio for the external axes here (index 7,8,...)
    PULSES_X_DEG = [1, 1, 1, 1, 1, 1, 1.0, 1.0, 1, 1]

    # Option to swap a specific axis
    # AXIS_INDEX = [0,1,2,3,4,5,6,7] # example to swap axes 7 and 8 (external axes 1-2)
    AXIS_INDEX = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]    # table is already swaped (final ids)

    # --------------------------------------------------------------------------------------------------------------------
    # Set default acceleration string for linear movements
    STR_A = ""
    
    # Set default acceleration string for joint movements
    STR_AJ = ""
    
    def setSpeed(self, speed_mms):
        """Changes the robot speed (in mm/s)"""
        speedl = 0
        if self.SPEED_CM_MIN:
            speed_cm_min = speed_mms * 60.0 / 10.0
            speedl = max(0.01,min(speed_cm_min,200.0)) # Important! Filter linear speed is in mm/s or cm/min (otherwise the program stops)
        else:
            speedl = max(0.01,min(speed_mms,2000.0)) # Important! Filter linear speed is in mm/s or cm/min (otherwise the program stops)
            
        #if speedl < 100:
        #    self.STR_V = "V=%.2f" % speedl
        #else:
        #    self.STR_V = "V=%.1f" % speedl
        self.STR_V = "V=%.1f" % speedl
        self.STR_V = self.STR_V + self.STR_A

    def setAcceleration(self, accel_mmss):
        """Changes the robot acceleration (in mm/s2)"""
        accel = max(0.0,min(accel_mmss,100.0))
        self.STR_A = ""
        if accel > 0:
            self.STR_A = " ACC=%.1f DEC=%.1f" % (accel, accel)
    
    def setSpeedJoints(self, speed_degs):
        """Changes the robot joint speed (in deg/s)"""
        speedj = max(0.01,min(speed_degs,100.0)) # Joint speed must be in %
        if speedj < 100:
            self.STR_VJ = "VJ=%.2f" % speedj
        else:
            self.STR_VJ = "VJ=%.1f" % speedj
            
        self.STR_VJ = self.STR_VJ + self.STR_AJ
    
    def setAccelerationJoints(self, accel_degss):
        """Changes the robot joint acceleration (in deg/s2)"""
        accel = max(0.0,min(accel_degss,100.0))
        self.STR_AJ = ""
        if accel > 0:
            self.STR_AJ = " ACC=%.1f DEC=%.1f" % (accel, accel)

if __name__== "__main__":
    exec("from v" + version_str + ".Motoman import test_post")
    test_post()

