|
5 | 5 | import threading |
6 | 6 | import copy |
7 | 7 |
|
| 8 | +import numpy as np |
| 9 | + |
8 | 10 | class OmniscientAgentDetector(Component): |
9 | 11 | """Obtains agent detections from a simulator""" |
10 | | - def __init__(self,vehicle_interface : GEMInterface): |
| 12 | + |
| 13 | + def __init__(self, vehicle_interface: GEMInterface): |
11 | 14 | self.vehicle_interface = vehicle_interface |
12 | 15 | self.agents = {} |
13 | 16 | self.lock = threading.Lock() |
14 | 17 |
|
| 18 | + self.start_pose = None |
| 19 | + |
15 | 20 | def rate(self): |
16 | 21 | return 4.0 |
17 | | - |
| 22 | + |
18 | 23 | def state_inputs(self): |
19 | | - return [] |
20 | | - |
| 24 | + return ['vehicle'] |
| 25 | + |
21 | 26 | def state_outputs(self): |
22 | 27 | return ['agents'] |
23 | 28 |
|
24 | 29 | def initialize(self): |
25 | | - self.vehicle_interface.subscribe_sensor('agent_detector',self.agent_callback, AgentState) |
26 | | - |
27 | | - def agent_callback(self, name : str, agent : AgentState): |
| 30 | + self.vehicle_interface.subscribe_sensor('agent_detector', self.agent_callback, AgentState) |
| 31 | + |
| 32 | + def agent_callback(self, name: str, agent: AgentState): |
28 | 33 | with self.lock: |
29 | 34 | self.agents[name] = agent |
30 | 35 |
|
31 | | - def update(self) -> Dict[str,AgentState]: |
| 36 | + def update(self, vehicle : VehicleState) -> Dict[str, AgentState]: |
32 | 37 | with self.lock: |
33 | | - return copy.deepcopy(self.agents) |
| 38 | + res = {} |
| 39 | + ped_num = 0 |
| 40 | + |
| 41 | + if self.start_pose is None: |
| 42 | + self.start_pose = vehicle.pose |
| 43 | + # print("\nVehicle start state self.start_pose:", self.start_pose) |
| 44 | + # print("\nVehicle current state:", vehicle.pose, vehicle.v) |
| 45 | + |
| 46 | + for n, a in self.agents.items(): |
| 47 | + # print("\nBefore to_frame: Agent:", n, a.pose, a.velocity) |
| 48 | + a = a.to_frame(ObjectFrameEnum.START, current_pose = a.pose, start_pose_abs = self.start_pose) |
| 49 | + # print("\nAfter to_frame START: Agent:", n, a.pose, a.velocity) |
| 50 | + # print('==============', a.pose.frame==ObjectFrameEnum.START) |
| 51 | + res[n] = a |
| 52 | + if a.type == AgentEnum.PEDESTRIAN: |
| 53 | + ped_num += 1 |
| 54 | + if ped_num > 0: |
| 55 | + print("\nDetected", ped_num, "pedestrians") |
| 56 | + return res |
0 commit comments