Arduino 实战 HMC5883L 三轴磁场传感器

发布于: 27 January, 2014
分享:

[caption id="attachment_190" align="alignnone" width="400"]

Arduino Uno 连接 HMC5883L 磁场传感器模块

 Arduino Uno 连接 HMC5883L 磁场传感器模块[/caption]

基本实现代码:

  /* @Author: TONYLABS @Date: 2013/03/05 */ #include #include

HMC5883L compass; //@声明 HMC5883L 对象

void setup() {    Serial.begin(9600);//@初始化串口    Wire.begin(); //@启动 Wire    compass = HMC5883L(); //@实例化 HMC5883L 对象    setupHMC5883L(); //@调用后面的 setupHMC5883L 函数,设置 HMC5883L 对象 }

void loop() {    float heading = getHeading(); //@声明浮点数变量 heading    Serial.println(heading); //@串口打印输出 heading 的值    delay(100); //@延时100毫秒 }

void setupHMC5883L() {    //@配置 HMC5883L 对象,检查I2C是否返回错误代码    int error;    error = compass.SetScale(1.3); //@设置放大比率    if(error != 0) Serial.println(compass.GetErrorText(error)); //@检查错误,如有则串口输出该错误    error = compass.SetMeasurementMode(Measurement_Continuous); //@设置测量模式.    if(error != 0) Serial.println(compass.GetErrorText(error)); //@检查错误,如有则串口输出该错误 }

float getHeading() {    //@获取并计算角速度    MagnetometerScaled scaled = compass.ReadScaledAxis(); //@格式化    float heading = atan2(scaled.YAxis, scaled.XAxis);

   //@    if(heading < 0) heading += 2PI;    if(heading > 2PI) heading -= 2*PI;

   return heading * RAD_TO_DEG; //@弧度转换为角度 }     // Reference the I2C Library #include // Reference the HMC5883L Compass Library #include

// Store our compass as a variable. HMC5883L compass; // Record any errors that may occur in the compass. int error = 0;

// Out setup routine, here we will configure the microcontroller and compass. void setup() { // Initialize the serial port. Serial.begin(9600);

Serial.println("Starting the I2C interface."); Wire.begin(); // Start the I2C interface.

Serial.println("Constructing new HMC5883L"); compass = HMC5883L(); // Construct a new HMC5883 compass.

Serial.println("Setting scale to +/- 1.3 Ga"); error = compass.SetScale(1.3); // Set the scale of the compass. if(error != 0) // If there is an error, print it out.   Serial.println(compass.GetErrorText(error));

Serial.println("Setting measurement mode to continous."); error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous if(error != 0) // If there is an error, print it out.   Serial.println(compass.GetErrorText(error)); }

// Our main program loop. void loop() { // Retrive the raw values from the compass (not scaled). MagnetometerRaw raw = compass.ReadRawAxis(); // Retrived the scaled values from the compass (scaled to the configured scale). MagnetometerScaled scaled = compass.ReadScaledAxis();

// Values are accessed like so: int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

// Calculate heading when the magnetometer is level, then correct for signs of axis. float heading = atan2(scaled.YAxis, scaled.XAxis);

// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location. // Find yours here: http://www.magnetic-declination.com/ // Mine is: 2� 37' W, which is 2.617 Degrees, or (which we need) 0.0456752665 radians, I will use 0.0457 // If you cannot find your Declination, comment out these two lines, your compass will be slightly off. float declinationAngle = 0.0457; heading += declinationAngle;

// Correct for when signs are reversed. if(heading < 0)   heading += 2*PI;

// Check for wrap due to addition of declination. if(heading > 2PI)   heading -= 2PI;

// Convert radians to degrees for readability. float headingDegrees = heading * 180/M_PI;

// Output the data via the serial port. Output(raw, scaled, heading, headingDegrees);

// Normally we would delay the application by 66ms to allow the loop // to run at 15Hz (default bandwidth for the HMC5883L). // However since we have a long serial out (104ms at 9600) we will let // it run at its natural speed. // delay(66); }

// Output the data down the serial port. void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees) { Serial.print("Raw:\t"); Serial.print(raw.XAxis); Serial.print("  ");  Serial.print(raw.YAxis); Serial.print("  ");  Serial.print(raw.ZAxis); Serial.print("  \tScaled:\t");

Serial.print(scaled.XAxis); Serial.print("  ");  Serial.print(scaled.YAxis); Serial.print("  ");  Serial.print(scaled.ZAxis);

Serial.print("  \tHeading:\t"); Serial.print(heading); Serial.print(" Radians  \t"); Serial.print(headingDegrees); Serial.println(" Degrees  \t"); }  

例程下载:

这里跳转

分享:

0 留言

留言

您的留言将被人工审核,请勿发表色情、反动言论。