*-----------------------------------------------------------------------------
*  Energy Option
*-----------------------------------------------------------------------------
*  Module Description
*    By this feature the energy efficiency of a machine tool can be improved.
*    Units currently not in use are set to standby or switched off.
*
*-----------------------------------------------------------------------------


; Lokale Definitionen   ML_Spawn_in_process

#define /s    ML_energy_option_initialized             M
#define /s    ML_axes_in_position                      M
#define /s    ML_spindle_stand_still_temp              M
#define /s    ML_machine_in_position                   M
#define /s    ML_energy_opt_release                    M
#define /s    ML_energy_opt_release_temp               M
#define /s    ML_energy_opt_release_pulse              M
#define /s    ML_autostart_test                        M

#define /s    KL_shutdown_mode_defined_in_MP4040       K +2
#define /s    KL_format_time_h_m                       K +15
#define /s    KL_mode_synchronous                      K +0
#define /s    KL_mode_asynchronous                     K +1
#define /s    KL_memory_10_MB                          K +10
#define /s    KL_terminate_python_process              K +0
#define /s    KL_python_process_active                 K +0
#define /s    KL_kill_python_process                   K +1

#define /s    BL_identifier_init                       B
#define /s    BL_identifier_warmup                     B
#define /s    BL_return_value                          B

#define /s    DL_axes_in_clamping_mode                 D
#define /s    DL_error_energy_opt                      D
#define /s    DL_exit_energy_opt                       D

#define /s    KS_PY_energy_opt                         S"PLC:\\PYTHON\\Energy_Option.PY"


#include Mac_Lib.inc

GLOBAL Energy_Option

;----------------------------
LBL Energy_Option
;----------------------------

  LN     ML_energy_option_initialized
  A      MG_machine_parameter_are_read
  IFT

    RPLY   BL_identifier_init
    ==     K+0
    IFT
      SUBM   Energy_opt_init
      =      BL_identifier_init
      ==     K+0
      S      PN_error_submit_spawn_queue_full

    ENDI
  ENDI

; Common release

  L      MG_energy_opt_on
  AN     MG_energy_opt_edit_on
  A      MG_control_operational
  AN     MG_control_in_operation_on
  A      MG_TNC_initialized
  AN     NP_M4173_1st_PLC_run_after_compile
  A      MG_reference_axes_traversed
  A      MG_machine_parameter_are_read
  =      ML_energy_opt_release

  L      ML_energy_opt_release
  AN     ML_energy_opt_release_temp
  =      ML_energy_opt_release_pulse

  L      ML_energy_opt_release
  =      ML_energy_opt_release_temp

