Guest User

Untitled

a guest
Feb 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. from __future__ import division
  2. from geopy.distance import vincenty
  3. import math
  4. from random import randint
  5.  
  6. class Car(object):
  7. def __init__(self):
  8. '''
  9. Initialize class level variables here. If we needed to pass in objects (instances of classes)
  10. we could do that too.
  11. '''
  12. self.color = "white"
  13. self.engine = "gasoline"
  14. self.driver = None
  15. self.fuelTankCapacity = 10
  16. self.fuelUsed = 0
  17. self.fuelGauge = 100
  18. self.fuelLevel = 10
  19. self.milesPerGallon = 10
  20.  
  21. def updateFuelGauge(self):
  22. '''
  23. This function controls the update of the fuel gauge. Since we need to update the fuel gauge from many places
  24. in the code we want to make this a function that we can call versus duplicating the code everywhere/
  25. '''
  26. self.fuelGauge = int((self.fuelLevel/self.fuelTankCapacity)*100)
  27.  
  28. def burnGas(self, miles):
  29. '''
  30. Each time the PID is run this is how we tell the car to update how much gas it has burned. We also update the fuel gauge for
  31. convenience sake.
  32. '''
  33. burntGallons = miles/self.milesPerGallon
  34. self.fuelUsed += burntGallons
  35. self.fuelLevel -= burntGallons
  36. self.updateFuelGauge()
  37.  
  38. def howMuchGasIsLeft(self):
  39. """
  40. This Class level function returns the fuel level value
  41. """
  42. return self.fuelGauge
  43.  
  44. def fillGasTank(self, gallons):
  45. """
  46. This function refills the gas tank until it is full without regard to how much you are trying to
  47. put into the tank.
  48. """
  49. if (self.fuelLevel + gallons) >= self.fuelTankCapacity:
  50. self.fuelLevel = self.fuelTankCapacity
  51. else:
  52. self.fuelLevel += gallons
  53.  
  54. self.updateFuelGauge()
  55.  
  56.  
  57. class Driver(object):
  58. def __init__(self, drivername, car):
  59. '''
  60. Here in the initialization of the class, I am passing in an instance of a car, as well
  61. as the driver's name. I need the car instance to observe when I need to get gas. Since the driver
  62. drives the car, this is where the PID loop (aka Driving) is logically located. I also set up
  63. a class level variable to let me know how far I have driven the car. This could also be an odometer on the
  64. car object, but we are using this like a trip mileage counter.
  65. '''
  66. self.name = drivername
  67. self.car = car
  68. self.milesDriven = 0
  69.  
  70. def drive(self, route):
  71. interval = 10
  72. for i in range(0, route.distance, interval):
  73. '''
  74. This is the main PID loop for the "drive the car" concept.
  75. This loop will run from the starting point, over the distance, but we only execute this code below
  76. over the interval. Right now, I have it set to only report every 10 miles.
  77. '''
  78. self.milesDriven += interval
  79. self.car.burnGas(interval)
  80. print("%i miles driven "%self.milesDriven)
  81. print("Fuel gauge: %i gallons used overall on this trip, %i percent left in tank"%(self.car.fuelUsed, self.car.howMuchGasIsLeft()))
  82. self.checkGasLevel()
  83. print("Driving......")
  84.  
  85. def checkGasLevel(self):
  86. '''
  87. Checking to see if we need to get gas or not; typically I like to get gas when I am about 1/4 tank.
  88. '''
  89. if self.car.howMuchGasIsLeft() <= 25:
  90. print("%i percent of gas left! Running OUT OF GAS!!!"%self.car.howMuchGasIsLeft())
  91. self.getGas()
  92.  
  93. def getGas(self):
  94. '''
  95. for fun, I only put from 1 to 8 gallons in my tank at a time. Here I operate on the car object instance
  96. telling the car that I am filling the gas tank.
  97. '''
  98. print("Stopping for gas.")
  99. boughtGallons = randint(1, 8)
  100. print("Bought %i gallons of gas"%boughtGallons)
  101. self.car.fillGasTank(boughtGallons)
  102.  
  103.  
  104. class GoSomewhere(object):
  105. def __init__(self, pointA, pointB):
  106. self.pointA = pointA
  107. self.pointB = pointB
  108. #floor the distance result and then make it an integer explicitly, because python
  109. self.distance = int(math.floor( vincenty(newport_ri, cleveland_oh).miles))
  110.  
  111.  
  112. '''
  113. This codeblock below is the magic...
  114. It allows you to construct the needed classes and operate on them
  115. It is magic, because we use this when testing, but when in production there is other magic.
  116. Essentially it allows us to test theses classes in a standalone fashion
  117. '''
  118. if __name__ == '__main__':
  119. '''
  120. This make the execution of the code very easy to understand.
  121. '''
  122. #Create an instance of a car
  123. car = Car()
  124. #Create an instance of a driver
  125. driver = Driver("John", car)
  126. #Set up a waypoint
  127. newport_ri = (41.49008, -71.312796)
  128. #and another
  129. cleveland_oh = (41.499498, -81.695391)
  130. #create a route to go somewhere
  131. gotoCleveland = GoSomewhere(newport_ri, cleveland_oh)
  132. #drive the car
  133. print("Hi! I am %s and I am driving to Cleveland"%(driver.name))
  134. driver.drive(gotoCleveland)
  135. #Go back home
  136. print("Going back home")
  137. goHome = GoSomewhere(cleveland_oh, newport_ri)
  138. driver.drive(goHome)
  139. print("Arrived at home.")
Add Comment
Please, Sign In to add comment