Skip to content

Conversation

@pszwan84
Copy link
Owner

  1. class SelfDrivingVehicle:
  2. def __init__(self, make, model):
    
  3.     self.make = make
    
  4.     self.model = model
    
  5.     self.current_speed = 0
    
  6.     self.is_engine_on = False
    
  7. def start_engine(self):
    
  8.     self.is_engine_on = True
    
  9. def stop_engine(self):
    
  10.     self.is_engine_on = False
    
  11. def accelerate(self, speed):
    
  12.     if self.is_engine_on:
    
  13.         self.current_speed += speed
    
  14. def brake(self, speed):
    
  15.     if self.current_speed >= speed:
    
  16.         self.current_speed -= speed
    
  17.     else:
    
  18.         self.current_speed = 0
    
  19. def get_speed(self):
    
  20.     return self.current_speed
    
  21. def autonomous_drive(self):
    
  22.     if self.is_engine_on:
    
  23.         # AI perception module
    
  24.         obstacle_distance = self.detect_obstacle()
    
  25.         # AI decision-making module
    
  26.         if obstacle_distance < 10:  # If obstacle is closer than 10 units
    
  27.             self.brake(5)  # Apply brakes
    
  28.         else:
    
  29.             self.accelerate(10)  # Continue accelerating
    
  30.         # AI control module
    
  31.         # Send control signals to actuators (e.g., steering, acceleration, braking)
    
  32. def detect_obstacle(self):
    
  33.     # Simulated obstacle detection algorithm
    
  34.     # Returns the distance to the nearest obstacle
    
  35.     return 8.5  # Simulated obstacle distance
    
  36. Create an instance of a self-driving vehicle

  37. car = SelfDrivingVehicle("Tesla", "Model S")
  38. Start the engine

  39. car.start_engine()
  40. Simulate autonomous driving

  41. car.autonomous_drive()
  42. Print the current speed

  43. print("Current Speed:", car.get_speed())
  44. Stop the engine

  45. car.stop_engine()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants