ESP32 interrupt for sampling MPU6886

Post here to discuss any new features, components, chips, etc, that you would like to see in Flowcode.
Post Reply
stefan.erni
Valued Contributor
Posts: 883
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 173 times
Been thanked: 203 times

ESP32 interrupt for sampling MPU6886

Post by stefan.erni »

Hi Ben

The interrupt on the ESP32 working nice if you just set a variable.
If I want to sample my IMU with an always constant time it would be practical to do this with the timer.
But I cannot insert commands in the macro called by the timer without the program in ESP32 crashing later.
Is there a way to change this?

regards

Stefan

Steve-Matrix
Matrix Staff
Posts: 1384
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 184 times
Been thanked: 322 times

Re: ESP32 interrupt for sampling MPU6886

Post by Steve-Matrix »

You could maybe set a flag in the timer code and then react to that flag in the main loop of your program. It's always best to minimise the actual code within an interrupt routine.

stefan.erni
Valued Contributor
Posts: 883
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 173 times
Been thanked: 203 times

Re: ESP32 interrupt for sampling MPU6886

Post by stefan.erni »

Hi Steve

Yes that what I do at the moment. I use a flag

With one big disadvantage.
When I write the buffer to the sd-card I miss the samples and the speed and position is not correct in the calculation later in the Appdeveloper.

Maybe you have a good idea

regards

Stefan

Snag_53bb5a3.png
Snag_53bb5a3.png (12.3 KiB) Viewed 11384 times
Attachments
IMU_sdcard_Ch1.fcfx
(128.74 KiB) Downloaded 564 times

BenR
Matrix Staff
Posts: 1895
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 491 times
Been thanked: 664 times

Re: ESP32 interrupt for sampling MPU6886

Post by BenR »

Hello,

Are you using ADC in other parts of your program? If you are then this could be the source of the crash if the ADC code in the interrupt collides with the same code in the main.

Otherwise it might be worth trying the Raw method of enabling the channel and then doing the sample inside the interrupt. You will need to use the CAL ADC component to get access to the raw functions.

Setting up the ESP for automated sampling via DMA is also probably possible though a bit more messy in terms of using C code.

I have just got released a new SD component for ESP that allows the 1-bit and 4-bit SD modes. The speed gain is noticable but isn't ground breaking so may or may not be useful for you. In my tests I can stream 460,800 Byte bitmap to a SPI display in 1.3s over SPI and 0.9s over SD 4-wire.

stefan.erni
Valued Contributor
Posts: 883
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 173 times
Been thanked: 203 times

Re: ESP32 interrupt for sampling MPU6886

Post by stefan.erni »

Hi Ben

I don't need the ADC at the moment but I will need it later in another project.
Now I only need to read in an IMU and store the values in one of two buffers alternately.
Then when the first buffer is full,I switch to the another buffer and store the unused buffer on the sd card.

But I cannot insert some commands in the macro called by the timer without the program in the ESP32 crashing later.
just calculation is working to set a "flag"

if possible this code in the interrupt...
get_IMU_data.fcm
(9.26 KiB) Downloaded 562 times
A quick method to write on card is to make one big block at a time and write to the sd card all 512byte at once
file_write_block.fcm
(6.29 KiB) Downloaded 555 times


Very good with the 4Bit SD card.
I will look for a suitable hardware where the SD card are wired to the SD_Data pin and not to the GPIO pin.
And check it out!
Snag_4b6d92.png
Snag_4b6d92.png (114.08 KiB) Viewed 11352 times

regards

Stefan

stefan.erni
Valued Contributor
Posts: 883
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 173 times
Been thanked: 203 times

Re: ESP32 interrupt for sampling MPU6886

Post by stefan.erni »

Hi Ben

For my project, it would be much easier if I could sample an IMU via I2C in the interrupt part. This would allow me to store some values in a buffer and then save it, in one go, on the sd-disk (4Bit). The sampling rate would also be more constant and I could significantly increase the sample rate speed.
I use the component LSM9DS1 9 Axis Sensor.

regards

Stefan

mnfisher
Valued Contributor
Posts: 1213
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 118 times
Been thanked: 622 times

Re: ESP32 interrupt for sampling MPU6886

Post by mnfisher »

Why not create a seperate task (xTaskCreate) that reads the sensor and saves in a loop. Will need a minimum of 10ms delay per loop - but otherwise can run happily without blocking an interrupt.

Martin

stefan.erni
Valued Contributor
Posts: 883
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 173 times
Been thanked: 203 times

Re: ESP32 interrupt for sampling MPU6886

Post by stefan.erni »

Yes sounds good,
I can then also consider whether to take the sd-card or the sensor in the lopp?
10m sec would give 100Hz, better than now. Maybe I can also sample several times in the loop.
Does FC already support the x-task?

mnfisher
Valued Contributor
Posts: 1213
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 118 times
Been thanked: 622 times

Re: ESP32 interrupt for sampling MPU6886

Post by mnfisher »

It's easy to create a task - but it does take a line of C,

xTaskCreate(FCM_Task, "TaskName", stk_size, params, priority, handle) - where stackize is 4096 (or more/less as needed) params - can be 0, priority can be 0 (or higher) and handle can be 0)

I posted an example here = updates a ws2812 string - viewtopic.php?t=1726&start=10#p14339

As mentioned - the task must yield for a minimum of 10ms at regular (<WDT intervals) to allow other tasks (and the OS has some) to run. So a delay of 50ms would give 20 samples per second (less the sample/save time) - but other types of delay are possible if timing is more important - see https://docs.espressif.com/projects/esp ... ertos.html

Martin

xTaskCreate( vTaskCode, "NAME", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );
configASSERT( xHandle );

stefan.erni
Valued Contributor
Posts: 883
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 173 times
Been thanked: 203 times

Re: ESP32 interrupt for sampling MPU6886

Post by stefan.erni »

Hi to All

Thanks Martin, I have understood working with two tasks.
But I can't take breaks with sampling.
I need all samples at the right time, only then will the machine learning part, work fine in Matlab.
So in my application I need to sample an IMU constantly and write values of all 9 axes to the SD card or send them with Bluetooth to the APP on the PC and save it there.
If I could use the component to configure the parameters of the LSM9DS1 and read the 9 registers of the axes with a C code and fill them into a buffer it would be great.
I use two identical buffers A and B.
When I sample I first store the values in buffer A and when it is full I store A on the sd-card and sample in buffer B. Then I switch again.
I did something like this with the PIC32 and was able to sample and store 12 axes at 2Khz.
Unfortunately the PIC32 does not have Bluetooth and using it with the RN4677 BT module was awkward.

regards

Stefan

Post Reply