supernpp.py
Go to the documentation of this file.
1 #!/usr/bin/python3
2 
3 import os
4 import re
5 from subprocess import Popen
6 from os.path import normpath, basename
7 from threading import Thread
8 import time
9 from pathlib import Path
10 import sys
11 import threading
12 
13 sys.path.insert(1, '../logInspector')
14 sys.path.insert(1, '..')
15 
16 from logReader import Log
17 
18 class SuperNPP():
19  def __init__(self, directory, config_serials, startMode=0, computeRMS=0): # start mode 0=hot, 1=cold, 2=factory
20  self.config_serials = config_serials
21  self.directory = Path(directory)
22  self.startMode = startMode
23  self.computeRMS = computeRMS
24  self.subdirs = []
25  self.log = Log()
26  self.rmsPassResults = []
27  self.rmsFailResults = []
28  self._key_lock = threading.Lock()
29 
30  print("===================== Init SuperNPP =====================")
31  print(" Directory: ", self.directory)
32  print(" config_serials:", self.config_serials)
33  print(" startMode:", self.startMode)
34  self.findLogFiles(self.directory)
35  print(" subdirs:", self.subdirs)
36 
37 
38  def exitHack(self):
39  self.log.exitHack()
40 
41  def findLogFiles(self, directory):
42  # print("findLogFiles: ", directory)
43  for file in os.listdir(directory):
44  if ".sdat" in file or ".dat" in file:
45  self.subdirs.append(directory)
46  break
47  # Recursively search for data in sub directories
48  for subdir in os.listdir(directory):
49  subdir2 = os.path.join(directory, subdir)
50  if not os.path.isdir(subdir2):
51  continue
52  if "post_processed" in subdir:
53  continue
54  self.findLogFiles(subdir2)
55 
56  def run(self):
57  print('\nDirectories to reprocess:')
58  print(self.subdirs)
59  print('\n' + str(len(self.subdirs)) + ' directories')
60  time.sleep(2) # seconds
61  self.rmsFailResults = []
62  self.rmsPassResults = []
63 
64  # Start threads
65  threads = [Thread(target=self.run_log_folder, args=(folder, self.config_serials)) for folder in self.subdirs]
66  for thread in threads:
67  thread.start()
68  for thread in threads:
69  thread.join()
70 
71 
72  if self.computeRMS:
73  for subdir in self.subdirs:
74  sdir = subdir + "/post_processed"
75  self.log.load(sdir)
76 
77  # Compute and output RMS Report
78  self.log.calculateRMS()
79  passRMS = self.log.printRMSReport()
80  if passRMS == 1:
81  self.rmsPassResults.append(sdir)
82  print("RMS Test PASSED: " + sdir)
83  else:
84  self.rmsFailResults.append(sdir)
85  print("RMS Test FAILED: " + sdir)
86 
87 
88  def run_log_folder(self, folder, config_serials):
89  # Find the serial numbers in the log
90  # subdir = os.path.basename(os.path.normpath(folder))
91  (folder, subdir) = os.path.split(folder)
92 
93  if config_serials == ["ALL"]:
94  serials = []
95  for file in os.listdir(os.path.join(folder,subdir)):
96  if ".sdat" in file or ".dat" in file:
97  ser = int(re.sub('[^0-9]','', file.split("_")[1]))
98  if ser not in serials:
99  serials.append(ser)
100  else:
101  serials = config_serials
102 
103  print("serial numbers")
104  print(serials)
105 
106  if os.name == 'posix':
107  cmds = ['./navpp -d ' + folder + ' -s ' + str(s) + " -sd " + subdir for s in serials]
108  npp_build_folder = "../../../cpp/NavPostProcess/build"
109  else:
110  cmds = [r'.\NavPostProcess.exe -d "' + folder + r'" -s ' + str(s) + " -sd " + subdir for s in serials]
111  npp_build_folder = "../../../cpp/NavPostProcess/VS_project/Release"
112 
113  if self.startMode == 1:
114  for i in range(len(cmds)):
115  cmds[i] += ' -mode COLD -kml' # Cold init, enable KML output
116 
117  if self.startMode == 2:
118  for i in range(len(cmds)):
119  cmds[i] += ' -mode FACTORY -kml' # Factory init, enable KML output
120 
121  for i in range(len(cmds)):
122  cmds[i] += ' --outputoff' # disable INS display output
123 
124  print("Running NPP...")
125 
126  for cmd in cmds:
127  print(cmd)
128 
129  processes = [Popen(cmd, shell=True, cwd=npp_build_folder) for cmd in cmds]
130  for p in processes:
131  p.wait()
132 
133 
155 
156  print("All processes done!")
157 
158 def buildNPP(npp_build_folder):
159  if not os.path.exists(npp_build_folder):
160  os.makedirs(npp_build_folder)
161 
162  print("building NPP")
163  cmd = ['cmake .. -DCMAKE_BUILD_TYPE=Debug && make -j12 -l12']
164  process = Popen(cmd, shell=True, cwd=npp_build_folder)
165  process.wait()
166 
167 if __name__ == "__main__":
168 
169  print("Running SuperNPP")
170  npp_build_folder = "../../../cpp/NavPostProcess/build"
171  # buildNPP(npp_build_folder)
172 
173  # import yaml
174  # file = open(os.path.expanduser("~/Documents/Inertial_Sense/config.yaml"), 'r')
175  # config = yaml.load(file, Loader=yaml.FullLoader)
176  # directory = config["directory"]
177  # serials = config["serials"]
178 
179  # 2nd argument: Log Directory
180  if len(sys.argv) >= 2:
181  directory = sys.argv[1]
182 
183  # Debug
184  # directory = '/home/walt/src/IS-src/scripts/../../goldenlogs/AHRS'
185  # directory = os.path.join('C:/','_IS','goldenlogs','AHRS')
186  # directory = os.path.join('C:/','_IS','goldenlogs')
187  # directory = 'C:/_IS/goldenlogs'
188 
189  if 'directory' not in locals():
190  print("First parameter must be directory!")
191  exit()
192 
193  # 3rd argument: Serial #s
194  if len(sys.argv) >= 3:
195  serials = sys.argv[2]
196  else:
197  serials = ['ALL']
198  print("Using default value for serials: ", serials)
199 
200  # 4th argument: Compute RMS Comparison Report
201  if len(sys.argv) >= 4:
202  computeRMS = sys.argv[3]
203  else:
204  computeRMS = 1
205 
206  # Run Super NPP
207  snpp = SuperNPP(directory, serials, computeRMS=computeRMS)
208  snpp.run()
209 
210  print("==================== Super NPP Results ====================")
211  rmsPassFilename = directory+"/rms_pass.txt"
212  rmsFailFilename = directory+"/rms_fail.txt"
213  # print(rmsPassFilename)
214  # print(rmsFailFilename)
215 
216  # Remove old files
217  try:
218  os.remove(rmsPassFilename)
219  except OSError:
220  pass
221  try:
222  os.remove(rmsFailFilename)
223  except OSError:
224  pass
225 
226  if snpp.rmsPassResults != []:
227  print(" RMS Tests PASSED")
228  f = open(rmsPassFilename, "w")
229  for val in snpp.rmsPassResults:
230  print(" ", val)
231  f.write(val+"\n")
232  f.close()
233 
234  if snpp.rmsFailResults != []:
235  print(" RMS Tests FAILED:")
236  f = open(rmsFailFilename, "w")
237  for val in snpp.rmsFailResults:
238  print(" ", val)
239  f.write(val+"\n")
240  f.close()
241 
242  snpp.exitHack()
243  print("=============================================================")
244 
GeneratorWrapper< T > range(T const &start, T const &end, T const &step)
Definition: catch.hpp:4141
def findLogFiles(self, directory)
Definition: supernpp.py:41
def run_log_folder(self, folder, config_serials)
Definition: supernpp.py:88
def __init__(self, directory, config_serials, startMode=0, computeRMS=0)
Definition: supernpp.py:19
def buildNPP(npp_build_folder)
Definition: supernpp.py:158


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:58