Grove - 6-Axis Accelerometer&Compass V2.0
Version från den 29 oktober 2018 kl. 09.13 av Christian (diskussion | bidrag)
En 3-axlig accelerometer i kombination med en 3-axlig magnetisk sensor.
Innehåll
Kompatibilitet
- Arduino
- Raspberry Pi
Port
- I2C
Exempelkod
Arduino
/*
hardware & software comment
I2C mode:
1, solder the jumper "I2C EN" and the jumper of ADDR to 0x1E
2, use Lsm303d.initI2C() function to initialize the Grove by I2C
SPI mode:
1, break the jumper "I2C_EN" and the jumper ADDR to any side
2, define a pin as chip select for SPI protocol.
3, use Lsm303d.initSPI(SPI_CS) function to initialize the Grove by SPI
SPI.h sets these for us in arduino
const int SDI = 11;
const int SDO = 12;
const int SCL = 13;
*/
#include <LSM303D.h>
#include <Wire.h>
#include <SPI.h>
/* Global variables */
int accel[3]; // raw acceleration values store
int mag[3]; // raw magnetometer values store
float realAccel[3]; // calculated acceleration values
float heading, titleHeading;
#define SPI_CS 10
void setup()
{
char rtn = 0;
Serial.begin(9600); // Serial is used for debugging
Serial.println("\r\npower on");
rtn = Lsm303d.initI2C();
//rtn = Lsm303d.initSPI(SPI_CS);
if(rtn != 0) // Initialize the LSM303, using a SCALE full-scale range
{
Serial.println("\r\nLSM303D is not found");
while(1);
}
else
{
Serial.println("\r\nLSM303D is found");
}
}
void loop()
{
Serial.println("\r\n**************");
//getLSM303_accel(accel); // get the acceleration values and store them in the accel array
Lsm303d.getAccel(accel);
while(!Lsm303d.isMagReady());// wait for the magnetometer readings to be ready
Lsm303d.getMag(mag); // get the magnetometer values, store them in mag
for (int i=0; i<3; i++)
{
realAccel[i] = accel[i] / pow(2, 15) * ACCELE_SCALE; // calculate real acceleration values, in units of g
}
heading = Lsm303d.getHeading(mag);
titleHeading = Lsm303d.getTiltHeading(mag, realAccel);
printValues();
delay(200); // delay for serial readability
}
void printValues()
{
Serial.println("Acceleration of X,Y,Z is");
for (int i=0; i<3; i++)
{
Serial.print(realAccel[i]);
Serial.println("g");
}
/* print both the level, and tilt-compensated headings below to compare */
Serial.println("The clockwise angle between the magnetic north and x-axis: ");
Serial.print(heading, 3); // this only works if the sensor is level
Serial.println(" degrees");
Serial.print("The clockwise angle between the magnetic north and the projection");
Serial.println(" of the positive x-axis in the horizontal plane: ");
Serial.print(titleHeading, 3); // see how awesome tilt compensation is?!
Serial.println(" degrees");
}
Raspberry Pi
import lsm303d
try:
acc_mag=lsm303d.lsm303d()
while True:
# Get accelerometer values
acc=acc_mag.getRealAccel()
# Wait for compass to get ready
while True:
if acc_mag.isMagReady():
break
# Read the heading
heading= acc_mag.getHeading()
print("Acceleration of X,Y,Z is %.3fg, %.3fg, %.3fg" %(acc[0],acc[1],acc[2]))
print("Heading %.3f degrees\n" %(heading))
except IOError:
print("Unable to read from accelerometer, check the sensor and try again")
Bibliotek (Arduino) på Github
https://github.com/Seeed-Studio/6Axis_Accelerometer_And_Compass_v2
Mer information
http://wiki.seeedstudio.com/Grove-6-Axis_AccelerometerAndCompass_V2.0/