is it possible to work with 2 setup() functions and 2 loop() within the ESP32-Arduino enviroment

YvesDS
Posts: 13
Joined: Sat Sep 30, 2017 10:57 am
Location: Bredene (Belgium)

is it possible to work with 2 setup() functions and 2 loop() within the ESP32-Arduino enviroment

Postby YvesDS » Fri May 25, 2018 11:35 am

Hi all,
maybe a very,very silly question, but anyway here i go ...

Does anyone have a idea on using 1x ESP32 but using both cores with 2 separate programs in each one running arduino code
so one would need 2 void setup() and 2 void loop() functions... at first there is no need of data to travel between both loop() functions

For example declaring void setup1() to run once and void loop1() to continously loop, and then again with void setup2() and void loop2()???

something like this i mean :

Code: Select all

static uint32_t var1 = 0;
static uint32_t var2 = 0;
static uint32_t var3 = 1000;

void setup1(){ //should run only once
	//code for this setup1() function on core 1
	var1 = 1000000;
	}
void setup2(){ //should also run only once on core 2
	//code for this setup1() function
	var2 = 250;
	var3 = 0;
	}

void loop()1{
	// code to be checked over and over again for this loop()1 on core 1
	dosomethingwithvar1();
}
void loop()2{
	// code to be checked over and over again for this loop()2 on core 2
	dosomethingwithvar2();
	dosomethingwithvar3();
}

That would make a big difference in the arduino/esp32 world... and be more than welcome, since at this moment we only use one core at a time within the ESP-Arduino, and if both cores are used, it is not dedicated witch one would be used for what part of the code, not?
So in that case, we are only using half of the power we have in hands with the ESP32 platform...

Grtz,
Yves

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

Re: is it possible to work with 2 setup() functions and 2 loop() within the ESP32-Arduino enviroment

Postby kolban » Fri May 25, 2018 2:45 pm

This is just a loose thought ... but is there a threading/task based package/library for the ESP32 Arduino? If there is, then one could create a task/thread that loosely looks like:

Code: Select all

void taskEntry() {
   setup();
   while(1) {
      loop();
   }
}
and then in your REAL setup() create two (or more) instances of this task.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: is it possible to work with 2 setup() functions and 2 loop() within the ESP32-Arduino enviroment

Postby chegewara » Fri May 25, 2018 2:48 pm

Two loop functions is not an issue, you can create one or two tasks and run your code in that tasks. Now is the question, why do you need two setup functions?

YvesDS
Posts: 13
Joined: Sat Sep 30, 2017 10:57 am
Location: Bredene (Belgium)

Re: is it possible to work with 2 setup() functions and 2 loop() within the ESP32-Arduino enviroment

Postby YvesDS » Fri May 25, 2018 3:52 pm

Hi chegewara,
chegewara wrote:Two loop functions is not an issue, you can create one or two tasks and run your code in that tasks. Now is the question, why do you need two setup functions?
Well the issue for me is that i am building a chronometer for dogsports, but since the dogs run against eachother on two separate lanes, all the software needs to be double (one for each lane), and above of this issue, the dogs start simultaniously, so in order to prevent multiple ESP32 talking to eachother, it would be better that everything could be handled from one ESP32.
Both lanes would have exactly the same code, except the variables, they would be named to the lane the dogs compete in (ex. LeftLaneVar1 against RightLaneVar1)
At this moment there is no need to exchange data between the two lanes, each lane would come with a dedicated LCD display to show the results
So the setup functions would be needed (i think?) to initiate the right and left lane in one core each (no???)
Since there are interrupts involved, by this i could pin those in setup1() to the first core and setup2() to second core (no???)

That is roughly the idea behind it, later WiFi or BLE would be needed to transfer data to tablets or laptops

I use Arduino-ESP32 platform, and the Arduino IDE, a have Platformio with VSCode on my computer, but still in a experimenting phase, i'm not used to work with this inderface for the ESP32 or Arduino

Grtz,
Yves

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

Re: is it possible to work with 2 setup() functions and 2 loop() within the ESP32-Arduino enviroment

