1010import numpy as np
1111
1212
13-
1413class CarSpawner (object ):
1514 def __init__ (self ):
1615 self .node = Node ()
@@ -23,22 +22,23 @@ def __init__(self):
2322 self .spawn_positions = [
2423 (2 , - 20.0 , 1.5 , 1.57 ),
2524 (- 2 , 20.0 , 1.5 , - 1.57 ),
26- (20.0 , 2 , 1.5 , 3.14 )
25+ (20.0 , 2 , 1.5 , 3.14 ),
2726 ]
27+
2828 def update (self , info , _ecm ):
2929 current_time = time .time ()
30-
30+
3131 # Spawn new car
3232 if current_time - self .last_spawn > self .spawn_interval :
3333 coords = random .choice (self .spawn_positions )
3434 model_name = f"car_{ random .randint (1000 ,9999 )} "
35-
36- yaw = coords [3 ]
35+
36+ yaw = coords [3 ]
3737 qx , qy , qz , qw = etoq (yaw , 0 , 0 )
38-
38+
3939 req = EntityFactory ()
40-
41- req .relative_to = ''
40+
41+ req .relative_to = ""
4242 req .sdf_filename = self .sdf_path
4343 req .name = model_name
4444 req .pose .position .x = coords [0 ]
@@ -48,11 +48,11 @@ def update(self, info, _ecm):
4848 req .pose .orientation .x = qx
4949 req .pose .orientation .y = qy
5050 req .pose .orientation .z = qz
51-
51+
5252 self .node .request (
53- f"/world/{ self .world } /create" ,
54- req , EntityFactory , Boolean , 1000 )
55-
53+ f"/world/{ self .world } /create" , req , EntityFactory , Boolean , 1000
54+ )
55+
5656 self .active_models [model_name ] = current_time
5757 self .last_spawn = current_time
5858 print (f"Spawned { model_name } " )
@@ -63,25 +63,30 @@ def update(self, info, _ecm):
6363 self .node .request (
6464 f"/world/{ self .world } /remove" ,
6565 Entity (name = name , type = Entity .MODEL ),
66- Entity , Boolean , 1000 )
66+ Entity ,
67+ Boolean ,
68+ 1000 ,
69+ )
6770 del self .active_models [name ]
6871 print (f"Removed { name } " )
6972
73+
7074def etoq (yaw , pitch , roll ):
71-
72- cy = np .cos (yaw * 0.5 )
73- sy = np .sin (yaw * 0.5 )
74- cp = np .cos (pitch * 0.5 )
75- sp = np .sin (pitch * 0.5 )
76- cr = np .cos (roll * 0.5 )
77- sr = np .sin (roll * 0.5 )
7875
79- qw = cr * cp * cy + sr * sp * sy
80- qx = sr * cp * cy - cr * sp * sy
81- qy = cr * sp * cy + sr * cp * sy
82- qz = cr * cp * sy - sr * sp * cy
76+ cy = np .cos (yaw * 0.5 )
77+ sy = np .sin (yaw * 0.5 )
78+ cp = np .cos (pitch * 0.5 )
79+ sp = np .sin (pitch * 0.5 )
80+ cr = np .cos (roll * 0.5 )
81+ sr = np .sin (roll * 0.5 )
82+
83+ qw = cr * cp * cy + sr * sp * sy
84+ qx = sr * cp * cy - cr * sp * sy
85+ qy = cr * sp * cy + sr * cp * sy
86+ qz = cr * cp * sy - sr * sp * cy
87+
88+ return qx , qy , qz , qw
8389
84- return qx , qy , qz , qw
8590
8691def get_system ():
87- return CarSpawner ()
92+ return CarSpawner ()
0 commit comments