common.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import math
  2. import multiprocessing
  3. import numpy as np
  4. from abc import ABC, abstractmethod
  5. from collections import namedtuple
  6. W, H = 1928, 1208
  7. vec3 = namedtuple("vec3", ["x", "y", "z"])
  8. class GPSState:
  9. def __init__(self):
  10. self.latitude = 0
  11. self.longitude = 0
  12. self.altitude = 0
  13. def from_xy(self, xy):
  14. """Simulates a lat/lon from an xy coordinate on a plane, for simple simulation. TODO: proper global projection?"""
  15. BASE_LAT = 32.75308505188913
  16. BASE_LON = -117.2095393365393
  17. DEG_TO_METERS = 100000
  18. self.latitude = float(BASE_LAT + xy[0] / DEG_TO_METERS)
  19. self.longitude = float(BASE_LON + xy[1] / DEG_TO_METERS)
  20. self.altitude = 0
  21. class IMUState:
  22. def __init__(self):
  23. self.accelerometer: vec3 = vec3(0,0,0)
  24. self.gyroscope: vec3 = vec3(0,0,0)
  25. self.bearing: float = 0
  26. class SimulatorState:
  27. def __init__(self):
  28. self.valid = False
  29. self.is_engaged = False
  30. self.ignition = True
  31. self.velocity: vec3 = None
  32. self.bearing: float = 0
  33. self.gps = GPSState()
  34. self.imu = IMUState()
  35. self.steering_angle: float = 0
  36. self.user_gas: float = 0
  37. self.user_brake: float = 0
  38. self.user_torque: float = 0
  39. self.cruise_button = 0
  40. self.left_blinker = False
  41. self.right_blinker = False
  42. @property
  43. def speed(self):
  44. return math.sqrt(self.velocity.x ** 2 + self.velocity.y ** 2 + self.velocity.z ** 2)
  45. class World(ABC):
  46. def __init__(self, dual_camera):
  47. self.dual_camera = dual_camera
  48. self.image_lock = multiprocessing.Semaphore(value=0)
  49. self.road_image = np.zeros((H, W, 3), dtype=np.uint8)
  50. self.wide_road_image = np.zeros((H, W, 3), dtype=np.uint8)
  51. self.exit_event = multiprocessing.Event()
  52. @abstractmethod
  53. def apply_controls(self, steer_sim, throttle_out, brake_out):
  54. pass
  55. @abstractmethod
  56. def tick(self):
  57. pass
  58. @abstractmethod
  59. def read_state(self):
  60. pass
  61. @abstractmethod
  62. def read_sensors(self, simulator_state: SimulatorState):
  63. pass
  64. @abstractmethod
  65. def read_cameras(self):
  66. pass
  67. @abstractmethod
  68. def close(self, reason: str):
  69. pass
  70. @abstractmethod
  71. def reset(self):
  72. pass