Sampling 2pcs IMU 6axis @ 200Hz with PIC32,STM32,or ESP32?

For general Flowcode discussion that does not belong in the other sections.
stefan.erni
Valued Contributor
Posts: 1065
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 201 times
Been thanked: 225 times

Sampling 2pcs IMU 6axis @ 200Hz with PIC32,STM32,or ESP32?

Post by stefan.erni »

Hi
I would like to sample two IMUs with 6 axes each at 200Hz and send them to the PC APP via USB.
But I don't know yet which processor is best to use.

With the PIC32MZ2048 and STM32F469, I can use interrupt from the timer to sample data.
With the ESP32 no possibility to sample something in the timer loop just errors.
Can the timer interrupt in the ESP32 be improved?
What would work best of these 3 processors?
Or a completely different type?

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: Sampling 2pcs IMU 6axis @ 200Hz with PIC32,STM32,or ESP32?

Post by mnfisher »

Hi Stefan,

On the esp32 - could create a new task (or 2) and just sample in that (them) with a loop and delay?

Martin

stefan.erni
Valued Contributor
Posts: 1065
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 201 times
Been thanked: 225 times

Re: Sampling 2pcs IMU 6axis @ 200Hz with PIC32,STM32,or ESP32?

Post by stefan.erni »

Hi Martin

Would you make a second task for sampling or sending?
In the PIC32 I have sampling in the interrupt and sending in the main.

send if the buffer is full(send ready=1)
2024-10-18_15-25-08.PNG
2024-10-18_15-25-08.PNG (26.65 KiB) Viewed 4218 times
fill the buffer and make a copy and set (send ready=1)
2024-10-18_15-20-48.PNG
2024-10-18_15-20-48.PNG (104.04 KiB) Viewed 4218 times

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: Sampling 2pcs IMU 6axis @ 200Hz with PIC32,STM32,or ESP32?

Post by mnfisher »

You could both sample and send in the task - but there is a proviso to that. If you have multiple tasks sending the data can get 'mixed'- so you'll need some sort of semaphore (or lock). Alternatively the tasks could feed the data into a queue and main (or another 'send' task) output the data.
Again - needs a little bit of care if multiple tasks are accessing the same data to avoid corruption.

Martin

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: Sampling 2pcs IMU 6axis @ 200Hz with PIC32,STM32,or ESP32?

Post by mnfisher »


stefan.erni
Valued Contributor
Posts: 1065
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 201 times
Been thanked: 225 times

Re: Sampling 2pcs IMU 6axis @ 200Hz with PIC32,STM32,or ESP32?

Post by stefan.erni »

Hi Martin
With the PIC
I copied the buffer to avoid accessing the same memory at the same time.
This is very fast with the PIC and probably also with the ESP
2024-10-18_17-00-51.PNG
2024-10-18_17-00-51.PNG (57.87 KiB) Viewed 4203 times
So at the moment:
With ESP i only set recsample to 1 in the interrupt. Nothing additional.
2024-10-18_17-09-03.PNG
2024-10-18_17-09-03.PNG (23.05 KiB) Viewed 4203 times
and in the main i sample the IMU and send just the array with the 12 axis. This works nice with 20 Hz
2024-10-18_17-06-53.PNG
2024-10-18_17-06-53.PNG (51.01 KiB) Viewed 4203 times
If I have understood you correctly then I can also collect about 120 values and then in a second task
I send the copie from the buffer.
I simply have to send faster than saving data in the buffer.
This i think i can controll with my osciloscope

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: Sampling 2pcs IMU 6axis @ 200Hz with PIC32,STM32,or ESP32?

Post by mnfisher »

A small example program:

It's rather contrived - and sends strings of data rather than 'real' data - and outputs the values received to UART - rather than processing them in any way.

I use two queues - and two tasks (Sensor1 and Sensor2) that push data into the queues (in this case the 'data' is just 8 characters "A..H" and for sensor2 "a..h" - that is rotated on each 'send'. The queue - here is defined using xQueueCreate to take 8 bytes of data per item.

There is a macro RcvQueue that outputs all the data from a queue to UART before returning - and here Sensor1 sends 1 'reading' per second and sensor2 one every 100ms.

I am just outputting the data from main here (by calling RcvQueue) - then waiting 5s and repeating.
Note - the queues are created in main and passed to the Sensors as parameters. This could be a separate task if required - but I don't need main to do anything else.

Every 5s - queue1 holds 5 data items and queue2 up to 20 - as set here, Sensor2 sends 10 items per second - but the queue is full at 20 items. Here I drop the additional items, but Sensor2 could wait either for a set period or until there is room in the queue as required.

Note that the data is copied to the queue - the sensor tasks can modify the data without affecting the sent data. Also all the tasks (including main) must 'yield' to allow the multi-tasking of RTOS to occur (there are always several tasks running - so every program must do this - otherwise WDT issues will occur)

Although I use two queues - using one also works - queues are thread safe. Using one queue - means the queue holds a 'mix' of the data from Sensor1 and Sensor2 (maybe the first byte could indicate which?)

In the real world - a check should be made that both the queues and the tasks can be created - and failure handled gracefully.

Martin
Attachments
Queue.fcfx
(20.32 KiB) Downloaded 147 times

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: Sampling 2pcs IMU 6axis @ 200Hz with PIC32,STM32,or ESP32?

Post by mnfisher »

Forgot to mention - if you want to read at 200Hz - set the delay in the sensor macro(s) to 5ms - and it will run okay - though you'll need to do something other with the data rather than outputting to UART every 5s...

stefan.erni
Valued Contributor
Posts: 1065
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 201 times
Been thanked: 225 times

Re: Sampling 2pcs IMU 6axis @ 200Hz with PIC32,STM32,or ESP32?

Post by stefan.erni »

Hi Martin

Thank you very much. The Queve works well.
Soon I connect the real sensor und trie it again.

Later I then try to read both sensors in one macro
and store them in one integer array and Send this array with one Queve.

stefan.erni
Valued Contributor
Posts: 1065
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 201 times
Been thanked: 225 times

Re: Sampling 2pcs IMU 6axis @ 200Hz with PIC32,STM32,or ESP32?

Post by stefan.erni »

Hi Martin, Hi Ben

At the moment it works with the ESP32, I can read and save both sensors. However, there is a problem with the SD card when saving the data. I use a string in excel format which also works very well for saving. It only needs 128uSec for the big string write to disk. But at regular intervals the saving takes 1.4mSec. Then I can't sample. Can I move the appendStringToFile into a separate task? or in the second core?
2025-01-21_15-31-17.PNG
2025-01-21_15-31-17.PNG (24.38 KiB) Viewed 1814 times
yellow=I2C read the IMU; green= write to disk
scope_41.png
scope_41.png (41.84 KiB) Viewed 1814 times
result .csv file
2025-01-21_15-47-50.PNG
2025-01-21_15-47-50.PNG (242.64 KiB) Viewed 1814 times

Post Reply