Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. import time
  2. import numpy as np
  3. from Queue import Queue
  4. from PD__Python_ToF.libMesaSR import SrCam
  5. from threading import Thread, Event
  6. import csv
  7. from copy import copy
  8. from numpy import savez
  9.  
  10.  
  11. def __get_coord_flt(cam):
  12. x = np.empty((cam.r, cam.c), dtype=np.float32)
  13. y = np.empty((cam.r, cam.c), dtype=np.float32)
  14. z = np.empty((cam.r, cam.c), dtype=np.float32)
  15. res=SrCam.lib.SR_CoordTrfFlt(cam.handle,x.ctypes,y.ctypes,z.ctypes, 4,4,4)
  16. return x,y,z
  17.  
  18.  
  19. def __list_2d_write_prepare(l):
  20. s = ''
  21. for dim in l:
  22. line = ' '.join(str(item) for item in dim)
  23. s += line + '\n'
  24. return s
  25.  
  26.  
  27. def cam_callback(event, path, queue):
  28. print 'Starting TOF...'
  29. times = []
  30.  
  31. cam=SrCam()
  32. res=cam.ReadSerial()
  33. #print 'Serial Number:',hex(res)
  34. cam.OpenDlg(1,0)
  35. time.sleep(0.1)
  36. cam.SetModulationFrequency(SrCam.ModulationFrq['MF_30MHz'])
  37. time.sleep(0.1)
  38. print(cam.GetMode())
  39. #???print(cam.GetMode() | SrCam.AcquireMode['AM_DENOISE_ANF'])
  40. #cam.SetMode(cam.GetMode() | SrCam.AcquireMode['AM_DENOISE_ANF'])
  41. cam.SetMode(0x0911)
  42. time.sleep(0.1)
  43. print(cam.GetMode())
  44.  
  45. # it = 50
  46. # while cam.GetIntegrationTime() != it:
  47. # cam.SetIntegrationTime(it)
  48. # time.sleep(0.01)
  49.  
  50. print 'TOF: IT Ok'
  51.  
  52. # integrationTime=cam.GetIntegrationTime()
  53. # amplitudeTreshold=cam.GetAmplitudeThreshold()
  54. # modulationFrequency=cam.GetModulationFrequency()
  55.  
  56. # with open('{}parameters.csv'.format(path), 'wb') as x:
  57. # writer = csv.writer(x,delimiter=';')
  58. # writer.writerow([integrationTime,amplitudeTreshold,modulationFrequency])
  59.  
  60.  
  61. i = 0
  62. while not event.is_set():
  63. cam.Acquire()
  64.  
  65. modulationFrequency=cam.GetModulationFrequency()
  66. integrationTime=cam.GetIntegrationTime()
  67. times.append([i,time.clock(),integrationTime,modulationFrequency])
  68.  
  69. #distance = cam.GetImage(SrCam.ImgType['IT_DISTANCE'])
  70. #amplitude = cam.GetImage(SrCam.ImgType['IT_AMPLITUDE'])
  71. #confidence = cam.GetImage(SrCam.ImgType['IT_CONF_MAP'])
  72. res, ie = cam.GetImageList()
  73. for idx in range(res):
  74. #print ie[idx].imgType
  75. #print 'idx',idx,':',
  76. #for attr in ie[idx]._fields_:
  77. # print attr[0],':',ie[idx].__getattribute__(attr[0]),
  78. #print(cam.GetImage(idx))
  79. if ie[idx].imgType == SrCam.ImgType['IT_AMPLITUDE']:
  80. amplitude = cam.GetImage(idx)
  81. elif ie[idx].imgType == SrCam.ImgType['IT_CONF_MAP']:
  82. confidence = cam.GetImage(idx)
  83.  
  84. x, y, z = __get_coord_flt(cam)
  85.  
  86. filename = "{}{}ToF.npz".format(path,i)
  87. queue.put((filename, copy(x), copy(y), copy(z), copy(amplitude), copy(confidence)))
  88.  
  89. i+=1
  90. time.sleep(0.001) # not ~10Hz
  91. with open('{}timestamp.csv'.format(path), 'ab') as f:
  92. writer = csv.writer(f,delimiter=';')
  93. writer.writerow(times)
  94.  
  95. cam.Close()
  96.  
  97.  
  98. def files_worker(event, queue):
  99. while not queue.empty() or not event.is_set():
  100. if event.is_set():
  101. print("remaining TOF files to save: {}".format(queue.qsize()))
  102. data = queue.get(timeout=1)
  103. path = data[0]
  104.  
  105. #buff = ''
  106. li = []
  107. for item in data[1:]:
  108. li.append(item)
  109. savez(path, li)
  110. #buff += __list_2d_write_prepare(item)
  111. #with open(path,"w+") as f:
  112. # f.write(buff)
  113. print('TOF files saved...')
  114.  
  115. class ToF(object):
  116.  
  117. def __init__(self, path):
  118. self.event = Event()
  119. self.queue = Queue()
  120. self.thread = Thread(target=cam_callback, args=(self.event, path, self.queue))
  121. self.file_thread = Thread(target=files_worker, args=(self.event, self.queue))
  122.  
  123. def start(self):
  124. if self.thread.is_alive() or self.file_thread.is_alive():
  125. print('Camera acquisition already started')
  126. return
  127. self.file_thread.start()
  128. self.thread.start()
  129.  
  130. def stop(self):
  131. if not self.thread.is_alive() and not self.file_thread.is_alive():
  132. print('Camera acquisition not started yet')
  133. return
  134. self.event.set()
  135. self.file_thread.join()
  136. self.thread.join()
  137. self.event.clear()
  138. print 'TOF Stopped...'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement