Log Test Code. Round 2

Dependencies:   C12832 mbed

Fork of RF_ID by A Clark

Revision:
4:5513c0e9fa1a
Parent:
3:9b47638100af
--- a/AntennaLog.cpp	Tue Apr 08 21:01:31 2014 +0000
+++ b/AntennaLog.cpp	Tue Apr 22 03:02:03 2014 +0000
@@ -1,6 +1,8 @@
 #include "mbed.h"
 #define PI 3.1415
 #include "C12832.h"
+#include "Servo.h"
+
 
 LocalFileSystem local("local");               // Create the local filesystem under the name "local"
 
@@ -8,52 +10,81 @@
 
 Timer     t;
 
-AnalogIn   ain_v1(p19); // Input voltage from antenna
+AnalogIn    ain_v1(p19);        // Input voltage from antenna
+Servo      servo(p21);         // Input commands and power for servo (Three PWM1 pins correspond to pin 21)
 
     // Global variables declared
-    float v1, m, b, p_in, Pt, Gt, Gr, Pr, f, c, r;
-    float lv_r[250], lv_v1[250];
-    int k;
+    float v1, m, b, p_in, Pt, Gt, Gr, Pr, Pr_scaled, f, c, r, offset;
+    float lv_r[250], lv_v1[250], lv_PrS[250], lv_time[250];
+    int k, n;
 
 int main()
 {
-    
-    // Open logging file
-    FILE *fp = fopen("/local/mBedOutp.m", "w"); 
+    n = 250;
+    servo.calibrate(0.00045, 45.0); // Halfway point (first number is some sort of control frequency)
     
     //logging setup 
-    for(k=0;k<250;k++)
+    for(k=0;k<n;k++)
      { 
-        lv_r[k]      = 0.0;
-        lv_v1[k]     = 0.0;
+        lv_r[k]         = 0.0;
+        lv_v1[k]        = 0.0;
+        lv_PrS[k]       = 0.0;
+        lv_time[k]      = 0.0;
      }
     k = 0;
     
+    
+    
     t.start();
-    while(k<250)
+    while(k<250)//=================================================================================================
     {
+        
+    // SENSE=============
+    
     // Voltage to Power calculations
-    v1      = ain_v1*3.33;           // ain_v1;   // Comes from antenna (Can adjust manually for now) (3.3 is for scaling voltage properly... ask Feemster why this is necessary.
-    b       = 2.095;            // (volts) Direct function generator measurement
-    m       = .0316;            // volts/dBm (Calculated at 2000 MHz and 0 and -40 dBm)
-    p_in    = (v1-b)/m;         // Derived from v = m*p+b (linear relationship of voltage and power)
+    v1      = ain_v1*3.33;          // 3.3 is for scaling voltage properly... ask Feemster why this is necessary.
+    b       = 2.095;                // (volts) Direct function generator measurement
+    m       = .0316;                // volts/dBm (Calculated at 2000 MHz and 0 and -40 dBm)
+    p_in    = (v1-b)/m;             // Derived from v = m*p+b (linear relationship of voltage and power)
    
     // Range equation Terms
-    Pt      = .0398;            //(watts or 1mW or 0 dBm) Power Transmitted. This is ~16 dBm (.0398 watts) for an iphone 5
-    Gt      = 1;                // Unitless (1 for FG)                      Shouldn't both of these gains be 'directive' gains, not power gains
-    Gr      = .007;                // (.007) (7 mW) How do we get this? 7 milliwatt?
-    Pr      = pow(10, ((p_in-30)/10)); // (watts)
+    Pt          = .0398;                    // (Watts) For iPhone 5
+    Gt          = .707;                     // sqrt(2)/2
+    Gr          = .007;                     // Approximation (Will calibrate in antenna chamber) 
+    Pr          = pow(10, ((p_in-30)/10));  // (watts)
+    Pr_scaled   = Pr*1000;
     
     // Free Space Loss Terms
-    f       = 2400000000;       // (Hz) This will be constant for a phone... but what to put for the FG
-    c       = 300000000;        // m/s
+    f       = 800000000;            // (Hz) This will be constant for a phone... but what to put for the FG
+    c       = 300000000;            // m/s
+    
+    r       = (c*sqrt(Pt*Gt*Gr/Pr))/(4*PI*f);
+    
+    //DECIDE=============
+    
+    if(r<=7.0)
+    {   
+        wait(0.2);
+        servo = .088;           // 0 to 1 where 1 is 90 degrees due to calibration command above
+    }
     
-    r       = (c*(sqrt(Pt*Gt*Gr)/Pr))/(4*PI*f);
+    if(r>=8.0 && r<=10.0)
+    {   
+        wait(0.2);
+        servo = .33;            // Above
+    }
+    
+    if(r>11.0 && r<=13.0)
+    {   
+        wait(0.2);
+        servo = .55;            // Above
+    }
+    
     
     printf("\rRange (meters)    = %.3f\n\r",r);
-    //printf("Power (watts)     = %.3f\n\r",Pr);
+    printf("Power (mW)        = %.3f\n\r",Pr_scaled);
     printf("Voltage into mBed = %.4f\n\r",v1);
-    //printf("%.4f\n\r",v1);
+    // Can print time if desired
     
     lcd.cls();
     lcd.locate(0,3);
@@ -62,21 +93,28 @@
     lcd.locate(0,15);
     lcd.printf("Range (meters) = %.4f",r);
     
-      //LOG stuff
-    if(k<250)
+    //LOG stuff
+    if(k<n)
     {   lv_r[k]     = r;
         lv_v1[k]    = v1;
+        lv_PrS[k]   = Pr;
+        lv_time[k]  = t;
         k++;
     }
     
     } // End of while=======================================================
     
+    // Open logging file (Must be done down here... not sure why)
+    FILE *fp = fopen("/local/mBedOutp.m", "w"); 
+    
     if(1)
     {
     
-    for(k=0;k<250;k++)
+    for(k=0;k<n;k++)
     {   fprintf(fp,"lv.r(%d,1)  = %.3f;\n",k+1,lv_r[k]);
         fprintf(fp,"lv.v1(%d,1) = %.4f;\n",k+1,lv_v1[k]);
+        fprintf(fp,"lv.PrS(%d,1) = %.8f;\n",k+1,lv_PrS[k]);
+        fprintf(fp,"lv.t(%d,1) = %.4f;\n",k+1,lv_time[k]);
     }
     
     }// End of IF loop