keithv
Posts: 5
Joined: Sat May 27, 2017 3:57 pm

Sense HAT data collection

Sat May 27, 2017 4:09 pm

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

bensimmo
Posts: 1737
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Sense HAT data collection

Sat May 27, 2017 5:04 pm

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.

keithv
Posts: 5
Joined: Sat May 27, 2017 3:57 pm

Re: Sense HAT data collection

Sat May 27, 2017 6:20 pm

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?

bensimmo
Posts: 1737
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Sense HAT data collection

Sat May 27, 2017 7:26 pm

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.

keithv
Posts: 5
Joined: Sat May 27, 2017 3:57 pm

Re: Sense HAT data collection

Sun May 28, 2017 1:25 am

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.

bensimmo
Posts: 1737
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Sense HAT data collection

Sun May 28, 2017 8:51 am

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.

keithv
Posts: 5
Joined: Sat May 27, 2017 3:57 pm

Re: Sense HAT data collection

Sun May 28, 2017 1:23 pm

It pitches the same error every time. The file gets created with the headers but no data.

AmandeepKainth123
Posts: 1
Joined: Mon May 29, 2017 11:06 am

Re: Sense HAT data collection

Mon May 29, 2017 11:07 am

Hello,

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

Thanks!

keithv
Posts: 5
Joined: Sat May 27, 2017 3:57 pm

Re: Sense HAT data collection

Thu Jun 08, 2017 12:05 pm

Any ideas on this? I'd like to keep using the Pi but the number of data points is just too much.

SkyRise
Posts: 137
Joined: Tue Jan 24, 2012 1:20 pm

Re: Sense HAT data collection

Thu Jun 08, 2017 12:43 pm

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

bensimmo
Posts: 1737
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Sense HAT data collection

Thu Jun 08, 2017 2:22 pm

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

SkyRise
Posts: 137
Joined: Tue Jan 24, 2012 1:20 pm

Re: Sense HAT data collection

Thu Jun 08, 2017 2:28 pm

Duh, you are right.
I saw "thread", read "thread" but didn't think "thread"... :?

Return to “Astro Pi”

Who is online

Users browsing this forum: No registered users and 1 guest