Пример #1
0
def getData(alive, duration, precision, imuRate, lidarRate, datafile, rowList):
    pre_exec()
    global imu
    startTime = time.time()
    lastTimeLidar = time.time()
    lastTimeIMU = lastTimeLidar
    lastTime = lastTimeLidar
    current = time.time()
    while current - startTime < duration and not alive.is_set():
        #define the precision, i.e. the gap between two consecutive IMU or LiDar read
        if current - lastTime > precision:
            lastTime = current
            if current - lastTimeIMU > imuRate and lib.lsm9ds1_accelAvailable(
                    imu
            ) > 0 and current - lastTimeLidar > lidarRate and ser.in_waiting > 8:
                lastTimeIMU = time.time()
                IMUdata = getIMU()
                Lidardata = getLidar()
                currentTime = str(datetime.datetime.fromtimestamp(lastTimeIMU))
                #currentTime = str(lastTimeIMU)
                lastTimeLidar = lastTimeIMU
                rowList.append([
                    currentTime, Lidardata, IMUdata[0], IMUdata[1], IMUdata[2],
                    IMUdata[3], IMUdata[4], IMUdata[5], IMUdata[6], IMUdata[7],
                    IMUdata[8]
                ])
            elif current - lastTimeIMU > imuRate and lib.lsm9ds1_accelAvailable(
                    imu) > 0:
                lastTimeIMU = time.time()
                IMUdata = getIMU()
                currentTime = str(datetime.datetime.fromtimestamp(lastTimeIMU))
                #currentTime = str(lastTimeIMU)
                rowList.append([
                    currentTime, "NA", IMUdata[0], IMUdata[1], IMUdata[2],
                    IMUdata[3], IMUdata[4], IMUdata[5], IMUdata[6], IMUdata[7],
                    IMUdata[8]
                ])
            elif current - lastTimeLidar > lidarRate and ser.in_waiting > 8:
                lastTimeLidar = time.time()
                Lidardata = getLidar()
                currentTime = str(
                    datetime.datetime.fromtimestamp(lastTimeLidar))
                #currentTime = str(lastTimeLidar)
                rowList.append([
                    currentTime, Lidardata, "NA", "NA", "NA", "NA", "NA", "NA",
                    "NA", "NA", "NA"
                ])

        current = time.time()

    print("start writing data")
    with open(datafile, "w") as csvfile:
        spamwriter = csv.writer(csvfile,
                                delimiter=',',
                                quoting=csv.QUOTE_MINIMAL)
        for row in rowList:
            spamwriter.writerow(row)
Пример #2
0
def getData():
    global lasttime
    global imu

    current = time.time()
    while current - startTime < 10:
        
        current = time.time()
        if lib.lsm9ds1_accelAvailable(imu) > 0 and ser.in_waiting > 8:
            IMUdata = getIMU()
            Lidardata = getLidar()
            
            currentTime = str(datetime.datetime.now()); #timestamp data
            row = [currentTime,Lidardata,IMUdata[0],IMUdata[1],IMUdata[2],IMUdata[3],IMUdata[4],IMUdata[5]] #the row being written to csv file, just x and y accel
            rowList.append(row)
            diff = time.time() - lasttime
            timeDiffer.append(diff)
            #print(diff)
            lasttime = time.time()
            #print(row)

    print("start writing data")
    with open(datafile,"a",newline = '') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
        for i in range(len(rowList)):
            row = [rowList[i]]
            spamwriter.writerow(row)
            
    print("start writing frequency")
    with open(filename,"a",newline = '') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
        for i in range(len(timeDiffer)):
            row = [timeDiffer[i]]
            spamwriter.writerow(row)
Пример #3
0
def getData():
    #connect with IMU
    imu = lib.lsm9ds1_create()
    lib.lsm9ds1_begin(imu)
    if lib.lsm9ds1_begin(imu) == 0:
        print("Failed to communicate with LSM9DS1.")
        quit()
    lib.lsm9ds1_calibrate(imu)
    while True:
        #get IMU accel data
        global lasttime

        #new structure testing
        if lib.lsm9ds1_accelAvailable(imu) > 0 and ser.in_waiting > 8:
            lib.lsm9ds1_readAccel(imu)
            ax = lib.lsm9ds1_getAccelX(imu)
            ay = lib.lsm9ds1_getAccelY(imu)
            cax = lib.lsm9ds1_calcAccel(imu, ax)
            cay = lib.lsm9ds1_calcAccel(imu, ay)
            #TFmini data
            recv = ser.read(9)
            ser.reset_input_buffer()

            if recv[0] == 0x59 and recv[1] == 0x59:
                distance = recv[2] + recv[3] * 256
                ser.reset_input_buffer()
                currentTime = str(datetime.datetime.now())
                #timestamp data
                row = [
                    currentTime, distance, cax, cay
                ]  #the row being written to csv file, just x and y accel
                diff = time.time() - lasttime
                print(diff)
                lasttime = time.time()
                print(row)
