【问题标题】:Magnetometer code: C++ vs C - iosstream, vectors - error iostream: No such file or directory磁力计代码:C++ 与 C - iosstream,向量 - 错误 iostream:没有这样的文件或目录
【发布时间】:2018-09-22 11:55:54
【问题描述】:

您好,我不确定为什么没有这样的文件或目录不断出现。目前我正在尝试使用 cout,但它不会让我这样做,但在我尝试使用向量之前发生过这种情况。

我的整个代码在下面,我确实从 github 上得到了它:

#include <Wire.h>
#include <SPI.h>
#include <SparkFunLSM9DS1.h>

LSM9DS1 imu;

// SDO_XM and SDO_G are both pulled high, so our addresses are:
#define LSM9DS1_M   0x1E // Would be 0x1C if SDO_M is LOW
#define LSM9DS1_AG  0x6B // Would be 0x6A if SDO_AG is LOW

//output
#define PRINT_CALCULATED
//#define PRINT_RAW
#define PRINT_SPEED 2500 // 250 ms between prints
static unsigned long lastPrint = 0; // Keep track of print time

#define DECLINATION -0.7 // Declination (degrees) in Cov, UK.

void setup() 
{

  Serial.begin(115200);

  imu.settings.device.commInterface = IMU_MODE_I2C;
  imu.settings.device.mAddress = LSM9DS1_M;
  imu.settings.device.agAddress = LSM9DS1_AG;
  if (!imu.begin())
  {
    Serial.println("Failed to communicate with LSM9DS1.");
    Serial.println("Double-check wiring.");
    Serial.println("Default settings in this sketch will " \
                  "work for an out of the box LSM9DS1 " \
                  "Breakout, but may need to be modified " \
                  "if the board jumpers are.");
    while (1)
      ;
  }
}

void loop()
{
  if ( imu.gyroAvailable() )
  {
    imu.readGyro();
  }
  if ( imu.accelAvailable() )
  {
    imu.readAccel();
  }
  if ( imu.magAvailable() )
  {
    imu.readMag();
  }

  if ((lastPrint + PRINT_SPEED) < millis())
  {
    printGyro();  // Print "G: gx, gy, gz"
    printAccel(); // Print "A: ax, ay, az"
    printMag();   // Print "M: mx, my, mz"
    printAttitude(imu.ax, imu.ay, imu.az, 
                 -imu.my, -imu.mx, imu.mz);
    Serial.println();

    lastPrint = millis(); // Update lastPrint time
  }
}

void printGyro()
{
  Serial.print("G: ");
#ifdef PRINT_CALCULATED
  Serial.print(imu.calcGyro(imu.gx), 2);
  Serial.print(", ");
  Serial.print(imu.calcGyro(imu.gy), 2);
  Serial.print(", ");
  Serial.print(imu.calcGyro(imu.gz), 2);
  Serial.println(" deg/s");
#elif defined PRINT_RAW
  Serial.print(imu.gx);
  Serial.print(", ");
  Serial.print(imu.gy);
  Serial.print(", ");
  Serial.println(imu.gz);
#endif
}

void printAccel()
{  
  Serial.print("A: ");
#ifdef PRINT_CALCULATED
  Serial.print(imu.calcAccel(imu.ax), 2);
  Serial.print(", ");
  Serial.print(imu.calcAccel(imu.ay), 2);
  Serial.print(", ");
  Serial.print(imu.calcAccel(imu.az), 2);
  Serial.println(" g");
#elif defined PRINT_RAW 
  Serial.print(imu.ax);
  Serial.print(", ");
  Serial.print(imu.ay);
  Serial.print(", ");
  Serial.println(imu.az);
#endif

}

void printMag()
{  
  #include <iostream>
  using namespace std;
  int initialxmag;
  cout << "Please enter the initial/cu x value of the magnetometer in G:";
  cin >> initialxmag;

  Serial.print("M: ");
#ifdef PRINT_CALCULATED
  Serial.print(imu.calcMag(imu.mx), 2);
  Serial.print(", ");
  Serial.print(imu.calcMag(imu.my), 2);
  Serial.print(", ");
  Serial.print(imu.calcMag(imu.mz), 2);
  Serial.println(" gauss");
  Serial.print("X magnetometer difference is:");
  int diff;
  diff=initialxmag-imu.calcMag(imu.mx);
  Serial.print(diff);
  if diff>-2;
  Serial.print("The magnet has moved away, between 2cm-4cm away from the magnetometer");
  if diff>2;
  Serial.print("The magnet has moved closer, to between 2cm-4cm away from the magnetometer");

#elif defined PRINT_RAW
  Serial.print(imu.mx);
  Serial.print(", ");
  Serial.print(imu.my);
  Serial.print(", ");
  Serial.println(imu.mz);
#endif
}
void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
{
  float roll = atan2(ay, az);
  float pitch = atan2(-ax, sqrt(ay * ay + az * az));

  float heading;
  if (my == 0)
    heading = (mx < 0) ? PI : 0;
  else
    heading = atan2(mx, my);

  heading -= DECLINATION * PI / 180;

  if (heading > PI) heading -= (2 * PI);
  else if (heading < -PI) heading += (2 * PI);

  // Convert everything from radians to degrees:
  heading *= 180.0 / PI;
  pitch *= 180.0 / PI;
  roll  *= 180.0 / PI;

  Serial.print("Pitch, Roll: ");
  Serial.print(pitch, 2);
  Serial.print(", ");
  Serial.println(roll, 2);
  Serial.print("Heading: "); Serial.println(heading, 2);
}

据我所知,这是 c++,我之前只真正做过 python,所以我可能是错的?虽然我仍然在这里找到了相互矛盾的建议,但它在 c++ 上说 iostream 将在 c++ 上工作(http://www.cplusplus.com/doc/tutorial/basic_io/),但随后一个 stackoverflow 的答案说它只在 c 中工作,所以我很困惑,不确定替代方案是什么,所以我没有t 得到“cout 未在此范围内声明”错误。

这是我正在尝试制作的代码部分:

  #include <iostream>
  using namespace std;
  int initialxmag;
  cout << "Please enter the initial/cu x value of the magnetometer in G:";
  cin >> initialxmag;

对此的任何建议将不胜感激。

【问题讨论】:

  • 标准头文件不应包含在函数中。它们旨在包含在文件范围内。

标签: c++ io


【解决方案1】:

不要#include函数中的头文件,而是全局范围内。

【讨论】:

  • 啊好吧!谢谢我没有意识到。不过在我移动它之后它仍然会出现:(
  • “iostream:没有这样的文件或目录”?那么你的编译器设置不正确。请查看有关如何设置默认包含目录的手册。
猜你喜欢
  • 2012-05-03
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
相关资源
最近更新 更多