SHARE
TWEET

Sense_Logger_v5.py

sweenig Aug 10th, 2017 (edited) 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##### Libraries #####
  2. from datetime import datetime
  3. from sense_hat import SenseHat
  4. from time import sleep
  5.  
  6. ##### Logging Settings #####
  7. FILENAME = "SenseData"
  8. OUTPUT_BUFFER_SIZE = 3 #number of data points to accumulate before writing out to a file
  9. MAX_LOG_FILE_SIZE = 1000 #number of data points to write to a file before starting a new file
  10. TEMP_H=True
  11. TEMP_P=False
  12. HUMIDITY=True
  13. PRESSURE=True
  14. ORIENTATION=True
  15. ACCELERATION=True
  16. MAG=True
  17. GYRO=True
  18. DELAY=300 #number of seconds between data point gatherings
  19. WRITE_HEADERS=True #whether or not to write the headers for a CSV
  20.  
  21. ##### Functions #####
  22. def get_sense_data():
  23.     sense_data=[]
  24.     if TEMP_H: sense_data.append(sense.get_temperature_from_humidity())
  25.     if TEMP_P: sense_data.append(sense.get_temperature_from_pressure())
  26.     if HUMIDITY: sense_data.append(sense.get_humidity())
  27.     if PRESSURE: sense_data.append(sense.get_pressure())
  28.     if ORIENTATION:
  29.         o = sense.get_orientation()
  30.         yaw = o["yaw"]
  31.         pitch = o["pitch"]
  32.         roll = o["roll"]
  33.         sense_data.extend([pitch,roll,yaw])
  34.     if MAG:
  35.         mag = sense.get_compass_raw()
  36.         mag_x = mag["x"]
  37.         mag_y = mag["y"]
  38.         mag_z = mag["z"]
  39.         sense_data.extend([mag_x,mag_y,mag_z])
  40.     if ACCELERATION:
  41.         acc = sense.get_accelerometer_raw()
  42.         x = acc["x"]
  43.         y = acc["y"]
  44.         z = acc["z"]
  45.         sense_data.extend([x,y,z])
  46.     if GYRO:
  47.         gyro = sense.get_gyroscope_raw()
  48.         gyro_x = ["x"]
  49.         gyro_y = ["y"]
  50.         gyro_z = ["z"]
  51.         sense_data.extend([gyro_x,gyro_y,gyro_z])
  52.     sense_data.append(datetime.now())
  53.     return sense_data
  54.  
  55. ##### Main Program #####
  56. sense = SenseHat() #create the sense hat object
  57. output_buffer=[] #output queue to contain the data files in memory before writing out to the file
  58. log_size=0 #tracks the current size of the log file currently being written to (in # of lines)
  59. while True:
  60.     output_buffer.append(get_sense_data()) #collect a data point and add it to the output queue
  61.     if log_size==0: #starting a new log, so get a new name and output the headers (if desired)
  62.         filename = FILENAME+"-"+str(datetime.now())+".csv" #generate the new filename
  63.         if WRITE_HEADERS: #write out the CSV headers (optional)
  64.             header =[] #list to contain the column headers
  65.             if TEMP_H: header.append("temp_h")
  66.             if TEMP_P: header.append("temp_p")
  67.             if HUMIDITY: header.append("humidity")
  68.             if PRESSURE: header.append("pressure")
  69.             if ORIENTATION: header.extend(["pitch","roll","yaw"])
  70.             if MAG: header.extend(["mag_x","mag_y","mag_z"])
  71.             if ACCELERATION: header.extend(["accel_x","accel_y","accel_z"])
  72.             if GYRO: header.extend(["gyro_x","gyro_y","gyro_z"])
  73.             header.append("timestamp")
  74.             with open(filename,"w") as f: f.write(",".join(str(value) for value in header)+ "\n") #write the column headers
  75.     if len(output_buffer) >= OUTPUT_BUFFER_SIZE: #if the output queue is full
  76.         print("Writing %s data points to file.." % len(output_buffer)) #give a message to the screen
  77.         log_size += len(output_buffer) #update the log file size
  78.         with open(filename,"a") as f: #append to the current file
  79.             for data_point in output_buffer: #for each data point in the output queue
  80.                 f.write(",".join(str(value) for value in data_point) + "\n") #write the data point from the output queue to file
  81.             output_buffer = [] #empty the output queue
  82.     if log_size >= MAX_LOG_FILE_SIZE: log_size=0 #if the log is too big, signal to start a new one
  83.     sleep(DELAY) #wait before getting the next data point
RAW Paste Data
Top