Gyro output to TCP in degrees with calibration and correction

Dependencies:   EthernetNetIf mbed

Files at this revision

API Documentation at this revision

Comitter:
SED9008
Date:
Mon Mar 19 11:06:20 2012 +0000
Parent:
1:a88d1309f810
Commit message:

Changed in this revision

L3G4200D.lib 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 a88d1309f810 -r 96f81996a332 L3G4200D.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/L3G4200D.lib	Mon Mar 19 11:06:20 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/SED9008/code/L3G4200D/#68f46af710d9
diff -r a88d1309f810 -r 96f81996a332 main.cpp
--- a/main.cpp	Fri Mar 02 22:31:23 2012 +0000
+++ b/main.cpp	Mon Mar 19 11:06:20 2012 +0000
@@ -33,6 +33,7 @@
 #include "EthernetNetIf.h"
 #include "TCPSocket.h"
 #include "ADXL345_I2C.h"
+#include "L3G4200D.h"
 //#############################################//
 
 //##############     DEFINES     ##############//
@@ -47,6 +48,7 @@
 int     print       = 0,
         go          = 0,
         calibrated  = 0;
+int  gyro_val[3] = {0,0,0};
 //#############################################//
 
 //##############PIN CONFIGURATION##############//
@@ -54,8 +56,9 @@
 DigitalOut led4(LED4, "led4");
 DigitalIn init(p5);
 DigitalIn calibrate(p20);
-//I2C i2c(p9, p10); // sda, scl
+I2C i2c(p9, p10); // sda, scl
 ADXL345_I2C acc(p9, p10);
+L3G4200D gyro(p9, p10);
 //#############################################//
 
 //##############    PROTOTYPES   ##############//
@@ -64,6 +67,7 @@
 void onConnectedTCPSocketEvent(TCPSocketEvent e);
 void onListeningTCPSocketEvent(TCPSocketEvent e);
 double getAngle(char axis, int precision);
+void getI2CAddress();
 //#############################################//
 
 //############## ETHERNET CONFIG ##############//
@@ -71,8 +75,8 @@
 EthernetNetIf eth(
     IpAddr(192,168,0,16), //IP Address
     IpAddr(255,255,255,0), //Network Mask
-    IpAddr(192,168,0,1), //Gateway
-    IpAddr(192,168,0,1)  //DNS
+    IpAddr(192,168,0,2), //Gateway
+    IpAddr(192,168,0,2)  //DNS
 );
 
 TCPSocket ListeningSock;
@@ -86,24 +90,15 @@
 //##############    INTERRUPT    ##############//
 void interrupt() 
 {
-//    int count = 0;
-//    for (int address=0; address<256; address+=2) {
-//        if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
-//             if(go) {
-//                tcp_send(" - I2C device found at address:");
-//                char buffer [128];
-//                sprintf (buffer, "%i\n",address);
-//                tcp_send(buffer);
-//             }
-//             count++;
-//        }
-//    } 
     if(go){
         if(calibrated){
             if(print){
+ //               char buffer [128];
+ //               sprintf (buffer, "x:%f - y:%f\n",cont[0],cont[1]);
+ //               tcp_send(buffer);  
                 char buffer [128];
-                sprintf (buffer, "x:%f - y:%f\n",cont[0],cont[1]);
-                tcp_send(buffer);  
+                sprintf (buffer, "x:%i - y:%i - z:%i\n",gyro_val[0],gyro_val[1],gyro_val[2]);
+                tcp_send(buffer);
                 print = 0;
             }
         }
@@ -147,6 +142,11 @@
     wait(0.1);
     //#############################################//
     
+    //##################GYRO INIT##################//
+    
+    
+    //#############################################//
+    
     Timer tmr;
     tmr.start();
     
@@ -167,10 +167,15 @@
             led4=!led4; //Show that we are alive
             tmr.reset();
         }
-        if(calibrate){
+/*        if(calibrate){
             while(calibrate){};
-            x_cor = getAngle('x',100);
-            y_cor = getAngle('y',100);
+            char buffer [128];
+            sprintf (buffer, "Calibrated\n");
+            tcp_send(buffer);
+            
+            
+            x_cor = getAngle('x',100);//-90
+            y_cor = getAngle('y',100);//-90
            
             char buffer [128];
             sprintf (buffer, "Calibration values:\n");
@@ -188,11 +193,18 @@
         }
         if(calibrated){
 
-            cont[0] = getAngle('x',10) - x_cor;
-            cont[1] = getAngle('y',10) - y_cor;
-
+//            cont[0] = getAngle('x',10) - x_cor;
+ //           cont[1] = getAngle('y',10) - y_cor;
+            gyro.read(gyro_val);
             print = 1;
+        }*/
+        if(go){
+            gyro.read(gyro_val);
+            char buffer [128];
+            sprintf (buffer, "x:%i - y:%i - z:%i\n",gyro_val[0],gyro_val[1],gyro_val[2]);
+            tcp_send(buffer);
         }
+        
     }  
 }
 //#############################################//
@@ -322,8 +334,19 @@
     y_angle = y_angle/precision;
 //    x_angle = atan(x_angle) * 180 / PI;
 //    y_angle = atan(y_angle) * 180 / PI;
-    if(axis == 'x')return atan(x_angle) * 180 / PI;
-    if(axis == 'y')return atan(y_angle) * 180 / PI;
+    if(axis == 'x')return (atan(x_angle) * 180 / PI);//+90
+    if(axis == 'y')return (atan(y_angle) * 180 / PI);//+90
 }
 
-
+void getI2CAddress()
+{
+    int count = 1;
+    for (int address=0; address<256; address+=2) {
+        if (!i2c.write(address, NULL, 0)) { // 0 returned is ok    
+            char buffer [128];
+            sprintf (buffer, "%i: - %i\n",count, address);
+            tcp_send(buffer);
+            count++;    
+        }           
+    }
+}