Sweep a servo according to Proximity sensor measure

Dependencies:   Servo X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1 by ST

main.cpp

Committer:
gallonm
Date:
2015-10-19
Revision:
10:5319abadb31e
Parent:
9:1d0e839edee8
Child:
11:657dbe7bf245

File content as of revision 10:5319abadb31e:

#include "mbed.h"
#include "x_nucleo_6180xa1.h"

#include <string.h>
#include <stdlib.h>
#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 */

void OnErrLog(void){};
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;

/* 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;	
	 board->sensor_top->DisableInterruptMeasureDetectionIRQ();
}	

//inserire le callback per gli altri sensori

int main()
{   
	 int status;
	 
   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);
	 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)
		  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);
	 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)
	 {
		  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
	 
#ifdef RANGE_CONTINUOUS_INTERRUPT
	 status=board->sensor_top->StartMeasurement(range_continuous_interrupt, SensorTopIRQ, &Data, NULL, NULL);
	 if(!status)
	 {
	    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 stop measurement!\n\r");
	 status=board->sensor_top->StopMeasurement(als_continuous_interrupt);
	 if(status)
		  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)
	 {
	    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");
	      }
		 }
	 }
	 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

}