ST Expansion SW Team / Mbed OS VL53L1CB_3sensors_poll_auton

Dependencies:   X_NUCLEO_53L1A2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * This VL53L1CB Expansion board test application performs range measurements
00003  * using the onboard embedded (centre) sensor, and Left & Right satellites, 
00004  * in polling mode.
00005  * Measured ranges are ouput on the Serial Port, running at 115200 baud.
00006  *
00007  * This is designed to work with MBed v2.x, & MBedOS v5.x / v6.x.
00008  *
00009  * The Reset button can be used to restart the program.
00010  *
00011  * *** NOTE :
00012  *     Default Mbed build system settings disable printf() floating-point support.
00013  *     Offline builds can enable this, again.
00014  *     https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md
00015  *     .\mbed-os\platform\mbed_lib.json
00016  *
00017  * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
00018  *            the X-NUCELO-53L1A1 expansion board are not made/OFF.
00019  *            These links must be made to allow interrupts from the Satellite boards
00020  *            to be received.
00021  *            U11 and U18 must be made/ON to allow interrupts to be received from the
00022  *            INT_L & INT_R positions; or
00023  *            U10 and U15 must be made/ON to allow interrupts to be received from the
00024  *            Alternate INT_L & INT_R positions.
00025  *            The X_NUCLEO_53L1A2 library defaults to use the INT_L/INT_R positions.
00026  *            INT_L is available on expansion board Arduino Connector CN5, pin 1 as D8.
00027  *            Alternate INT_L is on CN5 Connector pin 2 as D9.
00028  *            INT_R is available on expansion board Arduino Connector CN9, pin 3 as D2.
00029  *            Alternate INT_R is on CN9 Connector pin 5 as D4.
00030  *            The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A2/
00031  *
00032  */
00033 
00034 #include <stdio.h>
00035 #include <time.h>
00036 
00037 #include "mbed.h"
00038 
00039 #include "XNucleo53L1A2.h"
00040 #include "ToF_I2C.h"
00041 
00042 // i2c comms port pins
00043 #define I2C_SDA   D14
00044 #define I2C_SCL   D15
00045 
00046 
00047 #define NUM_SENSORS 3
00048 
00049 // define interrupt pins
00050 PinName CentreIntPin = A2;
00051 // the satellite pins depend on solder blobs on the back of the shield.
00052 // they may not exist or may be one of two sets.
00053 // the centre pin always exists
00054 //PinName LeftIntPin = D8;
00055 PinName RightIntPin = D2;
00056 // alternate set
00057 PinName LeftIntPin = D9;
00058 //PinName RightIntPin = D4;
00059 
00060 static XNucleo53L1A2 *board=NULL;
00061 
00062 #if (MBED_VERSION  > 60300)
00063 UnbufferedSerial  pc(USBTX, USBRX);
00064 extern "C" void wait_ms(int ms);
00065 #else
00066 Serial pc(SERIAL_TX, SERIAL_RX);
00067 #endif
00068 
00069 void print_results(int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
00070 
00071 
00072 VL53L1_Dev_t devCentre;
00073 VL53L1_DEV DevC = &devCentre;
00074 
00075 VL53L1_Dev_t devLeft;
00076 VL53L1_DEV DevL = &devLeft;
00077 
00078 VL53L1_Dev_t devRight;
00079 VL53L1_DEV DevR = &devRight;
00080 
00081 VL53L1CB *Sensor;
00082 
00083 
00084 
00085 /* flags that handle interrupt request for sensor and user blue button*/
00086 volatile bool int_sensor = false;
00087 volatile bool int_stop = false;
00088 
00089 /* ISR callback function of the centre sensor */
00090 void sensor_irq(void)
00091 {
00092     int_sensor = true;
00093     board->sensor_centre->disable_interrupt_measure_detection_irq();
00094 }
00095 
00096 /* Start the sensor ranging */
00097 int init_sensor()
00098 {
00099     int status = 0;
00100 
00101     DevC=&devCentre;
00102     DevC->comms_speed_khz = 400;
00103     DevC->comms_type = 1;
00104     DevC->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
00105     devCentre.i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
00106 //    Sensor=board->sensor_centre;
00107 
00108     DevL=&devLeft;
00109 //    Sensor=board->sensor_left;
00110     // configure the sensors
00111     DevL->comms_speed_khz = 400;
00112     DevL->comms_type = 1;
00113     DevL->i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS;
00114     devLeft.i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS;
00115 
00116     DevR=&devRight;
00117     // configure the sensors
00118     DevR->comms_speed_khz = 400;
00119     DevR->comms_type = 1;
00120     DevR->i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS;
00121     devRight.i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS;
00122 
00123     for (int i = 0; i < 3; i++)
00124     {
00125         if (i == 0) { Sensor=board->sensor_centre;  }
00126         if (i == 1) { Sensor=board->sensor_left;  }    
00127         if (i == 2) { Sensor=board->sensor_right;  }    
00128         
00129         if (Sensor != NULL)
00130         {
00131             if (i == 0) {  printf("configuring centre channel \n"); }
00132             if (i == 1) {  printf("configuring left channel \n"); }    
00133             if (i == 2) {  printf("configuring right channel \n"); }    
00134             
00135             /* Device Initialization and setting */
00136             status = Sensor->VL53L1CB_DataInit();
00137             status = Sensor->VL53L1CB_StaticInit();
00138 
00139             status = Sensor->VL53L1CB_SetPresetMode(VL53L1_PRESETMODE_AUTONOMOUS);
00140             status = Sensor->VL53L1CB_SetDistanceMode(VL53L1_DISTANCEMODE_LONG);
00141 
00142             status = Sensor->VL53L1CB_SetInterMeasurementPeriodMilliSeconds(500);
00143 
00144             status = Sensor->VL53L1CB_SetMeasurementTimingBudgetMicroSeconds(45000);
00145 
00146             status = Sensor->VL53L1CB_SetSequenceStepEnable(VL53L1_SEQUENCESTEP_MM1, 0);
00147             status = Sensor->VL53L1CB_SetSequenceStepEnable(VL53L1_SEQUENCESTEP_MM2, 0);
00148         }
00149     }    
00150 
00151     // create interrupt handler and start measurements
00152     if (board->sensor_centre!= NULL) {
00153         status = board->sensor_centre->stop_measurement();
00154         if (status != 0) {
00155             return status;
00156         }
00157 
00158         status = board->sensor_centre->VL53L1CB_StartMeasurement();
00159         if (status != 0) {
00160             return status;
00161         }
00162     }
00163     
00164     // create interrupt handler and start measurements
00165     if (board->sensor_left!= NULL) {
00166         status = board->sensor_left->stop_measurement();
00167         if (status != 0) {
00168             return status;
00169         }
00170 
00171         status = board->sensor_left->VL53L1CB_StartMeasurement();
00172         if (status != 0) {
00173             return status;
00174         }
00175     }
00176     
00177     // create interrupt handler and start measurements
00178     if (board->sensor_right!= NULL) {
00179         status = board->sensor_right->stop_measurement();
00180         if (status != 0) {
00181             return status;
00182         }
00183 
00184         status = board->sensor_right->VL53L1CB_StartMeasurement();
00185         if (status != 0) {
00186             return status;
00187         }
00188     }
00189     
00190     return status;
00191 }
00192 
00193 /* ISR callback function of the user blue button to switch measuring sensor. */
00194 void measuring_stop_irq(void)
00195 {
00196     int_stop = true;
00197 }
00198 
00199 /*=================================== Main ==================================
00200 =============================================================================*/
00201 int main()
00202 {
00203     int status;
00204 
00205     pc.baud(115200);  // baud rate is important as printf statements take a lot of time
00206 
00207     printf("mbed version : %d \r\n",MBED_VERSION);
00208 
00209 // create i2c interface
00210     ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
00211     /* creates the 53L1A2 expansion board singleton obj */
00212     board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin);    
00213 
00214     printf("board created!\r\n");
00215 
00216     /* init the 53L1A1 expansion board with default values */
00217     status = board->init_board();
00218     if (status) {
00219         printf("Failed to init board!\r\n");
00220         return status;
00221     }
00222 
00223     printf("board initiated! - %d\r\n", status);
00224 
00225     /* init an array with chars to id the sensors */
00226     status = init_sensor();
00227     if (status != 0) {
00228         printf("Failed to init sensors!\r\n");
00229         return status;
00230     }
00231 
00232     printf("loop forever\n");
00233 
00234     VL53L1_MultiRangingData_t MultiRangingData;
00235     VL53L1_MultiRangingData_t *pMultiRangingData = NULL;
00236 
00237     while (true) {
00238         pMultiRangingData = &MultiRangingData;
00239 
00240         if (board->sensor_centre!= NULL) {
00241             printf("Range result from Centre sensor\n");
00242             status = board->sensor_centre->VL53L1CB_WaitMeasurementDataReady();
00243     
00244             status = board->sensor_centre->VL53L1CB_GetMultiRangingData( pMultiRangingData);
00245     
00246             print_results( devCentre.i2c_slave_address, pMultiRangingData );
00247     
00248             status = board->sensor_centre->VL53L1CB_ClearInterrupt();
00249         }
00250     
00251         if (board->sensor_left!= NULL) {
00252             printf("Range result from Left Satellite\n");
00253             status = board->sensor_left->VL53L1CB_WaitMeasurementDataReady();
00254     
00255             status = board->sensor_left->VL53L1CB_GetMultiRangingData( pMultiRangingData);
00256     
00257             print_results( devLeft.i2c_slave_address, pMultiRangingData );
00258     
00259             status = board->sensor_left->VL53L1CB_ClearInterrupt();
00260         }
00261     
00262         // create interrupt handler and start measurements
00263         if (board->sensor_right!= NULL) {
00264             printf("Range result from Right Satellite\n");
00265             status = board->sensor_right->VL53L1CB_WaitMeasurementDataReady();
00266     
00267             status = board->sensor_right->VL53L1CB_GetMultiRangingData( pMultiRangingData);
00268     
00269             print_results( devRight.i2c_slave_address, pMultiRangingData );
00270     
00271             status = board->sensor_right->VL53L1CB_ClearInterrupt();
00272         }    
00273     }
00274 
00275     printf("Terminating.\n");
00276 }
00277 
00278 
00279 // print what ever results are required
00280 void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
00281 {
00282     int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
00283     int signal_rate = 0;
00284     int ambient_rate = 0;
00285 
00286     int RoiNumber = pMultiRangingData->RoiNumber;
00287     
00288  //   printf("no_of_object_found : %d\n", no_of_object_found);
00289 
00290     if (( no_of_object_found < 10 ) &&  ( no_of_object_found != 0)) {
00291         for(int j=0; j<no_of_object_found; j++) {
00292             if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) ||
00293                     (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) {
00294                 signal_rate = pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535;
00295                 ambient_rate = pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535;
00296                 printf("\t i2cAddr=%d \t RoiNumber=%d   \t status=%d, \t D=%5dmm, \t Signal=%d Mcps, \t Ambient=%d Mcps \n",
00297                        devNumber, RoiNumber,
00298                        pMultiRangingData->RangeData[j].RangeStatus,
00299                        pMultiRangingData->RangeData[j].RangeMilliMeter,
00300                        signal_rate,
00301                        ambient_rate);
00302 /*
00303 // online compiler disables printf() / floating-point support, for code-size reasons.                        
00304 // offline compiler can switch it on.
00305                 printf("\t i2cAddr=%d \t RoiNumber=%d   \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
00306                        devNumber, RoiNumber,
00307                        pMultiRangingData->RangeData[j].RangeStatus,
00308                        pMultiRangingData->RangeData[j].RangeMilliMeter,
00309                        pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0,
00310                        pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0);
00311 */                       
00312             }
00313         }
00314     } // if (( no_of_object_found < 10 ) &&  ( no_of_object_found != 0))
00315 }
00316 
00317 
00318 #if (MBED_VERSION  > 60300)
00319 extern "C" void wait_ms(int ms)
00320 {
00321     thread_sleep_for(ms);
00322 }
00323 #endif
00324 
00325 
00326