pythoncsvraspberry-piimu

collect data from magentometer on a sensehats IMU


I can't find information about how to import and collect data from an IMU on a sensehat connected to my raspberry pi. What do I write to import it and save the data?

I have made a code with the same function, however for the accelerometer:

import logging
import logzero
from logzero import logger
from sense_hat import SenseHat
import os
dir_path = os.path.dirname(os.path.realpath(__file__))

sh = SenseHat()

logzero.logfile(dir_path+"/accel.csv")

formatter = logging.Formatter('%(name)s - %(asctime)-15s - %(levelname)s: %(message)s');
logzero.formatter(formatter)

acceleration = sense.get_accelerometer_raw()
x = acceleration['x']
y = acceleration['y']
z = acceleration['z']

x=round(x, 0)
y=round(y, 0)
z=round(z, 0)

logger.info("%s,%s,%s", x, y, z, )

Solution

  • From Sense HAT documentation page (https://pythonhosted.org/sense-hat/api/#imu-sensor)

    You have to set sense.set_imu_config(True,True,True) #compass/gyroscope/accelerometer

    import logging
    import logzero
    from logzero import logger
    from sense_hat import SenseHat
    import os
    dir_path = os.path.dirname(os.path.realpath(__file__))
    
    sh = SenseHat()
    sh.set_imu_config(True, True, True)
    
    logzero.logfile(dir_path+"/accel.csv")
    
    formatter = logging.Formatter('%(name)s - %(asctime)-15s - %(levelname)s: %(message)s');
    logzero.formatter(formatter)
    
    acceleration = sh.get_accelerometer_raw()
    x = acceleration['x']
    y = acceleration['y']
    z = acceleration['z']
    
    x=round(x, 0)
    y=round(y, 0)
    z=round(z, 0)
    
    north = sh.get_compass()
    print("North: %s" % north) #prints direction of North in degrees
    
    logger.info("%s,%s,%s", x, y, z, )