void UpdateVehicles() { try { conn.do_timestep(); foreach (GameObject v in vehicles) { // get new position of the car SumoPosition2D newPosition = (SumoPosition2D)conn.do_job_get(de.tudresden.sumo.cmd.Vehicle.getPosition(v.GetComponent <VehicleContainer>().getId())); // convert into vector3 Vector3 newPositonV3 = new Vector3((float)newPosition.x, CarHeight, (float)newPosition.y); // update the position of car v.transform.position = newPositonV3; } } catch (Exception e) { Debug.Log(" Exception updating vehicles - " + e.Message + " " + e.StackTrace); if (conn.isClosed()) { conn.close(); vehiclesCreationSuccess = false; Debug.Log(" Application quit as connection to SUMO was terminated! "); Application.Quit(); return; } } }
void Start() { try { config_file = GameObject.FindGameObjectsWithTag("Persistant_Data")[0].GetComponent <persistant_data>().path_to_osm; // read data from menue conn = new SumoTraciConnection(sumo_bin, config_file); //set some options conn.addOption("step-length", "0.1"); //timestep 100 ms conn.addOption("start", ""); conn.runServer(); conn.do_timestep(); carList = new List <Auto>(); linkList = new List <Link>(); ReadLanes(); printLanes(linkList); //for (int i = 0; i < 300; i++) //{ // ReadCars(); //} //printCars(carList); //Debug.Log("Invoke?"); //InvokeRepeating("Readcars", 1, 0.3F); //Debug.Log("Afterinvoke"); } catch (System.Exception) { conn.close(); } }
//public static void step(NetworkStream bw, int j) //{ // byte[] bytes = { 0, 0, 0, 10, 6, 2, 0, 0, 0, 0 }; // bw.Write(bytes, 0, bytes.Length); // bw.Flush(); //} public static void Main(String[] argv) { //Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //socket.NoDelay = true; //try //{ // socket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60634)); //} //catch (Exception localConnectException) //{ // Console.WriteLine(localConnectException.StackTrace.ToString()); //} //NetworkStream ns = new NetworkStream(socket); ////BinaryWriter bw = new BinaryWriter(ns); //for (int i = 0; i < 1000; i++) //{ // step(ns,i); //} String configfile = "C:/Users/Biurrun/Tesis/Examples/Test2/Test2.sumo.cfg"; //start Simulation int seed = -1; bool withGui = true; int steplength = 300; SumoTraciConnection conn = new SumoTraciConnection(configfile, seed, withGui, steplength); //conn.addOption("step-length", steplength); float simtime; try { //start TraCI conn.runServer(); if (conn.isClosed()) { throw new InvalidOperationException("connection is closed"); } //load routes and initialize the simulation conn.do_timestep(); for (int i = 0; i < 500; i++) { simtime = i * steplength; //ms //current simulation time //int simtime = (int)conn.do_job_get(Simulation.getCurrentTime()); //Console.WriteLine(simtime); //conn.do_job_set(Vehicle.add("veh" + i, "car", "s1", simtime, 0, 13.8, (byte)1)); conn.do_timestep(); } //conn.get_vehicle_count(); //conn.get_vehicle_list(); //stop TraCI conn.close(); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }