Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unity settings file #2303

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Unity/UnityDemo/Assets/AirSimAssets/Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

183 changes: 183 additions & 0 deletions Unity/UnityDemo/Assets/AirSimAssets/Scripts/HUD/AirSimHUDScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,40 @@ namespace AirSimUnity {
*/

public class AirSimHUDScript : MonoBehaviour {
public GameObject VehiclePrefab;
public SimMode simModeSelected;

private readonly GameObject[] cameraViews = new GameObject[3];
private bool isViewCamerasSet;

private void Awake() {
//Needs to be the first initialization in the Simulation if not done.
if (AirSimSettings.GetSettings() == null)
{
Debug.Log("AirSimSettings not initialized.");
AirSimSettings.Initialize();
if(AirSimSettings.GetSettings().SimMode != "")
{
AirSimSettings.GetSettings().ValidateSettingsForSimMode();
}
else
{
Debug.Log("Notice: SimMode was not specified in 'settings.json' file. " +
"Using what was specified in AirSimHud game object property. for this scene.");
if (simModeSelected == SimMode.Car)
{
AirSimSettings.GetSettings().SimMode = "Car";
}
else if (simModeSelected == SimMode.Multirotor)
{
AirSimSettings.GetSettings().SimMode = "Multirotor";
}
AirSimSettings.GetSettings().ValidateSettingsForSimMode();
}
}

var simMode = AirSimSettings.GetSettings().SimMode;
InstantiateVehicle(simMode);
}

private void Start() {
Expand Down Expand Up @@ -52,6 +77,163 @@ private void LateUpdate() {
}
}

/// <summary>
/// Will instantiate vehicle prefab(s) (currently only one as multiple vehicles not yet implemented)
/// based on SimMode. If SimMode is multirotor, we check for SimpleFlight first, then PX4 and instantiate
/// the vehicle(s) if AutoCreate == true.
/// The VehicleName (from settings.json) is assigned to each Vehicle object and that name is what is used
/// by VehicleCompanion when to setting the vehicles companion name (which is used along with port number to
/// interact with AirSim lib.)
/// Also, we can use any settings pulled from the settings.json file to modify the initial environment or
/// camera, or vehicle pose. (As example the initial pose is set below)
/// </summary>
/// <param name="simmode"></param>
private void InstantiateVehicle(string simmode)
{
if (simmode == "Car")
{
bool carCreated = false;
foreach (var car in AirSimSettings.GetSettings().Vehicles.Vehicles_PhysXCar)
{
if (car.AutoCreate == true)
{
carCreated = true;
GameObject go = Instantiate(VehiclePrefab) as GameObject;
if (go.GetComponent<Vehicle>() != null)
{
if (go.GetComponent<Vehicle>().SetVehicleName(car.VehicleName))
{
go.name = car.VehicleName;
if (!car.Position.HasNan())
{
Vector3 pos = new Vector3(car.Position.X, car.Position.Y, car.Position.Z);
go.transform.position = pos;
}
if (!car.Rotation.HasNan())
{
Vector3 rot = new Vector3(car.Rotation.Pitch, car.Rotation.Yaw, car.Rotation.Roll);
go.transform.eulerAngles = rot;
}
}
else
{
Debug.Log("Notice: AirSimHUD could not set vehicle name.");
Destroy(go);
}
}
else
{
Debug.Log("Notice: Did not find GetComponet<Vehicle> for this prefab.");
Destroy(go);
}
}
if (carCreated)
{
//Currently multiple vehicles not supported. Only allowing one vehicle to be created.
break;
}
}
}
else if (simmode == "Multirotor")
{
bool simpleFlightCreated = false;
bool px4Created = false;
bool computerVisionCreated = false;
foreach (var sfDrone in AirSimSettings.GetSettings().Vehicles.Vehicles_SimpleFlight)
{
if (sfDrone.AutoCreate == true)
{
simpleFlightCreated = true;
GameObject go = Instantiate(VehiclePrefab) as GameObject;
if (go.GetComponent<Vehicle>() != null)
{
if (go.GetComponent<Vehicle>().SetVehicleName(sfDrone.VehicleName))
{
go.name = sfDrone.VehicleName;
if (!sfDrone.Position.HasNan())
{
Vector3 position = Vector3.zero;
DataManager.SetToUnity(new AirSimVector(sfDrone.Position.X, sfDrone.Position.Y, sfDrone.Position.Z), ref position);
//Vector3 pos = new Vector3(sfDrone.Position.X, sfDrone.Position.Y, sfDrone.Position.Z);
go.transform.position = position;
}
if (!sfDrone.Rotation.HasNan())
{
Vector3 rot = new Vector3(sfDrone.Rotation.Pitch, sfDrone.Rotation.Yaw, sfDrone.Rotation.Roll);
go.transform.eulerAngles = rot;
}
}
else
{
Debug.Log("Notice: AirSimHUD could not set vehicle name.");
Destroy(go);
}
}
else
{
Debug.Log("Notice: Did not find GetComponet<Vehicle> for this prefab.");
Destroy(go);
}
}
if (simpleFlightCreated)
{
//Currently multiple vehicles not supported. Only allowing one vehicle to be created.
break;
}
}
if (!simpleFlightCreated)
{
foreach (var px4Drone in AirSimSettings.GetSettings().Vehicles.Vehicles_PX4Multirotor)
{
if (px4Drone.AutoCreate == true)
{
px4Created = true;
GameObject go = Instantiate(VehiclePrefab) as GameObject;
if (go.GetComponent<Vehicle>() != null)
{
if (go.GetComponent<Vehicle>().SetVehicleName(px4Drone.VehicleName))
{
go.name = px4Drone.VehicleName;
if (!px4Drone.Position.HasNan())
{
Vector3 pos = new Vector3(px4Drone.Position.X, px4Drone.Position.Y, px4Drone.Position.Z);
go.transform.position = pos;
}
if (!px4Drone.Rotation.HasNan())
{
Vector3 rot = new Vector3(px4Drone.Rotation.Pitch, px4Drone.Rotation.Yaw, px4Drone.Rotation.Roll);
go.transform.eulerAngles = rot;
}
}
else
{
Debug.Log("Notice: AirSimHUD could not set vehicle name.");
Destroy(go);
}
}
else
{
Debug.Log("Notice: Did not find GetComponet<Vehicle> for this prefab.");
Destroy(go);
}
}
if (px4Created)
{
//Currently multiple vehicles not supported. Only allowing one vehicle to be created.
break;
}
}
}
if (!simpleFlightCreated || !px4Created)
{
// TODO: Add support (prefab) for ComputerVision
}
if (!simpleFlightCreated && !px4Created && !computerVisionCreated)
{
Debug.Log("Notice: 'settings.json' file did not have AutoCreate true for any Vehicles. No Vehicles created.");
}
}
}

//This method is linked through OnClick for RecordButton in AirSimHUD prefab.
public void ToggleRecording() {
Expand All @@ -61,6 +243,7 @@ public void ToggleRecording() {
//Sets up the cameras based on the settings.json file to render on the sub windows.
//Make sure the cameras are child of object with tag "ViewCameras"
public void SetUpViewCameras() {
Debug.Log("Setting up ViewCameras.");
GameObject viewCameras = GameObject.FindGameObjectWithTag("ViewCameras");

cameraViews[0] = transform.GetChild(0).gameObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using UnityEngine.Rendering;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace AirSimUnity {
/*
Expand Down Expand Up @@ -63,10 +64,10 @@ public static bool SetSegmentationId(string objectName, int segmentationId, bool
if (isNameRegex) {
bool isValueSet = false;
foreach (string s in keyList) {
if (!s.Contains(objectName)) {
if (!Regex.IsMatch(s, objectName)) {
continue;
}
segmentationIds[objectName] = segmentationId;
segmentationIds[s] = segmentationId;
isValueSet = true;
}
return isValueSet;
Expand Down
124 changes: 45 additions & 79 deletions Unity/UnityDemo/Assets/AirSimAssets/Scripts/InitializeAirSim.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using AirSimUnity;
using UnityEditor;
Expand All @@ -7,101 +9,65 @@

public class InitializeAirSim : MonoBehaviour
{
private static string AIRSIM_DATA_FOLDER = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/AirSim/";

void Awake()
{
if (GetAirSimSettingsFileName() != string.Empty)
if (!Directory.Exists(AIRSIM_DATA_FOLDER))
{
if(AirSimSettings.Initialize())
{

switch (AirSimSettings.GetSettings().SimMode)
{
case "Car":
{
LoadSceneAsPerSimMode(AirSimSettings.GetSettings().SimMode);
break;
}
case "Multirotor":
{
LoadSceneAsPerSimMode(AirSimSettings.GetSettings().SimMode);
break;
}
}
}
Directory.CreateDirectory(AIRSIM_DATA_FOLDER);
}
else
{
Debug.LogError("'Settings.json' file either not present or not configured properly.");
#if UNITY_EDITOR
EditorUtility.DisplayDialog("Missing 'Settings.json' file!!!", "'Settings.json' file either not present or not configured properly.", "Exit");
#endif
Application.Quit();
}
}

public static string GetAirSimSettingsFileName()
{
AirSimSettings.Initialize();

string fileName = Application.dataPath + "\\..\\settings.json";
if (File.Exists(fileName))
var simMode = AirSimSettings.GetSettings().SimMode;
if (simMode != "")
{
return fileName;
}

fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.Combine("AirSim", "settings.json"));
string linuxFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.Combine("Documents/AirSim", "settings.json"));
if (File.Exists(fileName))
{
return fileName;
}
else if (File.Exists(linuxFileName))
{
return linuxFileName;
}
if (CreateSettingsFileWithDefaultValues(fileName))
return fileName;
else if (CreateSettingsFileWithDefaultValues(linuxFileName))
return linuxFileName;
else
return string.Empty;
}

public static bool CreateSettingsFileWithDefaultValues(string fileName)
{
var result = false;
try
{
if (fileName.Substring(0, 5) == "/home")
Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Documents/AirSim"));
else
Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "AirSim"));

string content = "{\n \"SimMode\" : \"\", \n \"SettingsVersion\" : 1.2, \n \"SeeDocsAt\" : \"https://github.com/Microsoft/AirSim/blob/master/docs/settings.md\"\n}";
//settings file created at Documents\AirSim with name "setting.json".
StreamWriter writer = new StreamWriter(File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write));
writer.WriteLine(content);
writer.Close();
result = true;
}
catch (Exception ex)
{
Debug.LogError("Unable to create settings.json file @ " + fileName + " Error :- " + ex.Message);
result = false;
switch (simMode)
{
case "Car":
case "Multirotor":
case "ComputerVision":
LoadSceneAsPerSimMode(simMode);
break;
default:
Debug.Log("Notice: Unknown SimMode specified in 'settings.json' file.");
break;
}
}
return result;
}

public void LoadSceneAsPerSimMode(string load_name)
{
if (load_name == "Car")
{
AirSimSettings.GetSettings().SimMode = "Car";
SceneManager.LoadSceneAsync("Scenes/CarDemo", LoadSceneMode.Single);
}
else if (load_name == "Multirotor")
{
AirSimSettings.GetSettings().SimMode = "Multirotor";
SceneManager.LoadSceneAsync("Scenes/DroneDemo", LoadSceneMode.Single);


// Once SimMode is known we make final adjustments and check of settings based on the mode selected.
if (AirSimSettings.GetSettings().ValidateSettingsForSimMode())
{
if (load_name == "Car")
{
SceneManager.LoadSceneAsync("Scenes/CarDemo", LoadSceneMode.Single);
}
else if (load_name == "Multirotor")
{
SceneManager.LoadSceneAsync("Scenes/DroneDemo", LoadSceneMode.Single);
}
}
else
{
Debug.LogError("Error: Had a problem with the 'settings.json' file for SimMode selected '" + load_name + "'.");
#if UNITY_EDITOR
EditorUtility.DisplayDialog("Had a problem with the 'settings.json' file for SimMode selected: '" + load_name + "'.",
"Had a problem with the 'settings.json' file for SimMode selected: '" + load_name + "'.",
"Exit");
#endif
Application.Quit();
}
}

}

Loading