parameters: (self, bag_file)
docstring: Calculate mileage
docstring_summary: Calculate mileage
docstring_tokens: Calculate mileage
function: def calculate(self, bag_file):
          """
          Calculate mileage
          """
          last_pos = None
          last_mode = 'Unknown'
          mileage = collections.defaultdict(lambda: 0.0)
          chassis = chassis_pb2.Chassis()
          localization = localization_pb2.LocalizationEstimate()
          reader = RecordReader(bag_file)
          for msg in reader.read_messages():
              if msg.topic == kChassisTopic:
                  chassis.ParseFromString(msg.message)
                  # Mode changed
                  if last_mode != chassis.driving_mode:
                      if (last_mode == Chassis.COMPLETE_AUTO_DRIVE and
                              chassis.driving_mode == Chassis.EMERGENCY_MODE):
                          self.disengagements += 1
                      last_mode = chassis.driving_mode
                      # Reset start position.
                      last_pos = None
              elif msg.topic == kLocalizationTopic:
                  localization.ParseFromString(msg.message)
                  cur_pos = localization.pose.position
                  if last_pos:
                      # Accumulate mileage, from xyz-distance to miles.
                      mileage[last_mode] += 0.000621371 * math.sqrt(
                          (cur_pos.x - last_pos.x) ** 2 +
                          (cur_pos.y - last_pos.y) ** 2 +
                          (cur_pos.z - last_pos.z) ** 2)
                  last_pos = cur_pos
          self.auto_mileage += mileage[Chassis.COMPLETE_AUTO_DRIVE]
          self.manual_mileage += (mileage[Chassis.COMPLETE_MANUAL] +
                                  mileage[Chassis.EMERGENCY_MODE])
function_tokens: def calculate ( self , bag_file ) : last_pos = None last_mode = 'Unknown' mileage = collections . defaultdict ( lambda : 0.0 ) chassis = chassis_pb2 . Chassis ( ) localization = localization_pb2 . LocalizationEstimate ( ) reader = RecordReader ( bag_file ) for msg in reader . read_messages ( ) : if msg . topic == kChassisTopic : chassis . ParseFromString ( msg . message ) # Mode changed if last_mode != chassis . driving_mode : if ( last_mode == Chassis . COMPLETE_AUTO_DRIVE and chassis . driving_mode == Chassis . EMERGENCY_MODE ) : self . disengagements += 1 last_mode = chassis . driving_mode # Reset start position. last_pos = None elif msg . topic == kLocalizationTopic : localization . ParseFromString ( msg . message ) cur_pos = localization . pose . position if last_pos : # Accumulate mileage, from xyz-distance to miles. mileage [ last_mode ] += 0.000621371 * math . sqrt ( ( cur_pos . x - last_pos . x ) ** 2 + ( cur_pos . y - last_pos . y ) ** 2 + ( cur_pos . z - last_pos . z ) ** 2 ) last_pos = cur_pos self . auto_mileage += mileage [ Chassis . COMPLETE_AUTO_DRIVE ] self . manual_mileage += ( mileage [ Chassis . COMPLETE_MANUAL ] + mileage [ Chassis . EMERGENCY_MODE ] )