Esempio n. 1
0
    /// <summary>
    /// Create a Drone gameobject and attach DroneFlightSim, required ROSDroneConnnection and initialize the ROS connection.
    /// </summary>
    /// <param name="rosDroneConnectionInput"></param>
    private void InstantiateDrone(ROSDroneConnectionInput rosDroneConnectionInput)
    {
        // All the variables required to create the drone
        DroneType     droneType        = rosDroneConnectionInput.droneType;
        string        droneIP          = rosDroneConnectionInput.url;
        int           dronePort        = rosDroneConnectionInput.port;
        bool          simFlight        = rosDroneConnectionInput.simFlight;
        List <string> droneSubscribers = new List <string>();

        foreach (DroneSubscribers subscriber in rosDroneConnectionInput.droneSubscribers)
        {
            droneSubscribers.Add(subscriber.ToString());
        }

        // Create a new drone
        Drone droneInstance = new Drone(WorldProperties.worldObject.transform.position, uniqueID);

        Debug.Log("Drone that was just made " + droneInstance.gameObjectPointer.name);
        DroneProperties droneProperties = droneInstance.droneProperties;
        GameObject      droneGameObject = droneInstance.gameObjectPointer;

        droneGameObject.name = rosDroneConnectionInput.droneName;

        ROSDroneConnectionInterface rosDroneConnection = null;

        // Add corresponding ros drone connection script
        switch (droneType)
        {
        case DroneType.M100:
            Debug.Log("M100 created");
            M100_ROSDroneConnection M100_rosDroneConnection = droneGameObject.AddComponent <M100_ROSDroneConnection>();
            M100_rosDroneConnection.InitilizeDrone(uniqueID, droneIP, dronePort, droneSubscribers, simFlight, droneProperties);
            rosDroneConnection = M100_rosDroneConnection;
            droneGameObject.GetComponent <DroneProperties>().droneROSConnection = M100_rosDroneConnection;
            ROSDroneConnections.Add(uniqueID, M100_rosDroneConnection);
            break;

        case DroneType.M210:
            Debug.Log("M210 created");
            M210_ROSDroneConnection M210_rosDroneConnection = droneGameObject.AddComponent <M210_ROSDroneConnection>();
            M210_rosDroneConnection.InitilizeDrone(uniqueID, droneIP, dronePort, droneSubscribers, simFlight, droneProperties);
            rosDroneConnection = M210_rosDroneConnection;
            droneGameObject.GetComponent <DroneProperties>().droneROSConnection = M210_rosDroneConnection;
            ROSDroneConnections.Add(uniqueID, M210_rosDroneConnection);
            break;

        case DroneType.M600:
            Debug.Log("M600 created");
            M600_ROSDroneConnection M600_rosDroneConnection = droneGameObject.AddComponent <M600_ROSDroneConnection>();
            M600_rosDroneConnection.InitilizeDrone(uniqueID, droneIP, dronePort, droneSubscribers, simFlight, droneProperties);
            rosDroneConnection = M600_rosDroneConnection;
            droneGameObject.GetComponent <DroneProperties>().droneROSConnection = M600_rosDroneConnection;
            ROSDroneConnections.Add(uniqueID, M600_rosDroneConnection);
            break;

        case DroneType.Sprite:
            Debug.Log("Sprite class not implemented created");
            //Sprite_ROSDroneConnection drone_rosDroneConnection = drone.AddComponent<Sprite_ROSDroneConnection>();
            break;

        default:
            Debug.Log("No drone type selected");
            return;
        }

        // Create attached sensors
        foreach (ROSSensorConnectionInput rosSensorInput in rosDroneConnectionInput.attachedSensors)
        {
            ROSSensorConnectionInterface sensor = InstantiateSensor(rosSensorInput);
            droneInstance.AddSensor(sensor);
        }

        // Initilize drone sim manager script on the drone
        DroneSimulationManager droneSim = droneGameObject.GetComponent <DroneSimulationManager>();

        droneSim.InitDroneSim();
        droneProperties.droneSimulationManager = droneSim;

        // Get DroneMenu and instansiate.
        DroneMenu droneMenu = droneGameObject.GetComponent <DroneMenu>();

        droneMenu.InitDroneMenu(rosDroneConnection, droneSubscribers);
        droneGameObject.GetComponent <DroneProperties>().droneMenu = droneMenu;

        uniqueID++;
    }