Пример #4
0
 def detect(self):
     """
     This function tests if accel, gyro and mag are all available
     """
     if lib.lsm9ds1_accelAvailable(self.__conn) and lib.lsm9ds1_gyroAvailable(self.__conn) and lib.lsm9ds1_magAvailable(self.__conn):
         return True
     else:
         return False
def getData():
    global lasttime
    #connect with IMU
    imu = lib.lsm9ds1_create()
    lib.lsm9ds1_begin(imu)
    if lib.lsm9ds1_begin(imu) == 0:
        print("Failed to communicate with LSM9DS1.")
        quit()
    lib.lsm9ds1_calibrate(imu)

    current = time.time()
    while current - startTime < 10:
        
        current = time.time()
        if lib.lsm9ds1_accelAvailable(imu) > 0 and ser.in_waiting > 8:
            lib.lsm9ds1_readAccel(imu)
            ax = lib.lsm9ds1_getAccelX(imu)
            ay = lib.lsm9ds1_getAccelY(imu)
            az = lib.lsm9ds1_getAccelZ(imu)
            cax = lib.lsm9ds1_calcAccel(imu, ax)
            cay = lib.lsm9ds1_calcAccel(imu, ay)
            caz = lib.lsm9ds1_calcAccel(imu, az)
            gx = lib.lsm9ds1_getGyroX(imu)
            gy = lib.lsm9ds1_getGyroY(imu)
            gz = lib.lsm9ds1_getGyroZ(imu)
            cgx = lib.lsm9ds1_calcGyro(imu, gx)
            cgy = lib.lsm9ds1_calcGyro(imu, gy)
            cgz = lib.lsm9ds1_calcGyro(imu, gz)
            #TFmini data
            recv = ser.read(9)
            ser.reset_input_buffer()

            if recv[0] == 0x59 and recv[1] == 0x59:
                distance = recv[2] + recv[3] * 256
                ser.reset_input_buffer()
                currentTime = str(datetime.datetime.now()); #timestamp data
                row = [currentTime,distance,cax,cay,caz,cgx,cgy,cgz] #the row being written to csv file, just x and y accel
                rowList.append(row)
                diff = time.time() - lasttime
                timeDiffer.append(diff)
                #print(diff)
                lasttime = time.time()
                #print(row)

    print("start writing data")
    with open(datafile,"a",newline = '') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
        for i in range(len(rowList)):
            row = [rowList[i]]
            spamwriter.writerow(row)
            
    print("start writing frequency")
    with open(filename,"a",newline = '') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
        for i in range(len(timeDiffer)):
            row = [timeDiffer[i]]
            spamwriter.writerow(row)
Пример #6
0
def getData():
    global lasttime
    global imu

    current = time.time()
    while current - startTime < 10:

        current = time.time()
        if lib.lsm9ds1_accelAvailable(imu) > 0 and ser.in_waiting > 8:
            ##            result1 = pool.apply_async(getIMU,)
            ##            result2 = pool.apply_async(getLidar,)
            ##            IMUdata = result1.get(timeout=1)
            ##            Lidardata = result2.get(timeout=1)
            q = Queue()
            q2 = Queue()
            IMUdataP = Process(target=getIMU, args=(q, ))
            LidardataP = Process(target=getLidar, args=(q2, ))
            IMUdataP.start()
            LidardataP.start()
            IMUdata = q.get()
            Lidardata = q2.get()
            IMUdataP.join()
            LidardataP.join()

            currentTime = str(datetime.datetime.now())
            #timestamp data
            row = [currentTime, Lidardata, IMUdata
                   ]  #the row being written to csv file, just x and y accel
            rowList.append(row)
            print(row)
            diff = time.time() - lasttime
            timeDiffer.append(diff)
            #print(diff)
            lasttime = time.time()
            #print(row)

    print("start writing time")
    with open(filename, "a", newline='') as csvfile:
        spamwriter = csv.writer(csvfile,
                                delimiter=',',
                                quoting=csv.QUOTE_MINIMAL)
        for i in range(len(timeDiffer)):
            row = [timeDiffer[i]]
            spamwriter.writerow(row)

    print("start writing data")
    with open(datafile, "a", newline='') as csvfile:
        spamwriter = csv.writer(csvfile,
                                delimiter=',',
                                quoting=csv.QUOTE_MINIMAL)
        for i in range(len(rowList)):
            row = [rowList[i]]
            spamwriter.writerow(row)
