VL53L1CB expansion shield autonomous interrupt example.

Dependencies:   X_NUCLEO_53L1A2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * This VL53L1X Expansion board test application performs range measurements
00003  * using the onboard embedded centre sensor and two satelites, in autonomous, interrupt mode.
00004  * Measured ranges are ouput on the Serial Port, running at 115200 baud.
00005  *
00006  * This is designed to work with MBed V2 , MBed V5 and MBed V6.
00007  *
00008  *  The Reset button can be used to restart the program.
00009  *
00010  * *** Note : 
00011  * Default Mbed build system settings disable print floating-point support.
00012  * Offline builds can enable this, again.
00013  * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md
00014  * .\mbed-os\platform\mbed_lib.json
00015  *
00016  */
00017  
00018 #include <stdio.h>
00019 #include <time.h>
00020 
00021 #include "mbed.h"
00022 
00023 #include "XNucleo53L1A2.h"
00024 #include "ToF_I2C.h"
00025 
00026 // i2c comms port pins
00027 #define I2C_SDA   D14 
00028 #define I2C_SCL   D15  
00029 
00030 
00031 #define NUM_SENSORS 3
00032 
00033 // define the interrupt pins
00034 PinName CentreIntPin = A2;
00035 // the satellite pins depend on solder blobs on the back of the shield.
00036 // they may not exist or may be one of two sets.
00037 // the centre pin always exists
00038 PinName LeftIntPin = D9;
00039 PinName RightIntPin = D4;
00040 // alternate set
00041 //PinName LeftIntPin = D8;
00042 //PinName RightIntPin = D2;
00043 
00044 
00045 
00046 static XNucleo53L1A2 *board=NULL;
00047 
00048 #if (MBED_VERSION  > 60300) 
00049     UnbufferedSerial  pc(USBTX, USBRX);
00050     extern "C" void wait_ms(int ms);
00051 #else
00052     Serial pc(SERIAL_TX, SERIAL_RX); 
00053 #endif
00054 
00055 void print_results(int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
00056 
00057 
00058 VL53L1_Dev_t devCentre;
00059 VL53L1_Dev_t devLeft;
00060 VL53L1_Dev_t devRight;
00061 VL53L1_DEV Dev = &devCentre;
00062 VL53L1 *Sensor;
00063 
00064 
00065  
00066 /* flags that handle interrupt request for sensor and user blue button*/
00067 volatile bool int_sensor = false;
00068 volatile bool int_stop = false;
00069 
00070 /* ISR callback function of the centre sensor */
00071 void sensor_irq(void)
00072 {
00073     int_sensor = true;
00074     board->sensor_centre->disable_interrupt_measure_detection_irq();
00075 }
00076 
00077 /* Start the sensor ranging */
00078 int init_sensor()
00079 {
00080     int status = 0;
00081     
00082     Dev=&devCentre;
00083     Sensor=board->sensor_centre;
00084     
00085     // configure the sensors
00086     Dev->comms_speed_khz = 400;    
00087     Dev->comms_type = 1;
00088     Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
00089     
00090     printf("configuring centre channel \n");                    
00091     
00092     /* Device Initialization and setting */  
00093     status = Sensor->vl53L1_DataInit();
00094     status = Sensor->vl53L1_StaticInit();
00095     status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_AUTONOMOUS);
00096     status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG);
00097     status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds( 200 * 1000); 
00098 
00099    
00100     // set the ranging and signal rate filter
00101     VL53L1_DetectionConfig_t thresholdconfig;
00102 
00103     thresholdconfig.DetectionMode = VL53L1_DETECTION_DISTANCE_ONLY; /// type VL53L1_DetectionMode in vl53l1_def.h
00104     thresholdconfig.Distance.CrossMode = VL53L1_THRESHOLD_IN_WINDOW; // type VL53L1_ThresholdMode. ignore if distance outside high and low 
00105     thresholdconfig.Distance.High = 300;  // high distance in mm
00106     thresholdconfig.Distance.Low = 200;   // low distance in mm
00107     thresholdconfig.Rate.CrossMode=0;  // type VL53L1_ThresholdMode VL53L1_THRESHOLD_CROSSED_LOW VL53L1_THRESHOLD_CROSSED_HIGH VL53L1_THRESHOLD_OUT_OF_WINDOW VL53L1_THRESHOLD_IN_WINDOW 
00108     thresholdconfig.Rate.High = 0;
00109     thresholdconfig.Rate.Low = 0;
00110     thresholdconfig.IntrNoTarget = 0 ;// if 1 produce an interrupt even if there is no target found e.g out of range 
00111  
00112     status = Sensor->vl53L1_SetThresholdConfig(&thresholdconfig);
00113         
00114     // create interrupt handler and start measurements
00115     if (board->sensor_centre!= NULL) {        
00116         status = board->sensor_centre->stop_measurement();
00117         if (status != 0) {
00118             return status;
00119         }
00120 
00121         status = board->sensor_centre->start_measurement(&sensor_irq);
00122         if (status != 0) {
00123             return status;
00124         }
00125     }
00126     return status;
00127 }
00128 
00129 /* ISR callback function of the user blue button to switch measuring sensor. */
00130 void measuring_stop_irq(void)
00131 {
00132     int_stop = true;
00133 }
00134 
00135 /*=================================== Main ==================================
00136 =============================================================================*/
00137 int main()
00138 {   
00139 #if (MBED_VERSION  < 60000)
00140     #if USER_BUTTON==PC_13  // we are cross compiling for Nucleo-f401
00141     InterruptIn stop_button(USER_BUTTON);
00142     stop_button.rise(&measuring_stop_irq);
00143     #endif
00144 #endif
00145     int status;
00146     uint16_t distance = 0;
00147         
00148     pc.baud(115200);  // baud rate is important as printf statements take a lot of time
00149     
00150     printf("Autonomous Interrupt, mbed = %d \r\n",MBED_VERSION);
00151 
00152 // create i2c interface
00153     ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
00154     /* creates the 53L1A2 expansion board singleton obj */
00155     board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin);
00156     
00157     dev_I2C->frequency(400000); //also needs doing in spi_interface.c
00158         
00159     printf("board created!\r\n");
00160 
00161     /* init the 53L1A1 expansion board with default values */
00162     status = board->init_board();
00163     if (status) {
00164         printf("Failed to init board!\r\n");
00165         return 0;
00166     }
00167                
00168     printf("board initiated! - %d\r\n", status);
00169     
00170     /* init an array with chars to id the sensors */
00171     status = init_sensor();
00172     if (status != 0) {
00173         printf("Failed to init sensors!\r\n");
00174         return status;
00175     }
00176 
00177     printf("loop forever\n"); 
00178 
00179     // loop waiting for interrupts to happen. This is signaled by int_centre_result,int_left_result or int_right_result
00180     // being non zero. When the interrupts clear this is signaled by int_centre_dropped,int_left_dropped and int_right_dropped. 
00181     // These are set back to zero when processing is completed
00182 /*    while (1)
00183     {
00184         VL53L1_MultiRangingData_t MultiRangingData;
00185         VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData;   
00186 
00187         if ( int_left_dropped  || int_centre_dropped  || int_right_dropped )
00188             wait_ms(30);  
00189             
00190         // when the interrupt pin goes loww start new measurement
00191         if (int_centre_dropped != 0)
00192         {
00193             int_centre_dropped = 0;
00194             status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement();
00195         }
00196                           
00197         if (int_centre_result != 0)
00198         {
00199             status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData);
00200             if (status == 0)
00201             {
00202                 print_results( devCentre.i2c_slave_address, pMultiRangingData );
00203             }
00204                 
00205             // clear interrupt flag
00206             int_centre_result = 0;
00207 
00208         }
00209         wait_ms( 1 * 10);                          
00210     }
00211 */
00212 
00213     VL53L1_MultiRangingData_t MultiRangingData;
00214     VL53L1_MultiRangingData_t *pMultiRangingData = NULL;   
00215     
00216     while (true) {
00217         pMultiRangingData = &MultiRangingData;   
00218         
00219         if (int_sensor) {
00220             int_sensor = false;
00221 /*            
00222 //            status = board->sensor_centre->handle_irq(&distance);
00223 //            status = get_measurement(distance);
00224 //            status = board->sensor_centre->VL53L1_GetDistance(&distance);
00225             status = board->sensor_centre->VL53L1_ClearInterrupt();
00226             board->sensor_centre->enable_interrupt_measure_detection_irq();
00227             printf("distance: %d\r\n", distance);
00228 */            
00229             status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData);
00230             if (status == 0)
00231             {
00232                 print_results( devCentre.i2c_slave_address, pMultiRangingData );
00233             }
00234             status = board->sensor_centre->VL53L1_ClearInterrupt();
00235 //            status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement(); // doesnt run!
00236             board->sensor_centre->enable_interrupt_measure_detection_irq();
00237         }
00238 
00239         if (int_stop) {
00240             printf("\r\nEnding loop mode \r\n");
00241             break;
00242         }
00243     }
00244     
00245 }
00246     
00247     
00248  // print what ever results are required   
00249 void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
00250 {
00251     int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
00252             
00253     int RoiNumber=pMultiRangingData->RoiNumber;
00254 
00255     if (( no_of_object_found < 10 ) &&  ( no_of_object_found != 0)) 
00256     {
00257         for(int j=0;j<no_of_object_found;j++){
00258             if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) || 
00259                 (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL))
00260             {
00261                 printf("\t i2cAddr=%d \t RoiNumber=%d   \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
00262                     devNumber, RoiNumber,
00263                     pMultiRangingData->RangeData[j].RangeStatus,
00264                     pMultiRangingData->RangeData[j].RangeMilliMeter,
00265                     pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0,
00266                     pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0);   
00267             }
00268         }
00269     } // if (( no_of_object_found < 10 ) &&  ( no_of_object_found != 0)) 
00270 }    
00271 
00272         
00273 #if (MBED_VERSION  > 60300)
00274 extern "C" void wait_ms(int ms)
00275  {
00276     thread_sleep_for(ms);
00277  }
00278 #endif 
00279      
00280