/*******************************************************************************/
/*        Description file for the carousel in standalone mode                 */
/*                                                                             */
/*******************************************************************************/


package carouselStandalone

import org.lsst.ccs.subsystems.fcs.*
import org.lsst.ccs.subsystems.fcs.singlefiltertest.*
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.subsystems.fcs.testbench.*
import org.lsst.ccs.drivers.canopenjni.*
import org.lsst.ccs.drivers.canopenjni.rmi.*
import org.lsst.ccs.description.groovy.CCSBuilder

/* 
 * carousel positions when a socket is at STANDBY
 * example : socket 1 is at STANDBY when carousel position is -204200
 * */
def standbyPositionSocket = [-204200,-1075906,-1948470,-2821074,-3694258] as int[]

/* numOfAnalogInput on the CANopen device where we read the clamps sensors*/
def numOfAnalogInputFilterPresenceSensorXminus = [1, 2, 3, 4, 5] as int[]
def numOfAnalogInputLockSensorXminus = [6, 7, 8, 9, 10] as int[]
def numOfAnalogInputFilterPresenceSensorXplus = [11, 12, 13, 14, 15] as int[]
def numOfAnalogInputLockSensorXplus = [16, 17, 18, 19, 20] as int[]

//Carousel Can Open Devices (CAN-CBX-AI814) for Carousel brakes status + brakes temperatures
def adcName = "ai814"

//Carousel CAN OPEN device to read carousel clamp sensors
def caTTC580Name = "hyttc580"

//Carousel clamps controllers name
def clampXminusControllerName = "clampXminusController" //controller to unlock clamp on the side Xminus
def clampXplusControllerName =  "clampXplusController" //controller to unlock clamp on the side Xplus

def plutoGatewayName = "$caTTC580Name" // to monitor the fake autochanger sensors

CCSBuilder builder = ["carousel-standalone"]