Postby kolban » Fri May 25, 2018 4:00 pm

Just thinking out loud ... but based on your design description (thank you for that), do you really need to use both cores? Let's think about it ...

Imagine that when a dog crosses the finish line, it is detected. This is logically an "event" and an event happens at a given point in time. The event also has some data ... which dog crossed the line? In your sport you may have 2 dogs ... but maybe someday it is 3?

I am imagining two sensors ... one is triggered for lane 1 and one for lane 2. Now I am imagining two interrupts ... one for each sensor. I am also imagining that the interrupt can self identify its source (lane 1 vs lane 2).

Now your logic might be something like:

Code: Select all

while(racing) {
   if (we have received an event) {
      Update the display to show the time for that lane.
   }
   update the display to show the current elapsed time from the start of the race.
}
In this pseudo code, we don't need parallelism but appear to have our fine grained timing. This feels (to me and opinionated) a simpler design than trying to create parallelism.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: is it possible to work with 2 setup() functions and 2 loop() within the ESP32-Arduino enviroment

Postby chegewara » Fri May 25, 2018 4:18 pm

In your case its even simpler case. You can create one task, then you can pass some structure as argument to task. This structore can carry info that you need to initialize task (only data that is different for each task/line). Example:

Code: Select all

struct data_struct{
// some data
};

void task(void* data){
//initialization part
data_struct lane_data = *((data_struct*)data);
while(1){
// code that would be in loop() 
}

}

setup(){
data_struct lane1 = // setup data here
data_struct lane2 = // setup data here

xTaskCreate(task, "line 1", 8196, &lane1, 5, NULL);
xTaskCreate(task, "line 2", 8196, &lane2, 5, NULL);
}

loop(){
}

rin67630
Posts: 141
Joined: Sun Mar 11, 2018 5:13 pm

Re: is it possible to work with 2 setup() functions and 2 loop() within the ESP32-Arduino enviroment

Postby rin67630 » Wed Jun 06, 2018 12:58 pm

I can remember that Andreas Spiess has issued a video about that.
But remember the second core must provide Wifi+Bluetooth fuctionality, so it is to be used with care.
Anyhow you have to use freeRTOS and manage the cooperation with the base code to evoid jeopardizing the core functionality.
Last edited by rin67630 on Wed Jun 06, 2018 1:03 pm, edited 1 time in total.

rin67630
Posts: 141
Joined: Sun Mar 11, 2018 5:13 pm

Re: is it possible to work with 2 setup() functions and 2 loop() within the ESP32-Arduino enviroment

Postby rin67630 » Wed Jun 06, 2018 1:01 pm

YvesDS wrote:Hi all,
...and be more than welcome, since at this moment we only use one core at a time within the ESP-Arduino, and if both cores are used, it is not dedicated witch one would be used for what part of the code, not?
Remember: the second core is not idling, it provides WiFi+Bluettoth.

wan420
Posts: 11
Joined: Mon Nov 12, 2018 8:49 am

Re: is it possible to work with 2 setup() functions and 2 loop() within the ESP32-Arduino enviroment

Postby wan420 » Sun Nov 18, 2018 8:33 pm

Sorry to jump into an old post, but since we are talking about cores in ESP32, and as stated in the above comments that the 2nd core is used for BLE+WIFI, my small doubt is, can I use 1 core for BLE and 1 Core for Wifi?

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: is it possible to work with 2 setup() functions and 2 loop() within the ESP32-Arduino enviroment

Postby idahowalker » Mon Nov 19, 2018 9:47 am

freeRTOS and:

Code: Select all

xTaskCreatePinnedToCore( fUpdateDisplay, "fUpdateDisplay", TenK, NULL, Priority5, NULL, TaskCore1 ); // assigned to core 1
  xTaskCreatePinnedToCore( fBlinkBuiltIn, "fBlinkBuiltIn", TenK, NULL, Priority2, NULL, TaskCore0 ); //assigned to core 0
Just to show a few functions.
And my loop looks like

Code: Select all

void loop() {}

Who is online

Users browsing this forum: No registered users and 90 guests