Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[sumo-user] Errors while setting speeds with TraCI

# Objective

I want to use predefined speed vectors for individual vehicles to move them in SUMO simulation.  

# Data and files
There are 3 vehicles in the simulation. For 2 of those vehicles, I want to specify the speeds. The speed data is created in `Python` as follows:  

    import numpy as np
    import pandas as pd
    from pandas import Series, DataFrame

    data = "">car1','car2','car2','car2'],
            'Time_sec':[0,1,2,0,1,2],
            'Speed.kph':[50,51,52,0,0,52]}
    dframe = DataFrame(data)  

The route, network and configuration files for this simulation are available in a folder here:  https://1drv.ms/f/s!AsMFpkDhWcnw61EI5wR6hPaaRBJI  
I have also put the code in a `Script.py` file in the same folder.  

# What I tried
Following is the code that I've been trying to use, along with the error:  


    #start sumo
    sumoBinary = "C:/Users/Quinton/Desktop/Documents/Sumo/bin/sumo-gui"
    sumoCmd = [sumoBinary, "-c", "C:/Users/Quinton/Desktop/Example2/example2.sumocfg"]
    
    
    #importing libraries
    import traci
    import traci.constants as tc
    traci.start(sumoCmd)
    
    #subscribing to variables that we want to be printed once the copy has run
    traci.vehicle.subscribe("car1", (tc.VAR_SPEED, tc.VAR_ROAD_ID, tc.VAR_LANE_ID, tc.VAR_LANEPOSITION))
    traci.vehicle.subscribe("car2", (tc.VAR_SPEED, tc.VAR_ROAD_ID, tc.VAR_LANE_ID, tc.VAR_LANEPOSITION))
    traci.vehicle.subscribe("car3", (tc.VAR_SPEED, tc.VAR_ROAD_ID, tc.VAR_LANE_ID, tc.VAR_LANEPOSITION))
    
    #using traci.movetoXY to position car1 and car2 on network
    traci.vehicle.moveToXY(vehID="car1", edgeID="highway1.1", lane=0, x=1000, y=-3.3, keepRoute=0)
    traci.vehicle.moveToXY(vehID="car2", edgeID="highway1.1", lane=1, x=700, y=3.3, keepRoute=0)
    
    #disallows car1 and car2 from changing lanes during simulation
    traci.vehicle.setLaneChangeMode(vehID="car1", lcm=512)
    traci.vehicle.setLaneChangeMode(vehID="car2", lcm=512)
    
    
    #importing python modules
    import numpy as np
    import pandas as pd
    from pandas import Series, DataFrame
    
    #creating speed data
    data = "">car1','car2','car2','car2'],
            'Time_sec':[0,1,2,0,1,2],
            'Speed.kph':[50,51,52,0,0,52]}
    dframe = DataFrame(data)
    #print(dframe)
    
    step = 0
    
    #running traci
    for ado in dframe.groupby('ADO_name'):
      ado_name = ado[1]["ADO_name"]
      adoID = ado_name.unique()
    
      while step <= 2:
        traci.simulationStep()
        traci.vehicle.setSpeed(adoID, ado[1][ado[1].Time_sec == step]['Speed.kph'])
        print (traci.vehicle.getSubscriptionResults("car1"), traci.vehicle.getSubscriptionResults("car2"))
        
      step += 1  

# Error:

    Traceback (most recent call last):
      File "C:\Users\Quinton\AppData\Local\Temp\Rtmp6jCqR4\chunk-code-16888822790.txt", line 41, in <module>
        traci.vehicle.setSpeed(adoID, ado[1][ado[1].Time_sec == step]['Speed.kph'])
      File "C:\Program Files (x86)\DLR\Sumo\tools\traci\_vehicle.py", line 927, in setSpeed
        tc.CMD_SET_VEHICLE_VARIABLE, tc.VAR_SPEED, vehID, speed)
      File "C:\Program Files (x86)\DLR\Sumo\tools\traci\connection.py", line 139, in _sendDoubleCmd
        self._beginMessage(cmdID, varID, objID, 1 + 8)
      File "C:\Program Files (x86)\DLR\Sumo\tools\traci\connection.py", line 127, in _beginMessage
        self._packString(objID, varID)
      File "C:\Program Files (x86)\DLR\Sumo\tools\traci\connection.py", line 66, in _packString
        self._string += struct.pack("!Bi", pre, len(s)) + s.encode("latin1")
    AttributeError: 'numpy.ndarray' object has no attribute 'encode'  

# Notes 
From my understanding, the problem is somewhere in `#running traci` section of the code. I tested this code without invoking `TraCI` and using `print()` instead of  `traci.vehicle.setSpeed()` and got no error. So, it seems that `Python` side of things are fine. Could you please help me fixing this problem

Back to the top