package loader

import org.lsst.ccs.subsystems.fcs.*
import org.lsst.ccs.subsystems.fcs.common.*
import org.lsst.ccs.subsystems.fcs.drivers.*
import org.lsst.ccs.subsystems.fcs.simulation.*
import org.lsst.ccs.description.groovy.CCSBuilder


//********************************************************************************//
//Loader variables
//********************************************************************************//
def loaderTcpProxyName = "loaderTcpProxy" // the name of the tcpProxy
def loaderPlutoGatewayName = "loaderPlutoGateway" // to monitor the loader sensors
//********************************************************************************//

CCSBuilder builder = ["loader-standalone"]

builder.
    main ( LoaderMainModule, bridge:ref("loaderTcpProxy")
    )
{ //begin description of Main submodules

    /**********************************************************************/
    /*                FAKE AUTOCHANGER DESCRIPTION                       */
    /**********************************************************************/
    //in the loader standalone subsystem the fake autochanger is a switch which
    //simulate that autochanger holds a filter at HANDOFF 
    autochanger(FakeFilterHolder, plutoGatewayName:loaderPlutoGatewayName,
        holdingFilterSensor:ref("autochangerHoldingFilterSensor"))
    {//begin description of fakeAutochanger's children 
    
        autochangerHoldingFilterSensor(DigitalSensor,
            dioName:loaderPlutoGatewayName, inputNumero:3, byteNumero:10)
    }//end description of fakeAutochanger's children
    /**********************************************************************/
    /*                END of FAKE AUTOCHANGER DESCRIPTION                 */
    /**********************************************************************/

    // tcp Proxy
    loaderTcpProxy (CanOpenProxy,
        portNumber:50000,fieldBusTimeout:6000,clientName:"TestBench",
        hardwareBootTimeout:3000) {
        
        // can open devices
        hooksController (CanOpenEPOS,
            nodeID:"1b",serialNB:"79007141",
            paramsForCurrent:[
                ContinuousCurrentLimit:500,
                OutputCurrentLimit:6880,
                MaxSpeedInCurrentMode:750,
                MotorType:10,PolePairNumber:1,
                PositionSensorType:1],
            paramsForProfilePosition:[
                MinPositionLimit:-2147483648,
                MaxPositionLimit:537000,
                MotionProfileType:0],
            paramsForHoming:[HomeOffset:0]) 
                        
        carrierController (CanOpenEPOS,nodeID:"1a",serialNB:"80003184",
            paramsForCurrent:[
                ContinuousCurrentLimit:7290,
                OutputCurrentLimit:14580,
                MaxSpeedInCurrentMode:9840,
                MotorType:10,
                PositionSensorType:4,
                ThermalTimeConstantWinding:187,
                PolePairNumber:1],
            paramsForProfilePosition:[
                MinPositionLimit:-2147483648,
                MaxPositionLimit:1037800,
                MotionProfileType:1,
                MaxFollowingError:2000,
                MaxProfileVelocity:228, 
                ProfileVelocity:200,
                ProfileAcceleration:30,
                ProfileDeceleration:8,
                QuickStopDeceleration:1000],
            paramsForHoming:[
                HomeOffset:500,
                HomingAcceleration:200,
                CurrentThresholdHomingMode:500])
        
        /*
         * QuickStopDeceleration=10000 SpeedForSwitchSearch=10 SpeedForZeroSearch=10 HomingAcceleration=200 CurrentThresholdHomingMode=500 HomePosition=0
         * */
                        
            "$loaderPlutoGatewayName" (CanOpenPlutoGateway, 
            nodeID:"1",serialNB:"22d6")
                            
    }//end description of bridge's childrens
    //----------------------------------------------
    // END Bridge To LOADER description
    //----------------------------------------------
                
    //begin description of loader
    loader (LoaderModule,
        carrier:ref("carrier"),
        clamp:ref("clamp"),
        plutoGatewayName:loaderPlutoGatewayName,
        filterPresenceSensors:ref("loaderFilterPresenceSensors"),
        loaderOnCameraSensors:ref("loaderOnCameraSensors"),
    )
    {//begin description of loader's children

        carrier(LoaderCarrierModule,
            handoffSensors:ref("loaderHandoffSensors"),
            storageSensors:ref("loaderStorageSensors")
        )
        {//begin description of loader carrier's children 
            loaderHandoffSensors (RedondantSensors,
                sensor:ref("loaderHandoffPositionSensor0"), 
                sensorR: ref("loaderHandoffPositionSensor1")
            )
            {//begin description of loader handoffSensors
                loaderHandoffPositionSensor0 ( DigitalSensor ,
                    dioName:loaderPlutoGatewayName,inputNumero:6,byteNumero:3)

                loaderHandoffPositionSensor1 ( DigitalSensor ,
                    dioName:loaderPlutoGatewayName,inputNumero:7,byteNumero:3)
            }//end description of loader handoffSensors
            
            loaderStorageSensors (RedondantSensors,
                sensor:ref("loaderStoragePositionSensor0"),
                sensorR:ref("loaderStoragePositionSensor1"),
            )
            {//begin description of loader storageSensors
                loaderStoragePositionSensor0 ( DigitalSensor ,
                    dioName:loaderPlutoGatewayName, inputNumero:4,byteNumero:3)

                loaderStoragePositionSensor1 ( DigitalSensor ,
                    dioName:loaderPlutoGatewayName, inputNumero:5, byteNumero:3)  
            }//end description of loader storageSensors
        }//end description of loader carrier's children

        clamp(LoaderClampModule,
            hook1:ref("hook1"),hook2:ref("hook2"), hook3:ref("hook3"),hook4:ref("hook4"),
            forceSensor0:ref("forceSensor0"), forceSensor1:ref("forceSensor1"),
            clampedStatusSensor: ref("clampedStatusSensor"),
            )
        {//begin description of loader clamp's children
            int[] inputNumeroClose = [4,5,6,7];
            int[] inputNumeroOpen = [0,1,2,3];
            for (int i = 1; i<5; i++) {
            
            "hook${i}"(LoaderHookModule,
                    closeSensor:ref("loaderCloseSensor${i}"), openSensor:ref("loaderOpenSensor${i}"))
                {
                    "loaderCloseSensor${i}" ( DigitalSensor , //closeHookSensor0
                        dioName:loaderPlutoGatewayName, inputNumero:inputNumeroClose[i-1], byteNumero:2)
                       
                    "loaderOpenSensor${i}" ( DigitalSensor , //openHookSensor0
                        dioName:loaderPlutoGatewayName, inputNumero:inputNumeroOpen[i-1], byteNumero:2)
                }
            }
            
            forceSensor0(ForceSensor, byteNumero:4)
            
            forceSensor1(ForceSensor, byteNumero:8)
            
            forceSensorStatus0 ( DigitalSensor ,
                dioName:loaderPlutoGatewayName,inputNumero:2,byteNumero:7)
            
            forceSensorStatus1 ( DigitalSensor ,
                dioName:loaderPlutoGatewayName,inputNumero:3,byteNumero:7)
            
            clampedStatusSensor ( DigitalSensor ,
                dioName:loaderPlutoGatewayName,inputNumero:4,byteNumero:7)
        
        }//end description of loader clamp's children

       
        loaderFilterPresenceSensors (RedondantSensors,
            sensor:ref("loaderFilterPresenceSensor0"),
            sensorR:ref("loaderFilterPresenceSensor1"),
        )
        {
            loaderFilterPresenceSensor0(DigitalSensor ,
                dioName:loaderPlutoGatewayName,inputNumero:0, byteNumero:3)

            loaderFilterPresenceSensor1(DigitalSensor ,
                dioName:loaderPlutoGatewayName, inputNumero:1, byteNumero:3) 
        }

        loaderOnCameraSensors (RedondantSensors,
            sensor:ref("loaderOnCameraSensor0"),
            sensorR:ref("loaderOnCameraSensor1"),
        )
        {
            loaderOnCameraSensor0(DigitalSensor ,
                dioName:loaderPlutoGatewayName, inputNumero:2, byteNumero:3)

            loaderOnCameraSensor1(DigitalSensor ,
                dioName:loaderPlutoGatewayName, inputNumero:3, byteNumero:3)
        }
            
    }//end description of loader's children
    
} //end description of LoaderMainModule
                


