Arduino几秒钟后停止工作

时间:2015-02-27 03:48:46

标签: pid

我正在尝试为Segway型车辆制造PID控制器。我运行代码,几秒后它就会停止工作。我正在使用带有Arduino uno的sparkfun 9d0f IMU。我猜测某处有溢出或无限循环。有任何想法吗?它可以来自IMU吗?

 /******************************
 * segBoard Attitude Control 2.0
 * this is the second version of the attitude control code for the 
 * spring 2015 400d segBoard project. this code uses the sparkfun 
 * 9dof to gather data about the attitude of the board and convert 
 * it to PID for controlling the motor.
 * 
 * Written by: Antonio Cole 2/29/15
 * 
 */


#include <SPI.h> // Included for SFE_LSM9DS0 library
#include <Wire.h>
#include <SFE_LSM9DS0.h>

// SDO_XM and SDO_G are both grounded, so our addresses are:
#define LSM9DS0_XM  0x1D // Would be 0x1E if SDO_XM is LOW
#define LSM9DS0_G   0x6B // Would be 0x6A if SDO_G is LOW
// Create an instance of the LSM9DS0 library called `dof` the
// parameters for this constructor are:
// [SPI or I2C Mode declaration],[gyro I2C address],[xm I2C add.]
LSM9DS0 dof(MODE_I2C, LSM9DS0_G, LSM9DS0_XM);

const int leftPin = 9;
const int rightPin  = 10;
const int samples = 60;

int pitchControl = 0;
int index = 0;
int pitch = 0;

float angle = 0;
float accelX[samples];
float accelY[samples];
float accelZ[samples];
float accelXaverage = 0;
float accelYaverage = 0;
float accelZaverage = 0;

void setup()
{
  pinMode(leftPin, OUTPUT);
  pinMode(rightPin, OUTPUT);
  Serial.begin(115200); // Start serial at 115200 bps
  // Use the begin() function to initialize the LSM9DS0 library.
  // You can either call it with no parameters (the easy way):
  uint16_t status = dof.begin();
  /*for(int i = 0; i < 6 i++){
    dof.readAccel();
  }
  delay(1);
  */
  Serial.print("LSM9DS0 WHO_AM_I's returned: 0x");
  Serial.println(status, HEX);
  Serial.println("Should be 0x49D4");
  Serial.println();
}

void loop(){
  dof.readAccel();

  accelX[index] = dof.calcAccel(dof.ax) - 0.01;
  accelY[index] = dof.calcAccel(dof.ay) - 0.01;
  accelZ[index] = dof.calcAccel(dof.az) - 0.02;

  float accelXsum = 0;
  float accelYsum = 0;
  float accelZsum = 0;

  for(int i = 0; i < samples; i++) {
    accelXsum = accelXsum + accelX[i];
    accelYsum = accelYsum + accelY[i];
    accelZsum = accelZsum + accelZ[i];
  }
  accelXaverage = accelXsum / samples;
  accelYaverage = accelYsum / samples;
  accelZaverage = accelZsum / samples;

  angle = atan2(accelXaverage, sqrt(accelYaverage * accelYaverage)
    + (accelZaverage * accelZaverage))*100;
  pitch = constrain(angle, -15, 15);

  if(pitch < 0){
    pitchControl = map(pitch, -15, 0, 255, 0); 
    analogWrite(rightPin, 0);
    analogWrite(leftPin, pitchControl);
  }
  else if(pitch > 0){
    pitchControl = map(pitch, 0, 15, 0, 255); 
    analogWrite(leftPin, 0);
    analogWrite(rightPin, pitchControl);
  }
  else{
    analogWrite(leftPin, 0);
    analogWrite(rightPin, 0);
  }

  Serial.print(index);
  Serial.print(";");
  Serial.println(pitchControl);

  index++;
  if(index == samples){
    index = 0;
  }
}

1 个答案:

答案 0 :(得分:0)

在不了解该系统的情况下,您需要应用标准调试技术来隔离问题。

拉出所有内容,这样你就有了空的setup()和main()函数。也许添加打印语句。看看是否有效。然后继续添加代码,直到它中断。最后添加的是问题。