Skip to content

Instantly share code, notes, and snippets.

@stefan789
Created June 23, 2017 12:20
Show Gist options
  • Save stefan789/9ddaf3965971d4c7a2b97ae7bc60b057 to your computer and use it in GitHub Desktop.
Save stefan789/9ddaf3965971d4c7a2b97ae7bc60b057 to your computer and use it in GitHub Desktop.
Fluxgate readout KoreaPC
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import sys
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax1.ticklabel_format(style="plain")
fil = sys.argv[1]
def animate(i):
pullData = open(fil,"r").read()
dataArray = pullData.split('\n')
xar = []
yar = []
for eachLine in dataArray:
if len(eachLine)>1:
t1,t2,x,y,z,w = eachLine.split(' ')
xar.append(x)
yar.append(y)
ax1.clear()
ax1.plot(yar)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
import datetime
import numpy as np
import sys
import matplotlib
from matplotlib import pyplot as plt
fil = sys.argv[1]
X = []
Y = []
Z = []
with open(fil, "r") as f:
for line in f:
re = line.split(" ")
X.append(re[2])
Y.append(re[3])
Z.append(re[4])
#print X
plt.plot(X, "r-")
plt.plot(Y, "g-")
plt.plot(Z, "b-")
plt.show()
import nidaqmx
import numpy as np
import datetime
import sys
class readFG():
def __init__(self, nr = 1, vrange1=10, vrange2=10, sf=2000.0, taver=1):
self.dev = "Dev1"
self.channel = "ao0"
self.volt_range1 = vrange1
self.volt_range2 = vrange2
self.average_time = taver
self.sf = sf
self.task = nidaqmx.AnalogInputTask()
if nr == 1:
self.task.create_voltage_channel('Dev1/ai14,Dev1/ai7,Dev1/ai11', terminal = 'rse', min_val=-self.volt_range1, max_val=self.volt_range1)
elif nr == 2:
self.task.create_voltage_channel('Dev1/ai14,Dev1/ai7,Dev1/ai11,Dev1/ai0,Dev1/ai1,Dev1/ai2', terminal = 'rse', min_val=-self.volt_range2, max_val=self.volt_range2)
else:
raise ValueError("Wrong number of sensors, 1 or 2 sensors are available")
self.task.configure_timing_sample_clock(rate = self.sf)
self.task.start()
def readvalues(self):
samples = int(self.sf*self.average_time)
dat = np.asarray(self.task.read(samples, fill_mode='group_by_channel'))
print(len(dat[0]))
res = [7000*np.mean(i) for i in dat]
std = [7000*np.std(i) for i in dat]
print res, std
return res, std
def measure_to_file(self, filename):
with open(filename, "w") as f:
i = 0
while i < 36000:
i = i+1
read, std = self.readvalues()
now = datetime.datetime.now()
#print("{3} {0:.5f} {1:.5f} {2:.5f} {4} {5} {6}".format(read[0], read[1], read[2], now, std[0], std[1], std[2]))
print("{0} {1} {2}".format(now, read, std))
f.write("{0} {1} {2}\n".format(now, read, std))
f.flush()
def main(fil):
fg = readFG(nr = 2, vrange1=1, vrange2=1, sf=10000.0, taver=1)
fg.measure_to_file(fil)
if __name__ == "__main__":
try:
fil = sys.argv[1]
print("Writing to {}".format(fil))
except IndexError as e:
print("No filename given use data.txt")
fil = "data.txt"
main(fil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment