I am trying to take voltage samples with the ADS1115 and using an Ultimate GPS (USB) in cooperation with the computer clock to sample at 475 Hz. With this low of a frequency I am thinking that python should be able to handle the job. My code and some numerical info are attached. I am running the script from the terminal with the GUI disabled and several unnecessary functions on the Pi disabled. Introducing the thread has helped somewhat with the glitch so I am thinking it has to do with updating the GPS correctly. Is this variation in sampling rate to be expected? I am currently using cubic spline interpolation to get a linear, time-invariant signal out of the data but I would love to reduce the amount of 'garbage in' if at all possible. Please ignore commented-out code. I am still in rough-draft mode. Thank you!
Code: Select all
import time
import board
import busio
#import rtc
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
from adafruit_ads1x15.ads1x15 import Mode
import csv
from datetime import datetime
import adafruit_gps
import serial
import numpy as np
#---------------------------Voltage Setup----------------------------------
GAIN = 1
RATE = 475
SAMPLES = 5000
# Create the I2C bus with a fast frequency
i2c = busio.I2C(board.SCL, board.SDA) #freq. is i2C clock frequency
#i2c1 = busio.I2C(board.SCL, board.SDA)
# Create the ADC object using the I2C bus
ads = ADS.ADS1115(i2c, data_rate=RATE, gain=GAIN, mode = Mode.CONTINUOUS)
#ads1 = ADS.ADS1115(i2c1, data_rate=RATE, gain=GAIN, mode = Mode.CONTINUOUS)
# Create diff input on channel 0
chan0 = AnalogIn(ads, ADS.P0, ADS.P1)
#chan1 = AnalogIn(ads1, ADS.P2, ADS.P3)
#-----------------------GPS Setup---------------------------------------------
#GPS Setup
#uart = busio.UART(board.TX, board.RX, baudrate=9600, timeout=10)
# i2c = busio.I2C(board.SCL, board.SDA)
from threading import Thread
from time import sleep
def gps_update():
while True:
gps.update()
thread = Thread(target = gps_update)
uart = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=10)
gps = adafruit_gps.GPS(uart, debug=False)
gps.send_command(b"PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0")
gps.send_command(b"PMTK220,100")
#GPS Time Components
#uart = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=10)
# i2c = busio.I2C(board.SCL, board.SDA)
def _format_datetime(datetime):
return "{:02}:{:02}:{:02}".format(
datetime.tm_hour,
datetime.tm_min,
datetime.tm_sec,
)
#------------Wait for GPS-----------------
wait_seconds = 300
for x in range(wait_seconds):
gps.update()
if not gps.timestamp_utc:
time.sleep(1)
print('waiting for signal: ', x, 'seconds')
else:
break
#File Naming
now = datetime.now()
name = now.strftime("%Y%m%d_%H%M")
# =============================================================================
# #-----------GPS Data logging setup
# LOG_MODE = 'ab'
# LOG_FILE = '/media/pi/USB20FD1/%s_GPS.csv' % name
# =============================================================================
date_format_str = '%H:%M:%S.%f'
data = [None]*SAMPLES
start = time.monotonic()
thread.start() #starts continuous GPS Update
#==================== Data Collection =========================================
for i in range(SAMPLES):
current = datetime.now()
current_str = current.strftime(date_format_str)
time_stamp = datetime.strptime(current_str,date_format_str)
GPS_Time = _format_datetime(gps.timestamp_utc)
data[i] = chan0.voltage, GPS_Time,time_stamp
time.sleep(0.0014)
#outfile.write(GPS_Data)
#outfile.flush()
#time.sleep(0.0014) #T = 0.00?? samples at xxx.xx Hz for current loop setup
finish = time.monotonic()
total_time = finish - start
#===================== Write File ============================================
name = now.strftime("%Y%m%d_%H%M")
np.savetxt('/media/pi/USB20FD/%s.csv' % name, data, delimiter =", ",fmt="% s")
print("Time of capture: {}s".format(total_time))
print("Sample rate requested={} actual={}".format(RATE, SAMPLES / total_time))