在任务中使用的数组导致ESP32出现Guru错误。

huangapple go评论135阅读模式
英文:

Array in Tasks make a Guru error with ESP32

问题

Here's the translation of the code and the error message:

  1. 所以,我为这个ESP32编写了一个程序。它有一些任务,其中一个让我遇到了一些麻烦。它给我发送了一个Guru错误。以下是代码:
  2. main.cpp
  3. #include <Arduino.h>
  4. #include <Document.h>
  5. void updateDocument(void * par){ //用于更新文档或创建文档的任务(如果不存在)
  6. for(;;){
  7. Documentgetting();
  8. Documentupdate();
  9. }
  10. }
  11. void getLuxTempHum(void * par){ //用于获取湿度、温度和光照的任务
  12. for(;;){
  13. gHumidity();
  14. gLux();
  15. gTemperatur();
  16. }
  17. }
  18. void getgps(void * par){ // 用于获取GPS的任务
  19. for(;;){
  20. gGPS();
  21. }
  22. }
  23. void setaktor(void * par){ // 用于获取动力装置的任务
  24. for(;;){
  25. gmotor();
  26. /*LEDsss(dark);
  27. shakeit(shake);*/
  28. }
  29. }
  30. void getWindRain(void* arg){//用于风的任务
  31. for(;;){
  32. gWind();
  33. }
  34. }
  35. void getMittelwert(void* par){//从传感器获取平均值的任务
  36. for(;;){
  37. gMittelwerte();
  38. }
  39. }
  40. void setup()
  41. {
  42. //****************************** TMP117 MS8607 TSL2591*********************************
  43. if(!tmp117.begin()){}
  44. configureSensor();
  45. if(!tsl.begin()){}
  46. if(!ms8607.begin()){}
  47. ms8607.setHumidityResolution(MS8607_HUMIDITY_RESOLUTION_OSR_8b);
  48. xTaskCreate(
  49. getLuxTempHum,
  50. "获取光照温湿度",
  51. 10000, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
  52. NULL,
  53. 1,
  54. NULL
  55. );
  56. //***********************************************************************
  57. //***************************** 风/雨 ************************************
  58. setupwr();
  59. xTaskCreate(
  60. getWindRain,
  61. "获取风雨",
  62. 4500, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
  63. NULL,
  64. tskIDLE_PRIORITY,
  65. NULL
  66. );
  67. //*************************************************************************/
  68. //***************************** GPS ************************************
  69. setupGPS();
  70. xTaskCreate(
  71. getgps,
  72. "获取GPS",
  73. 7000, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
  74. NULL,
  75. 1,
  76. NULL
  77. );
  78. //*************************************************************************/
  79. //****************************** 动力装置 ************************************
  80. setupledandmotor();
  81. xTaskCreate(
  82. setaktor,
  83. "设置动力装置",
  84. 4500, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
  85. NULL,
  86. 1,
  87. NULL
  88. );
  89. //*************************************************************************
  90. //**************************** 平均值 *********************************
  91. mittelsetup();
  92. xTaskCreate(
  93. getMittelwert,
  94. "获取平均值",
  95. 10000, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
  96. NULL,
  97. 1,
  98. NULL
  99. );
  100. //*************************************************************************/
  101. //**************************** Firebase *******************************
  102. setupFire();
  103. xTaskCreate(
  104. updateDocument,
  105. "更新/创建文档",
  106. 20000, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
  107. NULL,
  108. 1,
  109. NULL
  110. );
  111. //*********************************************************************
  112. }
  113. void loop()
  114. {
  115. if(gotwindgeschwingkeit >= 11){
  116. Sperre = true;
  117. U = true;
  118. O = false;
  119. } else {
  120. Sperre = false;
  121. }
  122. delay(10);
  123. }

Mittelwert任务给我带来了问题:
Mittelwert.h:

  1. #include <Arduino.h>
  2. //******************************光照、温度、湿度*************************
  3. #include <Lux.h>
  4. #include <Temp.h>
  5. #include <Humidity.h>
  6. //********************************风/雨***********************************
  7. #include <WindRegen.h>
  8. bool gethalfhour = false;
  9. bool getthreeminutes = false;
  10. int halfhouri = 0;
  11. int threemini = 0;
  12. double sensors[] = {gotHum, gotLux, gotTemp, gotwindgeschwingkeit};
  13. double sensorshh[sizeof(sensors)];
  14. double sensorstm[sizeof(sensors)];
  15. void mittelsetup(){
  16. for(int i = sizeof(sensors) - 1; i >= 0; i--){
  17. sensorshh[i] = sensors[i];
  18. sensorstm[i] = sensors[i];
  19. }
  20. }
  21. void updatesenorssss(){
  22. sensors[0] = gotHum;
  23. sensors[1] = gotLux;
  24. sensors[2] = gotTemp;
  25. sensors[3] = gotwindgeschwingkeit;
  26. }
  27. void gMittelwerte(){
  28. updatesenorssss();
  29. vTaskDelay(1000/portTICK_PERIOD_MS);
  30. halfhouri++;
  31. threemini++;
  32. for(int i = sizeof(sensors) - 1; i >= 0; i--){
  33. sensorshh[i] += sensors[i];
  34. sensorstm[i] += sensors[i];
  35. }
  36. if(gethalfhour){
  37. for(int i = sizeof(sensors) - 1; i >= 0; i--){
  38. sensorshh[i] = sensorshh[i] / halfhouri;
  39. }
  40. halfhouri = 0;
  41. vTaskDelay(500/portTICK_PERIOD_MS);
  42. for(int i = sizeof(sensorshh) - 1; i >= 0; i--){
  43. sensorshh[i] = 0;
  44. }
  45. }
  46. if(getthreeminutes){
  47. for(int i = sizeof(sensors) - 1; i >= 0; i--){
  48. sensorstm[i] = sensorstm[i] / threemini;
  49. }
  50. threemini = 0;
  51. vTaskDelay(500/portTICK_PERIOD_MS);
  52. for(int i = sizeof(sensorstm) - 1; i >= 0; i--){
  53. sensorstm[i] = 0;
  54. }
  55. }
  56. }

