Hello,
...It is me again with another programming question. This one is about structures. I've created a simple program, using freeRTOS, to perform 4 tasks:
- Task1 randomly generates a 0 or 1
- Task2 randomly generates an integer between 500 and 1000
- Task3 randomly generates an integer between 0 and 100 and averages the last 4 values
- Task4 Log the values in tasks 1, 2 and 3 to the serial port.
This is the code:
// Use only core 1 to implement everything on the single core
#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif
int state; //Task1's variable
int frequency;//Task2's variable
unsigned short int filter_analg_val;//Task3's variable
void Task1(void *argp) {
while (1) {
state = rand() % 2; //generate a random int between 0 and 1 (i.e. 0 or 1)
vTaskDelay(200); //execute this task every 200ms
}
}
void Task2(void *argp) {
while (1) {
frequency = 500 + (rand() % (1000 - 500 + 1)); //randomly set frequency to an int between 500 to 1000
vTaskDelay(1000); //execute this periodic task every second
}
}
void Task3(void *argp) {
//filtered analogue value, by averaging the last 4 readings.
while (1) {
int new_analg_val = 0;
for (int i = 0; i <= 3; i++) { //take 4 readings
int analg_val = 0 + (rand() % (100 - 0 + 1)); //randomly set analg_val to an int between 0 to 100
new_analg_val = new_analg_val + analg_val;
}
filter_analg_val = new_analg_val / 4; //average of the last 4 readings
vTaskDelay(40); //execute this task every 40ms
}
}
//Task4 - print values to the serial port ONLY when state=1;
void Task4(void *argp) {
while (1)
{
if (state == 1) {
Serial.print(state); Serial.print(","); Serial.print(frequency); Serial.print(","); Serial.println(filter_analg_val);
}
else {
Serial.println("state = 0");
}
vTaskDelay(5000); //The system must executes this task every 5 seconds
}
}
void setup() {
Serial.begin(9600);
// Task1 to run forever
xTaskCreatePinnedToCore(
Task1, // Function to be called
"Task1", // Name of task
1024, // Stack size (bytes in ESP32, words in FreeRTOS)
NULL, // Parameter to pass to function
1, // Task priority (0 to configMAX_PRIORITIES - 1)
NULL, // Task handle
app_cpu); // Run on one core
// Task2 to run forever
xTaskCreatePinnedToCore(
Task2, // Function to be called
"Task2", // Name of task
1024, // Stack size (bytes in ESP32, words in FreeRTOS)
NULL, // Parameter to pass to function
1, // Task priority (0 to configMAX_PRIORITIES - 1)
NULL, // Task handle
app_cpu); // Run on one core
// Task3 to run forever
xTaskCreatePinnedToCore(
Task3, // Function to be called
"Task3", // Name of task
1024, // Stack size (bytes in ESP32, words in FreeRTOS)
NULL, // Parameter to pass to function
1, // Task priority (0 to configMAX_PRIORITIES - 1)
NULL, // Task handle
app_cpu); // Run on one core
// Task4 to run forever
xTaskCreatePinnedToCore(
Task4, // Function to be called
"Task4", // Name of task
1024, // Stack size (bytes in ESP32, words in FreeRTOS)
NULL, // Parameter to pass to function
1, // Task priority (0 to configMAX_PRIORITIES - 1)
NULL, // Task handle
app_cpu); // Run on one core
}
void loop() {
}
Even though this is a very simple program, I am using freeRTOS because I need it to be consistent with the main project I am doing. I just created this simple program to help me understand structures.
I am using ESP32. The program above complies and gives me the anticipated outputs. Great. BUT now my question is how can I create a global structure (struct), to store all the data printed to the serial port in task 4?
My progress so far:
I have done research into structs and tried to relate them to my problem. Here are my findings:
From that, the only solution I could think of is to create a new variable of type 'logged_info', every time task4 gets executed i.e. every 5 seconds. And apparently, according to this post c++ - change variable name with a loop - Stack Overflow it is not possible
to create variables dynamically. So what should I do?
Thanks again for your help ![]()
