IMU Logger Problem

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Post Reply
User avatar
shubhankar
 
Posts: 3
Joined: Wed Aug 10, 2022 8:48 am

IMU Logger Problem

Post by shubhankar »

Hi

I am fairly new to Circuit python. I am trying to make an IMU logger which would log IMU data (accelerations and rotation rates) onto a Micro SD card at 1 Khz.

Board - Adafruit stm32f405 Feather
IMU - MPU6050
Card - Strontium 16gb class 10 Micro SD card

The issue i am facing is that the board logs for a few lines onto the SD card but after the first few lines the code crashes. My code is pasted below.

Kindly help out with letting me know what i am doing wrong? If possible, please guide me to the right resources for getting it right.

Code: Select all

import  board
import digitalio
import sdioio
import storage
import time
import board
import adafruit_mpu6050

led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

i2c = board.I2C()  # uses board.SCL and board.SDA
mpu = adafruit_mpu6050.MPU6050(i2c)

sdcard = sdioio.SDCard(
    clock=board.SDIO_CLOCK,
    command=board.SDIO_COMMAND,
    data=board.SDIO_DATA,
    frequency=25000000)

vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")

with open("/sd/test.txt", "w") as f:
        f.write("Printing values :\n" )

a = 0
while True:
    a += 1
    with open("/sd/test.txt", "a") as f:
        led.value = True
        acc = mpu.acceleration
        gyr = mpu.gyro
        f.write( str(a) + " = " + str(acc)+ " " + str(gyr)+ "\n")
        led.value = False
    time.sleep (0.001)
Last edited by dastels on Wed Aug 10, 2022 9:09 am, edited 1 time in total.
Reason: Add code tag

User avatar
dastels
 
Posts: 11042
Joined: Tue Oct 20, 2015 3:22 pm

Re: IMU Logger Problem

Post by dastels »

Have you tried a slower update rate?

As it is your sampling rate is somewhat less than 1kHz. You have a delay of 1mS to which is added the time to do everything else in the loop: opening a file, taking the readings, string conversions and concatenation, writing to a file, flushing/closing the file. It all takes time, and Python isn't a speed demon to start with. If you add time.monotonic_ns() to your output you'll have a better idea of what your sampling rate actually is.

Dave

User avatar
shubhankar
 
Posts: 3
Joined: Wed Aug 10, 2022 8:48 am

Re: IMU Logger Problem

Post by shubhankar »

Hey Dave

By slower update rate, do you mean lowering the speed of the IMU, or the frequency of the SD card (25mhz) , or something else?

If I remove the 1ms delay, the code crashes anyway after writing about 10 lines to the SD card.

If I add a 1 sec or 0.5 sec delay (instead of 1ms), the code works fine and data gets written to the card.

But I need at least 1000 IMU samples per second, and even more if possible. I know SPI cant support such speeds hence I am trying to use SDIO.

User avatar
Claude_J_Greengrass
 
Posts: 47
Joined: Sat Jul 24, 2021 10:53 am

Re: IMU Logger Problem

Post by Claude_J_Greengrass »

consider: High Frequency, Long Duration Datalogging with remote control via Teensy3.2 and SD Card
https://www.forward.com.au/pfod/Arduino ... ngDur.html

I spoke with Bill Grieman the author of the SDFat library about speed as I have a project with a 1000 sample/sec requirement. He said it wasn't possible with the PyGamer board as the writes to the SD card is too slow. Also, it has been my experience that you will be taking a 40:1 hit with Python vs C in execution speed.

Having reviewed Bill's comments and Bill Ford's data logger info, I will use a Teensy4.1 for my 1000 sample/sec project. YMMV

User avatar
blakebr
 
Posts: 396
Joined: Tue Apr 17, 2012 6:23 pm

Re: IMU Logger Problem

Post by blakebr »

I noticed you open the file with append each loop but you never close the file. Is that an issue?
Also take flashing the LED code out. It just slows things down.
You may wish to move the delay right 4 spaces to make it part of the with loop.
How do you exit the with loop?

User avatar
shubhankar
 
Posts: 3
Joined: Wed Aug 10, 2022 8:48 am

Re: IMU Logger Problem

Post by shubhankar »

@claude, thank you for the link. I will try that out if this doesn't work out. Well need to get my hands on a teensy first (which seems to be out of stock everywhere)

@blake, from what i read in the documentation, the "with" function takes care of opening, writing and closing the file. So there is no separate function needed to close the file. This would also mean that the sleep function will do the same job whether inside or outside the loop. However, i tried both your suggestions, moving the delay inside and removing the LED indication, but the code still crashes.

I have tried this with multiple SD cards so the card is not a problem. The code works just fine if the delay is 0.5 secs.

User avatar
blakebr
 
Posts: 396
Joined: Tue Apr 17, 2012 6:23 pm

Re: IMU Logger Problem

Post by blakebr »

As I recall EEPROM requires about 25 milliseconds to write a page of data.
Try time.sleep(0.025) and see what happens.

Bruce

User avatar
Claude_J_Greengrass
 
Posts: 47
Joined: Sat Jul 24, 2021 10:53 am

Re: IMU Logger Problem

Post by Claude_J_Greengrass »

shubhankar wrote:@claude, thank you for the link. I will try that out if this doesn't work out. Well need to get my hands on a teensy first (which seems to be out of stock everywhere)

@blake, from what i read in the documentation, the "with" function takes care of opening, writing and closing the file. So there is no separate function needed to close the file. This would also mean that the sleep function will do the same job whether inside or outside the loop. However, i tried both your suggestions, moving the delay inside and removing the LED indication, but the code still crashes.

I have tried this with multiple SD cards so the card is not a problem. The code works just fine if the delay is 0.5 secs.
Found a second reference that might be of interest to your project. "High Frequency, Long Duration Datalogging with UNO/Mega2560 Arduino and SD Card"

As the title states, your don't need a teensy https://www.forward.com.au/pfod/Arduino ... ngDur.html

Post Reply
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”