如果我不启动Mittelwert任务,一切都正常运行。所以我不知道发生了什么,我尝试找出错误,但是正如你所看到的,我是一个初学者。

这是错误代码:

  1. Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
  2. Core 1 register dump:
  3. PC : 0x4016407f PS
  4. <details>
  5. <summary>英文:</summary>
  6. So, i wrote a Programm for this esp32. It has some Tasks and one get me in some trouble. It sends me a Guru error. Here the code:
  7. main.cpp:

#include <Arduino.h>
#include <Document.h>

void updateDocument(void * par){ //Task to Update a document or create if not existing
for(;;){
Documentgetting();
Documentupdate();
}
}

void getLuxTempHum(void * par){ //Task to get Humidity Temperatur and Lux
for(;;){
gHumidity();
gLux();
gTemperatur();
}
}

void getgps(void * par){ // Task to get GPS
for(;;){
gGPS();
}
}

void setaktor(void * par){ // Task to get Aktoren
for(;;){
gmotor();
/LEDsss(dark);
shakeit(shake);
/
}
}

void getWindRain(void* arg){//Task for wind
for(;;){
gWind();
}
}

void getMittelwert(void* par){//get Mittelwert from sensors
for(;;){
gMittelwerte();
}

}

void setup()
{
//****************************** TMP117 MS8607 TSL2591*********************************

  1. if(!tmp117.begin()){}
  2. configureSensor();
  3. if(!tsl.begin()){}
  4. if(!ms8607.begin()){}
  5. ms8607.setHumidityResolution(MS8607_HUMIDITY_RESOLUTION_OSR_8b);
  6. xTaskCreate(
  7. getLuxTempHum,
  8. &quot;get LuxTempHum&quot;,
  9. 10000, //make higher if complex task, 4500 mostlikely is enough for most jobs
  10. NULL,
  11. 1,
  12. NULL

);
//***********************************************************************

  1. //***************************** Wind/Regen ************************************
  2. setupwr();
  3. xTaskCreate(
  4. getWindRain,
  5. &quot;getWindRain&quot;,
  6. 4500, //make higher if complex task, 4500 mostlikely is enough for most jobs
  7. NULL,
  8. tskIDLE_PRIORITY,
  9. NULL

);
//*************************************************************************/

//***************************** GPS****************************************
setupGPS();
xTaskCreate(
getgps,
"get GPS",
7000, //make higher if complex task, 4500 mostlikely is enough for most jobs
NULL,
1,
NULL
);
//*************************************************************************/

//Aktoren******
setupledandmotor();
xTaskCreate(
setaktor,
"set Aktor",
4500, //make higher if complex task, 4500 mostlikely is enough for most jobs
NULL,
1,
NULL
);
//*************************************************************************
//Mittelwert*****
mittelsetup();
xTaskCreate(
getMittelwert,
"get Mittelwert",
10000, //make higher if complex task, 4500 mostlikely is enough for most jobs
NULL,
1,
NULL
);
//*********************************************/
//
Firebase *******************************
setupFire();

  1. xTaskCreate(
  2. updateDocument,
  3. &quot;update/create Document&quot;,
  4. 20000, //make higher if complex task, 4500 mostlikely is enough for most jobs
  5. NULL,
  6. 1,
  7. NULL

);

  1. //*********************************************************************

}

void loop()
{
if(gotwindgeschwingkeit>=11){
Sperre=true;
U=true;
O=false;

}else{
Sperre=false;
}
delay(10);

}

  1. the Task Mittelwert is giving me the problems:
  2. Mittelwert.h:

#include <Arduino.h>
//**Lux, Temp, Hum
#include <Lux.h>
#include <Temp.h>
#include <Humidity.h>
//Wind/Regen

#include <WindRegen.h>

bool gethalfhour = false;
bool getthreeminutes = false;

int halfhouri = 0;
int threemini = 0;

double sensors[]={gotHum, gotLux, gotTemp, gotwindgeschwingkeit};
double sensorshh[sizeof(sensors)];
double sensorstm[sizeof(sensors)];

