Sweep a servo according to Proximity sensor measure

Dependencies:   Servo X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1 by ST

Revision:
10:5319abadb31e
Parent:
9:1d0e839edee8
Child:
11:657dbe7bf245
--- a/main.cpp	Tue Oct 13 14:26:18 2015 +0200
+++ b/main.cpp	Mon Oct 19 13:57:36 2015 +0200
@@ -6,10 +6,29 @@
 #include <stdio.h>
 #include <assert.h>
 
-
 #define VL6180X_I2C_SDA I2C_SDA
 #define VL6180X_I2C_SCL I2C_SCL
 
+//#define RANGE_SINGLE_SHOT_POLLING
+//#define ALS_SINGLE_SHOT_POLLING
+//#define RANGE_CONTINUOUS_POLLING
+//#define ALS_CONTINUOUS_POLLING
+//#define RANGE_CONTINUOUS_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
+//#define ALS_CONTINUOUS_POLLING_LOW_THRESHOLD
+//#define ALS_CONTINUOUS_POLLING_HIGH_THRESHOLD
+//#define ALS_CONTINUOUS_POLLING_OUT_OF_WINDOW
+//#define RANGE_CONTINUOUS_INTERRUPT_LOW_THRESHOLD
+//#define RANGE_CONTINUOUS_INTERRUPT_HIGH_THRESHOLD
+//#define RANGE_CONTINUOUS_INTERRUPT_OUT_OF_WINDOW	
+//#define ALS_CONTINUOUS_INTERRUPT_LOW_THRESHOLD
+//#define ALS_CONTINUOUS_INTERRUPT_HIGH_THRESHOLD
+//#define ALS_CONTINUOUS_INTERRUPT_OUT_OF_WINDOW
+
 /* 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 */
 
@@ -24,10 +43,13 @@
 /* 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();
 }	
 
@@ -37,119 +59,177 @@
 {   
 	 int status;
 	 
-	 status=board->InitBoard();
+   status=board->InitBoard();
 	 if(status)
 		  printf("Failed to init the board!\n\r");
-	 
-	 /*	 
-	 status=board->sensor_top->StartMeasurement(range_single_shot_polling, NULL, &Data);
+
+#ifdef RANGE_SINGLE_SHOT_POLLING	 
+	 status=board->sensor_top->StartMeasurement(range_single_shot_polling, NULL, &Data, NULL, NULL);
 	 if(!status)
-	    printf("Range single shot: %d\n\r",Data.range_mm);
-	 else
+	    printf("Range single shot: %dmm\n\r",Data.range_mm);
+	 else if(status==INVALID_PARAMS)
 		  printf("Failed to start measurement!\n\r");
-	 status=board->sensor_top->StartMeasurement(als_single_shot_polling, NULL, &Data);
-	 if(!status)
-	    printf("Light single shot: %d\n\r",Data.lux);
 	 else
-		  printf("Failed to start measurement!\n\r");
+		  printf("Invalid range value!\n\r");
+	 status=board->sensor_top->StopMeasurement(range_single_shot_polling);
+	 if(status)
+		  printf("Failed to stop measurement!\n\r");
+#endif
 	 
-	 status=board->sensor_top->StartMeasurement(range_continuous_polling, NULL, &Data);
+#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);
+	 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)
+		  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=board->sensor_top->GetRangeMeasContinuousMode(&Data);
-	    if(!status)
-				 printf("1a Range: %d\n\r",Data.range_mm);
-	    else
-	       printf("Invalid range value!\n\r");
-			status=board->sensor_top->GetRangeMeasContinuousMode(&Data);
-	    if(!status)
-				 printf("2a Range: %d\n\r",Data.range_mm);
-	    else
-	       printf("Invalid range value!\n\r");
-			status=board->sensor_top->GetRangeMeasContinuousMode(&Data);
-	    if(!status)
-				 printf("3a Range: %d\n\r",Data.range_mm);
-	    else
-	       printf("Invalid range value!\n\r");
+		  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);
+	       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)
+		  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)
+	 {
+	    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);
+	       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)
+		  printf("Failed to stop measurement!\n\r");
+#endif
 	 
-	 status=board->sensor_top->StartMeasurement(als_continuous_polling, NULL, &Data);
+#ifdef RANGE_CONTINUOUS_INTERRUPT
+	 status=board->sensor_top->StartMeasurement(range_continuous_interrupt, SensorTopIRQ, &Data, NULL, NULL);
 	 if(!status)
 	 {
-	    status=board->sensor_top->GetAlsMeasContinuousMode(&Data);
-	    if(!status)
-				 printf("1a Light: %d\n\r",Data.lux);
-	    else
-	       printf("Invalid light value!\n\r");
-			status=board->sensor_top->GetAlsMeasContinuousMode(&Data);
-	    if(!status)
-				 printf("2a Light: %d\n\r",Data.lux);
-	    else
-	       printf("Invalid light value!\n\r");
-			status=board->sensor_top->GetAlsMeasContinuousMode(&Data);
-	    if(!status)
-				 printf("3a Light: %d\n\r",Data.lux);
-	    else
-	       printf("Invalid light value!\n\r");
-   }
+	    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);
+			    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)
+	 {
+	    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);
+			    else
+				    printf("Invalid light value!\n\r");
+	      }
+	    }
+	 }
 	 else
-	    printf("Failed to start measurement!\n\r");
-	 */
-	 
-	 /*board->sensor_top->StartMeasurement(range_continuous_interrupt, SensorTopIRQ, &Data);
-	 while(1)
-	 {
-	   if(flag_sensor_top)
-     {
- 		   flag_sensor_top=false;
-		   board->sensor_top->HandleIRQ(range_measure, &Data);
-			 if((Data.range_error==0)&&(Data.int_error==0))
-				 printf("Range int: %d\n\r",Data.range_mm);
-			 else
-				 printf("Invalid range value!\n\r");
-	   }
-	 }*/
+		  printf("Failed to stop measurement!\n\r");
+	 status=board->sensor_top->StopMeasurement(als_continuous_interrupt);
+	 if(status)
+		  printf("Failed to stop measurement!\n\r");
+#endif
 	 