builder.
    main ( CarouselMain, bridge:ref("tcpProxy"), autochanger:ref("autochanger")) 
{ //begin description of CarouselMain

    tcpProxy (CanOpenProxy, hardwareBootTimeout:2500) {
        
        canInterface(CanOpenCommands, coi:new CanOpenRMIClient("134.158.154.195", "canbus0"))  
        
        "$clampXminusControllerName" (CanOpenEPOSCarouselClamp,
            nodeID:0x1a, serialNB:"79003900",
            paramsForCurrent: [
                MotorType:11, 
                PositionSensorType:3,
                PolePairNumber:8,
                ContinuousCurrentLimit:2360,
                OutputCurrentLimit:4720,
                ThermalTimeConstantWinding:160,
                MaxSpeedInCurrentMode:25],
            paramsForProfilePosition: [MotionProfileType:0],
            paramsForHoming:[HomeOffset:0])
        
        "$clampXplusControllerName" (CanOpenEPOSCarouselClamp,
            nodeID:0x1b, serialNB:"79020968",
            paramsForCurrent: [
                MotorType:11, 
                PositionSensorType:3,
                PolePairNumber:8,
                ContinuousCurrentLimit:2360,
                OutputCurrentLimit:4720,
                ThermalTimeConstantWinding:160,
                MaxSpeedInCurrentMode:25],
            paramsForProfilePosition: [MotionProfileType:0],
            paramsForHoming:[HomeOffset:0])
        
        carouselController (CanOpenEPOSCarousel,
            nodeID:0xc, serialNB:"80003117",
            paramsForCurrent:[
                ContinuousCurrentLimit:9375,
                OutputCurrentLimit:12500,
                MaxSpeedInCurrentMode:5000,
                MotorType:11,PolePairNumber:12,
                PositionSensorType:8],
            paramsForProfilePosition: [
                MinPositionLimit:-2147483648,
                MaxPositionLimit:2147483647,
                MotionProfileType:1, /*0:trapezoidal, 1:sinusoidal*/
                ProfileAcceleration:2000,
                ProfileDeceleration:2000,
                MaxProfileVelocity:5371,
                QuickStopDeceleration:10000],
            paramsForHoming:[HomeOffset:0])
        
        // can open devices to read clamps sensors
        "$caTTC580Name" (CanOpenTTC580, nodeID:0x3, serialNB:"25008180")
        
        // can open devices to read brakes sensors and temperatures
        "$adcName" (CanCBXAI814, nodeID:0x1e, serialNB:"c7ca0645", transmissionType:1)
        
        // can open devices to read brakes sensors and temperatures
        pt100 (CanOpenDevice, nodeID:0x1f, serialNB:"c7c50149")  

    } //end of carousel's bridge hardware
    //**************************************************************************//
    // end of Bridge
    //**************************************************************************//
    //-------------------------
    // BEGIN Filter Manager description
    filterManager(FilterManager)
    {//begin description of filter manager's children
        filterU (Filter,filterID:29, weight:38)
        filterG (Filter,filterID:3, weight:33)
        filterR (Filter,filterID:60, weight:30)
        filterI (Filter,filterID:34, weight:28)
        filterZ (Filter,filterID:17, weight:28)
        filterY (Filter,filterID:15, weight:22.5)
        dummyFilter (Filter,filterID:46,weight:44.5)
    }//end description of filter manager's children
    // END Filter Manager description
    // ------------------------------
    


    /**********************************************************************/
    /*                BEGIN OF CAROUSEL DESCRIPTION                       */
    /**********************************************************************/

    //----------------------------
    // BEGIN Carousel description
    carousel(Carousel)
    {//begin description of carousel's children
        /**********************************************************************/
        /*SOCKETi*/
        for (int i = 1; i<6;i++ ) {
            "socket${i}" ( CarouselSocket,
                id:i,
                clampXminus:ref("clampXminus${i}"),
                clampXplus:ref("clampXplus${i}"),
                standbyPosition: standbyPositionSocket[i-1],
                slaveModuleSensor:ref("slaveModuleSensor${i}")
            )
            {   
                "slaveModuleSensor${i}" (CarouselSensor, deviceName: caTTC580Name)
                //-------------------------
                // BEGIN clampXminus description
                "clampXminus${i}" (CarouselClamp,
                    filterPresenceSensor:ref("filterPresenceXminus${i}"),
                    lockSensor:ref("lockSensorXminus${i}"),
//                    thermometer:ref("thermometerXminus${i}")
                ) 
                {
                    "filterPresenceXminus${i}" (CarouselClampSensor, deviceName: caTTC580Name)
                    
                    "lockSensorXminus${i}" (CarouselClampSensor, deviceName: caTTC580Name) 
                    
//                    "thermometerXminus${i}" (Thermometer)
                }
                //-------------------------
                // END clampXminus description
                
                //-------------------------
                // BEGIN clampXplus description
                "clampXplus${i}" (CarouselClamp,
                    filterPresenceSensor:ref("filterPresenceXplus${i}"),
                    lockSensor:ref("lockSensorXplus${i}"),
//                    thermometer:ref("thermometerXplus${i}")
                )
                {
                    "filterPresenceXplus${i}" (CarouselClampSensor, deviceName:caTTC580Name)
                    "lockSensorXplus${i}" (CarouselClampSensor, deviceName:caTTC580Name)
//                    "thermometerXplus${i}" (Thermometer)
                }
                //-------------------------
                // END clampXplus description
            } // END carouselSocket /*SOCKETi*/ description
        } // END of loop of carouselSocket description.
        
        acAF3 (ComplementarySensors,
            sensor:ref("acAF3s"),
            sensorC:ref("acAF3b"),
        ) {
            //TODO replace that by good values
            acAF3s (DigitalSensor,
                dioName:caTTC580Name, inputNumero:6, byteNumero:11)
            acAF3b (DigitalSensor,
                dioName:caTTC580Name, inputNumero:7, byteNumero:11)
        }
    }//end description of carousel's children
    //END Carousel description
    // ------------------------
    /**********************************************************************/
    /*                FAKE AUTOCHANGER DESCRIPTION                       */
    /**********************************************************************/
    //in the carousel standalone subsystem the fake autochanger is a switch which
    //simulate that autochanger holds a filter at STANDBY 
    
    autochanger(FakeFilterHolder, holdingFilterSensorName:"acAF3")


    /**********************************************************************/
    /*                END of FAKE AUTOCHANGER DESCRIPTION                 */
    /**********************************************************************/
} // end of CarouselMain description
