ST Expansion SW Team / Mbed 2 deprecated VL53L3_NoShield_1sensor_interrupt_Mb2

Dependencies:   mbed VL53L3ExpansionBoard

Main.cpp

Committer:
charlesmn
Date:
2020-10-14
Revision:
0:34269810c435
Child:
2:75271310def9

File content as of revision 0:34269810c435:

 /*
 *  my_no_shield_interrupt.h
 *  This pogram is a simple sample program which demonstrates the VL53L3 being used without 
 *  a nucleo sheild using interrupts to alert the program that a measurement is ready.
  * The shutdown pins are directly connected to the MCU pins D9,D4 and D3
 *  as defined by this line:
 *    status = board->init_board(D9,D4,D3);
 *  the interrupt pins are defined in
 *     board = NoShield53L3::instance(dev_I2C, A2, D8, D2);
 *
 * This program only uses one sensor but can easily be adapted to run 3.
 *
 *  Measured ranges are ouput on the Serial Port, running at 115200 baud.
 *
 *  The Reset button can be used to restart the program.
 */
 

 
#include <stdio.h>

#include "mbed.h"
#include "NoShield53L3.h"
#include "vl53L1x_I2c.h"
#include <time.h>

// define I2C pins
#define VL53L1_I2C_SDA   D14 
#define VL53L1_I2C_SCL   D15 

#define TIMINGBUDGET  50

static NoShield53L3 *board=NULL;
Serial pc(SERIAL_TX, SERIAL_RX); 

static int int_centre_dropped = 0;
static int int_left_dropped = 0;
static int int_right_dropped = 0;



class WaitForMeasurement {
public:


// this class services the interrupts from the ToF sensors.
// There is a limited amount you can do in an interrupt routine; printfs,mutexes break them among other things.
// We keep things simple by only raising a flag so all the real work is done outside the interrupt.
// This is designed around MBED V2 which doesn't have the RTOS features that would make this work nicely e.g. semaphores/queues.
WaitForMeasurement(): _interrupt(A1)
{
}


    // constructor - Sensor is not used and can be removed
    WaitForMeasurement(PinName pin,VL53LX_DEV Dev) : _interrupt(pin)          // create the InterruptIn on the pin specified to Counter
    {
         Devlocal = Dev;
        _interrupt.fall(callback(this, &WaitForMeasurement::lost_interrupt)); // attach increment function of this counter instance
        
    }
    

  // function is called every time an interupt is seen. A flag is raised which allows the main routine to service the interupt.
    
    void lost_interrupt()
    {
        if (Devlocal->I2cDevAddr == NEW_SENSOR_CENTRE_ADDRESS)
            int_centre_dropped = 1;  //flag to main that interrupt happened
        if (Devlocal->I2cDevAddr == NEW_SENSOR_LEFT_ADDRESS)
            int_left_dropped = 1;   //flag to main that interrupt happened
        if (Devlocal->I2cDevAddr == NEW_SENSOR_RIGHT_ADDRESS)
            int_right_dropped = 1;  //flag to main that interrupt happened    
    }

    
    //destructor
    ~WaitForMeasurement()
    {
        printf("destruction \n");
    }

private:
    InterruptIn _interrupt;
    VL53LX_DEV Devlocal;  //store the device for this instance of the class
    int status;
    
};


// sensor classes. We don't use left and right in this program as we only have one sensor
VL53LX_Dev_t                   devCentre;
VL53LX_Dev_t                   devLeft;
VL53LX_Dev_t                   devRight;
//VL53LX_DEV                     Dev = &devCentre;




 
/*=================================== Main ==================================
=============================================================================*/
int main()
{   
    int status;
  //  VL53LX * Sensor;
    uint16_t wordData;
    uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right
  
    // interupt classes for the sensors . we only use int1
    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");

// start the I2C interface
    vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);

    
    /* no expansion board so don't use stmpe1600. Define interupt pins, only A2 used with one sensor */
    board = NoShield53L3::instance(dev_I2C, A2, D8, D2);
    printf("board created!\r\n");

    /* define the shutdown pins. D4 and D3 aren't used with one sensor */
    status = board->init_board(D9,D4,D3);
    if (status) {
        printf("Failed to init board!\r\n");
        return 0;
    }
       
        
    printf("board initiated! - %d\r\n", status);
                                                

    wait_ms(15);

    if (board->sensor_centre== NULL ) {
        printf("Failed to init sensor!\r\n");
        return 0;
    }
    printf("configuring centre channel \n");



// configure the I2C interface

    devCentre.I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS;
    devCentre.comms_speed_khz = 400;
    devCentre.comms_type = 1;

/* Device Initialization and setting */  

    status = board->sensor_centre->VL53LX_DataInit();
    status = board->sensor_centre->VL53LX_SetDistanceMode(VL53LX_DISTANCEMODE_LONG);
    status = board->sensor_centre->VL53LX_SetMeasurementTimingBudgetMicroSeconds( TIMINGBUDGET * 1000);
    status = board->sensor_centre->VL53LX_SmudgeCorrectionEnable(VL53LX_SMUDGE_CORRECTION_SINGLE);
    status = board->sensor_centre->VL53LX_SetXTalkCompensationEnable(1);

    printf("starting interrupt centre\n");

    int1 =  new WaitForMeasurement(A2,&devCentre); // initialise interrupt for the sensor
    status = board->sensor_centre->VL53LX_StartMeasurement();
    status = board->sensor_centre->VL53LX_ClearInterruptAndStartMeasurement();
        
            
    
       // loop waiting for interrupts to happen. This is signaled by int_centre_dropped
       // being non zero. It is set back to zero when processing is completed
    while (1)
    {

        VL53LX_MultiRangingData_t MultiRangingData;
        VL53LX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;   
        
        wait_ms( 1 * 10);

        if (int_centre_dropped != 0)
        {
            int_centre_dropped = 0;
            // interrupt has occurred so a measurement is waiting
            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 == VL53LX_RANGESTATUS_RANGE_VALID) || 
                        (pMultiRangingData->RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL))
                    {
                        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/65536.0,
                        pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65536.0);
                    }
                }
            }

            wait_ms( TIMINGBUDGET);
            status = board->sensor_centre->VL53LX_ClearInterruptAndStartMeasurement();
        }

    }
    printf("terminated");
}