Arduino 函数不执行
Arduino function not executing
我在 Arduino 草图中实现了以下功能。问题是当在循环函数中调用该函数时,它不执行,程序停止。奇怪的是,如果调用此函数,甚至上一行的函数调用也会停止执行。如果函数调用被注释掉,程序似乎在没有它的情况下执行,只是没有完成所需的任务。对这个特殊问题的任何帮助表示赞赏。
备注:
草图执行正常,直到函数调用。
草图编译无误
movingAverageNode movingAverage::refresh(float r, float p, float h) {
//create pointers and new node
movingAverageNode* newNode = new movingAverageNode;
newNode->roll = r;
newNode->pitch = p;
newNode->heading = h;
movingAverageNode* nodePtr;
//append last node
if(!head){
head = newNode;
numNodes++;
}else{
while(nodePtr->next){
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
numNodes++;
}
//remove first node if nessecary
if (numNodes > NUM_NODES){
nodePtr = head;
head = nodePtr->next;
delete[] nodePtr;
numNodes--;
}
//average all values in list
float avgRoll;
float avgPitch;
float avgHeading;
nodePtr = head;
while (nodePtr != NULL){
avgRoll += nodePtr->roll;
avgPitch += nodePtr->pitch;
avgHeading += nodePtr->heading;
nodePtr = nodePtr->next;
}
avgRoll /= numNodes;
avgPitch /= numNodes;
avgHeading /= numNodes;
//create new node and return it
movingAverageNode result;
result.roll = avgRoll;
result.pitch = avgPitch;
result.heading = avgHeading;
return result;
}
如果有帮助,这里是循环:
void loop(void) {
Serial.print(F("i"));
sensors_vec_t orientation;
movingAverage data;
movingAverageNode averaged;
if (ahrs.getOrientation(&orientation)) {
Serial.print(F("j"));
data.refreshB();
averaged = data.refresh(orientation.roll,orientation.pitch,orientation.heading);
Serial.print(F("k"));
Serial.println(String(averaged.roll) + "\t" + String(averaged.pitch) + "\t" + String(averaged.heading));
}
delay(500);
}
nodePtr
未设置
试试这个:
movingAverageNode movingAverage::refresh(float r, float p, float h) {
//create pointers and new node
movingAverageNode* newNode = new movingAverageNode;
newNode->roll = r;
newNode->pitch = p;
newNode->heading = h;
movingAverageNode* nodePtr;
//append last node
if(!head){
head = newNode;
numNodes++;
}else{
nodePtr = head;
while(nodePtr->next){
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
numNodes++;
}
我在 Arduino 草图中实现了以下功能。问题是当在循环函数中调用该函数时,它不执行,程序停止。奇怪的是,如果调用此函数,甚至上一行的函数调用也会停止执行。如果函数调用被注释掉,程序似乎在没有它的情况下执行,只是没有完成所需的任务。对这个特殊问题的任何帮助表示赞赏。
备注:
草图执行正常,直到函数调用。
草图编译无误
movingAverageNode movingAverage::refresh(float r, float p, float h) {
//create pointers and new node
movingAverageNode* newNode = new movingAverageNode;
newNode->roll = r;
newNode->pitch = p;
newNode->heading = h;
movingAverageNode* nodePtr;
//append last node
if(!head){
head = newNode;
numNodes++;
}else{
while(nodePtr->next){
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
numNodes++;
}
//remove first node if nessecary
if (numNodes > NUM_NODES){
nodePtr = head;
head = nodePtr->next;
delete[] nodePtr;
numNodes--;
}
//average all values in list
float avgRoll;
float avgPitch;
float avgHeading;
nodePtr = head;
while (nodePtr != NULL){
avgRoll += nodePtr->roll;
avgPitch += nodePtr->pitch;
avgHeading += nodePtr->heading;
nodePtr = nodePtr->next;
}
avgRoll /= numNodes;
avgPitch /= numNodes;
avgHeading /= numNodes;
//create new node and return it
movingAverageNode result;
result.roll = avgRoll;
result.pitch = avgPitch;
result.heading = avgHeading;
return result;
}
如果有帮助,这里是循环:
void loop(void) {
Serial.print(F("i"));
sensors_vec_t orientation;
movingAverage data;
movingAverageNode averaged;
if (ahrs.getOrientation(&orientation)) {
Serial.print(F("j"));
data.refreshB();
averaged = data.refresh(orientation.roll,orientation.pitch,orientation.heading);
Serial.print(F("k"));
Serial.println(String(averaged.roll) + "\t" + String(averaged.pitch) + "\t" + String(averaged.heading));
}
delay(500);
}
nodePtr
未设置
试试这个:
movingAverageNode movingAverage::refresh(float r, float p, float h) {
//create pointers and new node
movingAverageNode* newNode = new movingAverageNode;
newNode->roll = r;
newNode->pitch = p;
newNode->heading = h;
movingAverageNode* nodePtr;
//append last node
if(!head){
head = newNode;
numNodes++;
}else{
nodePtr = head;
while(nodePtr->next){
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
numNodes++;
}