Beispiel #1
0
    def averages_calculations(self):
        accelerations = list()
        #decelerations = list()
        throttles = list()
        rpms = list()
        speeds = list()
        lats = list()
        longs = list()
        mpgs = list()
        coolants = list()

        accel_average = 0
        #decel_average = 0
        throttle_average = 0
        rpm_average = 0
        speed_average = 0
        mpg_average = 0
        coolant_average = 0
    
        record_accel = 0
        #record_decel = 0
        record_rpm = 0
        record_throttle = 0
        record_speed = 0
        total_distance = 0
        record_mpg = 0

        for value in VBoxData.select():
            if(value.acc_x > IMU_ZEROED_ACCEL_X): #exclude resting values
                accelerations.append(value.acc_x) 
            #else:
                #decelerations.append(value.acc_x)
            if not value.obd_throttle_pos == None and not value.obd_throttle_pos == 0:
                throttles.append(value.obd_throttle_pos) # need to change from obd_throttle_pos to obd_throttle_pos
            if not value.obd_rpm == None and not value.obd_rpm == 0:
                rpms.append(value.obd_rpm)
            if not value.obd_speed == None and not value.obd_speed == 0:
                speeds.append(value.obd_speed)
            if not value.gps_lat == None:
                lats.append(value.gps_lat)
            if not value.gps_lon == None:
                longs.append(value.gps_lat)
            if not value.obd_maf == None and value.obd_maf != 0 and not value.obd_speed == None and value.obd_speed != 0:
                #print '***MPG: ', 710.7 * (value.obd_speed / value.obd_maf)
                mpgs.append(710.7 * (value.obd_speed / value.obd_maf))
            if not value.obd_coolant_temp == None and value.obd_coolant_temp:
                coolants.append(value.obd_coolant_temp)
                    
                
        count = 0
        for distance in lats:
            if count+1 <= len(lats):
                total_distance = total_distance #+ distance_on_unit_sphere(lats[count+1], longs[count], lats[count+1], longs[count]) 
                count = count + 1   
                
        for value in ProfileData.select().where(ProfileData.flag == 1):
            record_accel = value.avg_accel
            #record_decel = value.avg_decel
            record_rpm = value.avg_rpm
            record_throttle = value.avg_throttle
            record_speed = value.avg_speed
            record_mpg = value.mpg
            
        try:
            accel_average = float(sum(accelerations)/len(accelerations))
            #decel_average = float(sum(decelerations)/len(decelerations))
            rpm_average = float(sum(rpms)/len(rpms))
            throttle_average = float(sum(throttles)/len(throttles))
            speed_average = float(sum(speeds)/len(speeds))
            mpg_average = float(sum(mpgs)/len(mpgs))
            coolant_average = float(sum(coolants)/len(coolants))  
        except ZeroDivisionError:
            #print "divide by zero\n"
            pass

        averages = [accel_average, throttle_average, rpm_average, speed_average, mpg_average, coolant_average, total_distance]

        return averages
Beispiel #2
0
    def run(self):

        while not self.stopped():
            try:
                if not EventBuffer.empty():
                 
                    # get next event
                    event_timestamp = EventBuffer.get()
                    
                    # commit remaining db transaction
                    with self.db.transaction() as txn:
                        for record in TXN:
                            record.save()
                    del TXN[:]
           
                    # Set save locations of files
                    records_location = EVENTS_PATH + event_timestamp.strftime(TIMESTAMP_TO_PATH_FORMAT) + ".txt"
                    events_location = EVENTS_PATH + 'AllEvents.txt'
                    records_file = open(records_location, 'w')
                    events_file = open(events_location, 'a')
                             
                    #Get certain records based on timestamp
                    lower_bound = event_timestamp - datetime.timedelta(seconds = RECORDING_DURATION_PRE_TRIGGER)
                    upper_bound = event_timestamp + datetime.timedelta(seconds = RECORDING_DURATION_POST_TRIGGER)
           
                    # write records to file
                    records = VBoxData.select().where((VBoxData.timestamp > lower_bound) & (VBoxData.timestamp < upper_bound))
                    for r in records:
                        timestamp = r.timestamp
                        gps_lat = "0.0" if r.gps_lat == None else "%.6f" % r.gps_lat
                        gps_lon = "0.0" if r.gps_lon == None else "%.6f" % r.gps_lon
                        acc_x = "-" if r.acc_x == None else "%.3f" % r.acc_x
                        acc_y = "-" if r.acc_y == None else "%.3f" % r.acc_y
                        gyro_x = "-" if r.gyro_x == None else "%.3f" % r.gyro_x
                        gyro_y = "-" if r.gyro_y == None else "%.3f" % r.gyro_y
                        gyro_z = "-" if r.gyro_z == None else "%.3f" % r.gyro_z
                        obd_mil = "-" if r.obd_mil == None else "%s" % r.obd_mil
                        obd_dtc_count = "-" if r.obd_dtc_count == None else "%d" % r.obd_dtc_count
                        obd_maf = "-" if r.obd_maf == None else "%.3f" % r.obd_maf
                        obd_speed = "-" if r.obd_speed == None else "%.3f" % r.obd_speed
                        obd_throttle_pos = "-" if r.obd_throttle_pos == None else "%.3f" % r.obd_throttle_pos
                        obd_rpm = "-" if r.obd_rpm == None else "%.3f" % r.obd_rpm
                        obd_coolant_temp = "-" if r.obd_coolant_temp == None else "%.3f" % r.obd_coolant_temp
                        obd_dtc = "-" if r.obd_dtc == None else r.obd_dtc

                        record_tuple = "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\n" % (
                                timestamp.strftime('%H:%M:%S'), gps_lat, gps_lon, acc_x, acc_y, gyro_x, gyro_y, gyro_z,
                                obd_mil, obd_dtc_count, obd_maf, obd_speed,
                                obd_throttle_pos, obd_rpm, obd_coolant_temp, obd_dtc)
                        records_file.write(record_tuple)
                    records_file.close()
                    
                    # update AllEvents
                    events_file.write(event_timestamp.strftime(TIMESTAMP_TO_PATH_FORMAT) + "\n")
                    events_file.close()
                    print "\tProcessData: Processed timestamp %s" % event_timestamp


                else:
                    profile_data = self.averages_calculations()
                    profile = ProfileData.get(ProfileData.flag == 1)
                    profile.avg_accel = profile_data[0]
                    #profile.avg_decel = profile_data[1]
                    profile.avg_throttle = profile_data[1]
                    profile.avg_rpm = profile_data[2]
                    profile.avg_speed = profile_data[3]
                    profile.total_distance = profile_data[4]
                    profile.avg_coolant = profile_data[5]
                    profile.mpg = profile_data[6]
                    profile.save()
                    
                    Averages.set("%.2f %.2f %.2f %.2f %.2f" %(profile_data[0], profile_data[0], profile_data[1], profile_data[3], profile_data[5])  )
                    #print averages_tuple             
                    
                time.sleep(.1)

            except Exception:
                print '\n\tProcessData: Unhandled Exception. Exiting.'
                print traceback.format_exc()
                break

        print "\tProcessData: done."