Sweep a servo according to Proximity sensor measure

Dependencies:   Servo X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1 by ST

Revision:
11:657dbe7bf245
Parent:
10:5319abadb31e
Child:
14:946e62f44f4f
--- a/main.cpp	Mon Oct 19 13:57:36 2015 +0200
+++ b/main.cpp	Tue Oct 20 16:46:29 2015 +0200
@@ -14,8 +14,8 @@
 //#define RANGE_CONTINUOUS_POLLING
 //#define ALS_CONTINUOUS_POLLING
 //#define RANGE_CONTINUOUS_INTERRUPT
-//#define ALS_CONTINUOUS_INTERRUPT
-#define INTERLEAVED_MODE_INTERRUPT
+#define ALS_CONTINUOUS_INTERRUPT
+//#define INTERLEAVED_MODE_INTERRUPT
 //#define RANGE_CONTINUOUS_POLLING_LOW_THRESHOLD
 //#define RANGE_CONTINUOUS_POLLING_HIGH_THRESHOLD
 //#define RANGE_CONTINUOUS_POLLING_OUT_OF_WINDOW
@@ -32,117 +32,143 @@
 /* Polling operating modes (1,2,3 and 4) don`t require callback function that handles IRQ 
    Callback functions are used only for measure that require interrupt */
 
-void OnErrLog(void){};
+void OnErrLog(void){}; // sistemare la funzione per stampare il messaggio che gli viene passato
 void DISP_ExecLoopBody(void){};
 
 DevI2C device_i2c(VL6180X_I2C_SDA, VL6180X_I2C_SCL);
 static X_NUCLEO_6180XA1 *board=X_NUCLEO_6180XA1::Instance(&device_i2c);
 
-MeasureData_t Data;
+MeasureData_t data_sensor_top, data_sensor_left, data_sensor_right;
 
+Timer timer; // timer to debug
+int start, end;	
+	
 /* flags that handle the call to HandleIRQ function */
 bool flag_sensor_top=false, flag_sensor_bottom=false, flag_sensor_left=false, flag_sensor_right=false;	
 
-//Timer timer;
-//int start, end;	
-	
 /* callback functions of the sensors */ 
 void SensorTopIRQ(void)
 {
-   flag_sensor_top=true;	
+   flag_sensor_top=true;
 	 board->sensor_top->DisableInterruptMeasureDetectionIRQ();
 }	
 
