// see documentation on single-filter-test on : https://confluence.slac.stanford.edu/display/LSSTCAM/Single+Filter+Test
package sftSimulation

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.description.groovy.CCSBuilder



//Carousel Can Open Devices (CAN-CBX-AI814)
def adcName = "ai814"

//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

//Autochanger Can Open Devices
def dacName = "ao412"  // to control the latches actuator
def railsSensorsDIOName = "dio8n1"  // to control the trucks and monitor the rails sensors
def filterSensorsDIOName = "dio8n2" // to monitor the filter sensors

CCSBuilder builder = ["single-filter-test-simu"]

builder.
    main ( SftMain, bridge:ref("tcpProxy")) {

    tcpProxy (CanOpenProxy, hardwareBootTimeout:2500) {
        
        canInterface(SimuCanOpenInterface)
        
        ai814 (SimuCanOpenADC, 
            nodeID:0x21,serialNB:"c7c40234",transmissionType:1)

        ao412 (CanOpenDevice,
            nodeID:0x22,serialNB:"c7c60307")            
            
        dio8n1 ( SimuCompactIO , 
            //name: railsSensorsDIOName) ,
            nodeID: 0x23, serialNB: "c7ca0914",initialValue:0x50)
                                    
        dio8n2 ( SimuCompactIO , 
            //name: filterSensorsDIOName) ,
            nodeID: 0x24,serialNB: "c7ca0913",initialValue:0x44)
        
        clampXminusController (SimuCarouselClampController,    
            //name: clampXminusControllerName),
            nodeID:0x1a,serialNB:"79003317",
            paramsForCurrent:[MotorType:11, PositionSensorType:3,PolePairNumber:8,
                ContinuousCurrentLimit:2360,OutputCurrentLimit:4720,
                ThermalTimeConstantWinding:160,
                MaxSpeedInCurrentMode:25],
            paramsForProfilePosition:[MotionProfileType:0],
            paramsForHoming:[HomeOffset:0])  

        clampXplusController (SimuCarouselClampController,
            //name: clampXplusControllerName),
            nodeID:0x1b,
            serialNB:"79003313",
            paramsForCurrent:[MotorType:11, PositionSensorType:3,PolePairNumber:8,
                ContinuousCurrentLimit:2360,OutputCurrentLimit:4720,
                ThermalTimeConstantWinding:160,
                MaxSpeedInCurrentMode:25],
            paramsForProfilePosition:[MotionProfileType:0],
            paramsForHoming:[HomeOffset:0])
    }

    dummyFilter (Filter, filterID:10,weight:44,socketName:"socket1") 
    
    //----------------------------
    // BEGIN Carousel description
    carousel (SftCarousel) {//begin description of carousel's children
        socket1 ( CarouselSocket,
            clampXminus:ref("clampXminus"),
            clampXplus:ref("clampXplus"),
            standbyPosition:0)
        {   
            //-------------------------
            // BEGIN clampXminus description
            clampXminus (CarouselClamp,
                
                filterPresenceSensor:ref("filterPresenceXminus"),
                lockSensor:ref("lockSensorXminus"),
//                thermometer:ref("thermometerXminus")
            )
            {
                filterPresenceXminus (SimuCarouselClampFilterPresenceSensor,
                    adc:ref("$adcName"),numOfAnalogInput:5)
                
                lockSensorXminus (SimuCarouselClampLockSensor,
                    adc:ref("$adcName"),numOfAnalogInput:7)
                
//                thermometerXminus (SimuThermometer)

            }
            //-------------------------
            // END clampXminus description
            
            //-------------------------
            // BEGIN clampXplus description
            clampXplus (CarouselClamp,
                filterPresenceSensor:ref("filterPresenceXplus"),
                lockSensor:ref("lockSensorXplus"),
//                thermometer:ref("thermometerXplus")
            )
            {
                filterPresenceXplus (SimuCarouselClampFilterPresenceSensor,
                    adc:ref("$adcName"),numOfAnalogInput:1)

                lockSensorXplus (SimuCarouselClampLockSensor,
                    adc:ref("$adcName"),numOfAnalogInput:3)

//                thermometerXplus (SimuThermometer)

            }
            //-------------------------
            // END clampXplus description
            
        } // END carouselSocket description
        
    }//end description of carousel's children
    //END Carousel description
    // ------------------------
    
    // ------------------------
    //BEGIN AUTO CHANGER
    autochanger (SftAutoChanger,
        
        latchXminus:ref("latchXminus"),
        latchXplus:ref("latchXplus"),
        truckXminus:ref("truckXminus"),
        truckXplus:ref("truckXplus"),
        railsSensorsDIOName:railsSensorsDIOName,
        filterSensorsDIOName:filterSensorsDIOName,
        sftTrucksMotor:ref("sftTrucksMotor"))

            {//begin description of autochanger's children

                // ------------------------
                //BEGIN  SFT TRUCKS MOTOR (AUTOCHANGER)
                sftTrucksMotor(SimuTruckMotor)


                //END  SFT TRUCKS MOTOR
                // ------------------------
                // ------------------------
                //BEGIN  latchMinux    (AUTOCHANGER)
                latchXminus (SftFilterLatch,
                            filterSensorsDIO:ref("$filterSensorsDIOName"),
                            lockSensor:ref("lockSensorLatchXminus"),
                            unlockSensor:ref("unlockSensorLatchXminus"),
                            filterPresenceSensor:ref("filterPresenceSensorLatchXminus"),
                            latchActuator:ref("latchActuatorXminus"))
                    {
                        lockSensorLatchXminus ( DigitalSensor ,
                            dioName:filterSensorsDIOName,inputNumero:1)

                        unlockSensorLatchXminus ( DigitalSensor ,
                            dioName:filterSensorsDIOName,inputNumero:2)

                        filterPresenceSensorLatchXminus ( DigitalSensor ,
                                dioName:filterSensorsDIOName,inputNumero:0)

                        latchActuatorXminus (SimuLatchActuator,
                            simuDIO:ref("$filterSensorsDIOName"),
                                currentLimitInVolt:0.150,
                                speedVoltageInVolt:4,
                                speedVoltageMaxValue:10,
                                speedVoltageMinValue:-10)
                    }
                    // ------------------------
                    //END  latchXminus    (AUTOCHANGER)

                    // ------------------------
                    //BEGIN  latchXplus    (AUTOCHANGER)
                    latchXplus (SftFilterLatch,
                        filterSensorsDIO:ref("$filterSensorsDIOName"),
                        lockSensor:ref("lockSensorLatchXplus"),
                        unlockSensor:ref("unlockSensorLatchXplus"),
                        filterPresenceSensor:ref("filterPresenceSensorLatchXplus"),
                        latchActuator:ref("latchActuatorXplus"))
                    {
                        lockSensorLatchXplus ( DigitalSensor ,
                            dioName:filterSensorsDIOName,inputNumero:5)
                        unlockSensorLatchXplus ( DigitalSensor ,
                            dioName:filterSensorsDIOName,inputNumero:6)
                        filterPresenceSensorLatchXplus ( DigitalSensor ,
                            dioName:filterSensorsDIOName,inputNumero:4)
                        latchActuatorXplus (SimuLatchActuator,
                            simuDIO:ref("$filterSensorsDIOName"),
                            currentLimitInVolt: 0.150,
                            speedVoltageInVolt: 4,
                            speedVoltageMinValue: -10,
                            speedVoltageMaxValue: 10)
                    }
                            //END  LATCH   (AUTOCHANGER)
                            // ------------------------

                    // ------------------------
                    //BEGIN  TRUCK   (AUTOCHANGER)
                    truckXminus (Truck ,
                        railSensorStandback:ref("railSensorStandbackXminus"),
                        railSensorStandby:ref("railSensorStandbyXminus"))
                    {
                        railSensorStandbackXminus (  DigitalSensor ,
                            dioName:railsSensorsDIOName,
                            inputNumero: 4)
                        railSensorStandbyXminus (  DigitalSensor ,
                            dioName:railsSensorsDIOName,inputNumero: 5)
                    }
                    truckXplus (Truck ,
                        railSensorStandback:ref("railSensorStandbackXplus"),
                        railSensorStandby:ref("railSensorStandbyXplus"))
                    {
                        railSensorStandbackXplus (  DigitalSensor ,
                            dioName:railsSensorsDIOName,inputNumero: 6)

                        railSensorStandbyXplus (  DigitalSensor ,
                            dioName:railsSensorsDIOName,inputNumero: 7)
                    }
                    //END  TRUCK   (AUTOCHANGER)
                    // ------------------------


            }//END AUTO CHANGER
             // ------------------------ 
                      




        //end description of Main submodules
        }

                


