Aidan Cleary / Mbed 2 deprecated ESlab3

Dependencies:   mbed MMA7660

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 //embedded systems lab question 3 Aidan Cleary T00209564
00003 //Lab 3: Digital sensors & equations in C
00004 
00005 /*
00006 q2:To test the result of the calculations. I downloaded the clinometer and tested
00007 the mbed device. The tera term results were very close to the results on my phone app.
00008 In my code I floated the X,Y,Z and roll angle on tera term. It would change results
00009 every second.
00010 
00011 High-pass filter, LMS algorithm and Kalman filter can be used to reduce offset and drift
00012 there by improving the static stability and position accuracy by reducing noise, offset.
00013 
00014 I initially used a /r/n after the printf statements. This resulted in a new line being
00015 printed every second full of numbers. It got very confusing. I didn’t want to see a load
00016 of numbers every second so decided to replace the code to /r instead. This just floated
00017 the values I wanted to see every second on the tera term interface. This was far easier
00018 to read and I am satisfied with the result of the program. One other issue is had was trying
00019 to tell which way was the X, Y and Z axis, but after moving it about by hand I was able to
00020 eventually figure it out.  In this lab it was also very interesting to see the way that
00021 formulas are implemented into the C++ language.
00022 
00023 */
00024 
00025 #include "mbed.h"//preprosser command
00026 #include "MMA7660.h"//preprocesser command
00027 
00028 #define PI 3.14159265//PI defined 
00029 
00030 Serial pc(USBTX,USBRX);//serial transmission to pc via USBTX, USBRX
00031 MMA7660 MMA(p28,p27);//IC2 pins associated with accelerometer
00032 
00033 float calculateAngle(float x,float y,float z)
00034 {
00035     float angle = 0;
00036 // calculate the angle from the formula given in the instructions
00037 
00038     angle = (y*y) + (z*z);//bottom of the division line
00039     angle = sqrt (angle);//square root
00040     angle = x/angle;
00041     angle = atan (angle);//arctan of angle
00042     angle = angle *180/PI; //radians to degrees!
00043     return angle;
00044 }
00045 
00046 int main() //main program
00047 {
00048     while (1) {
00049         pc.printf("    X%f     Y%f      Z%f     angle%.2f degrees\r", MMA.x(),
00050                   MMA.y(), MMA.z(), calculateAngle(MMA.x(), MMA.y(), MMA.z()));
00051         //i have used \r to repeat the function and keep the tera term tidy
00052         wait(1);//wait 1 second
00053     }
00054 }