egueli / TraCI4J

A high-level Java library to communicate with SUMO (Simulation of Urban MObility) through its TraCI protocol.
GNU General Public License v3.0
41 stars 27 forks source link

Added departure time in AddVehicleQuery #11

Closed marfl closed 9 years ago

beruit01 commented 9 years ago

Hey! Having the possibility of adding the departure time in AddVehicleQuery would be very nice. But the changes of this pull requests do not solve this issue. The new vehicles are still starting at simulation step 0.

marfl commented 9 years ago

Hi,

thanks for the feedback! We inherited this code from a student and it seems to work with his code base using SUMO 0.21. I took a closer look and the message built in AddVehicleQuery also seems to be correct according to the TraCI documentation at "http://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State" (look for the 0x80 opcode).

Can you describe (or send me) a simple scenario where the changes don't work? I'll try to have a closer look then.

marfl commented 9 years ago

Hi again, I also just added a test for the new and old functionality of AddVehicleQuery. The test passes on my setup with SUMO 0.21.

beruit01 commented 9 years ago

Hey! Thank you for your effort! But I realized that I'am an idiot.. I use the sources from egueli:master at the moment. Tomorrow I will download the sources of your fork and then it would work, I guess. ;-) Please apologize my mistake!

beruit01 commented 9 years ago

I downloaded the KIT master branch and the test works. But unfortunately this doesn´t solve my issue. What I want to do is add vehicles after the simulation has started. Currently I am trying to solve this with the following method:

public void addVehicle() {

    System.out.println("add vehicle..");

    int now = conn.getCurrentSimStep() * 1000; // time is in ms

    try {   
        AddVehicleQuery avqNow = conn.queryAddVehicle();        
        avqNow.setVehicleData("autGenVehicleNow", vehicleTypes.get("Car"), routes.get("route0"), 0, now, 0, 0);
        avqNow.run();
        System.out.println("Done.");

    } catch (IOException e2) {
        System.err.println( " Add vehicle failed!");
        e2.printStackTrace();
    }
}

But after trying to add a vehicle using addVehicle() I get the following error message:

it.polito.appeal.traci.TraCIException$UnexpectedData: Unexpected command/status ID: expected 164, got 2
    at it.polito.appeal.traci.Utils.checkStatusResponse(Utils.java:57)
    at it.polito.appeal.traci.ChangeStateQuery.pickResponses(ChangeStateQuery.java:83)
    at it.polito.appeal.traci.MultiQuery.run(MultiQuery.java:106)
    at it.polito.appeal.traci.SumoTraciConnection.nextSimStep(SumoTraciConnection.java:573)
    at prototyp.Simulation.run(Simulation.java:96)

I'am using a thread for the simulation in Simulation.java where I invoke the nextSimStep() method until the thread will be interrupted.

Are there any mechanisms for manipulating the simulation state on runtime? There occur similar problems when setting the state of a traffic light.

beruit01 commented 9 years ago

I solved my problem. TraCI4J is not thread save. Because of this, I added some synchronized blocks regarding the SumoTraciConnection object. Now inserting vehicles and manipulating the state of multiple traffic lights works on runtime, too.