@@ -82,19 +82,6 @@ def __init__(self, sensors, actuators, model=None, handler=None, config=None):
8282 monolithic_model_path = PRETRAINED_MODELS + model
8383 print ("Loading model from: " , monolithic_model_path )
8484
85- # self.monolithic_model = ModifiedDeepestLSTM(image_shape=(66, 200, 3), num_labels=2)
86- # self.monolithic_model = resnet18(weights=ResNet18_Weights.DEFAULT)
87- # self.monolithic_model.fc = nn.Linear(self.monolithic_model.fc.in_features, 2)
88- # self.monolithic_model.load_state_dict(torch.load(monolithic_model_path, map_location=self.device))
89- # self.monolithic_model.to(self.device)
90- # self.monolithic_model.eval()
91-
92- # self.monolithic_model = efficientnet_v2_s(weights=None)
93- # self.monolithic_model.classifier[-1] = nn.Linear(self.monolithic_model.classifier[-1].in_features, 2)
94- # self.monolithic_model.load_state_dict(torch.load(monolithic_model_path, map_location=self.device))
95- # self.monolithic_model.to(self.device)
96- # self.monolithic_model.eval()
97-
9885 # onnx model
9986 providers = [('CUDAExecutionProvider' , {})] if ort .get_available_providers ().__contains__ ('CUDAExecutionProvider' ) else ['CPUExecutionProvider' ]
10087 self .ort_session = ort .InferenceSession (str (monolithic_model_path ), providers = providers )
@@ -116,7 +103,7 @@ def __init__(self, sensors, actuators, model=None, handler=None, config=None):
116103 self .prev_yaw = None
117104 self .delta_yaw = 0
118105
119- self .target_point = [160.0 ,- 105 .3 ,0.42 ,0.00 ,0.00 ,180.00 ]
106+ self .target_point = [160.0 ,108 .3 ,0.42 ,0.00 ,0.00 ,180.00 ]
120107 self .termination_code = 0 # 0: not terminated; 1: arrived at target; 2: wrong turn
121108
122109 self ._last_tick = None # para calcular el tiempo entre ticks
@@ -158,8 +145,15 @@ def update_frame(self, frame_id, data):
158145
159146 def predict_controls (self , image_seg ): # se saco model como argumento
160147 # Asegurarse de que sea una imagen BGR como la cargada por cv2.imread
161- calzada_color = [128 , 64 , 128 ]
162- mask = cv2 .inRange (image_seg , np .array (calzada_color ), np .array (calzada_color ))
148+ if image_seg is None :
149+ raise ValueError ("Segmented image is None (sensor may not be ready)." )
150+
151+ if not isinstance (image_seg , np .ndarray ):
152+ print (f"[DEBUG] Segmentation image type: { type (image_seg )} " )
153+
154+
155+ calzada_color = np .array ([128 , 64 , 128 ], dtype = np .uint8 )
156+ mask = cv2 .inRange (image_seg , calzada_color , calzada_color )
163157
164158 masked_image = np .zeros_like (image_seg )
165159 masked_image [mask > 0 ] = [255 , 255 , 255 ]
@@ -176,16 +170,10 @@ def predict_controls(self, image_seg): # se saco model como argumento
176170
177171 gray = cv2 .cvtColor (resized , cv2 .COLOR_BGR2GRAY )
178172 rgb_like = cv2 .merge ([gray , gray , gray ])
179-
173+
174+ # rgb_like /= 255.0
175+
180176 input_tensor = torch .tensor (rgb_like , dtype = torch .float32 ).permute (2 , 0 , 1 )
181- # input_tensor = input_tensor.unsqueeze(0).to(self.device)
182-
183- # with torch.no_grad():
184- # prediction = model(input_tensor)
185-
186- # # steer, throttle = prediction[0].tolist()
187- # steer = prediction[0][0].item()
188- # throttle = prediction[0][1].item()
189177
190178 input_np = input_tensor .unsqueeze (0 ).cpu ().numpy () # [1,3,66,200] float32
191179
@@ -206,22 +194,12 @@ def predict_controls(self, image_seg): # se saco model como argumento
206194
207195 def execute (self ):
208196 """Main loop of the brain. This will be called iteratively each TIME_CYCLE (see pilot.py)"""
209- rgb_image = self .camera_rgb .getImage ().data # rgb_image shape: (768, 1024, 3)
210- seg_image = self .camera_seg .getImage ().data # seg_image shape: (80, 400, 3)
211-
197+ rgb_image = self .camera_rgb .getImage () # .data # rgb_image shape: (768, 1024, 3)
198+ seg_image = self .camera_seg .getImage () # .data # seg_image shape: (80, 400, 3)
199+
212200 self .update_frame ("frame_0" , rgb_image )
213201 self .update_frame ("frame_1" , seg_image )
214-
215- # if hasattr(self, 'pilot'):
216- # current = self.pilot.tick_counter
217- # if self._last_tick is None:
218- # delta = 0
219- # else:
220- # delta = current - self._last_tick
221- # logger.info(f"{delta} ticks -> inferencia")
222- # self._last_tick = current
223-
224- # print("Inferencia ejecutandose") # (600, 800, 3)
202+
225203
226204 steer , throttle = self .predict_controls (seg_image ) # se saco self.monolithic_model como argumento
227205 # print(f"Predicted - Steer: {steer:.3f}, Throttle: {throttle:.3f}")
@@ -236,18 +214,29 @@ def execute(self):
236214 # f"target: ({self.target_point[0]:.2f}, {self.target_point[1]:.2f})")
237215
238216 if self .target_point is not None :
239- distance_to_target = np .sqrt (
240- (self .target_point .x - vehicle_location .x ) ** 2 +
241- (self .target_point .y - (- vehicle_location .y )) ** 2 )
242-
243- # print(f'Euclidean distance to target: {distance_to_target}')
217+ # Aceptar lista o Location
218+ if isinstance (self .target_point , (list , tuple , np .ndarray )):
219+ tx , ty = self .target_point [0 ], self .target_point [1 ]
220+ elif hasattr (self .target_point , "x" ):
221+ tx , ty = self .target_point .x , self .target_point .y
222+ else :
223+ raise ValueError (f"Unsupported type for target_point: { type (self .target_point )} " )
224+
225+ # Coordenadas actuales
226+ vx , vy = vehicle_location .x , vehicle_location .y
227+ # print(f"x: {vx} -> {tx}, y: {vy} -> {ty}")
228+
229+ # Calcular distancia euclidiana 2D
230+ distance_to_target = math .hypot (tx - vx , ty - vy )
231+
244232 if distance_to_target < 1.5 :
245233 self .termination_code = 1
246- arrived = True
247- print (f"======== Arrived at target point { distance_to_target } m away============." )
248-
234+ print (f"======== Arrived at target point ({ tx :.1f} , { ty :.1f} ) | { distance_to_target :.2f} m away ==========" )
235+
249236
250237 self .motors .sendThrottle (throttle )
251238 self .motors .sendSteer (steer )
252239 self .motors .sendBrake (0.0 )
253-
240+
241+
242+
0 commit comments