Example #1
0
    def run(self):
        if self.db is None:
            print "AcquireData: No DB, aborting run."
            return

        try: 

            time.sleep(1)
            self.prevAcc = IMU.SharedData.AccXnorm
            
            txnTime = time.time()
            cycleStart = time.time()
            while not self.stopped():
                curTime = time.time()
                
                #create db record for this capture
                data = VBoxData()
                data.timestamp = datetime.datetime.now()

               
                #acquire GPS data
                gps_time = None
                gps_datetime = None
                if GPS.Reader.connected:
                    GPS.SharedData.lock.acquire()
                    try:
                        data.gps_lat = GPS.SharedData.LATITUDE
                        data.gps_lon = GPS.SharedData.LONGITUDE
                        gps_time = str(GPS.SharedData.TIME)
                        data.gps_speed = GPS.SharedData.SPEED 
                    finally:
                        GPS.SharedData.lock.release()
                    
                    # If we have a non-empty string
                    if gps_time != "None" and gps_time != "":
                        gps_datetime = datetime.datetime.strptime(gps_time, STR_TO_TIMESTAMP_FORMAT)
                        data.timestamp = gps_datetime
                    if self.verboseGPS:
                        print "\033[1;34;40m---\nGPS\n---\033[0m"
                        print 'latitude     :' , data.gps_lat
                        print 'longitude    :' , data.gps_lon
                        print 'time utc     :' , gps_datetime
                        print 'speed (m/s)  :' , data.gps_speed

                elif self.verboseGPS:
                    print "\033[1;34;40m---\nGPS\n---\033[0m\nDISCONNECTED"

                #acquire IMU data
                if IMU.Reader.connected:
                    IMU.SharedData.lock.acquire()
                    try:
                        data.acc_x = IMU.SharedData.AccXnorm
                        data.acc_y = IMU.SharedData.AccYnorm
                        data.gyro_x = IMU.SharedData.gyroXangle
                        data.gyro_y = IMU.SharedData.gyroYangle
                        data.gyro_z = IMU.SharedData.gyroZangle
                    finally:
                        IMU.SharedData.lock.release()
                    if self.verboseIMU:
                        print "\033[1;33;40m---\nIMU\n---\033[0m"
                        print "acc_x        :", data.acc_x
                        print "acc_y        :", data.acc_y
                        print "gyro_x       :", data.gyro_x
                        print "gyro_y       :", data.gyro_y
                        print "gyro_z       :", data.gyro_z

                elif self.verboseIMU:
                    print "\033[1;33;40m---\nIMU\n---\033[0m\nDISCONNECTED"

                #acquire OBD data
                if OBD.Reader.connected:
                    OBD.SharedData.lock.acquire()
                    try:

                        data.obd_mil             = OBD.SharedData.MIL
                        data.obd_dtc_count       = OBD.SharedData.DTC_count
                        data.obd_maf             = OBD.SharedData.MAF
                        data.obd_speed           = OBD.SharedData.SPEED
                        data.obd_throttle_pos    = OBD.SharedData.THROTTLE_POS
                        data.obd_rpm             = OBD.SharedData.RPM
                        data.obd_coolant_temp    = OBD.SharedData.COOLANT_TEMP
                        data.obd_dtc             = OBD.SharedData.GET_DTC
                    finally:
                        OBD.SharedData.lock.release()

                    if self.verboseOBD:
                        print "\033[1;32;40m---\nOBD\n---\033[0m"
                        print "Engine Load  : ", data.obd_engineload, OBD.SharedData.ENGINE_LOAD.unit
                        print "Speed        : ", data.obd_speed, OBD.SharedData.SPEED.unit
                        print "RPM          : ", data.obd_rpm, OBD.SharedData.RPM.unit
                        print "Error codes  : ", self.obdErrors

                elif self.verboseOBD:
                    print "\033[1;32;40m---\nOBD\n---\033[0m\nDISCONNECTED"
                
                #acquire camera
                if self.triggerEvent() == True:
                    if gps_datetime is not None:
                        Camera.Reader.GPStime = gps_datetime
                    else:
                        Camera.Reader.GPStime = data.timestamp
                    Camera.Reader.should_write = True
                    Camera.Reader.isRecording = True
                    


                #print "acc_x        :\r", data.acc_x
                #add db record to transaction batch
                if curTime - cycleStart > self.cycleDuration:
                    TXN.append(data)
                    cycleStart = curTime
                    self.prevAcc = data.acc_x

                    print "\033[1;32;40m---\nOBD\n---\033[0m"
                    print data.obd_mil 
                    print data.obd_dtc_count
                    print data.obd_maf  
                    print data.obd_speed         
                    print data.obd_throttle_pos   
                    print data.obd_rpm         
                    print data.obd_coolant_temp

                    print "\033[1;33;40m---\nIMU\n---\033[0m"
                    print "acc_x        :", data.acc_x
                    print "acc_y        :", data.acc_y
                    print "gyro_x       :", data.gyro_x
                    print "gyro_y       :", data.gyro_y
                    print "gyro_z       :", data.gyro_z

                    print "\033[1;34;40m---\nGPS\n---\033[0m"
                    print 'latitude     :' , data.gps_lat
                    print 'longitude    :' , data.gps_lon

                #write to db if enough time has passed
                if curTime - txnTime > self.txnDuration:
                    with self.db.transaction() as txn:
                        for record in TXN:
                            record.save()
                    txnTime = curTime 
                    del TXN[:]

                #delay
                #time.sleep(self.cycleDuration)

            # main loop has been terminated
            self.cleanup()
            print "\tAcquireData: done."


        #unhandled exception
        except:
            self.cleanup()
            print '\nAcquireData: Unhandled exception. Terminating.', sys.exc_info()[1]
            print traceback.format_exc()