Using the MMA7361 accelerometer in a RC plane.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using the MMA7361 accelerometer in a RC plane.

2,076 Views
rigelsales
Contributor I

I am an electrical engineering student and this is my first project without the supervision of a professor. I want to use the accelerometer MMA7361 connected to an Arduino in a RC plane in order to know the angle of the three axes of the aircraft, the acceleration that each axle suffers or the total acceleration of the plane. Initially I used this code for the measurement of angles of only two axes:

int maxi = 0,mini = 1023;

void setup() {

  Serial.begin(9600);

}

void loop() {

  int sensorX= analogRead(A0);

  sensorX = map(sensorX, 180, 530, 90, -90);

  Serial.print(" Angulo X = ");

  Serial.println(sensorX);

 

  int sensorY= analogRead(A1);

  sensorY = map(sensorY, 205, 585, 90, -90);

  Serial.print(" Angulo Y = ");

  Serial.println(sensorY);

 

  delay(600);

}

When I was testing this code I realized that if the angle remained the same but the accelerometer suffer any abrupt deceleration, the angle value indicated would change. Then I began to think of a solution to my problem. When reading the accelerometer library, I realized that there was a function called getXAccel that returns the acceleration of the X-axis as a int (1 G = 100.00). The code bellow uses that function and I found it in the library.

#include <AcceleroMMA7361.h>

AcceleroMMA7361 accelero;

int x;

int y;

int z;

void setup()

{

  Serial.begin(9600);

  accelero.begin(13, 12, 11, 10, A0, A1, A2);

  accelero.setARefVoltage(3.3);                  

  accelero.setSensitivity(LOW);                 

  accelero.calibrate();

}

void loop()

{

  x = accelero.getXAccel();

  y = accelero.getYAccel();

  z = accelero.getZAccel();

  Serial.print("\nx: ");

  Serial.print(x);

  Serial.print(" \ty: ");

  Serial.print(y);

  Serial.print(" \tz: ");

  Serial.print(z);

  Serial.print("\tG*10^-2");

  delay(500);                                    

}

Testing that code, initially everything seemed to be correct. In X and Y-axis the acceleration practically had not existed and in the Z-axis the acceleration was approximately 1G. But, when I leaned the accelerometer 90 in x, the acceleration was about to 4G, but actually I expected it to be 1G.

Can someone tell me what is going on? I also saw in the library the function getTotalVector that returns the magnitude of the total acceleration vector as an integer but I don't know how to use it. Anyone have a better ideia to find the angle, the total acceleration of the plane or the acceleration of each axis?

Thanks a lot.

