Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Data Collection Python Script for Excel Files
#Libraries#
from sense_hat import SenseHat
from datetime import datetime
import time
#Settings#
FILENAME = ""
WRITE_FREQUENCY = 50
#Functions#
def get_sensedata():
sense_data = []
sense_data.append(sense.get_temperature_from_humidity)
sense_data.append(sense.get_temperature_from_pressure())
sense_data.append(sense.get_humidity())
sense_data.append(sense.get_pressure())
o = sense.get_orientation()
yaw = o["yaw"]
pitch = o["pitch"]
roll = o["roll"]
sense_data.extend([pitch, roll, yaw])
mag = sense.get_compass_raw()
mag_x = mag["x"]
mag_y = mag["y"]
mag_z = mag["z"]
sense_data.extend([mag_x, mag_y, mag_z])
acc = sense.get_accelerometer_raw()
x = acc["x"]
y = acc["y"]
z = acc["z"]
sense_data.extend([x, y, z])
gyro = sense.get_gyroscope_raw()
gyro_x = gyro["x"]
gyro_y = gyro["y"]
gyro_z = gyro["z"]
sense_data.extend([gyro_x, gyro_y, gyro_z])
sense_data.append(datetime.now())
return sense_data
def log():
output_string = ",".join(str(value) for value in sense_data)
batch_data.append(output_string)
def file_setup(filename):
header = ["temp_h","temp_p", "humidity", "pressure", "pitch", "roll", "yaw", "mag_x", "mag_y", "mag_z", "accel_x", "accel_y", "accel_z", "gyro_x", "gyro_y", "gyro_z", "timestamp"]
with open(filename, "w") as f:
f.write(", ".join(str(value) for value in header) + "\n")
#Main#
sense = SenseHat()
batch_data = []
if FILENAME == "":
filename = "Sense_Log-" + str(datetime.now())+ ".xlsx"
else:
filename = FILENAME + "-"+str(datetime.now()) + ".xlsx"
file_setup(filename)
while True:
sense_data = get_sensedata()
log()
if len(batch_data) >= WRITE_FREQUENCY:
print("Writing to file...")
with open(filename, "a") as f:
for line in batch_data:
f.write(line + "\n")
batch_data = []
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.