ESP32 Create task with dynamic value parameter

danielfmo
Posts: 2
Joined: Mon Aug 06, 2018 11:46 am

ESP32 Create task with dynamic value parameter

Postby danielfmo » Mon Aug 06, 2018 12:09 pm

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

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: ESP32 Create task with dynamic value parameter

Postby kolban » Mon Aug 06, 2018 1:59 pm

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.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Available for ESP32 consulting.

ESP_Dazz
Posts: 32
Joined: Fri Jun 02, 2017 6:50 am

Re: ESP32 Create task with dynamic value parameter

Postby ESP_Dazz » Mon Aug 06, 2018 4:46 pm

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.

danielfmo
Posts: 2
Joined: Mon Aug 06, 2018 11:46 am

Re: ESP32 Create task with dynamic value parameter

Postby danielfmo » Wed Aug 08, 2018 5:23 pm

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

User avatar
fly135
Posts: 350
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: ESP32 Create task with dynamic value parameter

Postby fly135 » Tue Aug 14, 2018 4:36 pm

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

Who is online

Users browsing this forum: No registered users and 5 guests