Labels (3)
0 Kudos
0 Replies
%3CLINGO-SUB%20id%3D%22lingo-sub-426828%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EUsing%20the%20MMA7361%20accelerometer%20in%20a%20RC%20plane.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-426828%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EI%20am%20an%20electrical%20engineering%20student%20and%20this%20is%20my%20first%20project%20without%20the%20supervision%20of%20a%20professor.%20I%20want%20to%20use%20the%20accelerometer%20MMA7361%20connected%20to%20an%20Arduino%20in%20a%20RC%20plane%20in%20order%20to%20know%20the%20angle%20of%20the%20three%20axes%20of%20the%20aircraft%2C%20the%20acceleration%20that%20each%20axle%20suffers%20or%20the%20total%20acceleration%20of%20the%20plane.%20Initially%20I%20used%20this%20code%20for%20the%20measurement%20of%20angles%20of%20only%20two%20axes%3A%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3Eint%20maxi%20%3D%200%2Cmini%20%3D%201023%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3Evoid%20setup()%20%7B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.begin(9600)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3Evoid%20loop()%20%7B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20int%20sensorX%3D%20analogRead(A0)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%20style%3D%22line-height%3A%201.5em%3B%20font-size%3A%2010pt%3B%22%3E%26nbsp%3B%20sensorX%20%3D%20map(sensorX%2C%20180%2C%20530%2C%2090%2C%20-90)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.print(%22%20Angulo%20X%20%3D%20%22)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.println(sensorX)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%26nbsp%3B%20%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20int%20sensorY%3D%20analogRead(A1)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%20style%3D%22line-height%3A%201.5em%3B%20font-size%3A%2010pt%3B%22%3E%26nbsp%3B%20sensorY%20%3D%20map(sensorY%2C%20205%2C%20585%2C%2090%2C%20-90)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.print(%22%20Angulo%20Y%20%3D%20%22)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.println(sensorY)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%26nbsp%3B%20%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20delay(600)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3C%2FP%3E%3CP%3EWhen%20I%20was%20testing%20this%20code%20I%20realized%20that%20if%20the%20angle%20remained%20the%20same%20but%20the%20accelerometer%20suffer%20any%20abrupt%20deceleration%2C%20the%20angle%20value%20indicated%20would%20change.%20Then%20I%20began%20to%20think%20of%20a%20solution%20to%20my%20problem.%20When%20reading%20the%20accelerometer%20library%2C%20I%20realized%20that%20there%20was%20a%20function%20called%20%3CSTRONG%3E%3CEM%20style%3D%22text-decoration%3A%20underline%3B%22%3EgetXAccel%3C%2FEM%3E%3C%2FSTRONG%3E%20that%20returns%20the%20acceleration%20of%20the%20X-axis%20as%20a%20int%20(1%20G%20%3D%20100.00).%20The%20code%20bellow%20uses%20that%20function%20and%20I%20found%20it%20in%20the%20library.%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%23include%20%3CACCELEROMMA7361.H%3E%3C%2FACCELEROMMA7361.H%3E%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3EAcceleroMMA7361%20accelero%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3Eint%20x%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3Eint%20y%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3Eint%20z%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3Evoid%20setup()%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%7B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.begin(9600)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20accelero.begin(13%2C%2012%2C%2011%2C%2010%2C%20A0%2C%20A1%2C%20A2)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20accelero.setARefVoltage(3.3)%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20accelero.setSensitivity(LOW)%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20accelero.calibrate()%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%7D%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3Evoid%20loop()%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%7B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20x%20%3D%20accelero.getXAccel()%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20y%20%3D%20accelero.getYAccel()%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20z%20%3D%20accelero.getZAccel()%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.print(%22%5Cnx%3A%20%22)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.print(x)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.print(%22%20%5Cty%3A%20%22)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.print(y)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.print(%22%20%5Ctz%3A%20%22)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.print(z)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20Serial.print(%22%5CtG*10%5E-2%22)%3B%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CEM%3E%3CSTRONG%3E%26nbsp%3B%20delay(500)%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3CSTRONG%3E%3CEM%3E%7D%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%20style%3D%22padding-left%3A%20360px%3B%22%3E%3C%2FP%3E%3CP%3ETesting%20that%20code%2C%20initially%20everything%20seemed%20to%20be%20correct.%20In%20X%20and%20Y-axis%20the%20acceleration%20practically%20had%20not%20existed%20and%20in%20the%20Z-axis%20the%20acceleration%20was%20approximately%201G.%20But%2C%20when%20I%20leaned%20the%20accelerometer%2090%20in%20x%2C%20the%20acceleration%20was%20about%20to%204G%2C%20but%20actually%20I%20expected%20it%20to%20be%201G.%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3ECan%20someone%20tell%20me%20what%20is%20going%20on%3F%20I%20also%20saw%20in%20the%20library%20the%20function%20%3CEM%3E%3CSTRONG%3EgetTotalVector%20%3C%2FSTRONG%3E%3C%2FEM%3Ethat%20returns%20the%20magnitude%20of%20the%20total%20acceleration%20vector%20as%20an%20integer%20but%20I%20don't%20know%20how%20to%20use%20it.%20Anyone%20have%20a%20better%20ideia%20to%20find%20the%20angle%2C%20the%20total%20acceleration%20of%20the%20plane%20or%20the%20acceleration%20of%20each%20axis%3F%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3EThanks%20a%20lot.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-426828%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3EAccelerometers%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EGyroscope%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMagnetic%20Sensors%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E