; Autostart

  L      MG_pulse_1_min
  O      ML_autostart_test
  IFT
    L      STG_energy_opt_autostart.Day_of_the_week
    BT     DG_day_of_the_week
    O      ML_autostart_test
    IFT
      L      SG_Time_H_M
      ==     SG_energy_opt_time_on
      O      ML_autostart_test
      IFT

        L      MG_marker_one
        R      PN_error_energy_opt_standby_machine

        L      STG_energy_opt_autostart.Light
        AN     O_working_light
        S      MG_energy_opt_light_on

        L      PN_error_energy_opt_autostart
        A      STG_energy_opt_autostart.Machine
        A      I_emergency_button_unlocked
        AN     MG_guard_open
        AN     NP_M4660_HR5x0_active
        AN     NP_M4151_mode_electronic_handwheel
        S      MG_energy_opt_power_on

        L      MG_energy_opt_power_on
        A      STG_energy_opt_autostart.Drives
        S      MG_energy_opt_drive_on

        L      MG_energy_opt_drive_on
        A      STG_energy_opt_autostart.Warm_up_program
        A      MG_automatic_mode

        IFT
          RPLY   BL_identifier_warmup
          ==     K+0
          IFT
            SUBM   Warmup_pgm_select
            =      BL_identifier_warmup
            ==     K+0
            S      PN_error_submit_spawn_queue_full

          ENDI
        ENDI

        L      ML_autostart_test
        R      ML_autostart_test
      ENDI
    ENDI
  ENDI

  L      MG_energy_opt_power_on
  O      MG_energy_opt_drive_on
  O      MG_energy_opt_light_on
  O      MG_energy_opt_warmup_pgm_on
  IFT

    ; Machine
    L      I_ESA_control_operational
    R      MG_energy_opt_power_on

    ; Drives
    L      MG_energy_opt_drive_on
    A      NP_M4178_external_emergency_stop_active
    A      I_ESA_control_operational
    IFT
      PS     KG_key_CE
      CM     9180
      PLX

    M_display_module_error

      L      MG_energy_opt_drive_on
      R      MG_energy_opt_drive_on
    ENDI

    ; Light
    L      O_working_light
    R      MG_energy_opt_light_on

    ; Warmup pgm
    L      MG_control_in_operation_on
    A      I_ESA_control_operational
    R      MG_energy_opt_warmup_pgm_on

  ENDI

  L      MG_n_pulse_control_operational
  R      MG_energy_opt_warmup_pgm_on

  LN     I_ESA_control_operational
  A      MG_reference_axes_traversed
  AN     MG_guard_open
  A      MG_reset_init_macro_executed
  A      I_emergency_button_unlocked
  A      STG_energy_opt_autostart.Machine
  A      MG_energy_opt_on
  AN     MG_energy_opt_edit_on
  =      PN_error_energy_opt_autostart
  =      MG_win_energy_opt_autostart

; Evaluation axes

  * Timer start
  LN     NP_D1104_axes_in_position
  ==     K+0
  AN     ML_axes_in_position
  IFT
    L      STG_energy_opt_axes.M_on
    IFT
      PS     K &TR_energy_opt_axes
      PS     STG_energy_opt_axes.D_time
      CM     9006
      M_display_module_error
    ENDI
  ENDI

  LN     NP_D1104_axes_in_position
  ==     K+0
  A      ML_energy_opt_release
  =      ML_axes_in_position
  IFT
    LN     TR_energy_opt_axes
    S      STG_energy_opt_axes.M_active
  ELSE
    L      STG_energy_opt_axes.M_active
    R      STG_energy_opt_axes.M_active
  ENDI

  * Activation
  L      STG_energy_opt_axes.M_active
  AN     STG_energy_opt_axes.M_active_temp
  IFT
    L      STG_energy_opt_axes.D_filter
    A      NP_D1100_axes_release
    =      DL_axes_in_clamping_mode
    =      DG_clamping_mode_on_external
  ENDI

  * Deactivation
  LN     STG_energy_opt_axes.M_active
  A      STG_energy_opt_axes.M_active_temp
  O      ML_energy_opt_release_pulse
  IFT
    L      DL_axes_in_clamping_mode
    =      DG_clamping_mode_off_external
  ENDI

  * temp for pulse creation
  L      STG_energy_opt_axes.M_active
  =      STG_energy_opt_axes.M_active_temp

; Evaluation spindle

  * Timer start
  L      MG_spindle_stand_still
  AN     ML_spindle_stand_still_temp
  IFT
    L      STG_energy_opt_spindle.M_on
    IFT
      PS     K &TR_energy_opt_spindle
      PS     STG_energy_opt_spindle.D_time
      CM     9006
      M_display_module_error
    ENDI
  ENDI

  L      MG_spindle_stand_still
  A      ML_energy_opt_release
  AN     MG_spindle_C_axis_mode_active
  AN     MG_spindle_orientation_active
  A      ML_energy_opt_release
  =      ML_spindle_stand_still_temp
  IFT
    LN     TR_energy_opt_spindle
    S      STG_energy_opt_spindle.M_active
  ELSE
    L      STG_energy_opt_spindle.M_active
    R      STG_energy_opt_spindle.M_active
  ENDI

  * Activation
  L      STG_energy_opt_spindle.M_active
  AN     STG_energy_opt_spindle.M_active_temp
  IFT
    * nb
  ENDI

  * Deactivation
  LN     STG_energy_opt_spindle.M_active
  A      STG_energy_opt_spindle.M_active_temp
  IFT
    * nb
  ENDI

  * temp for pulse creation
  L      STG_energy_opt_spindle.M_active
  =      STG_energy_opt_spindle.M_active_temp

