// Encoder connect to digitalpin 2 and 3 on the Arduino.
volatile int counter = 0;
//This variable will increase or decrease depending on the rotation of encoder
float angle, temp = 0.0 ;
float a=0.0;
void setup() {
Serial.begin (9600);
pinMode(2, INPUT); // set pin to input
pinMode(3, INPUT); // set pin to input
digitalWrite(2, HIGH); // turn on pullup resistors
digitalWrite(3, HIGH); // turn on pullup resistors
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
attachInterrupt(1, ai1, RISING);
}
void loop() {
// Send the value of counter
if(counter >= 1200 || counter <= -1200) counter = 0;
angle = counter*(3.00/10.00) ;
if(angle != temp){
temp = angle;
a = millis();
//Serial.print (",");
Serial.print (a);
Serial.print (",");
Serial.println (angle);
//Serial.println (counter);
}
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(3)==LOW) {
counter++;
}else{
counter--;
}
}
void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(2)==LOW) {
counter--;
}else{
counter++;
}
}
import numpy as np
from scipy.fftpack import fft,ifft
import matplotlib.pyplot as plt
import scipy as sp
from ipywidgets import interact
from scipy.integrate import odeint
%matplotlib inline
time = []
data = []
#hex1mm_8_2.csv為數據檔,請改成要匯入的檔名
with open('hex1mm_8_2.csv','r',encoding='utf-8-sig') as f:
s = f.read()
b = s.split()
for i in b:
c=i.split(',')
time.append(c[0])
data.append(c[1])
print(len(time))
print(len(data))
time = np.array(time, dtype="float")/1000-int(time[0])/1000
data = np.array(data, dtype="float")
plt.plot(time,data)
plt.show()
total_time = max(time)-min(time)
sampling_rate = int(len(time)/total_time) #sampling_rate為 1/時間間隔
fft_size = len(time) #fft_size為總共數據數量
t = np.array(time, dtype="float")
x = np.array(data, dtype="float")
xs = x[:fft_size]
xf = np.fft.rfft(xs) / fft_size
xss = len(x)
ts = t[:xss]
x = np.array(x, dtype="float")
ts = np.array(ts, dtype="float")
plt.plot(ts,x)
plt.show()
freqs = np.linspace(0, sampling_rate/2, int(fft_size/2+1))
xfp = 20*np.log10(np.clip(np.abs(xf), 1e-2, 1e2))
freqs = np.array(freqs, dtype="float")
xfp = np.array(xfp, dtype="float")
freqs = freqs[1:]
xfp = xfp[1:]
plt.plot(freqs,xfp)
plt.show()
num = xfp.argmax(axis=0)
print("頻率:",freqs[num]) #頻率
print("週期:",1/freqs[num]) #週期
import numpy as np
from scipy.fftpack import fft,ifft
import matplotlib.pyplot as plt
import scipy as sp
from ipywidgets import interact
from scipy.integrate import odeint
%matplotlib inline
time = []
data = []
#hex1mm_8_2.csv為數據檔,請改成要匯入的檔名
with open('hex1mm_8_2.csv','r',encoding='utf-8-sig') as f:
s = f.read()
b = s.split()
for i in b:
c=i.split(',')
time.append(c[0])
data.append(c[1])
print(len(time))
print(len(data))
time = np.array(time, dtype="float")/1000-int(time[0])/1000
data = np.array(data, dtype="float")
plt.plot(time,data)
plt.show()
dx = []
dc = []
dt = []
dxx = []
dcc = []
for i in range(0,len(time)-1):
p = (data[i+1]+data[i])/2
t = (time[i+1]-time[i])
dc.append(p) #一次微分的角位置
dt.append(t) #時間差
for k in range(0,len(dc)):
xx = (data[k+1]-data[k])/dt[k]
dx.append(xx) #一次微分
for j in range(0,len(dx)-1):
pp = (dc[j+1]+dc[j])/2
dcc.append(pp) #二次微分的角位置
for m in range(0,len(dx)-1):
xxx = (dx[m+1]-dx[m])/dt[m]
dxx.append(xxx) #二次微分
plt.plot(dcc[10:],dxx[10:],"o")
np.polyfit(dcc,dxx,1)
x2 = np.arange(min(dcc),max(dcc),0.1)
y2 = np.polyfit(dcc[10:],dxx[10:],1)[0]*x2+np.polyfit(dcc[10:],dxx[10:],1)[1]
plt.plot(x2,y2)
plt.show()
print('y = %.2f x'%np.polyfit(dcc,dxx,1)[0],'+ %.2f'%np.polyfit(dcc,dxx,1)[1])
import numpy as np
from scipy.fftpack import fft,ifft
import matplotlib.pyplot as plt
import scipy as sp
from ipywidgets import interact
from scipy.integrate import odeint
%matplotlib inline
time = []
data = []
#tt1.csv為數據檔,請改成要匯入的檔名
with open('hex1mm_8_2.csv','r',encoding='utf-8-sig') as f:
s = f.read()
b = s.split()
for i in b:
c=i.split(',')
time.append(c[0])
data.append(c[1])
print(len(time))
print(len(data))
time = np.array(time, dtype="float")/1000-int(time[0])/1000
data = np.array(data, dtype="float")
plt.plot(time,data)
plt.show()
def decay(a,x,t,y0):
y = a*np.exp(-x/t)+y0
return y
#a(震幅), t(半衰期), y0(截距), T(時間)
def plot_osc(a, t = 0.81, y0= 2.36, T = 5):
N_data = np.arange(0,T,0.001)
#plt.title('Ultrasonic oscillations\nh=%2.2f, b=%2.2f, a=%2.2f, m=%2.2f, g=%2.2f, f=%2.2f\nfilename = %s'%(h,b,a,m,g,f,filename),fontsize=15)
#x0 = [h, 0.0]
plt.plot(time,data,'.')
plt.xlabel('time (sec)',fontsize=15)
plt.ylabel('amp (cm)',fontsize=15)
plt.plot(N_data, decay(a,N_data,t,y0), 'b', label='y(t)')
plt.legend(loc='best')
plt.xlabel('T')
plt.grid()
plt.show()
interact(plot_osc, a = (0.0, 360.0,0.01), t = (0,2,0.01), y0 = (0.00,10,0.01), T = (0.0, 8, 0.1));