Go Down

Topic: Stuck on calibration for (LSM303) Accelerometer (Read 2 times) previous topic - next topic

gdavisiv

Jan 31, 2013, 05:22 pm Last Edit: Jan 31, 2013, 09:12 pm by gdavisiv Reason: 1


Hi all,

This is my first foray into robotics so I've made myself a mini-tank with a: Triple-axis Accelerometer+Magnetometer (Compass) Board - LSM303

My plan was to teach myself how to use the accelerometer, and use it for simple applications ie. (tank getting stuck -> it executes a maneuver to become unstuck, and the tank uses the compass reading to always head in a northern direction.) I believe I am getting consistent readings from the Accelerometer, and while I was researching I found out that I need to calibrate it so I did some research on the forums and found this topic post:
http://arduino.cc/forum/index.php/topic,140494.0.html

I tried to recreate this within my code but I think I goofed somewhere or maybe I'm not applying it properly?? ** I tested just the X Axis so far**

Based off the reading from the Serial Monitor I'm not sure whats causing my readings to be off.  I tried to find the answer by reading the accelerometer documentation but it left me even more confused.
Accel X: 149620
-24
Accel X: 112000
-84
Accel X: 134120
-12
Accel X: 141620
-24

http://www.adafruit.com/datasheets/LSM303DLHC.PDF



Code:
Code: [Select]
[size=10pt]
#include <Wire.h>
#include <Adafruit_LSM303.h>

Adafruit_LSM303 lsm;

const int XMin = 1064;
const int XMax = 1032;
//const int YMin =
//const int YMax =
//const int ZMin
//const int ZMax

void setup()
{
 Serial.begin(9600);
 
 // Try to initialize and warn if it couldn't detect the chip
 if (!lsm.begin())
 {
   Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!");
   while (1);
 }
}

void loop()
{
 unsigned long XSum = 0;
 unsigned long YSum = 0;
 unsigned long ZSum = 0;
 
 for (int i=0; i<100; i++)
 {
   XSum += ((int)lsm.accelData.x);
   //YSum += analogRead(YPin);
   //ZSum += analogRead(ZPin);
 }
 
 lsm.read();

 //Serial.print("Accel X: ");

 Serial.print((int)lsm.accelData.x);
 Serial.print("\n");
 Serial.print("Accel X: ");
 Serial.print(map(XSum/100, XMin, XMax, -2000, 2000));
 Serial.print("\n");

 //Serial.print("Y: "); Serial.print((int)lsm.accelData.y);       Serial.print(" "); //Accel Y
 //Serial.print("Z: "); Serial.println((int)lsm.accelData.z);     Serial.print(" "); //Accel Z
 //Serial.print("Mag X: "); Serial.print((int)lsm.magData.x);     Serial.print(" ");
 //Serial.print("Y: "); Serial.print((int)lsm.magData.y);         Serial.print(" ");
 //Serial.print("Z: "); Serial.println((int)lsm.magData.z);       Serial.print(" ");
 delay(500);
}[/size]


Chagrin

This is not correct. lms.accelData.x (or y or z) is only updated after calls to lsm.read(). If you .read() it once then look at it 100 times it will be the same value.


Code: [Select]
 for (int i=0; i<100; i++)
 {
   XSum += ((int)lsm.accelData.x);
   //YSum += analogRead(YPin);
   //ZSum += analogRead(ZPin);
 }
 
 lsm.read();



Not sure why you're getting a crazy value here though:
Code: [Select]

Serial.print(map(XSum/100, XMin, XMax, -2000, 2000));


gdavisiv

ahh ok ok I didn't even notice that silly little mistake thanks!!

I'll try looking at it again at the map function maybe the last two values should be -1000, 1000 instead??

Chagrin

Your X readings are coming back -12, -24, -84, and you're trying to map that from the range 1032 to 1064 (Xmin to Xmax). Those numbers aren't inside that range. I don't know how map() will react to that.

michinyon

If you are trying to add up 100 readings  to get some sort of average,  then
(a)  after you add up 100 readings,  you need to divide by 100
(b)  make sure you don't overflow the int

try seeing what the actual readings are,  without confusing yourself and me with the stoopid map() function.

try reading the y and z axes as well,  that will give you a better idea if the device is actually working and the
communications to the device are actually working.

the answer from an accelerometer is going to look rather boring unless the device is actually accelerating.

Go Up