Go Down

Topic: Informazioni sketch (Read 679 times) previous topic - next topic

AMBUSH

Aug 21, 2013, 08:52 pm Last Edit: Aug 22, 2013, 06:47 am by leo72 Reason: 1
Buona sera a tutti.

Volevo chiedervi una mano a scioglere qualche dubbio su questo sketch.

Lo sketch è stato preso dal sito da dove ho comprato il sensore "http://www.seeedstudio.com/depot/grove-3axis-digital-compass-p-759.html?cPath=25_30" ( sezione wiki )

Grazie anticipatamente



Code: [Select]
// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>

// 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");
   
 Serial.println("Setting scale to +/- 1.3 Ga");
 error = compass.setScale(8.1); // 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(8.1); // 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' 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 > 2*PI)
   heading -= 2*PI;
 
 // 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(1000);//of course it can be delayed longer.
}

// 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");
}


edit by mod: per favore includi il codice usando gli appositi tag

cyberhs

#1
Aug 22, 2013, 03:24 am Last Edit: Aug 22, 2013, 03:26 am by cyberhs Reason: 1
Innanzitutto modifica il tuo post inserendo il codice con il tag "#", altrimenti il moderatore piange!

Poi... quali sono i tuoi dubbi?

leo72


AMBUSH

Scusati :-) effettivamente non vi ho chiesto quali sono i miei dubbi, per di più ho postato uno sketch leggermente modificato quindi vi posto quello originale

Code: [Select]
/*****************************************************************************/
// Function: Get the Geographic direction of the X-axis.
// If X-axis points to the North, it is 0 degree.
// If X-axis points to the East, it is 90 degrees.
// If X-axis points to the South, it is 180 degrees.
// If X-axis points to the West, it is 270 degrees.
//  Hardware:   Grove - 3-Axis Digital Compass
// Arduino IDE: Arduino-1.0
// Author: Frankie.Chu
// Date: Jan 10,2013
// Version: v1.0
// by www.seeedstudio.com
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
/*******************************************************************************/

// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>

// 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");
   
  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' 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 > 2*PI)
    heading -= 2*PI;
   
  // 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);//of course it can be delayed longer.
}

// 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");
}


come funziona questa linea di comando??
Code: [Select]
Serial.println("Setting measurement mode to continous.");
  error = compass.setMeasurementMode(MEASUREMENT_CONTINUOUS); // Set the measurement mode to Continuous


cioè come setto??

spero di essermi espresso bene ;-)

grazie



leo72

Quella riga non è da configurare. Legge un sensore, una bussola digitale.
La costante che passi alla funzione dice alla stessa di leggere in continuo. La lib avrà altre costanti predefinite per configurare il sensore.

AMBUSH

vale lo stesso anche per questa riga di codice?

Code: [Select]
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));

leo72

Le lib di solito forniscono dei metodi semplificati per accedere a dei componenti, con costanti predefinite per aiutare la comprensione di cosa faccia una funzione. Ti consiglio di guardare nel readme della lib o nella pagina della stessa per vedere cosa permette di fare la lib.

Go Up