Page 1 of 1

ESP32 Create task with dynamic value parameter

Posted: Mon Aug 06, 2018 12:09 pm
by danielfmo
Hello all,

I would like to have your advice on a project that I'm trying to accomplish with my ESP32, and before going to the question I'll give you some context.
I want to use my ESP32 to control 8 DC motors by interpreting commands/data from BLE or/and WIFI. The motors will be controlled by sending commands though I2C bus to 4 wemos motor shields.

I'm using at the moment Blynk connected through BLE to send imput information and when sending information of 2 motors it's response is sluggish so after some research I believe that it would be best to have a solution as following:
  1. Have a global int array[8] where each element will be the direction/speed of each motor;
  2. In the loop() function read the BLE/Wifi input and update the array;
  3. Create a Task pinned in the core 0 that periodically (max each 100ms) will read the values of the array and send the I2C commands to the shields.
My questions are:
  1. In the above case hoe can I manage concurrency? Should I use mutexes when updating the array values?
  2. Is this solution too overkill? do you recommend another solution?
Thank you in advance

Re: ESP32 Create task with dynamic value parameter

Posted: Mon Aug 06, 2018 1:59 pm
by kolban
Howdy and welcome to the forum.

I think to assist you best we are going to have to learn in more detail what the perceived nature of the problem may be. When we say that the response is "sluggish" doesn't tell us much. Maybe if we say a data flow diagram of the transmission path of requests that would help. Sluggish also implies that there is a latency between a request and an actuation. That means that time is being spent somewhere. It would be useful to see fine grained timing measurement of where time is being spent while processing a command.

Re: ESP32 Create task with dynamic value parameter

Posted: Mon Aug 06, 2018 4:46 pm
by ESP_Dazz
danielfmo wrote: I'm using at the moment Blynk connected through BLE to send imput information and when sending information of 2 motors it's response is sluggish so after some research I believe that it would be best to have a solution as following:
  1. Have a global int array[8] where each element will be the direction/speed of each motor;
  2. In the loop() function read the BLE/Wifi input and update the array;
  3. Create a Task pinned in the core 0 that periodically (max each 100ms) will read the values of the array and send the I2C commands to the shields.
Just use FreeRTOS tasks and queues. Create a task for BLE operations, and another for task for I2C, then have the BLE task send the commands through a queue to the I2C task. Doing it this way will be completely event driven and prevent CPU time from being wasted by polling.

Re: ESP32 Create task with dynamic value parameter

Posted: Wed Aug 08, 2018 5:23 pm
by danielfmo
ESP_Dazz wrote: Just use FreeRTOS tasks and queues. Create a task for BLE operations, and another for task for I2C, then have the BLE task send the commands through a queue to the I2C task. Doing it this way will be completely event driven and prevent CPU time from being wasted by polling.
Hello ESP_Dazz,

Thank you for your idea Ill implement something like that and see it improves.
I took sometime to read about available FreeRTOS "functions" and I'll also try to just have a global structure to store motor direction and speed, a task to read BLE and update the structure, and another to read from it and send I2C commands and compare both.

@kolban I have no problem I just and to do better :D by learning more about ESP32 and its capabilities

Re: ESP32 Create task with dynamic value parameter

Posted: Tue Aug 14, 2018 4:36 pm
by fly135
danielfmo wrote:In the above case hoe can I manage concurrency? Should I use mutexes when updating the array values?
Yes, you should use a mutex for both getting and putting to the array. Otherwise a getting task may get partly old data and partly new if it gets while another task is putting. Any variable that isn't atomic should be protected by a mutex in a situation where multiple tasks are accessing.

http://preshing.com/20130618/atomic-vs- ... perations/

John A