-	 /*board->sensor_top->StartMeasurement(als_continuous_interrupt, SensorTopIRQ, &Data);
-   while(1)
-	 {
-	   if(flag_sensor_top)
-     {
- 		   flag_sensor_top=false;
-		   board->sensor_top->HandleIRQ(light_measure, &Data);
-	     if((Data.als_error==0)&&(Data.int_error==0))
-				 printf("Light int: %d\n\r",Data.lux);
-			 else
-				 printf("Invalid light value!\n\r");
-	   }
-	 }*/
-	 
-	 
-	 board->sensor_top->StartMeasurement(interleaved_mode, SensorTopIRQ, &Data);
-	 while(1)
+#ifdef INTERLEAVED_MODE_INTERRUPT	 
+	 status=board->sensor_top->StartMeasurement(interleaved_mode_interrupt, SensorTopIRQ, &Data, NULL, NULL);
+	 if(!status)
 	 {
-	   if(flag_sensor_top)
-     {
- 		   flag_sensor_top=false;
-		   board->sensor_top->HandleIRQ(light_range_measure, &Data);
-			 if((Data.range_error==0)&&(Data.int_error==0))
-				 printf("Interleaved_mode->Range: %d\n\r",Data.range_mm);
-			 else
-				 printf("Invalid range value!\n\r");
-			 if((Data.als_error==0)&&(Data.int_error==0))
-				 printf("Interleaved_mode->Light: %d\n\r",Data.lux);
-			 else
-				 printf("Invalid light value!\n\r");
-	   }
+	    while(1)
+	    {
+	      if(flag_sensor_top)
+        {
+ 		      flag_sensor_top=false;
+		      status=board->sensor_top->HandleIRQ(interleaved_mode_interrupt, &Data);
+			    if(!status)
+					{
+				    printf("Interleaved_mode->Range: %dmm\n\r",Data.range_mm);
+						printf("Interleaved_mode->Light: %dlux\n\r",Data.lux);
+					}
+			    else
+				    printf("Invalid range or light value!\n\r");
+	      }
+		 }
 	 }
-	 
-	 
-	 
-	 //board->~X_NUCLEO_6180XA1();
+	 else
+	    printf("Failed to stop measurement!\n\r");
+	 status=board->sensor_top->StopMeasurement(interleaved_mode_interrupt);
+	 if(status)
+		  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)
+	 {
+	    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);
+			    else
+				    printf("Invalid range value!\n\r");
+	      }
+	    }
+	 }
+	 else 
+		  printf("Failed to start measurement!\n\r");
+	 status=board->sensor_top->StopMeasurement(range_continuous_interrupt_low_threshold);
+	 if(status)
+		  printf("Failed to stop measurement!\n\r");
+#endif
 
 }
 
 
-