ST Expansion SW Team / Mbed OS VL53L3CX_Shield_3Sensors_Interrupt_MbOS6x

Dependencies:   X_NUCLEO_53L3A2

Files at this revision

API Documentation at this revision

Comitter:
johnAlexander
Date:
Tue May 11 08:51:35 2021 +0000
Parent:
6:cf009c2392fe
Commit message:
Optimise performance.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue May 11 08:46:26 2021 +0000
+++ b/main.cpp	Tue May 11 08:51:35 2021 +0000
@@ -1,16 +1,14 @@
 /*
  * This VL53L3 Expansion board test application performs range measurements
- * using the onboard embedded centre sensor, in singleshot, polling mode.
- * Measured ranges are ouput on the Serial Port, running at 115200 baud.
- *
- * The User Blue button stops the current measurement and entire program,
- * releasing all resources.
+ * using the onboard embedded sensor, and satellite boards, in interrupt mode.
+ * Measured ranges are output on the Serial Port, running at 115200 baud.
  *
  * The Reset button can be used to restart the program.
  *
  * *** Note :
- * Default Mbed build system settings disable print floating-point support.
- * Offline builds can enable this, again.
+ * Default Mbed build system settings disable printf floating-point support.
+ * Online builds seem unable to configure this.
+ * Offline builds can enable printf floating-point support.
  * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md
  * .\mbed-os\platform\mbed_lib.json
  *
@@ -28,7 +26,7 @@
 #define I2C_SDA   D14
 #define I2C_SCL   D15
 
-#define MEASUREMENTTIMING  55
+#define MEASUREMENTTIMING  2 // 5  // 10 // 20 // 30 // 55
 
 static XNucleo53L3A2 *board=NULL;
 #if (MBED_VERSION  > 60300)
@@ -101,10 +99,10 @@
 
 
 
-VL53LX_Dev_t                   devCentre;
-VL53LX_Dev_t                   devLeft;
-VL53LX_Dev_t                   devRight;
-VL53LX_DEV                     Dev = &devCentre;
+VL53LX_Dev_t devCentre;
+VL53LX_Dev_t devLeft;
+VL53LX_Dev_t devRight;
+VL53LX_DEV Dev = &devCentre;
 
 
 
@@ -115,25 +113,21 @@
 int main()
 {
     int status;
-    VL53L3 * Sensor;
-    uint16_t wordData;
+    VL53L3 *Sensor;
     uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right
 
+    WaitForMeasurement *int2;
+    WaitForMeasurement *int1;
+    WaitForMeasurement *int3;
+
 
-    WaitForMeasurement* int2;
-    WaitForMeasurement* int1;
-    WaitForMeasurement* int3;
-
-    pc.baud(115200);  // baud rate is important as printf statements take a lot of time
-
-    printf("Hello world!\r\n");
-
     vl53L3_DevI2C *dev_I2C = new vl53L3_DevI2C(I2C_SDA, I2C_SCL);
-
     /* creates the 53L1A1 expansion board singleton obj */
     board = XNucleo53L3A2::instance(dev_I2C, A2, D8, D2);
     printf("board created!\r\n");
 
+    pc.baud(115200);  // baud rate is important as printf statements take a lot of time
+    
     /* init the 53L1A1 expansion board with default values */
     status = board->init_board();
     if (status) {
@@ -141,31 +135,30 @@
         return 0;
     }
 
-
     printf("board initiated! - %d\r\n", status);
 
