英文:
Array in Tasks make a Guru error with ESP32
问题
Here's the translation of the code and the error message:
所以,我为这个ESP32编写了一个程序。它有一些任务,其中一个让我遇到了一些麻烦。它给我发送了一个Guru错误。以下是代码:
main.cpp:
#include <Arduino.h>
#include <Document.h>
void updateDocument(void * par){ //用于更新文档或创建文档的任务(如果不存在)
for(;;){
Documentgetting();
Documentupdate();
}
}
void getLuxTempHum(void * par){ //用于获取湿度、温度和光照的任务
for(;;){
gHumidity();
gLux();
gTemperatur();
}
}
void getgps(void * par){ // 用于获取GPS的任务
for(;;){
gGPS();
}
}
void setaktor(void * par){ // 用于获取动力装置的任务
for(;;){
gmotor();
/*LEDsss(dark);
shakeit(shake);*/
}
}
void getWindRain(void* arg){//用于风的任务
for(;;){
gWind();
}
}
void getMittelwert(void* par){//从传感器获取平均值的任务
for(;;){
gMittelwerte();
}
}
void setup()
{
//****************************** TMP117 MS8607 TSL2591*********************************
if(!tmp117.begin()){}
configureSensor();
if(!tsl.begin()){}
if(!ms8607.begin()){}
ms8607.setHumidityResolution(MS8607_HUMIDITY_RESOLUTION_OSR_8b);
xTaskCreate(
getLuxTempHum,
"获取光照温湿度",
10000, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
NULL,
1,
NULL
);
//***********************************************************************
//***************************** 风/雨 ************************************
setupwr();
xTaskCreate(
getWindRain,
"获取风雨",
4500, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
NULL,
tskIDLE_PRIORITY,
NULL
);
//*************************************************************************/
//***************************** GPS ************************************
setupGPS();
xTaskCreate(
getgps,
"获取GPS",
7000, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
NULL,
1,
NULL
);
//*************************************************************************/
//****************************** 动力装置 ************************************
setupledandmotor();
xTaskCreate(
setaktor,
"设置动力装置",
4500, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
NULL,
1,
NULL
);
//*************************************************************************
//**************************** 平均值 *********************************
mittelsetup();
xTaskCreate(
getMittelwert,
"获取平均值",
10000, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
NULL,
1,
NULL
);
//*************************************************************************/
//**************************** Firebase *******************************
setupFire();
xTaskCreate(
updateDocument,
"更新/创建文档",
20000, //如果任务复杂,请增加此值;对于大多数任务,4500通常足够
NULL,
1,
NULL
);
//*********************************************************************
}
void loop()
{
if(gotwindgeschwingkeit >= 11){
Sperre = true;
U = true;
O = false;
} else {
Sperre = false;
}
delay(10);
}
Mittelwert任务给我带来了问题:
Mittelwert.h:
#include <Arduino.h>
//******************************光照、温度、湿度*************************
#include <Lux.h>
#include <Temp.h>
#include <Humidity.h>
//********************************风/雨***********************************
#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);
halfhouri++;
threemini++;
for(int i = sizeof(sensors) - 1; i >= 0; i--){
sensorshh[i] += sensors[i];
sensorstm[i] += sensors[i];
}
if(gethalfhour){
for(int i = sizeof(sensors) - 1; i >= 0; i--){
sensorshh[i] = sensorshh[i] / halfhouri;
}
halfhouri = 0;
vTaskDelay(500/portTICK_PERIOD_MS);
for(int i = sizeof(sensorshh) - 1; i >= 0; i--){
sensorshh[i] = 0;
}
}
if(getthreeminutes){
for(int i = sizeof(sensors) - 1; i >= 0; i--){
sensorstm[i] = sensorstm[i] / threemini;
}
threemini = 0;
vTaskDelay(500/portTICK_PERIOD_MS);
for(int i = sizeof(sensorstm) - 1; i >= 0; i--){
sensorstm[i] = 0;
}
}
}
如果我不启动Mittelwert任务,一切都正常运行。所以我不知道发生了什么,我尝试找出错误,但是正如你所看到的,我是一个初学者。
这是错误代码:
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x4016407f PS
<details>
<summary>英文:</summary>
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:
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*********************************
if(!tmp117.begin()){}
configureSensor();
if(!tsl.begin()){}
if(!ms8607.begin()){}
ms8607.setHumidityResolution(MS8607_HUMIDITY_RESOLUTION_OSR_8b);
xTaskCreate(
getLuxTempHum,
"get LuxTempHum",
10000, //make higher if complex task, 4500 mostlikely is enough for most jobs
NULL,
1,
NULL
);
//***********************************************************************
//***************************** Wind/Regen ************************************
setupwr();
xTaskCreate(
getWindRain,
"getWindRain",
4500, //make higher if complex task, 4500 mostlikely is enough for most jobs
NULL,
tskIDLE_PRIORITY,
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();
xTaskCreate(
updateDocument,
"update/create Document",
20000, //make higher if complex task, 4500 mostlikely is enough for most jobs
NULL,
1,
NULL
);
//*********************************************************************
}
void loop()
{
if(gotwindgeschwingkeit>=11){
Sperre=true;
U=true;
O=false;
}else{
Sperre=false;
}
delay(10);
}
the Task Mittelwert is giving me the problems:
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);
halfhouri++;
threemini++;
for(int i = sizeof(sensors)-1; i>=0; i--){
sensorshh[i]+=sensors[i];
sensorstm[i]+=sensors[i];
}
if(gethalfhour){
for(int i = sizeof(sensors)-1; i>=0; i--){
sensorshh[i]=sensorshh[i]/halfhouri;
}
halfhouri=0;
vTaskDelay(500/portTICK_PERIOD_MS);
for(int i = sizeof(sensorshh)-1; i>=0; i--){
sensorshh[i]=0;
}
}
if(getthreeminutes){
for(int i = sizeof(sensors)-1; i>=0; i--){
sensorstm[i]=sensorstm[i]/threemini;
}
threemini=0;
vTaskDelay(500/portTICK_PERIOD_MS);
for(int i = sizeof(sensorstm)-1; i>=0; i--){
sensorstm[i]=0;
}
}
}
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.
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
Thank you for your Help!!
And also, i am Austrian, so my English is not the best.
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.
</details>
# 答案1
**得分**: 0
问题出在你使用`sizeof`运算符与`sensors`数组的方式上。
`sensors`是一个包含4个double值的数组,假设每个double需要8字节的内存,那么`sizeof(sensors)`将返回8x4=32,而不是你期望的4。因此,在你的for循环中,你尝试访问数组中超出你定义的4个元素限制的位置,导致你得到的`LoadProhibited`错误。
要获取数组的实际长度,你应该使用`sizeof(sensors) / sizeof(sensors[0])`。
```c
double sensors[] = {1, 2, 3, 4};
printf("sizeof(sensors) = %ld\n", sizeof(sensors)); // 输出:sizeof(sensors) = 32
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])
.
double sensors[] = {1, 2, 3, 4};
printf("sizeof(sensors) = %ld\n", sizeof(sensors)); // Output: sizeof(sensors) = 32
printf("Array length = %ld\n", sizeof(sensors) / sizeof(sensors[0])); // Output: Array length = 4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论