understanding code: accelerometer with arduino

Discussion in 'Arduino' started by meowth08, Jan 14, 2013.

1. meowth08Member

Joined:
Aug 18, 2011
Messages:
241
Likes:
7
Location:
Philippines
Code (text):
#define ANALOG0 14

class Accelerometer
{
int p[3]; // which analog pins
int a[3]; // acceleration, zero-based
int b[3]; // acceleration bias/calibration information
int g, t, r; // cached copies of calculations
int scale; // scaling factor between ADC and gravity

public:
Accelerometer(int pinX, int pinY, int pinZ, int pinVCC, int pinGND)
{
pinMode(pinGND + ANALOG0, OUTPUT); digitalWrite(pinGND + ANALOG0, LOW);
pinMode(pinVCC + ANALOG0, OUTPUT); digitalWrite(pinVCC + ANALOG0, HIGH);
pinMode((p[0] = pinX) + ANALOG0, INPUT);
pinMode((p[1] = pinY) + ANALOG0, INPUT);
pinMode((p[2] = pinZ) + ANALOG0, INPUT);
for (int i = 0; i < 3; i++)
b[i] = 512;
g = t = r = 0;
scale = 100;
}

void update()
{
for (int i = 0; i < 3; i++)
g = t = r = 0;
}

void dump()
{
Serial.print(  "x="); Serial.print(a[0]);
Serial.print("\ty="); Serial.print(a[1]);
Serial.print("\tz="); Serial.print(a[2]);
Serial.print("\tmg="); Serial.print(milligee());
Serial.print("\tpitch="); Serial.print(pitch());
Serial.print("\troll="); Serial.print(roll());
Serial.println();
}

void calibrate()
{
for (int i = 0; i < 3; i++)
b[2] -= scale;
update();
}

int milligee()
{
if (g != 0) return g;
long squared = 0.0;
for (int i = 0; i < 3; i++)
squared += (long)a[i] * (long)a[i];
g = squared * 1000 / (scale*scale);
return g;
}

int accel(int axis)
{
if (axis < 0 || axis > 3) return 0;
return a[axis];
}

int roll()
{
if (r != 0) return r;
r = (int)(atan2(a[0], a[2]) * 180. / M_PI);
return r;
}

int pitch()
{
if (t != 0) return t;
t = (int)(acos(a[1] / (float)scale) * 180. / M_PI);
return t;
}

void loop()
{
update();
}
};
Hi,
I copied this code from the net.
Kindly add some comments on what each part of the code is for.
I am having a hard time understanding C.

m8

Joined:
Mar 28, 2011
Messages:
9,145
Likes:
907
Location:
Rochdale UK
ONLINE
Unfortunately this is worse... Its C++... If you don't grasp C++ ( Object Orientated ) It won't be easy to explain

Code (C):

#define ANALOG0 14

class Accelerometer             /// Name of the class
{
// the following are class variables ( private and can't be seen outside the scope of the class)
int p[3]; // which analog pins
int a[3]; // acceleration, zero-based
int b[3]; // acceleration bias/calibration information
int g, t, r; // cached copies of calculations
int scale; // scaling factor between ADC and gravity

public:     // All these functions below WILL be seen outside of the class scope.
Accelerometer(int pinX, int pinY, int pinZ, int pinVCC, int pinGND)
{
pinMode(pinGND + ANALOG0, OUTPUT); digitalWrite(pinGND + ANALOG0, LOW);
pinMode(pinVCC + ANALOG0, OUTPUT); digitalWrite(pinVCC + ANALOG0, HIGH);
pinMode((p[0] = pinX) + ANALOG0, INPUT);
pinMode((p[1] = pinY) + ANALOG0, INPUT);
pinMode((p[2] = pinZ) + ANALOG0, INPUT);     // pinmode is a library function
for (int i = 0; i < 3; i++)
b[i] = 512;
g = t = r = 0;
scale = 100;
}

void update()   //  This can be called to refresh all the above variables.
{
for (int i = 0; i < 3; i++)
g = t = r = 0;
}

void dump()     // this is called to print the variables
{
Serial.print(  "x="); Serial.print(a[0]);       // serial is the serial class. Print is one of the functions
Serial.print("\ty="); Serial.print(a[1]);
Serial.print("\tz="); Serial.print(a[2]);
Serial.print("\tmg="); Serial.print(milligee());
Serial.print("\tpitch="); Serial.print(pitch());
Serial.print("\troll="); Serial.print(roll());
Serial.println();
}

void calibrate()    // Again.. Called to calibrate the accelerometer
{
for (int i = 0; i < 3; i++)
b[2] -= scale;
update();
}

int milligee()      // returns the G force
{
if (g != 0) return g;
long squared = 0.0;
for (int i = 0; i < 3; i++)
squared += (long)a[i] * (long)a[i];
g = squared * 1000 / (scale*scale);
return g;
}

int accel(int axis) // this returns the acceration as an Int
{
if (axis < 0 || axis > 3) return 0;
return a[axis];
}

int roll()  // this returns the roll as an Int
{
if (r != 0) return r;
r = (int)(atan2(a[0], a[2]) * 180. / M_PI);
return r;
}

int pitch()     // this returns the pitch as an Int
{
if (t != 0) return t;
t = (int)(acos(a[1] / (float)scale) * 180. / M_PI);
return t;
}

void loop()
{
update();
}

Joined:
Aug 18, 2011
Messages:
241
Likes:
7
Location:
Philippines
Hi,