0

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 = {'ADO_name':['car1','car1','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 = {'ADO_name':['car1','car1','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

umair durrani
  • 5,597
  • 8
  • 45
  • 85

1 Answers1

1

The problem is not in the traci part but in your adoID which is an array but should be a single id (a string) so you should probably just take the first element of that array.

Michael
  • 3,510
  • 1
  • 11
  • 23
  • Thanks for your answer. I tried `adoID[0]` after removing the data for `car2`. It worked. However, if I keep the data for both vehicles along with `adoID[0]`, I get the error `required argument is not a float`. – umair durrani Mar 01 '18 at 13:39
  • 1
    I am not sure what your code is trying to achieve. Do you want to feed the data for both cars into traci or only for one or first one and then the other? If you want to do both simultaneously, my approach would be to simply sort the data by time and then apply each entry. There is no groupBy needed. – Michael Mar 01 '18 at 14:54
  • Thanks. I wanted to feed speeds of both cars simultaneously. As you suggested, I simply used `for loop` without `groupby` and it is running without any problem. Just curious, is there a more elegant way to feed speeds of multiple vehicles? I have 13 in total. – umair durrani Mar 01 '18 at 15:51
  • 1
    I am afraid no. There is no such thing as setMultipleSpeeds and while it would be certainly not hard to implement it is a little against the usual TraCI philosophy of addressing each object and its properties individually. – Michael Mar 01 '18 at 20:24