I am currently working on a project where I utilise NI USB - 6009 and an optical sensor to register wheel rotation then from there calculate the angular and linear velocity. My plan is to plot that output in the python GUI (tkinter) and if possible the graph as well. My arrangement is as followed: digital output from sensor to NI USB 6009 digital input, vcc to 5v and gnd to ground (I think that is the standard wiring). So far, I can put the number of pulses on the gui but it has to be plotted together with the plot here and also I am not able to calculate the time between pulses because the task module takes extra work. Did anyone have a similar experience so one might share the solution? or should I go with arduino instead? Appreciate your help
here are my codes:
import nidaqmx
import math
from nidaqmx.constants import Edge
from nidaqmx.constants import AcquisitionType
from nidaqmx.constants import VoltageUnits
from nidaqmx.constants import TerminalConfiguration
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
import time
import nidaqmx.system
from nidaqmx.system import system as nidaqSystem
import tkinter as tk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
nfft = 1000
fs = 1000.0
win = tk.Tk()
win.configure(bg='white')
win.geometry("650x400")
win.resizable(False,False)
with nidaqmx.Task() as task:
task.di_channels.add_di_chan("Dev1/port1/line0")
# task.timing.cfg_samp_clk_timing(fs, active_edge=Edge.RISING, sample_mode=AcquisitionType.CONTINUOUS, samps_per_chan=nfft)
fig = plt.figure()
# ax1 = fig.add_subplot(211)
# ax2 = fig.add_subplot(212)
# freqs = np.linspace(0, int(fs/2), int(nfft/2+1))
while True:
t1 = (int(round(time.time() * 1000)))
data = task.read(number_of_samples_per_channel=nfft)
if data == True:
data == 1
else:
data == 0
pulse = math.floor(np.sum(np.diff(data) > 0)/2)
print(pulse)
count_label = ttk.Label(win,text=pulse,background='white')
count_label.place(x=10, y=10)
# graph = FigureCanvasTkAgg(fig,win)
# graph.draw()
# graph.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH,expand=True)
# count_entry = ttk.Entry(win,textvariable=print(pulse))
# count_entry.place(x=10,y=30)
# count_entry = tk.StringVar()
# ax1.clear()
# ax2.clear()
# ax1.plot(data)
# ax2.plot(pulse)
fig.show()
plt.pause(0.1)
# t3 = (int(round(time.time() * 1000)))
win.mainloop()