-//inserire le callback per gli altri sensori
+void SensorBottomIRQ(void)
+{
+   flag_sensor_bottom=true;	
+	 board->sensor_bottom->DisableInterruptMeasureDetectionIRQ();
+}	
+
+void SensorLeftIRQ(void)
+{
+   flag_sensor_left=true;	
+	 board->sensor_left->DisableInterruptMeasureDetectionIRQ();
+}
+
+void SensorRightIRQ(void)
+{
+   flag_sensor_right=true;	
+	 board->sensor_right->DisableInterruptMeasureDetectionIRQ();
+}
 
 int main()
 {   
-	 int status;
-	 
+	 int status, status_t, status_b, status_l, status_r;
+	
    status=board->InitBoard();
 	 if(status)
 		  printf("Failed to init the board!\n\r");
 
 #ifdef RANGE_SINGLE_SHOT_POLLING	 
-	 status=board->sensor_top->StartMeasurement(range_single_shot_polling, NULL, &Data, NULL, NULL);
-	 if(!status)
-	    printf("Range single shot: %dmm\n\r",Data.range_mm);
+	 status_t=board->sensor_top->StartMeasurement(range_single_shot_polling, NULL, &data_sensor_top, NULL, NULL);
+	 if(!status_t)
+	    printf("Top Range: %dmm\n\r",data_sensor_top.range_mm);
 	 else if(status==INVALID_PARAMS)
 		  printf("Failed to start measurement!\n\r");
 	 else
 		  printf("Invalid range value!\n\r");
-	 status=board->sensor_top->StopMeasurement(range_single_shot_polling);
-	 if(status)
+	 status_t=board->sensor_top->StopMeasurement(range_single_shot_polling);
+	 if(status_t)
 		  printf("Failed to stop measurement!\n\r");
 #endif
 	 
 #ifdef ALS_SINGLE_SHOT_POLLING
-	 status=board->sensor_top->StartMeasurement(als_single_shot_polling, NULL, &Data, NULL, NULL);
-	 if(!status)
-	    printf("Light single shot: %dlux\n\r",Data.lux);
+	 status_t=board->sensor_top->StartMeasurement(als_single_shot_polling, NULL, &data_sensor_top, NULL, NULL);
+	 if(!status_t)
+	    printf("Top Light: %dlux\n\r",data_sensor_top.lux);
 	 else if(status==INVALID_PARAMS)
 		  printf("Failed to start measurement!\n\r");
 	 else
 		  printf("Invalid light value!\n\r");
-	 status=board->sensor_top->StopMeasurement(als_single_shot_polling);
-	 if(status)
+	 status_t=board->sensor_top->StopMeasurement(als_single_shot_polling);
+	 if(status_t)
 		  printf("Failed to stop measurement!\n\r");
 #endif
 
 #ifdef RANGE_CONTINUOUS_POLLING
-	 status=board->sensor_top->StartMeasurement(range_continuous_polling, NULL, &Data, NULL, NULL);
-	 if(!status)
+	 status_t=board->sensor_top->StartMeasurement(range_continuous_polling, NULL, &data_sensor_top, NULL, NULL);
+	 if(!status_t)
 	 {
 		  int i;
 		  for(i=0;i<10;i++)
 		  {
-				 status=board->sensor_top->GetMeasurement(range_continuous_polling, &Data);
-	       if(!status)
-				    printf("Range measure %d: %dmm\n\r",i+1,Data.range_mm);
+				 status_t=board->sensor_top->GetMeasurement(range_continuous_polling, &data_sensor_top);
+	       if(!status_t)
+				    printf("Top Range measure %d: %dmm\n\r",i+1,data_sensor_top.range_mm);
 	       else
 	          printf("Invalid range value!\n\r");
 			}
    }
 	 else
 	    printf("Failed to start measurement!\n\r");
-	 status=board->sensor_top->StopMeasurement(range_continuous_polling);
-	 if(status)
+	 status_t=board->sensor_top->StopMeasurement(range_continuous_polling);
+	 if(status_t)
 		  printf("Failed to stop measurement!\n\r");
 #endif 
 	
 #ifdef ALS_CONTINUOUS_POLLING	 
-	 status=board->sensor_top->StartMeasurement(als_continuous_polling, NULL, &Data, NULL, NULL);
-	 if(!status)
+	 status_t=board->sensor_top->StartMeasurement(als_continuous_polling, NULL, &data_sensor_top, NULL, NULL);
+	 if(!status_t)
 	 {
 	    int i;
 		  for(i=0;i<10;i++)
 		  {
-				 status=board->sensor_top->GetMeasurement(als_continuous_polling, &Data);
-	       if(!status)
-				    printf("Light measure %d: %dlux\n\r",i,Data.lux);
+				 status=board->sensor_top->GetMeasurement(als_continuous_polling, &data_sensor_top);
+	       if(!status_t)
+				    printf("Top Light measure %d: %dlux\n\r",i,data_sensor_top.lux);
 	       else
 	          printf("Invalid light value!\n\r");
 			}
    }
 	 else
 	    printf("Failed to start measurement!\n\r");
-	 status=board->sensor_top->StopMeasurement(als_continuous_polling);
-	 if(status)
+	 status_t=board->sensor_top->StopMeasurement(als_continuous_polling);
+	 if(status_t)
 		  printf("Failed to stop measurement!\n\r");
 #endif
 	 
-#ifdef RANGE_CONTINUOUS_INTERRUPT
-	 status=board->sensor_top->StartMeasurement(range_continuous_interrupt, SensorTopIRQ, &Data, NULL, NULL);
-	 if(!status)
+/*#ifdef RANGE_CONTINUOUS_INTERRUPT
+	 status_t=board->sensor_top->StartMeasurement(range_continuous_interrupt, SensorTopIRQ, &data_sensor_top, NULL, NULL);
+	 status_l=board->sensor_left->StartMeasurement(range_continuous_interrupt, SensorLeftIRQ, &data_sensor_left, NULL, NULL);
+	 if((!status_t)&&(!status_l))
 	 {
-	    while(1)
+		  while(1)
 	    {
 	      if(flag_sensor_top)
         {
  		      flag_sensor_top=false;
-		      status=board->sensor_top->HandleIRQ(range_continuous_interrupt, &Data);
-			    if(!status)
-				    printf("Range int: %dmm\n\r",Data.range_mm);
+		      status_t=board->sensor_top->HandleIRQ(range_continuous_interrupt, &data_sensor_top);
+			    if(!status_t)
+				    printf("Top Range: %dmm\n\r",data_sensor_top.range_mm);
+			    else
+				    printf("Invalid range value!\n\r");
+	      }
+				if(flag_sensor_left)
+        {
+ 		      flag_sensor_left=false;
+		      status_l=board->sensor_left->HandleIRQ(range_continuous_interrupt, &data_sensor_left);
+			    if(!status_l)
+				    printf("Left Range: %dmm\n\r",data_sensor_left.range_mm);
 			    else
 				    printf("Invalid range value!\n\r");
 	      }
@@ -150,23 +176,50 @@
 	 }
 	 else 
 		  printf("Failed to start measurement!\n\r");
-	 status=board->sensor_top->StopMeasurement(range_continuous_interrupt);
-	 if(status)
-		  printf("Failed to stop measurement!\n\r");
+	 status_t=board->sensor_top->StopMeasurement(range_continuous_interrupt);
+	 if(status_l)
+		  printf("Failed to stop sensor_top measurement!\n\r");
+	 status_l=board->sensor_left->StopMeasurement(range_continuous_interrupt);
+	 if(status_l)
+		  printf("Failed to stop sensor_left measurement!\n\r");
+#endif*/
+
+#ifdef RANGE_CONTINUOUS_INTERRUPT
+     status=board->sensor_top->StartMeasurement(range_continuous_interrupt, SensorTopIRQ, &data_sensor_top, NULL, NULL);
+     if(!status)
+     {
+        while(1)
+        {
+          if(flag_sensor_top)
+          {
+             flag_sensor_top=false;
+             status=board->sensor_top->HandleIRQ(range_continuous_interrupt, &data_sensor_top);
+             if(!status)
+                printf("Range: %dmm\n\r",data_sensor_top.range_mm);
+             else
+                printf("Invalid range value!\n\r");
+          }
+        }
+     }
+     else 
+        printf("Failed to start measurement!\n\r");
+     status=board->sensor_top->StopMeasurement(range_continuous_interrupt);
+     if(status)
+          printf("Failed to stop measurement!\n\r");
 #endif
 
 #ifdef ALS_CONTINUOUS_INTERRUPT
-	 status=board->sensor_top->StartMeasurement(als_continuous_interrupt, SensorTopIRQ, &Data, NULL, NULL);
-   if(!status)
+	 status_t=board->sensor_top->StartMeasurement(als_continuous_interrupt, SensorTopIRQ, &data_sensor_top, NULL, NULL);
+   if(!status_t)
 	 {
 	    while(1)
 	    {
 	      if(flag_sensor_top)
         {
  		      flag_sensor_top=false;
-		      status=board->sensor_top->HandleIRQ(als_continuous_interrupt, &Data);
-	        if(!status)
-				    printf("Light int: %dlux\n\r",Data.lux);
+		      status_t=board->sensor_top->HandleIRQ(als_continuous_interrupt, &data_sensor_top);
+	        if(!status_t)
+				    printf("Top Light: %dlux\n\r",data_sensor_top.lux);
 			    else
 				    printf("Invalid light value!\n\r");
 	      }
@@ -174,25 +227,25 @@
 	 }
 	 else
 		  printf("Failed to stop measurement!\n\r");
-	 status=board->sensor_top->StopMeasurement(als_continuous_interrupt);
-	 if(status)
+	 status_t=board->sensor_top->StopMeasurement(als_continuous_interrupt);
+	 if(status_t)
 		  printf("Failed to stop measurement!\n\r");
 #endif
 	 
 #ifdef INTERLEAVED_MODE_INTERRUPT	 
-	 status=board->sensor_top->StartMeasurement(interleaved_mode_interrupt, SensorTopIRQ, &Data, NULL, NULL);
-	 if(!status)
+	 status_t=board->sensor_top->StartMeasurement(interleaved_mode_interrupt, SensorTopIRQ, &data_sensor_top, NULL, NULL);
+	 if(!status_t)
 	 {
 	    while(1)
 	    {
 	      if(flag_sensor_top)
         {
  		      flag_sensor_top=false;
-		      status=board->sensor_top->HandleIRQ(interleaved_mode_interrupt, &Data);
-			    if(!status)
+		      status_t=board->sensor_top->HandleIRQ(interleaved_mode_interrupt, &data_sensor_top);
+			    if(!status_t)
 					{
-				    printf("Interleaved_mode->Range: %dmm\n\r",Data.range_mm);
-						printf("Interleaved_mode->Light: %dlux\n\r",Data.lux);
+				    printf("Top Range: %dmm     ",data_sensor_top.range_mm);
+						printf("Top Light: %dlux\n\r",data_sensor_top.lux);
 					}
 			    else
 				    printf("Invalid range or light value!\n\r");
@@ -201,23 +254,23 @@
 	 }
 	 else
 	    printf("Failed to stop measurement!\n\r");
-	 status=board->sensor_top->StopMeasurement(interleaved_mode_interrupt);
-	 if(status)
+	 status_t=board->sensor_top->StopMeasurement(interleaved_mode_interrupt);
+	 if(status_t)
 		  printf("Failed to stop measurement!\n\r"); 
 #endif
 
 #ifdef RANGE_CONTINUOUS_INTERRUPT_LOW_THRESHOLD
-	 status=board->sensor_top->StartMeasurement(range_continuous_interrupt_low_threshold, SensorTopIRQ, &Data, 80, NULL);
-	 if(!status)
+	 status_t=board->sensor_top->StartMeasurement(range_continuous_interrupt_low_threshold, SensorTopIRQ, &data_sensor_top, 80, NULL);
+	 if(!status_t)
 	 {
 	    while(1)
 	    {
 	      if(flag_sensor_top)
         {
  		      flag_sensor_top=false;
-		      status=board->sensor_top->HandleIRQ(range_continuous_interrupt_low_threshold, &Data);
-			    if(!status)
-				    printf("Range int low threshold: %dmm\n\r",Data.range_mm);
+		      status_t=board->sensor_top->HandleIRQ(range_continuous_interrupt_low_threshold, &data_sensor_top);
+			    if(!status_t)
+				    printf("Range int low threshold: %dmm\n\r",data_sensor_top.range_mm);
 			    else
 				    printf("Invalid range value!\n\r");
 	      }
@@ -225,11 +278,48 @@
 	 }
 	 else 
 		  printf("Failed to start measurement!\n\r");
-	 status=board->sensor_top->StopMeasurement(range_continuous_interrupt_low_threshold);
-	 if(status)
+	 status_t=board->sensor_top->StopMeasurement(range_continuous_interrupt_low_threshold);
+	 if(status_t)
 		  printf("Failed to stop measurement!\n\r");
 #endif
 
+/*
+status_l=board->sensor_left->StartMeasurement(range_continuous_interrupt, SensorLeftIRQ, &data_sensor_left, NULL, NULL);	
+status_r=board->sensor_right->StartMeasurement(als_continuous_interrupt, SensorRightIRQ, &data_sensor_right, NULL, NULL);
+if((!status_l)&&(!status_r))
+{
+   while(1)
+	 {
+	   if(flag_sensor_left)
+     {
+ 		   flag_sensor_left=false;
+		   status_l=board->sensor_left->HandleIRQ(range_continuous_interrupt, &data_sensor_left);
+			 if(!status_l)
+				 printf("Left Range: %dmm\n\r",data_sensor_left.range_mm);
+			 else
+				 printf("Invalid range value!\n\r");
+	   }
+		 if(flag_sensor_right)
+     {
+ 		   flag_sensor_right=false;
+		   status_r=board->sensor_right->HandleIRQ(als_continuous_interrupt, &data_sensor_right);
+			 if(!status_r)
+				 printf("Right Light: %dlux\n\r",data_sensor_right.lux);
+			 else
+				 printf("Invalid light value!\n\r");
+		 }
+	 }
+}
+else 
+	 printf("Failed to start measurement!\n\r");
+status_t=board->sensor_top->StopMeasurement(range_continuous_interrupt_low_threshold);
+if(status_t)
+	 printf("Failed to stop measurement!\n\r");
+status_t=board->sensor_top->StopMeasurement(als_continuous_interrupt);
+if(status_t)
+	 printf("Failed to stop measurement!\n\r");
+*/ 
+
 }