Im using an ESP32 with the Arduino framework to control a robot that needs a control rate of at least 2000hz but ideal would be 4000hz. My idea was to get one task to only read sensor data from an MPU9250 at 4khz and another task to send data to a computer.
Problem is if i dont use a delay function and just check if enough time has past to read the sensor, the other task to send data is blocked and wont run or very slowly. Is far as ive seen and understand, the delay function only goes down to 1ms which gives me a max of 1khz and delayMicroseconds doesnt free the cpu.
How can i get around this problem without putting everything in one task?
In most cases that is too fast for a task. Can you generate the frequency using a timer interrupt and do the necessary processing in the interrupt service routine?
Yes, that would be problematic. ISRs should be as short as possible.
The usual pattern is to do just the minimum IRQ handling in the ISR, notify an associated post-processing task about the HW event and do the heavy lifting there.
But then the post-processing task also needs to run at 4khz because i need to send commands to outputs at 4khz (the same speed at which the IMU runs).
If i use one CPU core just for the one task. Will this one task also end up blocking tasks running one the other CPU core?
Say Core 1 just for getting sensor data, post-processing and then also output and core 0 for telemetry data and controlling the robot autonomously?
But I would like to avoid this to make programming easier (multiple tasks on both cores).
Seems that your application is pretty demanding …
But since you have an obviously beefy multi-core MCU you could make use of it.
It depends on the provided performance whether you need it or not.
Here is an example how to make use of multi-core MCUs with uni-core FreeRTOS. FreeRTOS AMP example
Hope it helps making a good design decision