一段时间后Arduino void循环停止

时间:2019-12-19 07:39:46

标签: loops arduino

/ ************************************************** ****************************************************** *******

我目前正在制造自动驾驶汽车。

它的工作原理如下:绘制GPS信息和磁力计传感器数据。

然后,在收集所有传感器数据后,Arduino决定转向哪种方式。

这是通过计算航向误差来完成的。

*************************************************** ****************************************************** ****** /

/ ************************************************** ****************************************************** *******

我的期望:

运行时,Arduino收集传感器数据。

arduino根据传感器数据确定转弯方向。

当信号发送到电机驱动器时,arduino循环以收集传感器数据。

这必须相对快速地完成,因为只有一个车轮转向轮才能使车辆完全旋转。

*************************************************** ****************************************************** ****** /

单独运行时,Arduino无需停止即可获取磁力计传感器和GPS信息。

但是,当组合代码运行时,Arduino会在一段时间后停止。

此外,在运行电动机时,磁力计的响应似乎也变慢了。 / ***************************************************** ****************************************************** ****

要解决此问题,我在if语句和“ SmallLeftTurn”函数中均具有return函数,以尝试打破似乎卡住的内容。

下面是困扰Arduino的故障代码。

void calcDesiredTurn()
{
  //Calculate Where We Need To Turn to Head To Destination.
  headingError = targetHeading - currentHeading;

  //adjust for compass wrap
  if (headingError < -180)
    headingError += 360;
  if (headingError > 180)
    headingError -= 180;

  //Calculate Which Way To Turn To Intercept the target Heading.
  if (abs(headingError) <= HEADING_TOLERANCE)
  {
    Straight();
    return;
  }
  else if (headingError < 0)
  {
    SmallLeftTurn();
  }
  else if (headingError > 0)
  {
    SmallRightTurn();

  }
  else
  {
    Straight();
  }
}

void Straight()
{
  RightMotor.setSpeed(50);
  LeftMotor.setSpeed(-50);
}
void SmallLeftTurn()
{
  LeftMotor.setSpeed(0);
  RightMotor.setSpeed(50);
}

void SmallRightTurn()
{
  RightMotor.setSpeed(0);
  LeftMotor.setSpeed(-50);
}
void Stop()
{
  LeftMotor.setSpeed(0);
  RightMotor.setSpeed(0);
}

我的空循环是用这种方式构造的。

void loop() {
  // put your main code here, to run repeatedly:
  if (GPS.newNMEAreceived())
  {
    Serial.println("GPS NMEA Received");
    Serial.println(GPS.lastNMEA());
    if (GPS.parse(GPS.lastNMEA()))
      GPSDataProcess();
  }
  //navigate
  currentHeading = readCompass();
  Serial.println(currentHeading);
  calcDesiredTurn();
  delay(200);
}

0 个答案:

没有答案
相关问题