Page 1 of 1

Sense HAT data collection

Posted: Sat May 27, 2017 4:09 pm
by keithv
I've found code online to collect data from the sensors and write it to a CSV. The HAT collects data ~12/second. Is it possible to dial this back? Would this be done in the script or elsewhere? thanks

Re: Sense HAT data collection

Posted: Sat May 27, 2017 5:04 pm
by bensimmo
Search the website for SenseHat Datalogging. It written in Python, but all the code for everything you need with explanations is there.
It works very well.

You can set logging rate, set write batch writing times etc.
Even select at the start what you wish to log.

But you basically just slow the data collection loop down with a pause.

Re: Sense HAT data collection

Posted: Sat May 27, 2017 6:20 pm
by keithv
I'm using the code https://www.raspberrypi.org/learning/se ... gger_v4.py

When I try to change DELAY=0 to anything besides 0 the script just hangs. What else do I need to change?

Re: Sense HAT data collection

Posted: Sat May 27, 2017 7:26 pm
by bensimmo
You are using Python3 (command line) or IDLE3 (Pixel desktop)

I would the thread is failing.
When you ctrl+C to quit, what's the error, just in case that helps.

It should just work.

Re: Sense HAT data collection

Posted: Sun May 28, 2017 1:25 am
by keithv
Using IDLE3 on Raspbarian.

Traceback (most recent call last):
File "/home/pi/Desktop/sensors v4.py", line 120, in <module>
sense_data = get_sense_data()
File "/home/pi/Desktop/sensors v4.py", line 72, in get_sense_data
mag = sense.get_compass_raw()
File "/usr/lib/python3/dist-packages/sense_hat/sense_hat.py", line 776, in get_compass_raw
raw = self._get_raw_data('compassValid', 'compass')
File "/usr/lib/python3/dist-packages/sense_hat/sense_hat.py", line 703, in _get_raw_data
if self._read_imu():
File "/usr/lib/python3/dist-packages/sense_hat/sense_hat.py", line 691, in _read_imu
attempts += 1
KeyboardInterrupt


THat's what I get on the keyboard interrupt.

Re: Sense HAT data collection

Posted: Sun May 28, 2017 8:51 am
by bensimmo
Can a mod move this to the SenseHat section, since we have one. (I've notified)
The developers may see/know the problem.

Do you get the same error everytime (might pinpoint where it hangs)

Even on 5 seconds and is there any file created.

Re: Sense HAT data collection

Posted: Sun May 28, 2017 1:23 pm
by keithv
It pitches the same error every time. The file gets created with the headers but no data.

Re: Sense HAT data collection

Posted: Mon May 29, 2017 11:07 am
by AmandeepKainth123
Hello,

Did you find a solution to this issue? I am having the exact same problem.

Thanks!

Re: Sense HAT data collection

Posted: Thu Jun 08, 2017 12:05 pm
by keithv
Any ideas on this? I'd like to keep using the Pi but the number of data points is just too much.

Re: Sense HAT data collection

Posted: Thu Jun 08, 2017 12:43 pm
by SkyRise
The problem seems to lie here:

Code: Select all

if DELAY > 0:
    sense_data = get_sense_data()
    Thread(target= timed_log).start()
Nowhere in that loop does it actually write to the file as it does later in the code which it will never get to because of the above loop:

Code: Select all

while True:
    sense_data = get_sense_data()

    if DELAY == 0:
        log_data()

    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 = []
:

Re: Sense HAT data collection

Posted: Thu Jun 08, 2017 2:22 pm
by bensimmo
It should because that is threaded out so running at the same time. (and i've been using it for a long time now)
It logs data

Code: Select all

def timed_log():
    while True:
        log_data()
        sleep(DELAY)
in a loop at a set time interval.

it then logs the data into 'batch_data'

Code: Select all

def log_data():
    output_string = ",".join(str(value) for value in sense_data)
    batch_data.append(output_string)
in the meantime the other (original) while loop is still running as fast as it can getting values from the sensehat, but when it see's 'batch_data' get to it's requested size, it writes.

Code: Select all

while True:
    sense_data = get_sense_data()

    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 = []

Re: Sense HAT data collection

Posted: Thu Jun 08, 2017 2:28 pm
by SkyRise
Duh, you are right.
I saw "thread", read "thread" but didn't think "thread"... :?