; Evaluation machine

  LN     NP_D1104_axes_in_position
  ==     K+0
  A      MG_spindle_stand_still
  AN     ML_machine_in_position
  A      ML_energy_opt_release
  IFT
    L      STG_energy_opt_machine.M_on
    IFT
      PS     K &TR_energy_opt_machine
      PS     STG_energy_opt_machine.D_time
      CM     9006
      M_display_module_error
    ENDI

    L      STG_energy_opt_control.M_on
    IFT
      PS     K &TR_energy_opt_control
      PS     STG_energy_opt_control.D_time
      CM     9006
      M_display_module_error
    ENDI

    L      STG_energy_opt_light.M_on
    IFT
      PS     K &TR_energy_opt_light
      PS     STG_energy_opt_light.D_time
      CM     9006
      M_display_module_error
    ENDI

    L      STG_energy_opt_air.M_on
    IFT
      PS     K &TR_energy_opt_air
      PS     STG_energy_opt_air.D_time
      CM     9006
      M_display_module_error
    ENDI

    L      STG_energy_opt_oil.M_on
    IFT
      PS     K &TR_energy_opt_oil
      PS     STG_energy_opt_oil.D_time
      CM     9006
      M_display_module_error
    ENDI

  ENDI

  LN     NP_D1104_axes_in_position
  ==     K+0
  A      MG_spindle_stand_still
  A      ML_energy_opt_release
  =      ML_machine_in_position
  IFT
    LN     TR_energy_opt_machine
    S      STG_energy_opt_machine.M_active
    S      PN_error_energy_opt_standby_machine

    LN     TR_energy_opt_control
    S      STG_energy_opt_control.M_active

    LN     TR_energy_opt_light
    A      O_working_light
    S      STG_energy_opt_light.M_active

    LN     TR_energy_opt_air
    S      STG_energy_opt_air.M_active

    LN     TR_energy_opt_oil
    S      STG_energy_opt_oil.M_active

  ELSE
    L      MG_marker_one
    R      STG_energy_opt_machine.M_active
    R      STG_energy_opt_control.M_active
    R      STG_energy_opt_light.M_active
    R      STG_energy_opt_air.M_active
    R      STG_energy_opt_oil.M_active

  ENDI

  LN     O_working_light
  R      STG_energy_opt_light.M_active

; Timer set

  L      MG_marker_one
  S      TR_energy_opt_axes
  S      TR_energy_opt_spindle
  S      TR_energy_opt_machine
  S      TR_energy_opt_control
  S      TR_energy_opt_light
  S      TR_energy_opt_air
  S      TR_energy_opt_oil

EM

;----------------------------
LBL Warmup_pgm_select

  PS     K &SG_energy_opt_program
  CM     9290

  LN     NP_M4203_error_module_9xxx
  S      MG_energy_opt_warmup_pgm_on

EM

;----------------------------
LBL Energy_opt_init

  PS     S"Energy_opt"
  CM     9297                          ;Status of a Python instance
  PLW
  ==     KL_python_process_active
  IFF
    PS     KL_mode_asynchronous
    PS     KL_memory_10_MB
    PS     KS_PY_energy_opt
    PS     S"Energy_opt"
    PS     S"INIT"
    CM     9295                        ;Start python process
    PL     DL_error_energy_opt
    PL     DL_exit_energy_opt

    M_display_module_error

    LN     ML_energy_option_initialized
    S      ML_energy_option_initialized
  ENDI

EM

