program 1 driver

Dependencies:   mbed motorController2

Revision:
2:50b081df251a
Parent:
1:3370c513ff5c
Child:
3:e3b61eb0590b
--- a/main.cpp	Sun Dec 04 15:43:34 2016 +0000
+++ b/main.cpp	Sun Dec 04 16:15:40 2016 +0000
@@ -20,6 +20,9 @@
 const float micro_steps2 = 1/(stp2/range2);//saln que hara 53 steps
 
 
+const int N =100; //Number of measures x position
+
+
 
 DigitalOut myled(LED1); // Digital OutPut
 Serial pc(SERIAL_TX, SERIAL_RX); // Default USART is: 8N1 NO FlowControl
@@ -30,8 +33,40 @@
 uint16_t measTR = 0;
 uint16_t measBL = 0;
 uint16_t measBR = 0;
-int i=0;
+int i=0; //iterator 1
+int j=0; //iteerator 2
+
 char choice;
+int pos2;
+
+int mitja_esquerra_adalt= 0; //Average of measures of PH
+int mitja_dreta_adalt=0;     //Average of measures of PH
+int mitja_esquerra_abaix=0;  //Average of measures of PH
+int mitja_dreta_abaix = 0;   //Average of measures of PH
+
+int val_top_left [N]= {0};  //instantaneous value of measure of PH
+int val_top_right [N]= {0}; //instantaneous value of measure of PH
+int val_bot_left [N]= {0};  //instantaneous value of measure of PH
+int val_bot_right [N]= {0}; //instantaneous value of measure of PH
+
+double sum_top_left= 0;  //sumatory of measures of PH
+double sum_top_right= 0; //sumatory of measures of PH
+double sum_bot_left= 0;  //sumatory of measures of PH
+double sum_bot_right= 0; //sumatory of measures of PH
+
+double sum_var_top_left=0;
+double sum_var_top_right=0;
+double sum_var_bot_left=0;
+double sum_var_bot_right=0;
+
+int val_var_top_left=0;     //Variance Value for this PH
+int val_var_top_right=0;    //Variance Value for this PH
+int val_var_bot_left=0;     //Variance Value for this PH
+int val_var_bot_right=0;    //Variance Value for this PH
+
+
+
+
 
 //DigitalOut led(LED1);
 
@@ -65,6 +100,73 @@
             stepper2.runToPosition();//Gives the order to go to that target position
             wait_ms(1500);
         }
+        pos2=rint(micro_steps2*(i-60));
+        stepper2.moveTo(pos2);//moveTo Sets target position
+        stepper2.runToPosition();//Gives the order to go to that target position        
+        wait_ms(1000);
+        
+        sum_top_left=0;
+        sum_top_right=0;
+        sum_bot_left=0;
+        sum_bot_right=0;
+        
+        sum_var_top_left=0;
+        sum_var_top_right=0;
+        sum_var_bot_left=0;
+        sum_var_bot_right=0;
+        
+        while(j<N)
+         {
+            val_top_left[j]=top_left.read_u16();    //Taking N measures 
+            val_top_right[j]=top_right.read_u16();  //Taking N measures 
+            val_bot_left[j]=bot_left.read_u16();    //Taking N measures     
+            val_bot_right[j]=bot_right.read_u16();  //Taking N measures 
+
+            
+            
+            sum_top_left = sum_top_left + val_top_left[j];      //Making the sum of the measure for each PH
+            sum_top_right = sum_top_right + val_top_right[j];   //Making the sum of the measure for each PH
+            sum_bot_left = sum_bot_left + val_bot_left[j];      //Making the sum of the measure for each PH
+            sum_bot_right = sum_bot_right + val_bot_right[j];   //Making the sum of the measure for each PH
+
+            j++;
+
+         }        
+        
+         mitja_esquerra_adalt=double(rint((sum_top_left/N))); //Computiong the average
+         mitja_dreta_adalt=double(rint((sum_top_right/N)));   //Computiong the average
+         mitja_esquerra_abaix=double(rint((sum_bot_left/N))); //Computiong the average
+         mitja_dreta_abaix=double(rint((sum_bot_right/N)));   //Computiong the average
+           
+         for (j=0;j<N;j++)
+         {
+            sum_var_top_left= ((val_top_left[j]-mitja_esquerra_adalt)*(val_top_left[j]-mitja_esquerra_adalt)) + sum_var_top_left;   //Variance for each measure
+            sum_var_top_right= ((val_top_right[j]-mitja_dreta_adalt)*(val_top_right[j]-mitja_dreta_adalt)) + sum_var_top_right;     //Variance for each measure
+            sum_var_bot_left= ((val_bot_left[j]-mitja_esquerra_abaix)*(val_bot_left[j]-mitja_esquerra_abaix)) + sum_var_bot_left;   //Variance for each measure 
+            sum_var_bot_right= ((val_bot_right[j]-mitja_dreta_abaix)*(val_bot_right[j]-mitja_dreta_abaix)) + sum_var_bot_right;     //Variance for each measure
+
+         }
+         val_var_top_left=double(rint(sqrt(sum_var_top_left/N)));    //Obtaining Variance Value for this PH
+         val_var_top_right=double(rint(sqrt(sum_var_top_right/N)));  //Obtaining Variance Value for this PH
+         val_var_bot_left=double(rint(sqrt(sum_var_bot_left/N)));    //Obtaining Variance Value for this PH    
+         val_var_bot_right=double(rint(sqrt(sum_var_bot_right/N)));  //Obtaining Variance Value for this PH
+         
+         
+         //QUEDA PRINTEJAR RESULTATS
+         
+         if(i==121)
+         {
+            i=0;
+            stepper2.moveTo (-60*micro_steps2);
+            stepper2.runToPosition();
+  
+         }
+         else if(i!=121)
+         { 
+           i++;
+         }
+         /*
+        
         // Read the analog input value
         measTL = top_left.read_u16();
         measTR = top_right.read_u16();
@@ -75,6 +177,9 @@
         pc.printf("TOP_LEFT == %d || TOP_RIGHT == %d || BOT_LEFT == %d || BOT_RIGHT == %d ", measTL, measTR , measBL, measBR);
 
         wait_ms(800); // 800 ms
+        */
+    
+    
     }
 }