Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #--------------------------------------------------
- #!/usr/bin/env python3
- #--------------------------------------------------
- import time
- import os
- import serial
- import struct
- import csv
- from lcd import *
- from var_save import *
- from threading import Timer
- #--------------------------------------------------
- #global vars
- h_store = 0
- prev_fsize = 0
- ser = 0
- #--------------------------------------------------
- def init_vars():
- global h_store
- global prev_fsize
- global ser
- h_store = 0
- prev_fsize = 0
- der = 0
- #--------------------------------------------------
- def init_ser():
- global ser
- try:
- ser = serial.Serial(
- port = '/dev/ttyUSB0',
- baudrate = 460800,
- parity = serial.PARITY_NONE,
- stopbits = serial.STOPBITS_TWO,
- bytesize = serial.EIGHTBITS,
- timeout = 1
- )
- except:
- raise ValueError('SERIAL INIT')
- #--------------------------------------------------
- def millis():
- millis = int(round(time.time() * 1000))
- return millis
- #--------------------------------------------------
- def btus(b_in, index):
- temp = int(b_in[index])
- temp = temp << 8
- temp += b_in[index+1]
- return temp;
- #--------------------------------------------------
- def btfp(b_in, index):
- byte_list=[b_in[index+3], b_in[index+2], b_in[index+1], b_in[index]]
- aa= bytearray(byte_list)
- return struct.unpack('<f', aa)[0];
- #--------------------------------------------------
- def write_csv_headings(log_file):
- f = open("/home/pi/Desktop/Scripts/DMU30_LOGGER/dmu30_logs/dmu30_log"+str(log_file)+".csv", 'w')
- c = csv.writer(f)
- 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"])
- f.close()
- #--------------------------------------------------
- def write_to_csv(read_in, t_stamp, log_file):
- f = open("/home/pi/Desktop/Scripts/DMU30_LOGGER/dmu30_logs/dmu30_log"+str(log_file)+".csv", 'a')
- c = csv.writer(f)
- #mc 0
- #axr 2
- #axa 6
- #ayr 10
- #aya 14
- #azr 18
- #aza 22
- #aux 26
- #tmp 30
- #axdt 34
- #axdv 38
- #aydt 42
- #aydv 46
- #azdt 50
- #azdv 54
- #ss 58
- #so 60
- #eo 62
- #cs 64
- 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),
- btfp(read_in,34), btfp(read_in,38), btfp(read_in,42), btfp(read_in,46), btfp(read_in,50), btfp(read_in,54)]
- c.writerow(items)
- f.close()
- #--------------------------------------------------
- def test_header():
- global h_store
- h_new = 0
- try:
- h_new = ser.read(1)[0]
- except:
- raise ValueError('SERIAL READ1')
- if (h_new == 0xaa) and (h_store == 0x55):
- h_store = 0
- return 1
- else:
- h_store = h_new
- return 0
- #--------------------------------------------------
- def us_wrap_add(us1, us2):
- output = us1 + us2
- if output > 0xffff:
- output = (output % 0xffff) - 1
- return output
- #--------------------------------------------------
- def calc_chksum(read_in):
- chksum = 0x55aa #Header
- x = 0
- while x< 64:
- us_bytes = bytes([read_in[x], read_in[x+1]])
- chksum = us_wrap_add(chksum, btus(us_bytes, 0))
- x += 2
- chksum ^= 0xffff
- chksum += 1
- return chksum;
- #--------------------------------------------------
- def init_logfile(log_file):
- try:
- log_file = read_var_from_file("/home/pi/Desktop/Scripts/DMU30_LOGGER/log_count")
- except:
- try:
- write_var_to_file("/home/pi/Desktop/Scripts/DMU30_LOGGER/log_count", 1)
- log_file = read_var_from_file("/home/pi/Desktop/Scripts/DMU30_LOGGER/log_count")
- except:
- raise ValueError('JSON1')
- else:
- if log_file != 1:
- raise ValueError('JSON2')
- else:
- log_file = 0
- pass
- else:
- try:
- write_var_to_file("/home/pi/Desktop/Scripts/DMU30_LOGGER/log_count", log_file+1)
- except:
- raise ValueError('JSON3')
- else:
- pass
- return log_file
- #--------------------------------------------------
- def main():
- s_time = 100
- log_file = -1
- count = 0
- init_vars()
- lcd_init()
- init_ser()
- lcd_clear()
- lcd_print("DMU30 - Data Logger", 0,0)
- time.sleep(1)
- log_file = init_logfile(log_file)
- try:
- write_csv_headings(log_file)
- except:
- raise ValueError('CSV_WRITE1')
- else:
- pass
- drop_count = 0
- stx = millis()
- lcd_clear()
- lcd_print_pad("STATUS: RUNNING", 0)
- lcd_print_pad("FILE: dmu30_log"+str(log_file),1)
- lcd_print_pad("SIZE (kb): 0", 2)
- lcd_print_pad("DROPPED: 0", 3)
- while 1:
- bytes_waiting = 0
- try:
- bytes_waiting = ser.in_waiting
- except:
- raise ValueError('SERIAL WAIT')
- else:
- pass
- if bytes_waiting>65:
- if test_header():
- read_in = bytearray()
- try:
- read_in = ser.read(66)
- except:
- raise ValueError('SERIAL READ2')
- else:
- pass
- chksum = calc_chksum(read_in)
- chksum_in = bytes([read_in[64], read_in[65]])
- if(btus(chksum_in, 0) == chksum):
- if((millis() - stx) >= s_time):
- try:
- count+=1
- write_to_csv(read_in, s_time*(count-1), log_file)
- fsize = os.path.getsize("/home/pi/Desktop/Scripts/DMU30_LOGGER/dmu30_logs/dmu30_log"+str(log_file)+".csv")
- fsize /= 1000#mb
- fsize = int(fsize)
- global prev_fsize
- if fsize > prev_fsize:
- lcd_print_pad("SIZE (kb): "+str(fsize), 2)
- prev_fsize = fsize
- except:
- raise ValueError('CSV WRITE2')
- else:
- pass
- stx = millis()
- else:
- drop_count+=1
- lcd_print_pad("DROPPED: "+str(drop_count), 3)
- if __name__ == '__main__':
- while 1:
- try:
- main()
- except ValueError as e:
- print("YO BITCH - DAS ERROR")
- lcd_error(str(e))
- timeout = 10
- t = Timer(timeout, print, ['Sorry, times up'])
- t.start()
- prompt = "You have %d seconds to choose the correct answer...\n" % timeout
- answer = input(prompt)
- t.cancel()
- if answer == "y":
- quit()
- pass
- else:
- pass
- #--------------------------------------------------
- ###EOF###
- #--------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement