Arduino函数未执行

时间:2015-09-11 16:50:59

标签: c++ arduino arduino-ide

我在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);
}

1 个答案:

答案 0 :(得分:0)

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++;
  }