0

I wrote the script below with python and i implemented it on sumo,in order to obtain the number of vehicles between two inductionLoop,every 60 seconds,in a lane. But this one gives each second .

   #!/usr/bin/env python
   # -*-coding:Latin-1 -*
   import os, sys
   import optparse
   import subprocess
   import random
   import threading
   import time

   SUMO_HOME = "/home/khadija/Téléchargements/sumo-0.25.0"

   try:
      sys.path.append(os.path.join(SUMO_HOME, "tools"))
      from sumolib import checkBinary
   except ImportError:
       sys.exit("please declare environment variable 'SUMO_HOME' as the root directory of your sumo installation (it should contain folders 'bin', 'tools' and 'docs')")

   import traci
   routeFile="data2/cross.rou.xml"
   PORT = 8873

   #SIGN CONFIGURATIONS : NESW
   NSgreen = "GrGr"
   EWgreen = "rGrG"
   PROGRAM = (NSgreen,EWgreen)


   def nbr_veh_entr_indloop(i,o):
  # i et j se sont les inductions loop input et output
       threading.Timer(60.0, nbr_veh_entr_indloop).start()
       x = traci.inductionloop.getLastStepMeanLength(i) -  traci.inductionloop.getLastStepMeanLength(o)
       return x 
   def run():
       steps = open("data2/step.txt","w")
       traci.init(int(PORT))
       step = 0
       while step < 7200 :
             a = nbr_veh_entr_indloop("4i","40")
             k=str(a)
             print >> steps , "nombre des veh : " + k  #concaténation 
             traci.simulationStep() 
             step +=1



       steps.close()
       traci.close()
       sys.stdout.flush()


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


# this is the main entry point of this script
   if __name__ == "__main__":
      options = get_options()

# this script has been called from the command line. It will start sumo as a
# server, then connect and run
      if options.nogui:
         sumoBinary = checkBinary('sumo')
      else:
          sumoBinary = checkBinary('sumo-gui')


     # this is the normal way of using traci. sumo is   started as a
     # subprocess and then the python script connects and runs
      sumoProcess = subprocess.Popen([sumoBinary, "-c",  "data2/cross.sumocfg", "--tripinfo-output","tripinfo.xml", "--remote-port",  str(PORT)], stdout=sys.stdout, stderr=sys.stderr)
      run()
      sumoProcess.wait()

Thanks for help in advance.

Regards,

Michael
  • 3,510
  • 1
  • 11
  • 23
mk10
  • 9
  • 5

1 Answers1

0

You probably want to have have the value every 60 simulation seconds not every 60 wallclock seconds, so a timer is pointless here. Simply ask for the value after 60 simulation steps (assuming you use sumo's default step length of one second). So you could write something like:

     if step % 60 == 0:
         print >> steps , "nombre des veh : " + k

This will print the value for the last step every 60 steps. If you want the value for the last minute you need to aggregate (sum up) yourself.

Michael
  • 3,510
  • 1
  • 11
  • 23