void mittelsetup(){
for(int i = sizeof(sensors)-1; i>=0; i--){
sensorshh[i]=sensors[i];
sensorstm[i]=sensors[i];
}
}

void updatesenorssss(){
sensors[0]=gotHum;
sensors[1]=gotLux;
sensors[2]=gotTemp;
sensors[3]=gotwindgeschwingkeit;

}

void gMittelwerte(){
updatesenorssss();
vTaskDelay(1000/portTICK_PERIOD_MS);

  1. halfhouri++;
  2. threemini++;
  3. for(int i = sizeof(sensors)-1; i&gt;=0; i--){
  4. sensorshh[i]+=sensors[i];
  5. sensorstm[i]+=sensors[i];
  6. }
  7. if(gethalfhour){
  8. for(int i = sizeof(sensors)-1; i&gt;=0; i--){
  9. sensorshh[i]=sensorshh[i]/halfhouri;
  10. }
  11. halfhouri=0;
  12. vTaskDelay(500/portTICK_PERIOD_MS);
  13. for(int i = sizeof(sensorshh)-1; i&gt;=0; i--){
  14. sensorshh[i]=0;
  15. }
  16. }
  17. if(getthreeminutes){
  18. for(int i = sizeof(sensors)-1; i&gt;=0; i--){
  19. sensorstm[i]=sensorstm[i]/threemini;
  20. }
  21. threemini=0;
  22. vTaskDelay(500/portTICK_PERIOD_MS);
  23. for(int i = sizeof(sensorstm)-1; i&gt;=0; i--){
  24. sensorstm[i]=0;
  25. }
  26. }

}

  1. If I dont start the Task it funktions perfektly fine. So i dont know what is happening, i tried to find the error but, as you can see, I am an beginner.
  2. Here the errorcode:

Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.

Core 1 register dump:
PC : 0x4016407f PS : 0x00060030 A0 : 0x80164165 A1 : 0x3ffb4e70
A2 : 0x00000000 A3 : 0x3ffb4ebe A4 : 0x00000001 A5 : 0x00000000
A6 : 0x00000000 A7 : 0x00000000 A8 : 0x80083c3c A9 : 0x3ffb4e60
A10 : 0x3ff000e0 A11 : 0x00000001 A12 : 0x3ffbddc8 A13 : 0x00000000
A14 : 0x007bf1f8 A15 : 0x003fffff SAR : 0x0000001d EXCCAUSE: 0x0000001c
EXCVADDR: 0x0000000c LBEG : 0x40089dcc LEND : 0x40089de2 LCOUNT : 0xffffffff

Backtrace:0x4016407c:0x3ffb4e700x40164162:0x3ffb4e90 0x400e3695:0x3ffb4eb0 0x400e385f:0x3ffb4ee0 0x400d35d5:0x3ffb4f00 0x400d35f7:0x3ffb4f20 0x400d365a:0x3ffb4f40

  1. Thank you for your Help!!
  2. And also, i am Austrian, so my English is not the best.
  3. I tried to commend some parts out too find the problem and was left wiht the Mittelwert.h. I couldn find out whats specificly wrong with it. SO if someone finds the problem pleas tell me. The first error was a WatchDog error, witch i safed with the Idle_Priority, but then ther was the Guru error.
  4. </details>
  5. # 答案1
  6. **得分**: 0
  7. 问题出在你使用`sizeof`运算符与`sensors`数组的方式上。
  8. `sensors`是一个包含4double值的数组,假设每个double需要8字节的内存,那么`sizeof(sensors)`将返回8x4=32,而不是你期望的4。因此,在你的for循环中,你尝试访问数组中超出你定义的4个元素限制的位置,导致你得到的`LoadProhibited`错误。
  9. 要获取数组的实际长度,你应该使用`sizeof(sensors) / sizeof(sensors[0])`
  10. ```c
  11. double sensors[] = {1, 2, 3, 4};
  12. printf("sizeof(sensors) = %ld\n", sizeof(sensors)); // 输出:sizeof(sensors) = 32
  13. printf("数组长度 = %ld\n", sizeof(sensors) / sizeof(sensors[0])); // 输出:数组长度 = 4
英文:

The issue is in the way you are using the sizeof operator with the sensors array.

sensors is an array of 4 double values, assuming each double requires 8 bytes of memory, then sizeof(sensors) will return 8x4=32, not the 4 you are expecting. As a result, in your for loops, you try to access a position in the array that is beyond the limits of the 4 elements you defined, resulting in the LoadProhibited error you get.

To get the actual length of the array, you should use sizeof(sensors) / sizeof(sensors[0]).

  1. double sensors[] = {1, 2, 3, 4};
  2. printf(&quot;sizeof(sensors) = %ld\n&quot;, sizeof(sensors)); // Output: sizeof(sensors) = 32
  3. printf(&quot;Array length = %ld\n&quot;, sizeof(sensors) / sizeof(sensors[0])); // Output: Array length = 4

huangapple
  • 本文由 发表于 2023年7月27日 18:48:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定