Tiltmeter program (designed for course ENGO 333 at UofC)

Dependencies:   C12832 mbed

Fork of tilt_angles by Mark Petovello

Files at this revision

API Documentation at this revision

Comitter:
mozhdehshahbazi
Date:
Sun Nov 26 04:16:25 2017 +0000
Parent:
1:64f1aefe1842
Commit message:
Tiltmeter (designed for course ENGO 333 at UofC)

Changed in this revision

Tiltmeter.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 64f1aefe1842 -r 03af85477e12 Tiltmeter.cpp
--- a/Tiltmeter.cpp	Fri Nov 25 15:18:28 2016 +0000
+++ b/Tiltmeter.cpp	Sun Nov 26 04:16:25 2017 +0000
@@ -5,3 +5,44 @@
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 // Paste your code from the pre-lab assignment here
 
+const float PI = atan(1.0) * 4.0;
+const float Rad2Deg = 180.0 / PI;
+
+Tiltmeter::Tiltmeter()
+{
+   // initialize the roll and pitch angles
+   RollAngle = 0.0;
+   PitchAngle = 0.0;
+}
+
+
+void Tiltmeter::ComputeTiltAngles()
+{
+   // read the acceleometers and store them
+   ReadAccelerometers();
+   float a_x = GetAccelX();
+   float a_y = GetAccelY();
+   float a_z = GetAccelZ();
+
+   // normalize the measurements
+   float a = sqrt( a_x * a_x + a_y * a_y + a_z * a_z );
+   a_x /= a;
+   a_y /= a;
+   a_z /= a;
+
+   // compute roll and pitch
+   RollAngle = atan2( -a_x, a_z ) * Rad2Deg;
+   PitchAngle = atan2( a_y, sqrt( a_x * a_x + a_z * a_z ) ) * Rad2Deg;
+}
+
+
+float Tiltmeter::GetRoll() const
+{
+   return RollAngle;
+}
+
+
+float Tiltmeter::GetPitch() const
+{
+   return PitchAngle;
+}
\ No newline at end of file
diff -r 64f1aefe1842 -r 03af85477e12 main.cpp
--- a/main.cpp	Fri Nov 25 15:18:28 2016 +0000
+++ b/main.cpp	Sun Nov 26 04:16:25 2017 +0000
@@ -37,14 +37,14 @@
     C12832 lcd(p5, p7, p6, p8, p11);
 
     // Create digital outputs for LED1 and LED2 that will be used to indicate
-    // if things are working properly -- both values are to LOW (off)
+    // if things are working properly -- both values are now set to LOW (0 / off)
     DigitalOut led1(LED1);
     DigitalOut led2(LED2);
     led1 = 0;
     led2 = 0;
 
     // Create bus input for joystick
-    BusIn joystick(p13, p16, p14);     // Binary [ Centre | Right | Left ] joystick
+    BusIn joystick(p13, p16, p14);     // Binary [ Left |  Right |  Center ] joystick
     
     
     // END: Setup/variable declaration
@@ -65,7 +65,7 @@
     else 
     {
         lcd.locate(1, 11);
-        lcd.printf("ERROR: Unknown sensor");
+        lcd.printf("ERROR: Unknown sensor"); //printing an error message to the lcl
         return 1;
     }
 
@@ -105,7 +105,7 @@
             
             
             // Short delay to avoid multiple measurements being displayed
-            wait_ms(250);
+            wait_ms(250); //this wait() function receives time to wait in milliseconds
 
 
             // Turn off second LED to indicate a measurement is complete