Skip to content

Commit

Permalink
Merge pull request #187 from COS301-SE-2024/hotfix/carla
Browse files Browse the repository at this point in the history
added lane following script
  • Loading branch information
ReynekeD authored Sep 30, 2024
2 parents 1a6f195 + f588508 commit 3770245
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
6 changes: 4 additions & 2 deletions Process/pipe4/temp/lane_following.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,14 +1457,16 @@ def follow_lane(out_image, filtered_results, original, previous_left_id=None, pr
out_image, vertical_line, right_diagonal, left_diagonal = get_angle_lines(out_image)
out_image, in_lane, left_intersections, middle_intersections, right_intersections = find_intersections_and_draw(out_image, vertical_line, right_diagonal, left_diagonal, mask)

image_copy = original.copy()

if in_lane:
print("The vehicle is in the lane.")

steer = analise_results(in_lane, left_intersections, middle_intersections, right_intersections)

cv2.putText(out_image, f'Steer: {steer:.2f}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.putText(image_copy, f'Steer: {steer:.2f}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

return out_image, mask, steer, previous_left_id, previous_right_id
return image_copy, mask, steer, previous_left_id, previous_right_id
else:
steer = 0
print("Take manual control of the vehicle.")
Expand Down
12 changes: 6 additions & 6 deletions Process/pipe4/temp/outputUnit.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ def process(self, data_token):
cv2.rectangle(annotated_frame, (x_min, y_min), (x_max, y_max), (0, 255, 0), 1)

if min_distances and min_distances[i] is not None:
if min_distances[i] is not None:
text = f"{bbox[-1]} Dist: {min_distances[i]:.2f}m"
cv2.putText(annotated_frame, text, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
else:
text = f"{bbox[-1]}"
cv2.putText(annotated_frame, text, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
if min_distances[i] is not None:
text = f"{bbox[-1]} Dist: {min_distances[i]:.2f}m"
cv2.putText(annotated_frame, text, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
else:
text = f"{bbox[-1]}"
cv2.putText(annotated_frame, text, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

if (self.la or self.all) and data_token.get_flag('has_lane_data'):
output = data_token.get_processing_result('laneUnit')
Expand Down
24 changes: 19 additions & 5 deletions Process/pipe4/temp/unit3.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,35 @@
object_avoidance = False
avoid = False

reverse_toggle = False
reverse_gear = False


def get_keyboard_control(vehicle):
# Toggle lane following with Q
global follow_lane
global object_avoidance
global reverse_toggle
global reverse_gear
control = carla.VehicleControl()

keys = pygame.key.get_pressed()
control = carla.VehicleControl()

if not follow_lane:
keys = pygame.key.get_pressed()
control = carla.VehicleControl()
control.throttle = 1.0 if keys[pygame.K_w] else 0.0
control.brake = 1.0 if keys[pygame.K_s] else 0.0
control.steer = -1.0 if keys[pygame.K_a] else 1.0 if keys[pygame.K_d] else 0.0
control.hand_brake = keys[pygame.K_SPACE]
control.reverse = not control.reverse if keys[pygame.K_r] else control.reverse

if keys[pygame.K_r] and not reverse_toggle:
reverse_gear = not reverse_gear
reverse_toggle = True

if not keys[pygame.K_r]:
reverse_toggle = False

control.reverse = reverse_gear
else:
control.throttle = 0.3

Expand Down Expand Up @@ -523,7 +537,7 @@ def main(pipe):
image_surface = convert_array_to_surface(processed_image_array)
display.blit(image_surface, (0, 0))

if pipe.dataToken.get_flag("hasObserverData") and object_avoidance:
if pipe.dataToken.get_flag("hasObeserverData") and object_avoidance:
# Render the text "Object avoidance on"
text_surface = font.render("Object avoidance on", True, (0, 255, 0)) # Green text
display.blit(text_surface, (10, 10))
Expand All @@ -537,7 +551,7 @@ def main(pipe):
# print(object_avoidance)
# print( pipe.dataToken.get_flag("hasObserverData"))

if pipe.dataToken.get_flag("hasObserverData") and object_avoidance:
if pipe.dataToken.get_flag("hasObeserverData") and object_avoidance:
print("avoiding")
observerToken = pipe.dataToken.get_processing_result('observerUnit')
breaking = observerToken['breaking']
Expand Down

0 comments on commit 3770245

Please sign in to comment.