ConfigReader.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using System;
  6. /**
  7. { "Database" : { "ClientConnection" : string,
  8. "DatabaseName" : string ,
  9. "DBCollection" : string
  10. },
  11. "Kspeedcontroller" : 0.4,
  12. "SimulationTime" : 100.0,
  13. "VelocitySetpointGenerator" : { "Dark" : 0.6,
  14. "Bright" : 1.0,
  15. "Threshold1" : 0.78,
  16. "Threshold2" : 0.83,
  17. "DistanceCenterToWheel" : 0.07,
  18. "K" : 2,
  19. "ForwardSpeed" : 0.1
  20. },
  21. "Parcours" : {
  22. "LineWidth" : 0.1,
  23. "Kind" : "PieceWise",
  24. "LinePieces" : [
  25. { "x" : 1.1, "y" : 12.2},
  26. { "x" : 1.43, "y" : 4.5},
  27. { "x" : 1.8, "y" : 3.5},
  28. { "x" : 1.1, "y" : 2.5}
  29. ]
  30. }
  31. } **/
  32. [System.Serializable]
  33. public class ConfigDatabase
  34. {
  35. public string ClientConnection;
  36. public string DatabaseName;
  37. public string DBCollection;
  38. }
  39. [System.Serializable]
  40. public class ConfigVelocitySetpointGenerator
  41. {
  42. public float Dark;
  43. public float Bright;
  44. public float Threshold1;
  45. public float Threshold2;
  46. public float DistanceCenterToWheel;
  47. public float K;
  48. public float ForwardSpeed;
  49. }
  50. [System.Serializable]
  51. public class ConfigLinePieces
  52. {
  53. public float x;
  54. public float y;
  55. }
  56. [System.Serializable]
  57. public class ConfigParcours
  58. {
  59. public float LineWidth;
  60. public string Kind; // PieceWise of Trail
  61. public ConfigLinePieces[] LinePieces;
  62. }
  63. [System.Serializable]
  64. public class Config
  65. {
  66. public ConfigDatabase Database;
  67. public float Kspeedcontroller;
  68. public float SimulationTime;
  69. public ConfigVelocitySetpointGenerator VelocitySetpointGenerator;
  70. public ConfigParcours Parcours;
  71. }
  72. public class ConfigReader : MonoBehaviour
  73. {
  74. public Config configuration;
  75. //private DatabaseConnection DatabaseConnection;
  76. void Awake() //before start
  77. {
  78. ReadConfig();
  79. if (DatabaseGivenInFile())
  80. {
  81. DatabaseConnectionCreate();
  82. }
  83. else
  84. {
  85. Debug.LogWarning("Database-credentials not given in config-file.");
  86. }
  87. }
  88. private void ReadConfig()
  89. {
  90. string curdir = Directory.GetCurrentDirectory(); // current directory of executable. The config-file should be there
  91. StreamReader reader = new StreamReader(curdir + "/config.json");
  92. string json_string = reader.ReadToEnd();
  93. configuration = JsonUtility.FromJson<Config>(json_string);
  94. //Debug.Log(json_string);
  95. }
  96. /*public DatabaseConnection GetConnection()
  97. {
  98. return DatabaseConnection;
  99. }*/
  100. private void Start()
  101. {
  102. if (DatabaseGivenInFile())
  103. {
  104. /* DatabaseConnection.Connect(); /* Scriptable Object DataConnection must already be defind in Awake() */
  105. }
  106. }
  107. private void DatabaseConnectionCreate()
  108. {
  109. /*DatabaseConnection = ScriptableObject.CreateInstance<DatabaseConnection>();
  110. DatabaseConnection.ClientConnection = configuration.Database.ClientConnection;
  111. DatabaseConnection.DatabaseName = configuration.Database.DatabaseName;
  112. DBCollection Collection = ScriptableObject.CreateInstance<DBCollection>(); // collection to use with config file
  113. Collection.name = configuration.Database.DBCollection;
  114. DatabaseConnection.mycollection = Collection;*/
  115. }
  116. public bool DatabaseGivenInFile()
  117. {
  118. if (String.IsNullOrEmpty(configuration.Database.ClientConnection)) return false;
  119. if (String.IsNullOrEmpty(configuration.Database.DatabaseName)) return false;
  120. if (String.IsNullOrEmpty(configuration.Database.DBCollection)) return false;
  121. return true;
  122. }
  123. public bool VelocitySetpointGeneratorAvailable()
  124. {
  125. if (float.IsNaN(configuration.VelocitySetpointGenerator.Bright)) return false;
  126. if (float.IsNaN(configuration.VelocitySetpointGenerator.Dark)) return false;
  127. if (float.IsNaN(configuration.VelocitySetpointGenerator.DistanceCenterToWheel)) return false;
  128. if (float.IsNaN(configuration.VelocitySetpointGenerator.ForwardSpeed)) return false;
  129. if (float.IsNaN(configuration.VelocitySetpointGenerator.K)) return false;
  130. if (float.IsNaN(configuration.VelocitySetpointGenerator.Threshold1)) return false;
  131. if (float.IsNaN(configuration.VelocitySetpointGenerator.Threshold2)) return false;
  132. return true;
  133. }
  134. public bool ValidParcours()
  135. {
  136. if (configuration.Parcours.LinePieces.Length > 1)
  137. {
  138. if (configuration.Parcours.Kind == "PieceWise")
  139. {
  140. // There should be an even amount of points
  141. if (configuration.Parcours.LinePieces.Length % 2 == 0)
  142. {
  143. return true;
  144. }
  145. else
  146. {
  147. return false;
  148. }
  149. }
  150. else
  151. {
  152. return true;
  153. }
  154. }
  155. else
  156. {
  157. return false;
  158. }
  159. }
  160. }