-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewMainFile.py
111 lines (94 loc) · 2.79 KB
/
newMainFile.py
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
#This is the actual file for operating both the servos and tfmini.
#Yaw means lef-right motion and Pitch means up-down motion here.
import myservoblaster,tfmini,time,os
#import socket
from csv import writer
tf = tfmini.Tfmini()
yawServo = myservoblaster.ServoBlaster(17) #Attachs yaw servo at GPIO pin 17.
pitchServo = myservoblaster.ServoBlaster(18) #Attachs pitch servo at GPIO pin 18.
#sc = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #Starts server.
#sc.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
#sc.bind((socket.gethostname(),1234))
#sc.listen(4)
def saveData(data): #Saves data in a csv file. Argument 'data' is a list.
with open('data.csv','a') as f:
csvW = writer(f)
csvW.writerow(data)
f.close()
#def sendData(data):
# ti = time.time()
# sc = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #Starts server.
# sc.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
# sc.bind(('127.0.0.1',1234))
# sc.listen(4)
# clientSocket,address = sc.accept()
# print(f"Connetion with {address} established.")
# clientSocket.send(bytes(data,"utf-8"))
# sc.shutdown(socket.SHUT_WR)
# sc.close()
# tj = time.time()
# print(f'time taken in data sending {tj-ti}')
step = 60 # Initial position of pitch control servo.
flag = 0 # Flag to check 'up' or 'down' motion. '0' for downward and '1' for upward.
lst = []
stepDelay = 0.00001 #step delay of yawServo
inr = 5 # Increament in step of pitch servo. Modify this variable to increase or decrease rows of data.csv
while True:
if step == 120: #Checking two extreme points of up-down motion.
flag = 1
# saveData(['EOS']) #EOS stands for End Of Scan.
with open('finished.txt','w') as f:
print('finished.txt created')
f.close()
while os.path.exists('sync.txt') != True:
print('Holding for sync.')
time.sleep(0.0001)
os.remove('sync.txt')
elif step == 60:
flag = 0
for ii in range(60,120):
distance = tf.getData()
lst.append(str(distance))
# sendData(str(distance))
print(distance)
yawServo.update(ii)
# time.sleep(stepDelay)
saveData(lst)
del lst[:]
# sendData(lst)
print('Left Sweep\n')
if flag == 0:
step += inr
elif flag == 1:
step -= inr
pitchServo.update(step)
print('Pitch movement\n')
if step == 120:
flag = 1
# saveData(['EOS'])
with open('finished.txt','w') as f:
print('finished.txt created')
f.close()
while os.path.exists('sync.txt') != True:
print('Holding for sync.')
time.sleep(0.0001)
os.remove('sync.txt')
elif step == 60:
flag = 0
for ii in range(120,60,-1):
distance = tf.getData()
lst.append(str(distance))
# sendData(str(distance))
print(distance)
yawServo.update(ii)
# time.sleep(stepDelay)
lst.reverse()
saveData(lst)
del lst[:]
print('Right Sweep\n')
if flag == 0:
step += inr
elif flag == 1:
step -= inr
pitchServo.update(step)
print("Pitch movement\n")