Пример #7
0
def getData():
    global last
    global sensorDataNumpyArray
    Timer(writeFrequency, writeSensorData).start()
    while True:
        time.sleep(readFrequency)
        if lib.lsm9ds1_accelAvailable(imu) > 0 and ser.in_waiting > 8:
            #print("time for sensors:%f" % (time.time() - last))
            Lidardata = getLidar()
            IMUdata = getIMU()
            currentTime = str(datetime.datetime.now())

            row = [
                currentTime, Lidardata, IMUdata[0], IMUdata[1], IMUdata[2],
                IMUdata[3], IMUdata[4], IMUdata[5]
            ]
            sensorData.append(row)
Пример #8
0
def getData():
    global start
    global imu
    global lasttime
    print("pid for sensors %i" % os.getpid())
    while time.time() - start < 10:
        if lib.lsm9ds1_accelAvailable(imu) > 0 and ser.in_waiting > 8:
            IMUdata = getIMU()
            Lidardata = getLidar()
            currentTime = str(datetime.datetime.now())
            row = [
                currentTime, Lidardata, IMUdata[0], IMUdata[1], IMUdata[2],
                IMUdata[3], IMUdata[4], IMUdata[5]
            ]
            rowList.append(row)
            diff = time.time() - lasttime
            timeDiffer.append(diff)
            lasttime = time.time()

    print("start writing sensors data")
    with open(datafile, "a", newline='') as csvfile:
        spamwriter = csv.writer(csvfile,
                                delimiter=',',
                                quoting=csv.QUOTE_MINIMAL)
        for i in range(len(rowList)):
            row = [rowList[i]]
            spamwriter.writerow(row)

    print("start writing sensors reading frequency")
    with open(filename, "a", newline='') as csvfile:
        spamwriter = csv.writer(csvfile,
                                delimiter=',',
                                quoting=csv.QUOTE_MINIMAL)
        for i in range(len(timeDiffer)):
            row = [timeDiffer[i]]
            spamwriter.writerow(row)

    return 1
Пример #9
0
def getData():
    global lasttime
    #connect with IMU
    imu = lib.lsm9ds1_create()
    lib.lsm9ds1_begin(imu)
    if lib.lsm9ds1_begin(imu) == 0:
        print("Failed to communicate with LSM9DS1.")
        quit()
    lib.lsm9ds1_calibrate(imu)

    while True:
        #beforeIf = time.time()
        time.sleep(0.2)
        if lib.lsm9ds1_accelAvailable(imu) > 0:
            afterIf = time.time()
            lib.lsm9ds1_readAccel(imu)
            ax = lib.lsm9ds1_getAccelX(imu)
            ay = lib.lsm9ds1_getAccelY(imu)
            az = lib.lsm9ds1_getAccelZ(imu)
            cax = lib.lsm9ds1_calcAccel(imu, ax)
            cay = lib.lsm9ds1_calcAccel(imu, ay)
            caz = lib.lsm9ds1_calcAccel(imu, az)
            gx = lib.lsm9ds1_getGyroX(imu)
            gy = lib.lsm9ds1_getGyroY(imu)
            gz = lib.lsm9ds1_getGyroZ(imu)
            cgx = lib.lsm9ds1_calcGyro(imu, gx)
            cgy = lib.lsm9ds1_calcGyro(imu, gy)
            cgz = lib.lsm9ds1_calcGyro(imu, gz)

            ##            #add to list
            ##            caxl.append(cax)
            ##            cayl.append(cay)
            ##            cazl.append(caz)
            ##            cgxl.append(cgx)
            ##            cgyl.append(cgy)
            ##            cgzl.append(cgz)
            print(cax, cay, caz, cgx, cgy, cgz)