-    for (ToFSensor=0; ToFSensor<3; ToFSensor++) {
+    for (ToFSensor = 0; ToFSensor < 3; ToFSensor++) {
         wait_ms(15);
         switch(ToFSensor) {
             case 0:
-                if (board->sensor_centre== NULL ) continue;
+                if (board->sensor_centre == NULL ) continue;
                 Dev=&devCentre;
-                Sensor=board->sensor_centre;
                 Dev->I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS;
+                Sensor = board->sensor_centre;
                 printf("configuring centre channel \n");
                 break;
             case 1:
-                if (board->sensor_left== NULL ) continue;
+                if (board->sensor_left == NULL ) continue;
                 Dev=&devLeft;
-                Sensor=board->sensor_left;
                 Dev->I2cDevAddr = NEW_SENSOR_LEFT_ADDRESS;
+                Sensor = board->sensor_left;
                 printf("configuring left channel \n");
                 break;
             case 2:
-                if (board->sensor_right== NULL ) continue;
+                if (board->sensor_right == NULL ) continue;
                 Dev=&devRight;
-                Sensor=board->sensor_right;
                 Dev->I2cDevAddr = NEW_SENSOR_RIGHT_ADDRESS;
+                Sensor = board->sensor_right;
                 printf("configuring right channel \n");
                 break;
             default:
@@ -174,78 +167,74 @@
 
 // configure the sensors
         Dev->comms_speed_khz = 400;
-
         Dev->comms_type = 1;
 
-        Sensor->VL53LX_RdWord(Dev, 0x01, &wordData);
-        printf("VL53L1X: %02X   %d\n\r", wordData,Dev->I2cDevAddr);
         /* Device Initialization and setting */
-
         status = Sensor->VL53LX_DataInit();
         status = Sensor->VL53LX_SetDistanceMode(VL53LX_DISTANCEMODE_LONG);
         status = Sensor->VL53LX_SetMeasurementTimingBudgetMicroSeconds( MEASUREMENTTIMING * 1000);
         status = Sensor->VL53LX_SmudgeCorrectionEnable(VL53LX_SMUDGE_CORRECTION_SINGLE);
         status = Sensor->VL53LX_SetXTalkCompensationEnable(1);
-
-
-    }
-
-    if (board->sensor_centre!= NULL ) {
-        printf("starting interrupt centre\n");
-        Sensor=board->sensor_centre;
-        devCentre.I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS;
-        int1 =  new WaitForMeasurement(A2,&devCentre);
-        status = Sensor->VL53LX_StartMeasurement();
-        printf("VL53L1_StartMeasurement %d \n",status);
-        status = board->sensor_centre->VL53LX_ClearInterruptAndStartMeasurement();
     }
 
 
-    if (board->sensor_left!= NULL ) {
+    if (board->sensor_centre != NULL ) {
+        printf("starting interrupt centre\n");
+        Sensor = board->sensor_centre;
+        devCentre.I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS;
+        int1 = new WaitForMeasurement(A2,&devCentre);
+        status = Sensor->VL53LX_StartMeasurement();
+        printf("VL53L1_StartMeasurement %d \n",status);
+        status = Sensor->VL53LX_ClearInterruptAndStartMeasurement();
+    }
+
+
+    if (board->sensor_left != NULL ) {
         printf("starting interrupt left\n");
-        Sensor=board->sensor_left;
+        Sensor = board->sensor_left;
         devLeft.I2cDevAddr = NEW_SENSOR_LEFT_ADDRESS;
         int2 = new WaitForMeasurement(D8,&devLeft);
         status = Sensor->VL53LX_StartMeasurement();
         printf("VL53L1_StartMeasurement %d \n",status);
-        status = board->sensor_left->VL53LX_ClearInterruptAndStartMeasurement();
+        status = Sensor->VL53LX_ClearInterruptAndStartMeasurement();
     }
 
-    if (board->sensor_right!= NULL ) {
+    if (board->sensor_right != NULL ) {
         printf("starting interrupt right\n");
-        Sensor=board->sensor_right;
+        Sensor = board->sensor_right;
         devRight.I2cDevAddr = NEW_SENSOR_RIGHT_ADDRESS;
         int3 = new WaitForMeasurement(D2,&devRight);
         status = Sensor->VL53LX_StartMeasurement();
         printf("VL53L1_StartMeasurement %d \n",status);
-        status = board->sensor_right->VL53LX_ClearInterruptAndStartMeasurement();
+        status = Sensor->VL53LX_ClearInterruptAndStartMeasurement();
     }
 
 
+    VL53LX_MultiRangingData_t MultiRangingData;
+    VL53LX_MultiRangingData_t *pMultiRangingData;
+    
     // loop waiting for interrupts to happen. This is signaled by   int_centre_result,int_left_result or int_right_result
     // being non zero. The are set back to zero when processing is completed
     while (1) {
+        pMultiRangingData = &MultiRangingData;
 
-        VL53LX_MultiRangingData_t MultiRangingData;
-        VL53LX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
-
-        wait_ms(10);
+        wait_ms(5); // 10
 
         if (int_centre_result != 0) {
-            int_centre_result = 0;
-            status = board->sensor_centre->VL53LX_GetMultiRangingData( pMultiRangingData);
-            int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
-            if (( no_of_object_found < 10 ) && ( no_of_object_found != 0 )) {
-                for(int j=0; j<no_of_object_found; j++) {
+            status = board->sensor_centre->VL53LX_GetMultiRangingData(pMultiRangingData);
+            int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
+            if ((no_of_object_found < 10) && (no_of_object_found != 0)) {
+                for(int j = 0; j < no_of_object_found; j++) {
                     if (pMultiRangingData->RangeData[j].RangeStatus == 0) {
                         printf("centre\t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
                                pMultiRangingData->RangeData[j].RangeStatus,
                                pMultiRangingData->RangeData[j].RangeMilliMeter,
-                               (pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65535.0),
-                               (pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65535.0));
+                               (pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0),
+                               (pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0));
                     }
                 }
             }
+            // clear the interrupt and wait for another result
             int_centre_result = 0;
             wait_ms( MEASUREMENTTIMING );
             status = board->sensor_centre->VL53LX_ClearInterruptAndStartMeasurement();
@@ -253,12 +242,11 @@
 
 
         if (int_left_result != 0) {
-            int_left_result = 0;
-            status = board->sensor_left->VL53LX_GetMultiRangingData( pMultiRangingData);
-            if ( status == 0) {
-                int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
-                if (( no_of_object_found < 10 ) && ( no_of_object_found != 0 )) {
-                    for(int j=0; j<no_of_object_found; j++) {
+            status = board->sensor_left->VL53LX_GetMultiRangingData(pMultiRangingData);
+            if (status == 0) {
+                int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
+                if ((no_of_object_found < 10) && (no_of_object_found != 0)) {
+                    for(int j = 0; j < no_of_object_found; j++) {
                         if (pMultiRangingData->RangeData[j].RangeStatus == 0) {
                             printf("left  \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
                                    pMultiRangingData->RangeData[j].RangeStatus,
@@ -269,34 +257,34 @@
                     }
                 }
             }
+            // clear the interrupt and wait for another result
+            int_left_result = 0;
             wait_ms( MEASUREMENTTIMING );
             status = board->sensor_left->VL53LX_ClearInterruptAndStartMeasurement();
         }
 
 
         if (int_right_result != 0) {
-            // clear interrupt flag
-            int_right_result = 0;
-            status = board->sensor_right->VL53LX_GetMultiRangingData( pMultiRangingData);
-            if ( status == 0) {
+            status = board->sensor_right->VL53LX_GetMultiRangingData(pMultiRangingData);
+            if (status == 0) {
                 // if valid result print it
-                int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
-                if (( no_of_object_found < 10 ) && ( no_of_object_found != 0 )) {
-                    for(int j=0; j<no_of_object_found; j++) {
+                int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
+                if ((no_of_object_found < 10) && (no_of_object_found != 0)) {
+                    for(int j = 0; j < no_of_object_found; j++) {
                         if (pMultiRangingData->RangeData[j].RangeStatus == 0) {
                             printf("right \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
                                    pMultiRangingData->RangeData[j].RangeStatus,
                                    pMultiRangingData->RangeData[j].RangeMilliMeter,
-                                   (pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65535.0),
-                                   (pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65535.0) );
+                                   (pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0),
+                                   (pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0) );
                         }
                     }
                 }
             }
+            // clear the interrupt and wait for another result
+            int_right_result = 0;
             wait_ms( MEASUREMENTTIMING );
-            // clear theinterrupt and wait for another result
             status = board->sensor_right->VL53LX_ClearInterruptAndStartMeasurement();
-
         }
     }
     printf("terminated");