Hi Ankur Sarker, your code looks OK except that you did not cover the case in decideSpeedValue when the condition "abc <= float(dis[j])" never gets true. The function will return None in this case which is not a valid float. So string conversion may not be your real problem.
Best regards, Michael Am 2016-10-06 23:14, schrieb Ankur Sarker: > Or, can anyone tell me how to convert String (in python) to TYPE_DOUBLE > ( > in SUMO)? *TYPE_DOUBLE* is defined in Sumo/tools/traci/constant.py. > > Actually, I am not so familiar with python programming language. > > Thanks, > Ankur Sarker > > > On Thu, Oct 6, 2016 at 1:38 PM, Ankur Sarker <[email protected]> > wrote: > >> Hi, >> >> I am trying to call traci.vehicle.setSpeed() using TraCI python >> interface >> and I am having hard time to convert string to double. >> >> Can anyone give me suggestion? >> >> *-------------------------------------* >> *My function: * >> >> def decideSpeedValue(abc): >> dis = [line.rstrip('\n') for line in open('distance.txt')] >> speed = [line.rstrip('\n') for line in open('speed.txt')] >> for j in range(len(dis)): >> if abc <= float(dis[j]): >> return float(speed[j]) >> >> *Function call:* >> >> vehicles = traci.vehicle.getIDList() >> for word in vehicles: >> if word == "kang": >> distance = traci.vehicle.getDistance("kang") >> >> *traci.vehicle.setSpeed("kang",decideSpeedValue(distance))* >> >> *I get the following error:* >> >> File "C:\Program Files (x86)\DLR\Sumo\tools\traci\_vehicle.py", line >> 757, in s >> etSpeed >> 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._string += struct.pack("!Bd", tc.TYPE_DOUBLE, value)* >> *struct.error: required argument is not a floa*t >> ---------------------------------------------- >> >> Thanks for your time! >> >> With Regards, >> Ankur Sarker >> > ------------------------------------------------------------------------------ > Check out the vibrant tech community on one of the world's most > engaging tech sites, SlashDot.org! http://sdm.link/slashdot > _______________________________________________ > sumo-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/sumo-user ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot _______________________________________________ sumo-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/sumo-user
