saluslab commited on
Commit
bf9f68d
·
verified ·
1 Parent(s): dfe0e1e

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. Acquisition.py +256 -0
Acquisition.py ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # modules for user_thread
2
+ import threading
3
+
4
+ # modules for video_thread
5
+ import numpy as np
6
+ import cv2
7
+ from datetime import datetime
8
+
9
+ # modules for DAQ_thread
10
+ import nidaqmx
11
+ from nidaqmx.constants import AcquisitionType
12
+ from nidaqmx.stream_readers import AnalogMultiChannelReader
13
+ import os
14
+
15
+ # modules for GPS_thread
16
+ import serial
17
+ import time
18
+
19
+ ###########################################################
20
+ ###################### Constants ##########################
21
+ ###########################################################
22
+ # global running variable
23
+ running = True
24
+
25
+ current_time = str(datetime.now().strftime("%Y_%m_%d_%H_%M_%S"))
26
+ print("System is started at {}. Files associated with this run will include this time stamp".format(current_time))
27
+
28
+ os.mkdir("C:\\{}".format(current_time))
29
+
30
+ print("Creating new directory for run")
31
+ run_directory = "C:\\{}".format(current_time)
32
+
33
+ print("Creating Video directory")
34
+ os.mkdir(run_directory + "\\Video_Data\\")
35
+
36
+ print("Creating Acceleration directory")
37
+ os.mkdir(run_directory + "\\Acceleration_Data\\")
38
+
39
+ print("Creating GPS directory")
40
+ os.mkdir(run_directory + "\\GPS_Data\\")
41
+
42
+ # paths
43
+ VIDEO_PATH = "C:\\{}\\Video_Data\\".format(current_time)
44
+ ACCEL_PATH = "C:\\{}\\Acceleration_Data\\".format(current_time)
45
+ GPS_PATH = "C:\\{}\\GPS_Data\\".format(current_time)
46
+
47
+ # GPS COM Port
48
+ GPS_PORT = "COM3"
49
+
50
+ # define some constants for the daq
51
+ sampling_rate = 2000
52
+ buffer_size_per_channel = 4000
53
+ channels = 6
54
+ data = np.zeros((channels, buffer_size_per_channel))
55
+
56
+ # get channel information for module 1 and 2
57
+ system = nidaqmx.system.System.local()
58
+ DAQ_device1 = system.devices['cDAQ9185-213ABA2Mod1']
59
+ DAQ_device2 = system.devices['cDAQ9185-213ABA2Mod2']
60
+
61
+ channel_names = [ai.name for ai in DAQ_device1.ai_physical_chans] + [ai.name for ai in DAQ_device2.ai_physical_chans]
62
+
63
+ print("Successfully found channels: ", channel_names)
64
+
65
+ if channels > len(channel_names):
66
+ print("Specified number of channels {} is less than the number of channels {} found!".format(channels, len(channel_names)))
67
+ running = False
68
+ exit()
69
+
70
+ ##########################################################
71
+ ############# user_thread function #######################
72
+ ##########################################################
73
+ '''
74
+ This is the global variable that tells everything to stop once running = False
75
+ The input keeps running from being false but once the user enters anything, running will be false
76
+ '''
77
+
78
+ def Ask_User():
79
+ global running
80
+ input("Enter anything to stop")
81
+ running = False
82
+
83
+ #########################################################
84
+ ################# video_thread function #################
85
+ #########################################################
86
+
87
+ '''
88
+ This is the function that operates the vision system to record video data.
89
+ There are two main variables, "cap" which is the video capture variable and
90
+ "out" which is the video writer variable.
91
+ '''
92
+
93
+ def Video():
94
+ global running
95
+
96
+ # define the video capture variable
97
+
98
+ cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
99
+ # set resolution and FPS
100
+ cap.set(cv2.CAP_PROP_FPS,100)
101
+ cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
102
+ cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
103
+ cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
104
+
105
+
106
+ # main loop which reads frames and saves the info
107
+ print("Video Capture Starting...")
108
+ print(cap.isOpened())
109
+ while cap.isOpened() and running:
110
+ # read from the video capture variable
111
+ ret, frame = cap.read()
112
+ if not ret:
113
+ print("Can't receive frame (stream end?). Exiting ...")
114
+ break
115
+
116
+ display_frame = cv2.resize(frame, (1280, 720), interpolation=cv2.INTER_CUBIC)
117
+
118
+ # display the resized frame in a window named 'Live Feed'
119
+ cv2.imshow('Live Feed', display_frame)
120
+
121
+ if cv2.waitKey(1) & 0xFF == ord('q'):
122
+ running = False
123
+ break
124
+
125
+ # write the video frame to the video file
126
+ cv2.imwrite(VIDEO_PATH + str(datetime.now().strftime("%Y-%m-%d_%H-%M-%S.%f")) + ".jpg", frame)
127
+
128
+ # release everything if job is finished
129
+ print("Video releasing and closing")
130
+ cap.release()
131
+ # close any open windows that cv2 generated (i.e. the video stream)
132
+ cv2.destroyAllWindows()
133
+
134
+
135
+ ##########################################################################
136
+ ############## DAQ_thread function/thread and helper function ############
137
+ ##########################################################################
138
+
139
+ # initialize the read task for all of the channels
140
+ def initialize_read_task():
141
+ global channel_names
142
+
143
+ # initalize read task
144
+ read_task = nidaqmx.Task()
145
+
146
+ # initialize channels sequentially
147
+ for channel_number in range(channels):
148
+ read_task.ai_channels.add_ai_voltage_chan(channel_names[channel_number])
149
+
150
+ # set channel timings
151
+ read_task.timing.cfg_samp_clk_timing(rate=sampling_rate,
152
+ sample_mode=AcquisitionType.CONTINUOUS,
153
+ samps_per_chan=buffer_size_per_channel)
154
+
155
+ # set up the reader for the read task
156
+ reader = AnalogMultiChannelReader(read_task.in_stream)
157
+
158
+ return read_task, reader
159
+
160
+ def DAQ():
161
+ global running
162
+
163
+ # initalize the read task and reader and start
164
+ read_task, reader = initialize_read_task()
165
+ read_task.start()
166
+
167
+ while running:
168
+ # create the file path
169
+ FULL_FILE_NAME = os.path.join(ACCEL_PATH, str(datetime.now().strftime("%d_%m_%Y_%H_%M_%S"))+".csv")
170
+
171
+ # read the samples
172
+ reader.read_many_sample(data=data, number_of_samples_per_channel=buffer_size_per_channel)
173
+
174
+ # save the data to file and add timestamp as header
175
+ np.savetxt(FULL_FILE_NAME, data, fmt="%f", delimiter=",", newline="\n",header=
176
+ 'time:' + str(datetime.now().strftime("%Y-%m-%d_%H-%M-%S.%f")) +
177
+ ',sampling_rate:'+ str(sampling_rate) +
178
+ ',channels:'+ str(channels) +
179
+ ',buffer_size:' + str(buffer_size_per_channel))
180
+
181
+ # pause the read task
182
+ read_task.stop()
183
+
184
+ # close the read task and free up the allocated memory (the buffer)
185
+ read_task.close()
186
+
187
+
188
+ ##############################################################
189
+ ################## GPS_thread function #######################
190
+ ##############################################################
191
+
192
+ def GPS():
193
+ global running
194
+
195
+ # initialize the GPS serial port
196
+ ser = serial.Serial(GPS_PORT, baudrate=57600, timeout= 2)
197
+
198
+ # if something crashes, the port may not be closed from the last session
199
+ ser.close()
200
+
201
+ # open the port
202
+ ser.open()
203
+
204
+ ser.flushInput()
205
+ ser.flushOutput()
206
+
207
+ GPS_FILE_NAME_BASE = GPS_PATH + current_time
208
+ with open(GPS_FILE_NAME_BASE+'.nmea', 'wb') as f_data, open(GPS_FILE_NAME_BASE + '.csv', 'w', newline='\n') as f_timestamp:
209
+ while running:
210
+ nmea_call = ser.readline()
211
+ f_timestamp.write(str(datetime.now().strftime("%Y-%m-%d_%H-%M-%S.%f")) + '\n')
212
+ f_data.write(nmea_call)
213
+ time.sleep(0.001)
214
+
215
+
216
+ # close the files and serial port
217
+ ser.close()
218
+ f_data.close()
219
+ f_timestamp.close()
220
+
221
+
222
+ ##############################################################
223
+ ################ Thread setup ################################
224
+ ##############################################################
225
+
226
+ # create the threads
227
+ video_thread = threading.Thread(target=Video)
228
+ DAQ_thread = threading.Thread(target=DAQ)
229
+ GPS_thread = threading.Thread(target=GPS)
230
+ user_thread = threading.Thread(target=Ask_User)
231
+
232
+ # start the threads
233
+ print("Starting Video Thread...")
234
+ video_thread.start()
235
+
236
+ print("Starting DAQ thread...")
237
+ DAQ_thread.start()
238
+
239
+ print("Starting GPS thread...")
240
+ GPS_thread.start()
241
+
242
+ print("Starting User thread...")
243
+ user_thread.start()
244
+
245
+ user_thread.join()
246
+ print("User thread closed")
247
+
248
+ video_thread.join()
249
+ print("Video thread closed")
250
+
251
+ DAQ_thread.join()
252
+ print("DAQ thead closed")
253
+
254
+ GPS_thread.join()
255
+ print("GPS thread closed")
256
+ print("All threads closed")