Skip to content

Commit

Permalink
Loading from json file
Browse files Browse the repository at this point in the history
  • Loading branch information
noahzemlin committed Nov 26, 2019
1 parent 8299aa3 commit 902322a
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 893 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ sysinfo.txt
# Crashlytics generated file
crashlytics-build.properties

# Ignore config
config.json
43 changes: 43 additions & 0 deletions Assets/IGVCConfigLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RosSharp.RosBridgeClient
{
public class IGVCConfigLoader : MonoBehaviour
{

private SimpleCarController simpleCarController;
private RosConnector rosConnector;
private ImagePublisher imagePublisher;
private LaserScanPublisher laserScanPublisher;
private IMUPublisher iMUPublisher;
private VelocityPublisher velocityPublisher;
private GPSPublisher gPSPublisher;
private MotorsSubscriber motorsSubscriber;

void Start()
{
if (MenuValues._instance != null)
{
simpleCarController = this.GetComponent<SimpleCarController>();
rosConnector = this.GetComponent<RosConnector>();
imagePublisher = this.GetComponent<ImagePublisher>();
laserScanPublisher = this.GetComponent<LaserScanPublisher>();
iMUPublisher = this.GetComponent<IMUPublisher>();
velocityPublisher = this.GetComponent<VelocityPublisher>();
gPSPublisher = this.GetComponent<GPSPublisher>();
motorsSubscriber = this.GetComponent<MotorsSubscriber>();

simpleCarController.useController = !MenuValues._instance.autonomous.Equals("True");
rosConnector.RosBridgeServerUrl = "ws://" + MenuValues._instance.ros_bridge_url;
imagePublisher.Topic = MenuValues._instance.camera_topic;
laserScanPublisher.Topic = MenuValues._instance.laser_scan_topic;
iMUPublisher.Topic = MenuValues._instance.imu_topic;
velocityPublisher.Topic = MenuValues._instance.velocity_topic;
gPSPublisher.Topic = MenuValues._instance.gps_topic;
motorsSubscriber.Topic = MenuValues._instance.motors_topic;
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/IGVCConfigLoader.cs.meta

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

69 changes: 54 additions & 15 deletions Assets/MenuScripts/MenuController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,76 @@
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
using System.IO;

public class MenuController : MonoBehaviour
{

public string jsonPath = "config.json";

public GameObject CameraTopic;
public GameObject HeadingTopic;
public GameObject IMUTopic;
public GameObject VelocityTopic;
public GameObject AccelerationTopic;
public GameObject GpsTopic;
public GameObject LaserScanTopic;
public GameObject MotorsTopic;
public GameObject RosBridgeTopic;
public GameObject Autonomous;

public void UpdateFields()
public void Start()
{

if (File.Exists(jsonPath))
{
StreamReader reader = new StreamReader(jsonPath);
string json_raw = reader.ReadToEnd();
MenuValues._instance = JsonUtility.FromJson<MenuValues>(json_raw);
reader.Close();
}
else
{
MenuValues._instance = new MenuValues
{
autonomous = "True",
camera_topic = "/igvc/camera/compressed",
imu_topic = "/igvc/imu",
velocity_topic = "/igvc/velocity",
gps_topic = "/igvc/gps",
laser_scan_topic = "/igvc/lidar",
motors_topic = "/igvc/motors_raw",
ros_bridge_url = "localhost:9090"

};
StreamWriter writer = new StreamWriter(jsonPath, true);
writer.WriteLine(JsonUtility.ToJson(MenuValues._instance, true));
writer.Close();
}

CameraTopic.GetComponent<TMP_InputField>().text = MenuValues._instance.camera_topic;
IMUTopic.GetComponent<TMP_InputField>().text = MenuValues._instance.imu_topic;
VelocityTopic.GetComponent<TMP_InputField>().text = MenuValues._instance.velocity_topic;
GpsTopic.GetComponent<TMP_InputField>().text = MenuValues._instance.gps_topic;
LaserScanTopic.GetComponent<TMP_InputField>().text = MenuValues._instance.laser_scan_topic;
MotorsTopic.GetComponent<TMP_InputField>().text = MenuValues._instance.motors_topic;
RosBridgeTopic.GetComponent<TMP_InputField>().text = MenuValues._instance.ros_bridge_url;
Autonomous.GetComponent<Toggle>().isOn = MenuValues._instance.autonomous.Equals("True");
}

public void UpdateDemFieldsYo()
{
MenuValues.camera_topic = CameraTopic.GetComponent<TMP_InputField>().text;
MenuValues.heading_topic = HeadingTopic.GetComponent<TMP_InputField>().text;
MenuValues.velocity_topic = VelocityTopic.GetComponent<TMP_InputField>().text;
MenuValues.acceleration_topic = AccelerationTopic.GetComponent<TMP_InputField>().text;
MenuValues.gps_topic = GpsTopic.GetComponent<TMP_InputField>().text;
MenuValues.laser_scan_topic = LaserScanTopic.GetComponent<TMP_InputField>().text;
MenuValues.motors_topic = MotorsTopic.GetComponent<TMP_InputField>().text;
MenuValues.ros_bridge_url = RosBridgeTopic.GetComponent<TMP_InputField>().text;
MenuValues.autonomous = Autonomous.GetComponent<Toggle>().isOn;

Debug.Log(MenuValues.camera_topic + MenuValues.heading_topic + MenuValues.velocity_topic + MenuValues.acceleration_topic +
MenuValues.gps_topic + MenuValues.laser_scan_topic + MenuValues.motors_topic + MenuValues.ros_bridge_url + MenuValues.autonomous);
MenuValues._instance.camera_topic = CameraTopic.GetComponent<TMP_InputField>().text;
MenuValues._instance.imu_topic = IMUTopic.GetComponent<TMP_InputField>().text;
MenuValues._instance.velocity_topic = VelocityTopic.GetComponent<TMP_InputField>().text;
MenuValues._instance.gps_topic = GpsTopic.GetComponent<TMP_InputField>().text;
MenuValues._instance.laser_scan_topic = LaserScanTopic.GetComponent<TMP_InputField>().text;
MenuValues._instance.motors_topic = MotorsTopic.GetComponent<TMP_InputField>().text;
MenuValues._instance.ros_bridge_url = RosBridgeTopic.GetComponent<TMP_InputField>().text;
MenuValues._instance.autonomous = Autonomous.GetComponent<Toggle>().isOn.ToString();
}

public void PlaySim()
{
UpdateDemFieldsYo();
SceneManager.LoadScene(1);
}
}
27 changes: 16 additions & 11 deletions Assets/MenuScripts/MenuValues.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

static class MenuValues
[Serializable]
public class MenuValues
{
public static bool autonomous;
public static string camera_topic;
public static string heading_topic;
public static string velocity_topic;
public static string acceleration_topic;
public static string gps_topic;
public static string laser_scan_topic;
public static string motors_topic;
public static string ros_bridge_url;

[NonSerialized]
public static MenuValues _instance;

public string autonomous;
public string camera_topic;
public string imu_topic;
public string velocity_topic;
public string gps_topic;
public string laser_scan_topic;
public string motors_topic;
public string ros_bridge_url;
}
Loading

0 comments on commit 902322a

Please sign in to comment.