tracker.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import numpy as np
  2. import warnings
  3. import logging
  4. class Sensor:
  5. def __init__(self, tracker, sensor_rad=150) -> None:
  6. self.type = 'Cam'
  7. self.tracker = tracker
  8. self.coverage_radius = sensor_rad
  9. def get_detection(self, targets):
  10. detections = []
  11. for target in targets:
  12. if np.linalg.norm(target.position - self.tracker.position) < self.coverage_radius:
  13. detection = (target.position-self.tracker.position)
  14. logging.info(
  15. f"Ture {self.tracker.name}{self.tracker.id} detection:\n\t\t" + str(detection))
  16. detection += np.random.normal(loc=0, scale=2, size=2)
  17. distance = np.linalg.norm(detection)
  18. if distance > self.coverage_radius:
  19. detection = detection * (self.coverage_radius/distance)
  20. detections.append(detection)
  21. logging.info(
  22. f"Noisy {self.tracker.name}{self.tracker.id} detection:\n\t\t"+str(detection))
  23. return detections
  24. class Robot:
  25. def __init__(self, name: str, id: int, position: np.array, rate: int) -> None:
  26. """The base Robot class
  27. Parameters
  28. ----------
  29. name : str
  30. Robot's name
  31. id : int
  32. Robot's ID
  33. position : np.array
  34. Robot's start position
  35. rate : int
  36. Simulator's update rate
  37. """
  38. self.name = name
  39. self.id = id
  40. self.position = position
  41. self.desierd_position = self.position
  42. self.in_position = True
  43. self.speed = 0.0
  44. self.rate = rate
  45. self.max_speed = 10.0 # m/s
  46. def move(self):
  47. """Move the robot to a position
  48. Parameters
  49. ----------
  50. f : Function, optional
  51. a funcrtion that returns a position, by default None
  52. """
  53. logging.warning("Moving function not impelemented")
  54. # warnings.warn("moving function not Impelement")
  55. pass
  56. def waypoint_ctrl(self, speed, desierd_pos: np.array = None):
  57. # if got new waypoint, update
  58. if not np.any(desierd_pos == None):
  59. self.desierd_position = desierd_pos
  60. if speed > self.max_speed:
  61. logging.warning("Given speed is over maximum")
  62. self.speed = speed
  63. if np.any(self.position != self.desierd_position):
  64. one_time_step_length = speed/self.rate
  65. difference = self.desierd_position - self.position
  66. distance = np.linalg.norm(difference)
  67. # if it's close enough to the goal position, make it in position
  68. if distance <= one_time_step_length:
  69. self.position = self.desierd_position
  70. else:
  71. # move to the goal point at the given speed
  72. direction = difference/distance
  73. self.position = self.position + direction*self.speed/self.rate
  74. def job(self):
  75. pass
  76. # self.position = self.speed*(1/self.rate) + self.position
  77. class Tracker(Robot):
  78. def __init__(self, name: str, id: int, position: np.array, sensor_rad, rate: int) -> None:
  79. super().__init__(name, id, position, rate)
  80. self.sensor = Sensor(self, sensor_rad)
  81. self.neighbor = set()
  82. def job(self):
  83. self.waypoint_ctrl(speed=50, desierd_pos=np.array([500, 500]))