carla.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #!/usr/bin/env python
  2. # Copyright (c) 2021 Intel Corporation
  3. #
  4. # This work is licensed under the terms of the MIT license.
  5. # For a copy, see <https://opensource.org/licenses/MIT>.
  6. """
  7. This module provides mocked CARLA classes for unittesting
  8. """
  9. import copy
  10. class command:
  11. blueprint = None
  12. def SpawnActor(blueprint, point):
  13. new_command = command()
  14. new_command.blueprint = copy.deepcopy(blueprint)
  15. return new_command
  16. def SetSimulatePhysics(blueprint, physics):
  17. return None
  18. def FutureActor():
  19. return None
  20. def ApplyTransform():
  21. return None
  22. def SetAutopilot(actor, autopilot, port):
  23. return None
  24. def SetVehicleLightState():
  25. return None
  26. def DestroyActor(actor):
  27. return None
  28. def then(self, other_command):
  29. return self
  30. class CarlaBluePrint(object):
  31. def __init__(self):
  32. self.id = 0
  33. self.attributes = {'role_name': ''}
  34. def has_attribute(self, attribute_string=''):
  35. return attribute_string in self.attributes
  36. def set_attribute(self, key, value):
  37. self.attributes[key] = value
  38. def has_tag(self, tag_string=''):
  39. return False
  40. class CarlaBluePrintLibrary:
  41. def filter(self, filterstring):
  42. return [CarlaBluePrint()]
  43. def __len__(self):
  44. return 1
  45. def find(self, filterstring):
  46. return CarlaBluePrint()
  47. class GeoLocation:
  48. longitude = 0
  49. latitude = 0
  50. class Vector3D:
  51. x = 0
  52. y = 0
  53. z = 0
  54. def __init__(self, x=0, y=0, z=0):
  55. self.x = x
  56. self.y = y
  57. self.z = z
  58. class Location():
  59. x = 0
  60. y = 0
  61. z = 0
  62. def __init__(self, x=0, y=0, z=0):
  63. self.x = x
  64. self.y = y
  65. self.z = z
  66. def distance(self, other):
  67. return 0
  68. class Rotation():
  69. pitch = 0
  70. roll = 0
  71. yaw = 0
  72. def __init__(self, pitch=0, roll=0, yaw=0):
  73. self.pitch = pitch
  74. self.roll = roll
  75. self.yaw = yaw
  76. def get_forward_vector(self):
  77. return Vector3D()
  78. class Transform:
  79. location = Location(0, 0, 0)
  80. rotation = Rotation(0, 0, 0)
  81. def __init__(self, location=Location(0, 0, 0), rotation=Rotation(0, 0, 0)):
  82. self.location = location
  83. self.rotation = rotation
  84. class Waypoint():
  85. transform = Transform(Location(), Rotation())
  86. road_id = 0
  87. lane_id = 0
  88. s = 0
  89. lane_width = 0
  90. class Map:
  91. name = ""
  92. def get_spawn_points(self):
  93. return []
  94. def transform_to_geolocation(self, transform):
  95. return GeoLocation()
  96. def get_waypoint(self, transform):
  97. return Waypoint()
  98. def get_waypoint_xodr(self, a, b, c):
  99. return Waypoint()
  100. def get_topology(self):
  101. return []
  102. class TrafficLightState:
  103. Red = 0
  104. Green = 1
  105. Yellow = 2
  106. Off = 3
  107. class WeatherParameters:
  108. cloudiness = 0.000000
  109. cloudiness = 0.000000
  110. precipitation = 0.000000
  111. precipitation_deposits = 0.000000
  112. wind_intensity = 0.000000
  113. sun_azimuth_angle = 0.000000
  114. sun_altitude_angle = 0.000000
  115. fog_density = 0.000000
  116. fog_distance = 0.000000
  117. fog_falloff = 0.000000
  118. wetness = 0.000000
  119. scattering_intensity = 0.000000
  120. mie_scattering_scale = 0.000000
  121. rayleigh_scattering_scale = 0.033100
  122. class WorldSettings:
  123. synchronous_mode = False
  124. no_rendering_mode = False
  125. fixed_delta_seconds = 0
  126. substepping = True
  127. max_substep_delta_time = 0.01
  128. max_substeps = 10
  129. max_culling_distance = 0
  130. deterministic_ragdolls = False
  131. class ActorList:
  132. def __init__(self, actor_list):
  133. self.actor_list = actor_list
  134. def filter(self, filterstring):
  135. return []
  136. def __len__(self):
  137. return len(self.actor_list)
  138. def __getitem__(self, i):
  139. return self.actor_list[i]
  140. class Control:
  141. steer = 0
  142. throttle = 0
  143. brake = 0
  144. class Actor:
  145. def __init__(self):
  146. self.attributes = {'role_name': ''}
  147. self.id = 0
  148. self.type_id = None
  149. self.location = Location()
  150. self.rotation = Rotation()
  151. self.transform = Transform(self.location, self.rotation)
  152. self.is_alive = True
  153. def get_transform(self):
  154. return self.transform
  155. def get_location(self):
  156. return self.location
  157. def get_world(self):
  158. return World()
  159. def get_control(self):
  160. return Control()
  161. def destroy(self):
  162. del self
  163. def listen(self, callback):
  164. pass
  165. class Walker(Actor):
  166. is_walker = True
  167. class Vehicle(Actor):
  168. is_vehicle = True
  169. class World:
  170. actors = []
  171. def get_settings(self):
  172. return WorldSettings()
  173. def get_map(self):
  174. return Map()
  175. def get_blueprint_library(self):
  176. return CarlaBluePrintLibrary()
  177. def wait_for_tick(self):
  178. pass
  179. def get_actors(self, ids=[]):
  180. actor_list = []
  181. for actor in self.actors:
  182. if actor.id in ids:
  183. actor_list.append(actor)
  184. return ActorList(actor_list)
  185. def try_spawn_actor(self, blueprint, spawn_point):
  186. new_actor = Vehicle()
  187. new_actor.attributes['role_name'] = blueprint.attributes['role_name']
  188. new_actor.id = len(self.actors)
  189. self.actors.append(new_actor)
  190. return new_actor
  191. def spawn_actor(self, blueprint, spawn_point, attach_to):
  192. new_actor = self.try_spawn_actor(blueprint, spawn_point)
  193. return new_actor
  194. class Client:
  195. world = World()
  196. def load_world(self, name):
  197. return None
  198. def get_world(self):
  199. return self.world
  200. def get_trafficmanager(self, port):
  201. return None
  202. def apply_batch_sync(self, batch, sync_mode=False):
  203. class Response:
  204. def __init__(self, id):
  205. self.actor_id = id
  206. self.error = None
  207. reponse_list = []
  208. for batch_cmd in batch:
  209. if batch_cmd is not None:
  210. new_actor = Vehicle()
  211. new_actor.attributes['role_name'] = batch_cmd.blueprint.attributes['role_name']
  212. new_actor.id = len(self.world.actors)
  213. self.world.actors.append(new_actor)
  214. reponse_list.append(Response(new_actor.id))
  215. return reponse_list