1# -*- coding: utf-8 -*-
2# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3# Copyright (C) 2008-2019 German Aerospace Center (DLR) and others.
4# This program and the accompanying materials
5# are made available under the terms of the Eclipse Public License v2.0
6# which accompanies this distribution, and is available at
7# http://www.eclipse.org/legal/epl-v20.html
8# SPDX-License-Identifier: EPL-2.0
9
10# @file    _vehicletype.py
11# @author  Michael Behrisch
12# @author  Lena Kalleske
13# @date    2008-10-09
14# @version $Id$
15
16from __future__ import absolute_import
17from .domain import Domain
18from .storage import Storage
19import struct
20from . import constants as tc
21from . import exceptions
22
23_RETURN_VALUE_FUNC = {tc.VAR_LENGTH: Storage.readDouble,
24                      tc.VAR_MAXSPEED: Storage.readDouble,
25                      tc.VAR_SPEED_FACTOR: Storage.readDouble,
26                      tc.VAR_SPEED_DEVIATION: Storage.readDouble,
27                      tc.VAR_ACCEL: Storage.readDouble,
28                      tc.VAR_DECEL: Storage.readDouble,
29                      tc.VAR_EMERGENCY_DECEL: Storage.readDouble,
30                      tc.VAR_APPARENT_DECEL: Storage.readDouble,
31                      tc.VAR_ACTIONSTEPLENGTH: Storage.readDouble,
32                      tc.VAR_IMPERFECTION: Storage.readDouble,
33                      tc.VAR_TAU: Storage.readDouble,
34                      tc.VAR_VEHICLECLASS: Storage.readString,
35                      tc.VAR_EMISSIONCLASS: Storage.readString,
36                      tc.VAR_SHAPECLASS: Storage.readString,
37                      tc.VAR_MINGAP: Storage.readDouble,
38                      tc.VAR_WIDTH: Storage.readDouble,
39                      tc.VAR_HEIGHT: Storage.readDouble,
40                      tc.VAR_MAXSPEED_LAT: Storage.readDouble,
41                      tc.VAR_MINGAP_LAT: Storage.readDouble,
42                      tc.VAR_LATALIGNMENT: Storage.readString,
43                      tc.VAR_COLOR: lambda result: result.read("!BBBB")}
44
45
46class VehicleTypeDomain(Domain):
47
48    def __init__(self):
49        Domain.__init__(self, "vehicletype", tc.CMD_GET_VEHICLETYPE_VARIABLE, tc.CMD_SET_VEHICLETYPE_VARIABLE,
50                        tc.CMD_SUBSCRIBE_VEHICLETYPE_VARIABLE, tc.RESPONSE_SUBSCRIBE_VEHICLETYPE_VARIABLE,
51                        tc.CMD_SUBSCRIBE_VEHICLETYPE_CONTEXT, tc.RESPONSE_SUBSCRIBE_VEHICLETYPE_CONTEXT,
52                        _RETURN_VALUE_FUNC)
53
54    def getLength(self, typeID):
55        """getLength(string) -> double
56
57        Returns the length in m of the vehicles of this type.
58        """
59        return self._getUniversal(tc.VAR_LENGTH, typeID)
60
61    def getMaxSpeed(self, typeID):
62        """getMaxSpeed(string) -> double
63
64        Returns the maximum speed in m/s of vehicles of this type.
65        """
66        return self._getUniversal(tc.VAR_MAXSPEED, typeID)
67
68    def getSpeedFactor(self, typeID):
69        """getSpeedFactor(string) -> double
70
71        .
72        """
73        return self._getUniversal(tc.VAR_SPEED_FACTOR, typeID)
74
75    def getSpeedDeviation(self, typeID):
76        """getSpeedDeviation(string) -> double
77
78        Returns the maximum speed deviation of vehicles of this type.
79        """
80        return self._getUniversal(tc.VAR_SPEED_DEVIATION, typeID)
81
82    def getAccel(self, typeID):
83        """getAccel(string) -> double
84
85        Returns the maximum acceleration in m/s^2 of vehicles of this type.
86        """
87        return self._getUniversal(tc.VAR_ACCEL, typeID)
88
89    def getDecel(self, typeID):
90        """getDecel(string) -> double
91
92        Returns the maximal comfortable deceleration in m/s^2 of vehicles of this type.
93        """
94        return self._getUniversal(tc.VAR_DECEL, typeID)
95
96    def getEmergencyDecel(self, typeID):
97        """getEmergencyDecel(string) -> double
98
99        Returns the maximal physically possible deceleration in m/s^2 of vehicles of this type.
100        """
101        return self._getUniversal(tc.VAR_EMERGENCY_DECEL, typeID)
102
103    def getApparentDecel(self, typeID):
104        """getApparentDecel(string) -> double
105
106        Returns the apparent deceleration in m/s^2 of vehicles of this type.
107        """
108        return self._getUniversal(tc.VAR_APPARENT_DECEL, typeID)
109
110    def getActionStepLength(self, typeID):
111        """getActionStepLength(string) -> double
112
113        Returns the action step length for vehicles of this type.
114        """
115        return self._getUniversal(tc.VAR_ACTIONSTEPLENGTH, typeID)
116
117    def getImperfection(self, typeID):
118        """getImperfection(string) -> double
119
120        .
121        """
122        return self._getUniversal(tc.VAR_IMPERFECTION, typeID)
123
124    def getTau(self, typeID):
125        """getTau(string) -> double
126
127        Returns the driver's reaction time in s for vehicles of this type.
128        """
129        return self._getUniversal(tc.VAR_TAU, typeID)
130
131    def getVehicleClass(self, typeID):
132        """getVehicleClass(string) -> string
133
134        Returns the class of vehicles of this type.
135        """
136        return self._getUniversal(tc.VAR_VEHICLECLASS, typeID)
137
138    def getEmissionClass(self, typeID):
139        """getEmissionClass(string) -> string
140
141        Returns the emission class of vehicles of this type.
142        """
143        return self._getUniversal(tc.VAR_EMISSIONCLASS, typeID)
144
145    def getShapeClass(self, typeID):
146        """getShapeClass(string) -> string
147
148        Returns the shape class of vehicles of this type.
149        """
150        return self._getUniversal(tc.VAR_SHAPECLASS, typeID)
151
152    def getMinGap(self, typeID):
153        """getMinGap(string) -> double
154
155        Returns the offset (gap to front vehicle if halting) of vehicles of this type.
156        """
157        return self._getUniversal(tc.VAR_MINGAP, typeID)
158
159    def getWidth(self, typeID):
160        """getWidth(string) -> double
161
162        Returns the width in m of vehicles of this type.
163        """
164        return self._getUniversal(tc.VAR_WIDTH, typeID)
165
166    def getHeight(self, typeID):
167        """getHeight(string) -> double
168
169        Returns the height in m of vehicles of this type.
170        """
171        return self._getUniversal(tc.VAR_HEIGHT, typeID)
172
173    def getColor(self, typeID):
174        """getColor(string) -> (integer, integer, integer, integer)
175
176        Returns the color of this type.
177        """
178        return self._getUniversal(tc.VAR_COLOR, typeID)
179
180    def getMaxSpeedLat(self, vehID):
181        """getMaxSpeedLat(string) -> double
182
183        Returns the maximum lateral speed in m/s of this type.
184        """
185        return self._getUniversal(tc.VAR_MAXSPEED_LAT, vehID)
186
187    def getLateralAlignment(self, vehID):
188        """getLateralAlignment(string) -> string
189
190        Returns The preferred lateral alignment of the type
191        """
192        return self._getUniversal(tc.VAR_LATALIGNMENT, vehID)
193
194    def getMinGapLat(self, vehID):
195        """getMinGapLat(string) -> double
196
197        Returns The desired lateral gap of this type at 50km/h in m
198        """
199        return self._getUniversal(tc.VAR_MINGAP_LAT, vehID)
200
201    def setLength(self, typeID, length):
202        """setLength(string, double) -> None
203
204        Sets the length in m of the vehicles of this type.
205        """
206        self._connection._sendDoubleCmd(
207            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_LENGTH, typeID, length)
208
209    def setMaxSpeed(self, typeID, speed):
210        """setMaxSpeed(string, double) -> None
211
212        Sets the maximum speed in m/s of vehicles of this type.
213        """
214        self._connection._sendDoubleCmd(
215            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_MAXSPEED, typeID, speed)
216
217    def setVehicleClass(self, typeID, clazz):
218        """setVehicleClass(string, string) -> None
219
220        Sets the class of vehicles of this type.
221        """
222        self._connection._sendStringCmd(
223            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_VEHICLECLASS, typeID, clazz)
224
225    def setSpeedFactor(self, typeID, factor):
226        """setSpeedFactor(string, double) -> None
227
228        .
229        """
230        self._connection._sendDoubleCmd(
231            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_SPEED_FACTOR, typeID, factor)
232
233    def setSpeedDeviation(self, typeID, deviation):
234        """setSpeedDeviation(string, double) -> None
235
236        Sets the maximum speed deviation of vehicles of this type.
237        """
238        self._connection._sendDoubleCmd(
239            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_SPEED_DEVIATION, typeID, deviation)
240
241    def setEmissionClass(self, typeID, clazz):
242        """setEmissionClass(string, string) -> None
243
244        Sets the emission class of vehicles of this type.
245        """
246        self._connection._sendStringCmd(
247            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_EMISSIONCLASS, typeID, clazz)
248
249    def setWidth(self, typeID, width):
250        """setWidth(string, double) -> None
251
252        Sets the width in m of vehicles of this type.
253        """
254        self._connection._sendDoubleCmd(
255            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_WIDTH, typeID, width)
256
257    def setHeight(self, typeID, height):
258        """setHeight(string, double) -> None
259
260        Sets the height in m of vehicles of this type.
261        """
262        self._connection._sendDoubleCmd(
263            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_HEIGHT, typeID, height)
264
265    def setMinGap(self, typeID, minGap):
266        """setMinGap(string, double) -> None
267
268        Sets the offset (gap to front vehicle if halting) of vehicles of this type.
269        """
270        self._connection._sendDoubleCmd(
271            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_MINGAP, typeID, minGap)
272
273    def setMinGapLat(self, typeID, minGapLat):
274        """setMinGapLat(string, double) -> None
275
276        Sets the minimum lateral gap at 50km/h of this type.
277        """
278        self._connection._sendDoubleCmd(
279            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_MINGAP_LAT, typeID, minGapLat)
280
281    def setMaxSpeedLat(self, typeID, speed):
282        """setMaxSpeedLat(string, double) -> None
283
284        Sets the maximum lateral speed of this type.
285        """
286        self._connection._sendDoubleCmd(
287            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_MAXSPEED_LAT, typeID, speed)
288
289    def setLateralAlignment(self, typeID, latAlignment):
290        """setLateralAlignment(string, string) -> None
291
292        Sets the preferred lateral alignment of this type.
293        """
294        self._connection._sendStringCmd(
295            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_LATALIGNMENT, typeID, latAlignment)
296
297    def setShapeClass(self, typeID, clazz):
298        """setShapeClass(string, string) -> None
299
300        Sets the shape class of vehicles of this type.
301        """
302        self._connection._sendStringCmd(
303            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_SHAPECLASS, typeID, clazz)
304
305    def setAccel(self, typeID, accel):
306        """setAccel(string, double) -> None
307
308        Sets the maximum acceleration in m/s^2 of vehicles of this type.
309        """
310        self._connection._sendDoubleCmd(
311            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_ACCEL, typeID, accel)
312
313    def setDecel(self, typeID, decel):
314        """setDecel(string, double) -> None
315
316        Sets the maximal comfortable deceleration in m/s^2 of vehicles of this type.
317        """
318        self._connection._sendDoubleCmd(
319            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_DECEL, typeID, decel)
320
321    def setEmergencyDecel(self, typeID, decel):
322        """setDecel(string, double) -> None
323
324        Sets the maximal physically possible deceleration in m/s^2 of vehicles of this type.
325        """
326        self._connection._sendDoubleCmd(
327            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_EMERGENCY_DECEL, typeID, decel)
328
329    def setActionStepLength(self, typeID, actionStepLength, resetActionOffset=True):
330        """setActionStepLength(string, double, bool) -> None
331
332        Sets the action step length for vehicles of this type. If resetActionOffset == True (default), the
333        next action point is scheduled immediately for all vehicles of the type.
334        If resetActionOffset == False, the interval between the last and the next action point is
335        updated to match the given value for all vehicles of the type, or if the latter is smaller
336        than the time since the last action point, the next action follows immediately.
337        """
338        if actionStepLength < 0:
339            raise exceptions.TraCIException("Invalid value for actionStepLength. Given value must be non-negative.")
340        # Use negative value to indicate resetActionOffset == False
341        if not resetActionOffset:
342            actionStepLength *= -1
343        self._connection._sendDoubleCmd(
344            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_ACTIONSTEPLENGTH, typeID, actionStepLength)
345
346    def setApparentDecel(self, typeID, decel):
347        """setDecel(string, double) -> None
348
349        Sets the apparent deceleration in m/s^2 of vehicles of this type.
350        """
351        self._connection._sendDoubleCmd(
352            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_APPARENT_DECEL, typeID, decel)
353
354    def setImperfection(self, typeID, imperfection):
355        """setImperfection(string, double) -> None
356
357        .
358        """
359        self._connection._sendDoubleCmd(
360            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_IMPERFECTION, typeID, imperfection)
361
362    def setTau(self, typeID, tau):
363        """setTau(string, double) -> None
364
365        Sets the driver's tau-parameter (reaction time or anticipation time depending on the car-following model) in s
366        for vehicles of this type.
367        """
368        self._connection._sendDoubleCmd(
369            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_TAU, typeID, tau)
370
371    def setColor(self, typeID, color):
372        """setColor(string, (integer, integer, integer, integer)) -> None
373
374        Sets the color of this type.
375        """
376        self._connection._beginMessage(
377            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.VAR_COLOR, typeID, 1 + 1 + 1 + 1 + 1)
378        self._connection._string += struct.pack("!BBBBB", tc.TYPE_COLOR, int(color[0]), int(color[1]), int(color[2]),
379                                                int(color[3]) if len(color) > 3 else 255)
380        self._connection._sendExact()
381
382    def copy(self, origTypeID, newTypeID):
383        """copy(string, string) -> None
384
385        Duplicates the vType with ID origTypeID. The newly created vType is assigned the ID newTypeID
386        """
387        self._connection._sendStringCmd(
388            tc.CMD_SET_VEHICLETYPE_VARIABLE, tc.COPY, origTypeID, newTypeID)
389
390
391VehicleTypeDomain()
392