Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. #--------------------------------------------------
  2.  
  3. #!/usr/bin/env python3
  4.  
  5. #--------------------------------------------------
  6.  
  7.  
  8. import time
  9. import os
  10. import serial
  11. import struct
  12. import csv
  13. from lcd import *
  14. from var_save import *
  15. from threading import Timer
  16.  
  17.  
  18. #--------------------------------------------------
  19. #global vars
  20. h_store = 0
  21. prev_fsize = 0
  22. ser = 0
  23.  
  24.  
  25. #--------------------------------------------------
  26.  
  27. def init_vars():
  28. global h_store
  29. global prev_fsize
  30. global ser
  31.  
  32. h_store = 0
  33. prev_fsize = 0
  34. der = 0
  35.  
  36. #--------------------------------------------------
  37.  
  38. def init_ser():
  39.  
  40. global ser
  41. try:
  42. ser = serial.Serial(
  43.  
  44. port = '/dev/ttyUSB0',
  45. baudrate = 460800,
  46. parity = serial.PARITY_NONE,
  47. stopbits = serial.STOPBITS_TWO,
  48. bytesize = serial.EIGHTBITS,
  49. timeout = 1
  50.  
  51. )
  52.  
  53. except:
  54. raise ValueError('SERIAL INIT')
  55.  
  56. #--------------------------------------------------
  57.  
  58. def millis():
  59. millis = int(round(time.time() * 1000))
  60. return millis
  61.  
  62. #--------------------------------------------------
  63.  
  64.  
  65. def btus(b_in, index):
  66.  
  67. temp = int(b_in[index])
  68. temp = temp << 8
  69. temp += b_in[index+1]
  70. return temp;
  71.  
  72. #--------------------------------------------------
  73.  
  74.  
  75. def btfp(b_in, index):
  76.  
  77. byte_list=[b_in[index+3], b_in[index+2], b_in[index+1], b_in[index]]
  78. aa= bytearray(byte_list)
  79. return struct.unpack('<f', aa)[0];
  80.  
  81.  
  82. #--------------------------------------------------
  83.  
  84. def write_csv_headings(log_file):
  85. f = open("/home/pi/Desktop/Scripts/DMU30_LOGGER/dmu30_logs/dmu30_log"+str(log_file)+".csv", 'w')
  86. c = csv.writer(f)
  87. c.writerow(["t-(mS)","MC","x_r","x_a","y_r","y_a","z_r","z_a", "aux_v", "temp", "x_dt", "x_dv", "y_dt", "y_dv", "z_dt", "z_dv"])
  88. f.close()
  89.  
  90. #--------------------------------------------------
  91.  
  92. def write_to_csv(read_in, t_stamp, log_file):
  93.  
  94. f = open("/home/pi/Desktop/Scripts/DMU30_LOGGER/dmu30_logs/dmu30_log"+str(log_file)+".csv", 'a')
  95. c = csv.writer(f)
  96.  
  97. #mc 0
  98. #axr 2
  99. #axa 6
  100. #ayr 10
  101. #aya 14
  102. #azr 18
  103. #aza 22
  104. #aux 26
  105. #tmp 30
  106. #axdt 34
  107. #axdv 38
  108. #aydt 42
  109. #aydv 46
  110. #azdt 50
  111. #azdv 54
  112. #ss 58
  113. #so 60
  114. #eo 62
  115. #cs 64
  116.  
  117.  
  118. items = [t_stamp, btus(read_in,0), btfp(read_in,2), btfp(read_in,6), btfp(read_in,10), btfp(read_in,14), btfp(read_in,18), btfp(read_in,22), btfp(read_in,26), btfp(read_in,30),
  119. btfp(read_in,34), btfp(read_in,38), btfp(read_in,42), btfp(read_in,46), btfp(read_in,50), btfp(read_in,54)]
  120.  
  121. c.writerow(items)
  122.  
  123. f.close()
  124.  
  125.  
  126. #--------------------------------------------------
  127.  
  128.  
  129. def test_header():
  130.  
  131. global h_store
  132. h_new = 0
  133. try:
  134. h_new = ser.read(1)[0]
  135. except:
  136. raise ValueError('SERIAL READ1')
  137.  
  138.  
  139. if (h_new == 0xaa) and (h_store == 0x55):
  140. h_store = 0
  141. return 1
  142. else:
  143. h_store = h_new
  144. return 0
  145.  
  146.  
  147. #--------------------------------------------------
  148.  
  149.  
  150. def us_wrap_add(us1, us2):
  151. output = us1 + us2
  152. if output > 0xffff:
  153. output = (output % 0xffff) - 1
  154.  
  155. return output
  156.  
  157.  
  158. #--------------------------------------------------
  159.  
  160.  
  161. def calc_chksum(read_in):
  162.  
  163. chksum = 0x55aa #Header
  164. x = 0
  165.  
  166. while x< 64:
  167.  
  168. us_bytes = bytes([read_in[x], read_in[x+1]])
  169. chksum = us_wrap_add(chksum, btus(us_bytes, 0))
  170. x += 2
  171.  
  172.  
  173. chksum ^= 0xffff
  174. chksum += 1
  175.  
  176. return chksum;
  177.  
  178. #--------------------------------------------------
  179.  
  180.  
  181. def init_logfile(log_file):
  182. try:
  183. log_file = read_var_from_file("/home/pi/Desktop/Scripts/DMU30_LOGGER/log_count")
  184. except:
  185. try:
  186. write_var_to_file("/home/pi/Desktop/Scripts/DMU30_LOGGER/log_count", 1)
  187. log_file = read_var_from_file("/home/pi/Desktop/Scripts/DMU30_LOGGER/log_count")
  188. except:
  189. raise ValueError('JSON1')
  190. else:
  191. if log_file != 1:
  192. raise ValueError('JSON2')
  193. else:
  194. log_file = 0
  195. pass
  196. else:
  197. try:
  198. write_var_to_file("/home/pi/Desktop/Scripts/DMU30_LOGGER/log_count", log_file+1)
  199. except:
  200. raise ValueError('JSON3')
  201. else:
  202. pass
  203. return log_file
  204.  
  205. #--------------------------------------------------
  206.  
  207. def main():
  208.  
  209.  
  210. s_time = 100
  211. log_file = -1
  212. count = 0
  213.  
  214.  
  215. init_vars()
  216. lcd_init()
  217. init_ser()
  218. lcd_clear()
  219. lcd_print("DMU30 - Data Logger", 0,0)
  220. time.sleep(1)
  221.  
  222. log_file = init_logfile(log_file)
  223.  
  224.  
  225.  
  226. try:
  227. write_csv_headings(log_file)
  228. except:
  229. raise ValueError('CSV_WRITE1')
  230. else:
  231. pass
  232.  
  233. drop_count = 0
  234.  
  235. stx = millis()
  236.  
  237.  
  238. lcd_clear()
  239. lcd_print_pad("STATUS: RUNNING", 0)
  240. lcd_print_pad("FILE: dmu30_log"+str(log_file),1)
  241. lcd_print_pad("SIZE (kb): 0", 2)
  242. lcd_print_pad("DROPPED: 0", 3)
  243.  
  244.  
  245.  
  246. while 1:
  247.  
  248. bytes_waiting = 0
  249.  
  250. try:
  251. bytes_waiting = ser.in_waiting
  252. except:
  253. raise ValueError('SERIAL WAIT')
  254. else:
  255. pass
  256.  
  257. if bytes_waiting>65:
  258.  
  259. if test_header():
  260.  
  261. read_in = bytearray()
  262. try:
  263. read_in = ser.read(66)
  264. except:
  265. raise ValueError('SERIAL READ2')
  266. else:
  267. pass
  268.  
  269. chksum = calc_chksum(read_in)
  270. chksum_in = bytes([read_in[64], read_in[65]])
  271.  
  272.  
  273. if(btus(chksum_in, 0) == chksum):
  274.  
  275. if((millis() - stx) >= s_time):
  276. try:
  277. count+=1
  278. write_to_csv(read_in, s_time*(count-1), log_file)
  279. fsize = os.path.getsize("/home/pi/Desktop/Scripts/DMU30_LOGGER/dmu30_logs/dmu30_log"+str(log_file)+".csv")
  280. fsize /= 1000#mb
  281. fsize = int(fsize)
  282. global prev_fsize
  283. if fsize > prev_fsize:
  284. lcd_print_pad("SIZE (kb): "+str(fsize), 2)
  285. prev_fsize = fsize
  286.  
  287.  
  288. except:
  289. raise ValueError('CSV WRITE2')
  290. else:
  291. pass
  292. stx = millis()
  293. else:
  294. drop_count+=1
  295. lcd_print_pad("DROPPED: "+str(drop_count), 3)
  296.  
  297.  
  298.  
  299. if __name__ == '__main__':
  300. while 1:
  301. try:
  302. main()
  303. except ValueError as e:
  304. print("YO BITCH - DAS ERROR")
  305. lcd_error(str(e))
  306. timeout = 10
  307. t = Timer(timeout, print, ['Sorry, times up'])
  308. t.start()
  309. prompt = "You have %d seconds to choose the correct answer...\n" % timeout
  310. answer = input(prompt)
  311. t.cancel()
  312. if answer == "y":
  313. quit()
  314. pass
  315. else:
  316. pass
  317.  
  318. #--------------------------------------------------
  319.  
  320. ###EOF###
  321.  
  322. #--------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement