-1

THe following is my autobahn.net.xml

<?xml version="1.0" encoding="UTF-8"?><net version="1.9" junctionCornerDetail="5" limitTurnSpeed="5.50" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/net_file.xsd"><location netOffset="0.00,0.00" convBoundary="-124.21,-12.52,21.96,76.82" origBoundary="10000000000.00,10000000000.00,-10000000000.00,-10000000000.00" projParameter="!"/><edge id=":gneJ11_0" function="internal"><lane id=":gneJ11_0_0" index="0" speed="13.89" length="0.10" shape="-119.32,-13.29 -119.32,-13.29"/></edge><edge id=":gneJ9_0" function="internal"><lane id=":gneJ9_0_0" index="0" speed="13.89" length="0.10" shape="11.42,48.97 11.42,48.97"/></edge><edge id="entr" from="gneJ11" to="gneJ9" priority="-1" shape="-119.59,-11.72 -92.19,-6.96 9.84,49.35 9.96,49.62"><lane id="entr_0" index="0" speed="13.89" length="146.26" shape="-119.32,-13.29 -91.65,-8.49 11.09,48.21 11.42,48.97"/></edge><edge id="entry" from="gneJ0" to="gneJ11" priority="-1"><lane id="entry_0" index="0" speed="13.89" length="4.69" shape="-123.94,-14.10 -119.32,-13.29"/></edge><edge id="exit" from="gneJ9" to="gneJ1" priority="-1"><lane id="exit_0" index="0" speed="13.89" length="29.73" shape="11.42,48.97 23.42,76.17"/></edge><junction id="gneJ0" type="dead_end" x="-124.21" y="-12.52" incLanes="" intLanes="" shape="-124.21,-12.52 -123.66,-15.67"/><junction id="gneJ1" type="dead_end" x="21.96" y="76.82" incLanes="exit_0" intLanes="" shape="24.89,75.53 21.96,76.82"/><junction id="gneJ11" type="priority" x="-119.59" y="-11.72" incLanes="entry_0" intLanes=":gneJ11_0_0" shape="-119.59,-11.72 -119.04,-14.87 -119.59,-11.72"><request index="0" response="0" foes="0" cont="0"/></junction><junction id="gneJ9" type="priority" x="9.96" y="49.62" incLanes="entr_0" intLanes=":gneJ9_0_0" shape="9.96,49.62 12.89,48.33 9.96,49.62"><request index="0" response="0" foes="0" cont="0"/></junction><connection from="entr" to="exit" fromLane="0" toLane="0" via=":gneJ9_0_0" dir="s" state="M"/><connection from="entry" to="entr" fromLane="0" toLane="0" via=":gneJ11_0_0" dir="s" state="M"/><connection from=":gneJ11_0" to="entr" fromLane="0" toLane="0" dir="s" state="M"/><connection from=":gneJ9_0" to="exit" fromLane="0" toLane="0" dir="s" state="M"/></net>

The following is my autobahn.rou.xml

<routes><vType id="normal_car" vClass="passenger" maxSpeed="40" speedFactor="0.9" speedDev="0.2" sigma="0.5" /><vType id="sporty_car" vClass="passenger" maxSpeed="60" speedFactor="1.3" speedDev="0.1" sigma="0.1" /><vType id="trailer" vClass="trailer"  maxSpeed="30" speedFactor="1.1" speedDev="0.1" /><vType id="coach" vClass="coach"  maxSpeed="30" speedFactor="1." speedDev="0.1" /><flow id="normal" type="normal_car" begin="0" end="5000" number="5000" from="entr" to="exit" departPos="last" departLane="best" /><flow id="sporty" type="sporty_car" begin="0" end="5000" number="300" from="entr" to="exit" departPos="last" departLane="best" /><flow id="coach" type="coach" begin="0" end="5000" number="300" from="entry" to="exit" departPos="last" departLane="best" /><flow id="trailer" type="trailer" begin="0" end="5000" number="700" from="entry" to="exit" departPos="last" departLane="best" /></routes>
import os
import sys
import optparse

# we need to import some python modules from the $SUMO_HOME/tools directory
if 'SUMO_HOME' in os.environ:
    tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
    sys.path.append(tools)
else:
    sys.exit("please declare environment variable 'SUMO_HOME'")


from sumolib import checkBinary  # Checks for the binary in environ vars
import traci


def get_options():
    opt_parser = optparse.OptionParser()
    opt_parser.add_option("--nogui", action="store_true",
                         default=False, help="run the commandline version of sumo")
    options, args = opt_parser.parse_args()
    return options

def run():
    step = 0
    all_cars_coming_out = 0
    while traci.simulation.getMinExpectedNumber() > 0:
        veh_list = traci.simulation.getLoadedIDList()
        for veh in veh_list:
            # print(veh)
            print(traci.vehicle.getSpeed(veh))
        traci.simulationStep()
        step += 1
        all_cars_coming_out += len(veh_list)
        # import pdb; pdb.set_trace()
    print(all_cars_coming_out)
        

if __name__ == "__main__":
    options = get_options()

    # check binary
    if options.nogui:
        sumoBinary = checkBinary('sumo')
    else:
        sumoBinary = checkBinary('sumo-gui')

    # traci starts sumo as a subprocess and then this script connects and runs
    traci.start(
        [sumoBinary,
        "-n", 
        "autobahn.net.xml",
        "-r", 
        "autobahn.rou.xml"])
    run()

I am using traci.vehicle.getSpeed(veh) to retrieve the speed of all vechiles in each step. However, I get values like -10343445. HOw do I get the right speed?

Echchama Nayak
  • 971
  • 3
  • 23
  • 44

1 Answers1

0

You retrieve the speed before you did a simulation step so you probably get the error value -1073741824 as mentioned in the docs: https://sumo.dlr.de/docs/TraCI/Vehicle_Value_Retrieval.html. This is because the vehicles did not have a chance to be inserted in the network.

Michael
  • 3,510
  • 1
  • 11
  • 23
  • THank you for the response, can you correct the above example to make it work? I tried shifting the code after `traci.simulationStep()` – Echchama Nayak Jul 27 '21 at 10:27
  • It depends a little on what you want to achieve. Do you want the speed for all vehicles which are currently in the net or only the ones which departed in the last step? – Michael Aug 04 '21 at 10:47