-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnewGui.jl
354 lines (278 loc) · 9.5 KB
/
newGui.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using Gtk
using PyPlot
using Gtk.ShortNames
using SerialPorts
using PyPlot
using FFTW
sp = SerialPort("COM3:", 9600) # Opening serial port
win = GtkWindow("2D direction finder")
g = GtkGrid()
Heading = GtkLabel("Welcome to the 2D sonar direction finder") # a widget for entering text
start = GtkButton("Start Sonar")
exit = GtkButton("Exit")
# Now let's place these graphical elements into the Grid:
g[1:3,1] = Heading # Cartesian coordinates, g[x,y]
g[2,2] = start
g[2,3] = exit # spans both columns
set_gtk_property!(g, :column_homogeneous, true)
set_gtk_property!(g, :column_spacing, 15) # introduce a 15-pixel gap between columns
push!(win, g)
sc = Gtk.GAccessor.style_context(start)
pr = CssProviderLeaf(data="label {color:red;}")
push!(sc, StyleProvider(pr), 600)
function on_start_click(w)
println("The button has been clicked")
global z=""
global l = "" #replace z
global d =""
global m= "" #replace d
PyPlot.show()
while true
println("Emptying buffer")
h = readavailable(sp)
println("Number of bytes emptied from buffer ",h)
println("Initiating chirp pulse")
println("Sending c command to teensy")
write(sp, "c") #writing to serial
sleep(0.2)
s = readavailable(sp)
if(s != "0")
println("Unexpected response from teensy: Restart code")
println("The value of unexpected s is ",s)
end
println("Received from c ", s)
sleep(1) #delay
println("Reading from the buffer by sending p command to teensy")
write(sp, "p") #writing to serial
global count=0
global n=0
global z= 0
while true
x=readavailable(sp)
if (length(x) == 0)
sleep(0.0001)
global n = n+1
else
global z = string(z, x)#append to a string
global n = 0
global count = count+1
end
if n>1000
break
end
end
println("count = ",count)
println("Number of bytes received from the teensy ",length(z))
Z = split(z, "\r\n")
#println(Z)
len = length(Z)-1;
v = Vector{Int64}(undef,len)
for n=1:len
v[n] = parse(Int64, Z[n])
end
println("Number of ADC samples ",length(v))
sleep(2)
#########################################################################################################################################
#########################################################################################################################################
########################################################################################################################################
#start another buffer Reading
println("Emptying buffer for the 2nd time")
h1 = readavailable(sp)
println("Number of bytes emptied from buffer on the 2nd time ",h1)
println("Initiating chirp pulse for the second Receiver")
println("Sending s command to teensy to transmit chirp pulse")
write(sp, "s") #writing to serial
sleep(0.2)
s = readavailable(sp)
if(s != "0")
println("Unexpected response from teensy: Restart code")
println("The value of unexpected s is ",s)
end
println("Received from s ", s)
sleep(1) #delay
println("Reading from the buffer by sending z command to teensy")
write(sp, "z") #writing to serial
global count1=0
global n1=0
global l =0
while true
x1=readavailable(sp)
if (length(x1) == 0)
sleep(0.0001)
global n1 = n1+1
else
global l = string(l, x1) #append to array
global n1 = 0
global count1 = count1+1
end
if n1>1000
break
end
end
println("count = ",count1)
println("Number of bytes received from the teensy ",length(l))
L = split(l, "\r\n") # making an array of values
len1 = length(L)-1;
e = Vector{Int64}(undef,len1)
for k=1:len1
e[k] = parse(Int64, L[k])
end
println("Number of ADC samples from the second receiver is ",length(e))
##end of 2nd buffer reading
########################################################################################################################################
########################################################################################################################################
#Chirp pulse 2
c = 343; # Speed of sound in air in m/s
fs = 500000; # This is the sample rate of the sonar.
dt = 1/fs; # This is the sample spacing
r_max = 0.5; # Maximum range in metres to which to simulate.
t_max = 50E-3; # Time delay to max range
N = 32000 #ADC Buffer size
pulse_T = 6E-3;
ds = 500000;
dts = 1/ds;
t_max_pulse = pulse_T;
t_max1=(20/343) +10E-3
t_match=collect(0:dts:t_max1);
f=40000
t_d=pulse_T/2;
t = (0:N-1).*dt; # t=0:dt:t_max defines a “range”.
print(t_max)
r = (c*t)/2;
f0 = 40000; # Centre frequency is 10 kHz
B = 4000; # Chirp bandwidth
T = 4E-3; # Chirp pulse length
K = B/T; # Chirp rate
rect(t) = (abs.(t) .<= 0.5)*1.0
v_tx = cos.( 2*pi*(f0*(t.-0.002 ) + 0.5*K*(t.-0.002 ).^2) ) .* rect.((t.-0.002)/T)
v_tx_match = cos.(2*pi*(f*(t_match.-t_d).+0.5*K*(t_match.-t_d).^2)).*rect((t_match .-t_d)/T);
r1=(343 .*t_match)/2
println("The length of v_tx_match is ", length(v_tx_match))
############################################################################################################################################
#Zero padding the cirp pulse to make it same size as the received signal
println()
len3 = length(v)
v_tx1=v_tx[1:len3]
len2=length(r1)-length(v)
j=zeros(len2)
append!(v,j)
len4=length(v)-length(v_tx_match)
###########################################################################################################################################
#Signal Processing for the first Receiver
V_TX1=fft(v_tx_match);
V_RX = fft(v)
H1 = conj(V_TX1);
V_MF1 = H1.*V_RX; # matched filter
v_mf = ifft(V_MF1);
v_mf = real(v_mf);
########################################################################################################################################
########################################################################################################################################
#Making an analytic signal for receiver 1
V_ANAL1 = 2*V_MF1;
N_ANAL1 = length(V_MF1);
println("The length of the matched filter array is: ", N_ANAL1);
if mod(N_ANAL1, 2)==0 # Case N even
neg_freq_range = Int(N_ANAL1/2):N_ANAL1; # define the range of the neg_freq_range
else # Case N Odd
neg_freq_range = Int((N_ANAL1+1)/2):N_ANAL1;
end
V_ANAL1[neg_freq_range].=0;
v_anal1 = ifft(V_ANAL1);
##########################################################################################################################################
#SIgnal processig for the second Receiver
println()
println("Processing data received from the second Receiver")
len5 = length(e)
v_tx2 = v_tx[1:len5]
len6 = length(r1)-length(e)
p =zeros(len6) # values used to zeropad the array
append!(e,j)
println("Done Zoro padding the received array")
len7=length(e)-length(v_tx_match)
#Signal Processing from the second receiver
println("Signal Processing from the second Receiver")
V_TX2 = fft(v_tx_match);
V_RX2 = fft(e)
H2 = conj(V_TX2);
V_MF2 = H2.*V_RX2;
v_mf2 = ifft(V_MF2);
v_mf2 = real(v_mf2);
#analytic Signal for the second receiver
V_ANAL2 = 2*V_MF2;
N_ANAL2 = length(V_MF2);
println("The length of the matched filter array is: ", N_ANAL2);
if mod(N_ANAL2, 2)==0 # Case N even
neg_freq_range2 = Int(N_ANAL2/2):N_ANAL2; # define the range of the neg_freq_range
else # Case N Odd
neg_freq_range2 = Int((N_ANAL2+1)/2):N_ANAL2;
end
V_ANAL2[neg_freq_range2].=0;
v_anal2 = ifft(V_ANAL2);
###############################################################################################################################################
#Applying windowing
t_rc = 50E-3;
dt_sample = 7.246e-6;
t_sample = collect(0:dt_sample:T);
t_rx = collect(0:dt_sample:t_rc);
Nw = length(t_rx);
df = 1/(Nw*dt_sample);
f_axis = (0:Nw-1)*df;
window = rect((f_axis)/B)
#Basebanding the signal
j=im;
v_bb1 = v_anal1.*exp.(-j*2*pi*f0*t_match); #Baseband signal for Analytic signal 1
v_bb2 = v_anal2.*exp.(-j*2*pi*f0*t_match); #Baseband signal for Analytic signal 2
V_BB1 = fft(v_bb1);
V_BB2 = fft(v_bb2);
phase_arr = angle.(v_bb1.*conj(v_bb2)) #calculating the phase array
##############################################################################################################################################
#making plots
PyPlot.clf()
subplot(4,1,1)
PyPlot.plot(r1,abs.(v_anal2))
title("Second Receiver Analytic Signal")
xlabel("Range in meters");
PyPlot.draw()
ylim([0,2E6])
xlim([0,10]);
subplot(4,1,2)
plot(r1,angle.(v_bb2))
title("Second Receiver Basband angle")
PyPlot.draw()
xlim([0,10]);
subplot(4,1,3)
plot(r1,angle.(v_bb1))
title("First Receiver Baseband angle")
PyPlot.draw()
xlim([0,10]);
subplot(4,1,4)
title("Wrapped Phase difference")
plot(r1, phase_arr,".")
xlim([0,3]);
PyPlot.draw()
PyPlot.sleep(0.05)
##############################################################################################################################################
#Some distance calculations
d = 0.018
k = 0;
lambda = 8.46e-3;
val_1 = phase_arr .+ k*2*pi
val_2 = lambda.*val_1
val_3 = 2*pi*d
val_4 = val_2./val_3
theta = asin.(val_4)
theta = theta.*(180/pi)
# println("The angle of arrival is ", theta)
x = r1.*cos.(theta)
y = r1.*sin.(theta)
#plot(x,y,".")
end
end
signal_connect(on_start_click, start, "clicked")
function on_exit_click(w)
close(sp)
Gtk.destroy(win)
println("Exit")
end
signal_connect(on_exit_click, exit, "clicked")
showall(win)