Skip to content

Commit f44578f

Browse files
author
Geoffroy Arenou
committed
Re write tasks
1 parent 878c91f commit f44578f

2 files changed

Lines changed: 82 additions & 43 deletions

File tree

include/main.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,12 @@ using namespace Printer;
1616

1717
int numPami = -1;
1818

19-
TaskHandle_t Task1;
20-
TaskHandle_t Task2;
21-
2219
extern int numPami;
2320

24-
/**
25-
* Get data from serial
26-
* Send data in a queue for the other thread to compute
27-
*/
28-
void TaskMatch(void *pvParameters);
29-
30-
/**
31-
* Get data from queue and compute them
32-
*/
33-
void TaskSerial(void *pvParameters);
21+
void TaskLoop(void *pvParameters);
3422

35-
void Blink();
23+
void TaskMatch(void *pvParameters);
24+
void TaskTeleplot(void *pvParameters);
25+
void TaskHandleCommand(void *pvParameters);
3626

3727
#endif

src/main.cpp

Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,29 @@ void setup()
5454
// println("Accel : %i", accelPref);
5555
// }
5656

57-
/* Task function. */
58-
/* name of task. */
59-
/* Stack size of task */
60-
/* parameter of the task */
61-
/* priority of the task */
62-
/* Task handle to keep track of created task */
63-
/* pin task to core 0 */
64-
xTaskCreatePinnedToCore(TaskMatch, "TaskMatch", 20000, NULL, 10, &Task1, 0);
65-
xTaskCreatePinnedToCore(TaskSerial, "TaskSerial", 20000, NULL, 5, &Task2, 0);
57+
TaskThread(TaskMatch, "TaskMatch", 20000, 15, 0);
58+
TaskThread(TaskTeleplot, "TaskTeleplot", 5000, 1, 0);
59+
TaskThread(TaskHandleCommand, "TaskHandleCommand", 10000, 15, 0);
60+
61+
TaskThread(TaskLoop, "TaskLoop", 20000, 20, 1);
6662
}
6763

6864
void loop()
65+
{
66+
// HACK Vérifier qu'on n'utilise pas les serialEvent !!!
67+
// C:\Users\xxx\.platformio\packages\framework-arduinoespressif32\cores\esp32\main.cpp
68+
// https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/main.cpp
69+
vTaskDelete(NULL); // Supprime immédiatement le task Arduino "loop"
70+
}
71+
72+
void TaskLoop(void *pvParameters)
73+
{
74+
Chrono chrono("Loop", 10000);
75+
76+
while (true)
77+
{
78+
chrono.Start();
79+
try
6980
{
7081
if (Match::matchState != Match::State::MATCH_STOP && Match::matchState != Match::State::MATCH_END && (motor_D.isRunning() || motor_G.isRunning()))
7182
{
@@ -77,14 +88,26 @@ void loop()
7788
disableMotors();
7889
}
7990
}
91+
catch (const std::exception &e)
92+
{
93+
printError(e.what());
94+
}
95+
if (chrono.Check())
96+
{
97+
printChrono(chrono);
98+
}
99+
vTaskDelay(1);
100+
}
101+
// led_strip.update();
102+
}
80103

81-
// Note the 1 Tick delay, this is need so the watchdog doesn't get confused
82104
void TaskMatch(void *pvParameters)
83105
{
84106
println("Start TaskMatch");
85-
107+
Chrono chrono("MainMatch", 1000);
86108
while (1)
87109
{
110+
chrono.Start();
88111
try
89112
{
90113
// En attente de retrait de la tirette pour démarrer le match
@@ -306,30 +329,35 @@ void TaskMatch(void *pvParameters)
306329
if (Match::matchState == Match::State::MATCH_END)
307330
{
308331
// Wait for reset
309-
if(IHM::switchMode == 0 && IHM::tirettePresent == 0)
332+
if (IHM::switchMode == 0 && IHM::tirettePresent == 0)
310333
Match::matchState = Match::State::MATCH_BOOT;
311-
312-
}
334+
}
313335
}
314336
catch (std::exception const &e)
315337
{
316338
printError(e.what());
317339
}
318-
vTaskDelay(1);
340+
if (chrono.Check())
341+
{
342+
printChrono(chrono);
343+
}
344+
vTaskDelay(10);
319345
}
320346
}
321347

322348
Pose MapBoundaries[] = {{0, 0, 0}, {0, 2000, 0}, {3000, 2000, 0}, {3000, 0, 0}};
323-
Timeout teleplotTO;
324-
// Note the 1 Tick delay, this is need so the watchdog doesn't get confused
325-
void TaskSerial(void *pvParameters)
349+
350+
void TaskTeleplot(void *pvParameters)
326351
{
327-
println("Start TaskSerial");
352+
int lastMatchTime = 0;
353+
println("Start TaskTeleplot");
354+
Timeout teleplotTO;
328355
teleplotTO.Start(500);
329-
int lastMatchTime = 0;
356+
Chrono chrono("Teleplot", 1000);
330357

331-
while (1)
358+
while (true)
332359
{
360+
chrono.Start();
333361
try
334362
{
335363
if (teleplotTO.IsTimeOut() && Match::matchState != Match::State::MATCH_RUN)
@@ -340,15 +368,33 @@ void TaskSerial(void *pvParameters)
340368
// Countdown
341369
if (lastMatchTime != (int)(Match::getMatchTimeSec()))
342370
{
343-
println("Match Time : %i", (int)(Match::getMatchTimeSec()));
371+
// println("Match Time : %i", (int)(Match::getMatchTimeSec()));
344372
lastMatchTime = (int)(Match::getMatchTimeSec());
345373
}
346-
//Printer::teleplot("mapBoundaries", MapBoundaries[0]);
347-
//Printer::teleplot("mapBoundaries", MapBoundaries[1]);
348-
//Printer::teleplot("mapBoundaries", MapBoundaries[2]);
349-
//Printer::teleplot("mapBoundaries", MapBoundaries[3]);
350-
}
374+
}
375+
}
376+
catch (const std::exception &e)
377+
{
378+
printError(e.what());
379+
}
380+
if (chrono.Check())
381+
{
382+
printChrono(chrono);
383+
}
384+
vTaskDelay(10);
385+
}
386+
}
351387

388+
void TaskHandleCommand(void *pvParameters)
389+
{
390+
println("Start TaskHandleCommand");
391+
Chrono chrono("HandleCommand", 1000);
392+
393+
while (true)
394+
{
395+
chrono.Start();
396+
try
397+
{
352398
// Check if we get commands from operator via debug serial
353399
if (ESP32_Helper::HasWaitingCommand())
354400
{
@@ -475,7 +521,10 @@ void TaskSerial(void *pvParameters)
475521
print("error : ");
476522
println(e.what());
477523
}
478-
vTaskDelay(1);
479-
// vTaskDelay(10 / portTICK_PERIOD_MS);
524+
if (chrono.Check())
525+
{
526+
printChrono(chrono);
527+
}
528+
vTaskDelay(10);
480529
}
481530
}

0 commit comments

Comments
 (0)