Funzione di "Follow"

salve a tutti...
sono alle prese ultimamente con l'output di questo sensore ...
con un esp32 mando il comando Modbus al sensore, scompatto la stringa ricevuta in risposta e mi ritrovo i due valori (distanza ed amplitudine) di tutti ed 8 i settori.

mettiamo il caso di un corpo riflettente che attraversa, uno dopo l'altro, gli 8 segmenti dell'intero campo visivo del sensore

ogni misurazione (di ognuno dei segmenti), quando il corpo è nel segmento, sarà simile a quella del segmento precedente (quando il corpo era nel segmento) sia rispetto alla distanza che all'amplitudine (se il corpo si sta avvicinando la distanza sarà leggermente minore e l'amplitudine leggermente maggiore e vice versa).

sto cercando di trovare/creare una funzione che segua quel corpo, indicandomi in una variabile il/i settore/i interessato/i in quel momento (tra 1 ed 8).

per dire all'esp cosa seguire potrei memorizzare dei valori di uno dei segmenti e lui potrebbe seguire partendo da quelli se il corpo si muove...

non so se mi sono spiegato al meglio...

questa L'ultima prova insoddisfacente fatta sommando mele e patate (è poco probabile che due corpi in segmenti adiacenti, uno lontano ed uno vicino possano avere lo stesso risultato sommando le due distanze con le relative amplitudini) per non avere un milione di if :

 void segui() {

  int BO1 = cm1 + Amp1;
  int BO2 = cm2 + Amp2;
  int BO3 = cm3 + Amp3;
  int BO4 = cm4 + Amp4;
  int BO5 = cm5 + Amp5;
  int BO6 = cm6 + Amp6;
  int BO7 = cm7 + Amp7;
  int BO8 = cm8 + Amp8;

  int RangeMax = 50;
  int RangeMin = -50;

  if (Settore == 1) {
    if ((BO1 - BO2) < RangeMax || (BO1 - BO2) > RangeMin) {
      Settore = 2;
    }
  }
  if (Settore == 2) {
    if ((BO1 - BO2) < RangeMax || (BO1 - BO2) > RangeMin) {
      Settore = 1;
    }
  }
  if (Settore == 2) {
    if ((BO2 - BO3) < RangeMax || (BO2 - BO3) > RangeMin) {
      Settore = 3;
    }
  }
  if (Settore == 3) {
    if ((BO2 - BO3) < RangeMax || (BO2 - BO3) > RangeMin) {
      Settore = 2;
    }
  }
  if (Settore == 3) {
    if ((BO3 - BO4) < RangeMax || (BO3 - BO4) > RangeMin) {
      Settore = 4;
    }
  }
  if (Settore == 4) {
    if ((BO3 - BO4) < RangeMax || (BO3 - BO4) > RangeMin) {
      Settore = 3;
    }
  }
  if (Settore == 4) {
    if ((BO4 - BO5) < RangeMax || (BO4 - BO5) > RangeMin) {
      Settore = 5;
    }
  }
  if (Settore == 5) {
    if ((BO4 - BO5) < RangeMax || (BO4 - BO5) > RangeMin) {
      Settore = 4;
    }
  }
  if (Settore == 5) {
    if ((BO5 - BO6) < RangeMax || (BO5 - BO6) > RangeMin) {
      Settore = 6;
    }
  }
  if (Settore == 6) {
    if ((BO5 - BO6) < RangeMax || (BO5 - BO6) > RangeMin) {
      Settore = 5;
    }
  }
  if (Settore == 6) {
    if ((BO6 - BO7) < RangeMax || (BO6 - BO7) > RangeMin) {
      Settore = 7;
    }
  }
  if (Settore == 7) {
    if ((BO6 - BO7) < RangeMax || (BO6 - BO7) > RangeMin) {
      Settore = 6;
    }
  }
  if (Settore == 7) {
    if ((BO7 - BO8) < RangeMax || (BO7 - BO8) > RangeMin) {
      Settore = 8;
    }
  }
  if (Settore == 8) {
    if ((BO7 - BO8) < RangeMax || (BO7 - BO8) > RangeMin) {
      Settore = 7;
    }
  }
}

cosa devo cercare? come si chiama quello che vorrei realizzare?

grazie!!

Qui si parla di UN CORPO SOLO

Qui i corpi diventano DUE

È necessario che specifichi meglio cosa vuoi fare:

  • Intercettare UN oggetto e seguirlo, indipendentemente da cosa succede negli altri segmenti, oppure essere in grado di intercettare e seguire PIÙ OGGETTI contemporaneamente?

Inoltre:

  • Gli oggetti a che distanza possono essere e a che velocità (min - max) si possono spodtare?

  • Che angolo di visuale hai adottato per ogni segmento, visto che il sensore da te indicato può avere 20, 48 o 100° di ampiezza segmento?

Ciao,
P.

ciao

  • Intercettare UN oggetto e seguirlo, indipendentemente da cosa succede negli altri segmenti, oppure essere in grado di intercettare e seguire PIÙ OGGETTI contemporaneamente?

quando parlo di due corpi, immagino quello che sto seguendo e quello/i che potrebbe/ro interferire e sui quali poi magari poter passare l'attenzione del follower..

  • Che angolo di visuale hai adottato per ogni segmento, visto che il sensore da te indicato può avere 20, 48 o 100° di ampiezza segmento?

il mio sensore è quello da FOV 48° quindi ognuno degli 8 settori è di 6°

  • Gli oggetti a che distanza possono essere e a che velocità (min - max) si possono spostare?

il range di distanza in cui il follow è utile direi che è tra 1 e 10mt, per quanto riguarda la velocità direi quella di un pedone in ritardo ... 1,5 mt/s

grazie..

Se marcare UNO SOLO OGGETTO e seguirlo potrebbe essere già una cosa abbastanza complicata, discernere DUE O PIÙ oggetti contemporaneamente in movimento potrebbe essere MOLTO complicato e sicuramente non alla mia altezza. Mi dispiace.

Ciao,
P.

che peccato!!!

sto cercando di adattare alle mie esigenze questo:

cercando di inserirmi al posto di tutta la parte I2c ...... vediamo cose ne esce....

grazie cmq

Quel progetto traccia UN SOLO oggetto. Sarebbe stato interessante vedere cosa succede con QUEL programma se gli oggetti diventano DUE.

In realtà il problema principale che vedrei non è tanto il distinguere DUE oggetti, ma cosa succede se gli oggetti si sovrappongono e diventano uno solo per il sensore.

Ciao, buon lavoro.
P.

per ora cerco di farlo funzionare adattandolo al mio sensore....

posterò sviluppi...

credio di aver trovato il punto in cui devo inserire i miei dati all'interno della libreria, per impedirgli di andare a scomodare dei sensori che tra l'altro non ci sono, ed invece prendere la scansione del mio sensore.

La funzione credo sia questa:

void FuzzyRadar::readData()
{ 
  if (millis() - readDataTimer < READ_DATA_DURATION) return;
  readDataTimer = millis();


  for (uint8_t index = startingSensorIndex; index <= endingSensorIndex; index++)
  {
    distance[index] = sensor[index].readReg16Bit(sensor[index].RESULT_RANGE_STATUS + 10);
    if (distance[index] > maximumRange) distance[index] = 0;
  }


  calculateData();
}

vediamo se trovo un modo elegante per passare i miei 8 valori all'interno di quel ciclo for....

forse cosi'?

void FuzzyRadar::readDataVu8()
{ 
  if (millis() - readDataTimer < READ_DATA_DURATION) return;
  readDataTimer = millis();

 int cm[] = {cm1, cm2, cm3, cm4, cm5, cm6, cm7, cm8};

  for (uint8_t index = startingSensorIndex; index <= endingSensorIndex; index++)
  {
    distance[index] = cm[index];    
    if (distance[index] > maximumRange) distance[index] = 0;
  }


  calculateData();
}

allego lo sketch che sto modificando..

//https://github.com/FuzzyNoodle/Fuzzy-Radar/blob/master/src/Fuzzy_Radar.cpp
#include "Fuzzy_Radar.h"

//#include <Servo.h>

//#include "Fuzzy_Radar.h"

const uint8_t NumberOfSensors = 8;
const uint8_t XshutnControlPin = 2;
const uint8_t SeperationDegrees = 6;


//The number of sensors value has to be provided in the constructor
FuzzyRadar radar(NumberOfSensors);

//const uint8_t YawServoPin = 10;
//Servo yawServo;

//const uint8_t PitchServoPin = 11;
//Servo pitchServo;
//
//const uint8_t LaserControlPin = 3;

//const uint16_t LaserJointHeightMM = 240; //The height for laser head servo, in millimeters.
const uint16_t LaserJointHeightMM = 1; //The height for laser head servo, in millimeters.

void setup()
{
  Serial.begin(115200);
  Serial.println(F("Starting sketch - Fuzzy Radar - Laser Tracer example."));

  //Two values: XshutnControlPin and SeperationDegrees need to be provided in the begin() function.
  radar.begin(XshutnControlPin, SeperationDegrees);

//  yawServo.attach(YawServoPin);
//
//  pitchServo.attach(PitchServoPin);

//  pinMode(LaserControlPin, OUTPUT);
//  digitalWrite(LaserControlPin, LOW);
}

void loop()
{
  radar.update();
  if (radar.available() == true)
  {
    if (radar.getDistanceMM() > 0)
    {
      uint16_t distance = radar.getDistanceMM();
      int16_t angle = radar.getAngleDegree();
//      uint16_t yawValue = map(angle, -40, 40, 2000,900); //This mapping needs some trial-and-error for your servo
      uint16_t yawValue = map(angle, -40, 40, 1,8); //This mapping needs some trial-and-error for your servo

//      yawServo.writeMicroseconds(yawValue);
    int   Settore = yawValue;


      //Set minimum distance. Also reduce the distance for laser head servo tilt. Aim at the foot area.
      if (distance > 100)
      {
        distance -= 100;
      }
      else
      {
        distance = 100;
      }

      float tiltDegree = atan(float(LaserJointHeightMM) / (float)(distance)) * 57.3;
      uint16_t pitchValue = map(tiltDegree, 10, 90, 1300, 700);  //This mapping needs some trial-and-error for your servo as well
//      pitchServo.writeMicroseconds(pitchValue);

      //Output data to serial window
      Serial.print(distance);
      Serial.print(" ");
      Serial.print(angle);
      Serial.print(" ");
      Serial.print(tiltDegree);
      Serial.println();

//      digitalWrite(LaserControlPin, HIGH); //Turn on the laser head
    }
    else
    {
//      digitalWrite(LaserControlPin, LOW); //Turn off the laser head
    }
  }
}

e le librerie Fuzzi_Radar.cpp...

/*
 Name:    Fuzzy_Radar.cpp
 Created: 8/17/2018 11:25:14 PM
 Author:  georgychen
 Editor:  http://www.visualmicro.com
*/

#include "Fuzzy_Radar.h"


FuzzyRadar::FuzzyRadar(uint8_t _numberOfSensors)
  :sensor(new VL53L0X[_numberOfSensors])
  ,address(new uint8_t[_numberOfSensors])
  ,distance(new int16_t[_numberOfSensors])
  ,weight(new float[_numberOfSensors])
{
  numberOfSensors = _numberOfSensors;

}

FuzzyRadar::~FuzzyRadar() 
{
  free(address);
  address = NULL;

  free(distance);
  distance = NULL;

  free(weight);
  weight = NULL;
}

void FuzzyRadar::begin(uint8_t _xshutnPin, float _seperationDegrees)
{
  xshutnPin = _xshutnPin;
  seperation = _seperationDegrees;
  startingSensorIndex = 0;
  endingSensorIndex = numberOfSensors-1;
  maximumRange = DEFAULT_MAXIMUM_RANGE;

  Wire.begin();

  
  //Initialize the I2C address array.
  uint8_t addressOffset = 0; //Avoid using default address 0x52 as new address.
  for (uint8_t index = 0; index < numberOfSensors; index++)
  {
    if ((STARTING_ADDRESS + index) == 0x52) addressOffset = 1;
    address[index] = STARTING_ADDRESS + index + addressOffset;
    if (address[index] > 127)
    {
      address[index] -= 127;
    }
  }



  /* Chip shutdown in now controlled by XSHUTN, using a NMOS inverter.
  XSHUTN is not pulled up nor pulled down.
  Only the first chip is controlled by arduino pin
  */

  #ifdef DEBUG_PRINT_INITILAZATION_PROGRESS
  Serial.println(F("Set chip 0 into reset mode."));
  #endif //DEBUG_PRINT_INITILAZATION_PROGRESS

  pinMode(xshutnPin, OUTPUT);
  digitalWrite(xshutnPin, HIGH);//set chip 0 into reset mode. All subsequent chips should go into reset mode as well.

  #ifdef DEBUG_PRINT_INITILAZATION_PROGRESS
  Serial.println(F("All status LEDs should be off."));
  Serial.println(F("Now configuring the sensors. LED should light up one by one."));
  #endif //DEBUG_PRINT_INITILAZATION_PROGRESS

  //delay(1000);

  for (uint8_t index = 0; index < numberOfSensors; index++)
  {
    #ifdef DEBUG_PRINT_INITILAZATION_PROGRESS
    Serial.print(F("Configuring chip "));
    Serial.println(index);
    #endif //DEBUG_PRINT_INITILAZATION_PROGRESS


    //Bring one chip out of reset mode
    if (index == 0)
    {
      //First chip
      digitalWrite(xshutnPin, LOW);//Enable first chip
    }
    else
    {
      //Subsequent chips, index = 1,2,3,4...
//      sensor[index - 1].setGPIO(LOW); //Enable chips other than the first chip//l'ho tolta io/////////////////////////////////////////////////
    }
    delay(5);//Required for VL53L0X firmware booting (1.2ms max).

    #ifdef DEBUG_PRINT_INITILAZATION_PROGRESS
    Serial.print(F("  - Reset I2C address to "));
    Serial.println(address[index]);
    #endif //DEBUG_PRINT_INITILAZATION_PROGRESS

    sensor[index].setAddress(address[index]);

    #ifdef DEBUG_PRINT_INITILAZATION_PROGRESS
    Serial.println(F("  - Initialize the sensor."));
    #endif //DEBUG_PRINT_INITILAZATION_PROGRESS

    sensor[index].init();
    sensor[index].setTimeout(500);

    //delay(1000);
  }

  #ifdef DEBUG_PRINT_INITILAZATION_PROGRESS
  Serial.println(F("Radar array configuration completed."));
  #endif //DEBUG_PRINT_INITILAZATION_PROGRESS



  //Start continuous reading mode.
  for (uint8_t index = 0; index < numberOfSensors; index++)
  {
    #ifdef DEBUG_PRINT_INITILAZATION_PROGRESS
    Serial.print(F("Start continuous ranging mode for chip "));
    Serial.println(index);
    #endif //DEBUG_PRINT_INITILAZATION_PROGRESS

    sensor[index].startContinuous(20);
  }

  //set the center of the array as 0 degree
  startSensorOffset = -seperation * ((float)(numberOfSensors-1))/2;

  hasNewData = false;
}

void FuzzyRadar::update()
{///////////////////////////////////////////////////////////////////////////////
  readData();//////////////////////////////////////////////////////////////////
}/////////////////////////////////////////////////////////////////////////////

int16_t FuzzyRadar::getAngleDegree()
{
  hasNewData = false;
  return filteredAngle;
}

uint16_t FuzzyRadar::getDistanceMM()
{
  hasNewData = false;
  return filteredMeanDistance;
}

void FuzzyRadar::readData()
{
  if (millis() - readDataTimer < READ_DATA_DURATION) return;
  readDataTimer = millis();


  for (uint8_t index = startingSensorIndex; index <= endingSensorIndex; index++)
  {
    distance[index] = sensor[index].readReg16Bit(sensor[index].RESULT_RANGE_STATUS + 10);
    if (distance[index] > maximumRange) distance[index] = 0;
  }


  calculateData();
}

void FuzzyRadar::calculateData()
{
  resetDataValues();
  calculateMeanDistance();

  #ifdef DEBUG_PRINT_RAW_DATA_BEFORE_FILTER
  printRawData();
  #endif //DEBUG_PRINT_RAW_DATA_BEFORE_FILTER

  if (meanDistance == 0)
  {
    readingCounter = 0;
  }

  if (meanDistance > 0)
  {
    //Deviation removal: remove the data that are too far away from mean value.
    bool recalculateMeanDistance = false;
    for (uint8_t index = startingSensorIndex; index <= endingSensorIndex; index++)
    {
      if ((distance[index]>0) && (abs(distance[index] - meanDistance) > DEVIATION_THRESHOLD))
      {
        distance[index] = 0;
        recalculateMeanDistance = true;
      }
    }
    if (recalculateMeanDistance == true)
    {

      resetDataValues();
      calculateMeanDistance();
    }
    //Noise removal
    if (readingCounter < NOISE_LENGTH)
    {
      readingCounter++;
      clearDataValues();
    }
  }

  
  if (meanDistance > 0)
  {

    /*
    Primary target filtering:
    Only the group with most number of sensor readings remains.
    If there are multiple groups with same number of readings, the closet target remains.

    1. Scan sequentially
    2. Note down the starting index for a group
    3. Note down the length for end of group (zero or end of sensor)
    4. Note down the mean of the group
    5. Replace the starting index/length to the stored primary if the new group length is larger.
    6. If the length is the same as stored, compare the mean. Replace if the new group is closer.

    */

    uint8_t currentGroupReadingCounter = 0;
    uint8_t currentGroupLength = 0;
    uint8_t currentGroupStartingIndex = 0;
    uint16_t currentGroupTotal = 0;
    uint16_t currentGroupMeanDistance = 0;
    uint8_t primaryGroupLength = 0;
    uint8_t primaryGroupStartingIndex = 0;
    uint16_t primaryGroupMeanDistance = 0;
    for (uint8_t index = startingSensorIndex; index <= endingSensorIndex; index++) //scanning
    {
      if (distance[index] > 0)
      {
        if (currentGroupReadingCounter > 0)
        {
          //continue a group
          //Serial.print(" CG=");
          //Serial.print(index);
        }
        else
        {
          //starting a new group
          currentGroupStartingIndex = index;
          //Serial.print(" NG=");
          //Serial.print(index);

        }
        currentGroupReadingCounter++;
      }

      if (currentGroupReadingCounter > 0) //a group has started
      {
        //Serial.print(" GS=");
        //Serial.print(index);
        if ((distance[index] == 0) || (index == endingSensorIndex))
        {
          //ending a group, or end of scanning
          //calculate length
          if (distance[index] == 0)
          {
            //zero reading, no matter end of scanning or not
            currentGroupLength = index - currentGroupStartingIndex;
            //Serial.print(" ZR=");
            //Serial.print(index);
          }
          else
          {
            //available reading, but end of scanning
            currentGroupLength = index - currentGroupStartingIndex + 1;
            //Serial.print(" ES=");
            //Serial.print(index);

          }
          //Serial.print(" GL=");
          //Serial.print(currentGroupLength);
          //calculate mean
          currentGroupTotal = 0;
          for (uint8_t groupIndex = currentGroupStartingIndex; groupIndex <= index; groupIndex++)
          {
            currentGroupTotal += distance[groupIndex];
          }
          currentGroupMeanDistance = currentGroupTotal / currentGroupLength;

          //replace primary target if required
          if (currentGroupLength > primaryGroupLength)
          {
            //get the largest target
            primaryGroupLength = currentGroupLength;
            primaryGroupStartingIndex = currentGroupStartingIndex;
            primaryGroupMeanDistance = currentGroupMeanDistance;
            //Serial.print(" LT=");
            //Serial.print(index);
          }
          else if (currentGroupLength == primaryGroupLength)
          {
            //Serial.print(" CM=");
            //Serial.print(currentGroupMeanDistance);
            //Serial.print(" PM=");
            //Serial.print(primaryGroupMeanDistance);

            if (currentGroupMeanDistance < primaryGroupMeanDistance)
            {
              //same size, get the closer target
              primaryGroupLength = currentGroupLength;
              primaryGroupStartingIndex = currentGroupStartingIndex;
              primaryGroupMeanDistance = currentGroupMeanDistance;
              //Serial.print(" CL=");
              //Serial.print(index);
            }

          }

          //reset values for next scanning
          currentGroupReadingCounter = 0;
          currentGroupLength = 0;
          currentGroupStartingIndex = 0;
          currentGroupTotal;
          currentGroupMeanDistance = 0;
        }

      } //if (currentGroupReadingCounter > 0) //a group has started //if (currentGroupReadingCounter > 0) //a group has started


    } //for (uint8_t index = startingSensorIndex; index <= endingSensorIndex; index++) //scanning


    if (primaryGroupLength != 0)
    {
      //remove non-primary data
      for (uint8_t index = startingSensorIndex; index <= endingSensorIndex; index++)
      {
        if ((index<primaryGroupStartingIndex) || (index>(primaryGroupStartingIndex + primaryGroupLength - 1)))
        {
          distance[index] = 0;
        }
      }
      calculateMeanDistance();
    }

    //get weight for each detected sensor
    for (uint8_t index = startingSensorIndex; index <= endingSensorIndex; index++)
    {
      if (distance[index] > 0)
      {
        weight[index] = (float)meanDistance / (float)distance[index];

        weightedTotal += weight[index] * index;
      }

    }

    weightedIndex = weightedTotal / numberOfReadings;

    angle = -(weightedIndex * seperation + startSensorOffset);

  }


  if (meanDistanceRegister == 0)
  {
    //fill the filter with first sample value
    meanDistanceRegister = meanDistance << MEAN_DISTANCE_FILTER_SHIFT;
    filteredMeanDistance = meanDistance;

    angleRegister = angle << ANGLE_FILTER_SHIFT;
    filteredAngle = angle;
  }
  else
  {
    if (meanDistance > 0)
    {
      //non-zero reading, filter the data
      meanDistanceRegister = meanDistanceRegister - (meanDistanceRegister >> MEAN_DISTANCE_FILTER_SHIFT) + meanDistance;
      filteredMeanDistance = meanDistanceRegister >> MEAN_DISTANCE_FILTER_SHIFT;

      angleRegister = angleRegister - (angleRegister >> ANGLE_FILTER_SHIFT) + angle;
      filteredAngle = angleRegister >> ANGLE_FILTER_SHIFT;
    }
    else
    {
      //zero reading, flush the filter register
      meanDistanceRegister = 0;
      filteredMeanDistance = 0;

      angleRegister = 0;
      filteredAngle = 0;
    }
  }

  
  #ifdef DEBUG_PRINT_RAW_DATA_AFTER_FILTER
  printRawData();
  #endif //DEBUG_PRINT_RAW_DATA_AFTER_FILTER

  #ifdef DEBUG_PRINT_DISTANCE_ANGLE
  Serial.print(" ");
  Serial.print("Distance = ");
  Serial.print((filteredMeanDistance < 10 ? "0" : ""));
  Serial.print((filteredMeanDistance < 100 ? "0" : ""));
  Serial.print((filteredMeanDistance < 1000 ? "0" : ""));
  Serial.print(filteredMeanDistance);
  Serial.print(" ");
  Serial.print("Angle = ");
  if (filteredAngle >= 0)
  {
    Serial.print(" ");
    Serial.print((filteredAngle < 10 ? "0" : ""));
    Serial.print((filteredAngle < 100 ? "0" : ""));
    Serial.print(filteredAngle);
  }
  else
  {
    Serial.print("-");
    Serial.print((filteredAngle > -10 ? "0" : ""));
    Serial.print((filteredAngle > -100 ? "0" : ""));
    Serial.print(abs(filteredAngle));
  }

  Serial.println();
  #endif //DEBUG_PRINT_DISTANCE_ANGLE

  hasNewData = true;
}

void FuzzyRadar::printRawData()
{
  Serial.print(" [");
  for (uint8_t index = startingSensorIndex; index <= endingSensorIndex; index++)
  {
    if (distance[index] != 0)
    {
      Serial.print((distance[index] < 10 ? "0" : ""));
      Serial.print((distance[index] < 100 ? "0" : ""));
      Serial.print((distance[index] < 1000 ? "0" : ""));
      Serial.print(distance[index]);
    }
    else
    {
      Serial.print("----");
    }
    Serial.print(" ");
  }
  Serial.print("(");
  Serial.print((meanDistance < 10 ? "0" : ""));
  Serial.print((meanDistance < 100 ? "0" : ""));
  Serial.print((meanDistance < 1000 ? "0" : ""));
  Serial.print(meanDistance);
  Serial.print(")]");

}

void FuzzyRadar::resetDataValues()
{
  numberOfReadings = 0;
  total = 0;
  meanDistance = 0;
  weightedTotal = 0;
  weightedIndex = 0;
  angle = 0;
}

void FuzzyRadar::calculateMeanDistance()
{
  numberOfReadings = 0;
  total = 0;
  for (uint8_t index = startingSensorIndex; index <= endingSensorIndex; index++)
  {/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////7
    if (distance[index] > 0)
    {
      numberOfReadings++;
      total += distance[index];
    }
  }/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  if (numberOfReadings > 0)
  {
    meanDistance = total / numberOfReadings;
  }
}

bool FuzzyRadar::available()
{
  return hasNewData;
}

void FuzzyRadar::clearAvailableFlag()
{
  hasNewData = false;
}

void FuzzyRadar::clearDataValues()
{
  //Serial.print("clearDataValues()");
  resetDataValues();
  for (uint8_t index = startingSensorIndex; index <= endingSensorIndex; index++)
  {
    distance[index] = 0;
  }
}

void FuzzyRadar::setMaximumRangeMM(int16_t _maximumRange)
{
  maximumRange = _maximumRange;
}

e Fuzzi_Radar.h

/*
 Name:    Fuzzy_Radar.h
 Created: 8/17/2018 11:25:14 PM
 Author:  georgychen
 Editor:  http://www.visualmicro.com
*/

#ifndef _Fuzzy_Radar_h
#define _Fuzzy_Radar_h

#if defined(ARDUINO) && ARDUINO >= 100
  #include "arduino.h"
#else
  #include "WProgram.h"
#endif

#include <Wire.h>
#include "VL53L0X.h"




#define DEFAULT_MAXIMUM_RANGE 900
#define DEVIATION_THRESHOLD 200 //signals are removed if the readings is more than this threshold value (mm)
#define STARTING_ADDRESS 0x53
#define READ_DATA_DURATION 24
#define MEAN_DISTANCE_FILTER_SHIFT 1
#define ANGLE_FILTER_SHIFT 1
#define NOISE_LENGTH 1 //For consecutive readings that length is equal of less than this value, the readings are considered noise and omitted.


//Debug switches for serial output. Comment out to disable the debug code.
//#define DEBUG_PRINT_INITILAZATION_PROGRESS
//#define DEBUG_PRINT_RAW_DATA_BEFORE_FILTER
//#define DEBUG_PRINT_RAW_DATA_AFTER_FILTER
//#define DEBUG_PRINT_DISTANCE_ANGLE





class FuzzyRadar
{
public:
  FuzzyRadar(uint8_t _numberOfSensors);
  ~FuzzyRadar();
  void begin(uint8_t _xshutnPin, float _seperationDegrees);
  void update();
  int16_t getAngleDegree();
  uint16_t getDistanceMM();
  bool available();
  void clearAvailableFlag();
  void printRawData();
  void setMaximumRangeMM(int16_t _maximumRange);

private:
  VL53L0X *sensor;
  uint8_t *address;
  uint8_t numberOfSensors;
  uint8_t xshutnPin;
  int16_t *distance;
  uint8_t numberOfReadings;
  float seperation;
  float startSensorOffset;
  uint32_t total;
  int16_t meanDistance;
  uint32_t meanDistanceRegister;
  float *weight;
  float weightedTotal;
  float weightedIndex;
  int16_t angle;
  int32_t angleRegister;
  uint16_t filteredMeanDistance;
  int16_t filteredAngle;
  uint8_t readingCounter;
  uint32_t readDataTimer;
  uint8_t startingSensorIndex;
  uint8_t endingSensorIndex;
  int16_t maximumRange;

  void readData();
  void calculateData();
  
  void resetDataValues();
  void calculateMeanDistance();
  bool hasNewData;
  void clearDataValues();
  
};

#endif

troppo elegante!!!!

vediamo se capisco come inserire il mio array....

... o forse no..

forse è qui che devo guardare....

VL53L0X.h (quella che readData invoca)

// Most of the functionality of this library is based on the VL53L0X API
// provided by ST (STSW-IMG005), and some of the explanatory comments are quoted
// or paraphrased from the API source code, API user manual (UM2039), and the
// VL53L0X datasheet.

#include "VL53L0X.h"
#include <Wire.h>

// Defines /////////////////////////////////////////////////////////////////////

// The Arduino two-wire interface uses a 7-bit number for the address,
// and sets the last bit correctly based on reads and writes
#define ADDRESS_DEFAULT 0b0101001

// Record the current time to check an upcoming timeout against
#define startTimeout() (timeout_start_ms = millis())

// Check if timeout is enabled (set to nonzero value) and has expired
#define checkTimeoutExpired() (io_timeout > 0 && ((uint16_t)(millis() - timeout_start_ms) > io_timeout))

// Decode VCSEL (vertical cavity surface emitting laser) pulse period in PCLKs
// from register value
// based on VL53L0X_decode_vcsel_period()
#define decodeVcselPeriod(reg_val)      (((reg_val) + 1) << 1)

// Encode VCSEL pulse period register value from period in PCLKs
// based on VL53L0X_encode_vcsel_period()
#define encodeVcselPeriod(period_pclks) (((period_pclks) >> 1) - 1)

// Calculate macro period in *nanoseconds* from VCSEL period in PCLKs
// based on VL53L0X_calc_macro_period_ps()
// PLL_period_ps = 1655; macro_period_vclks = 2304
#define calcMacroPeriod(vcsel_period_pclks) ((((uint32_t)2304 * (vcsel_period_pclks) * 1655) + 500) / 1000)

// Constructors ////////////////////////////////////////////////////////////////

VL53L0X::VL53L0X()
  : bus(&Wire)
  , address(ADDRESS_DEFAULT)
  , io_timeout(0) // no timeout
  , did_timeout(false)
{
}

// Public Methods //////////////////////////////////////////////////////////////

void VL53L0X::setAddress(uint8_t new_addr)
{
  writeReg(I2C_SLAVE_DEVICE_ADDRESS, new_addr & 0x7F);
  address = new_addr;
}

// Initialize sensor using sequence based on VL53L0X_DataInit(),
// VL53L0X_StaticInit(), and VL53L0X_PerformRefCalibration().
// This function does not perform reference SPAD calibration
// (VL53L0X_PerformRefSpadManagement()), since the API user manual says that it
// is performed by ST on the bare modules; it seems like that should work well
// enough unless a cover glass is added.
// If io_2v8 (optional) is true or not given, the sensor is configured for 2V8
// mode.
bool VL53L0X::init(bool io_2v8)
{
  // check model ID register (value specified in datasheet)
  if (readReg(IDENTIFICATION_MODEL_ID) != 0xEE) { return false; }

  // VL53L0X_DataInit() begin

  // sensor uses 1V8 mode for I/O by default; switch to 2V8 mode if necessary
  if (io_2v8)
  {
    writeReg(VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV,
      readReg(VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV) | 0x01); // set bit 0
  }

  // "Set I2C standard mode"
  writeReg(0x88, 0x00);

  writeReg(0x80, 0x01);
  writeReg(0xFF, 0x01);
  writeReg(0x00, 0x00);
  stop_variable = readReg(0x91);
  writeReg(0x00, 0x01);
  writeReg(0xFF, 0x00);
  writeReg(0x80, 0x00);

  // disable SIGNAL_RATE_MSRC (bit 1) and SIGNAL_RATE_PRE_RANGE (bit 4) limit checks
  writeReg(MSRC_CONFIG_CONTROL, readReg(MSRC_CONFIG_CONTROL) | 0x12);

  // set final range signal rate limit to 0.25 MCPS (million counts per second)
  setSignalRateLimit(0.25);

  writeReg(SYSTEM_SEQUENCE_CONFIG, 0xFF);

  // VL53L0X_DataInit() end

  // VL53L0X_StaticInit() begin

  uint8_t spad_count;
  bool spad_type_is_aperture;
  if (!getSpadInfo(&spad_count, &spad_type_is_aperture)) { return false; }

  // The SPAD map (RefGoodSpadMap) is read by VL53L0X_get_info_from_device() in
  // the API, but the same data seems to be more easily readable from
  // GLOBAL_CONFIG_SPAD_ENABLES_REF_0 through _6, so read it from there
  uint8_t ref_spad_map[6];
  readMulti(GLOBAL_CONFIG_SPAD_ENABLES_REF_0, ref_spad_map, 6);

  // -- VL53L0X_set_reference_spads() begin (assume NVM values are valid)

  writeReg(0xFF, 0x01);
  writeReg(DYNAMIC_SPAD_REF_EN_START_OFFSET, 0x00);
  writeReg(DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD, 0x2C);
  writeReg(0xFF, 0x00);
  writeReg(GLOBAL_CONFIG_REF_EN_START_SELECT, 0xB4);

  uint8_t first_spad_to_enable = spad_type_is_aperture ? 12 : 0; // 12 is the first aperture spad
  uint8_t spads_enabled = 0;

  for (uint8_t i = 0; i < 48; i++)
  {
    if (i < first_spad_to_enable || spads_enabled == spad_count)
    {
      // This bit is lower than the first one that should be enabled, or
      // (reference_spad_count) bits have already been enabled, so zero this bit
      ref_spad_map[i / 8] &= ~(1 << (i % 8));
    }
    else if ((ref_spad_map[i / 8] >> (i % 8)) & 0x1)
    {
      spads_enabled++;
    }
  }

  writeMulti(GLOBAL_CONFIG_SPAD_ENABLES_REF_0, ref_spad_map, 6);

  // -- VL53L0X_set_reference_spads() end

  // -- VL53L0X_load_tuning_settings() begin
  // DefaultTuningSettings from vl53l0x_tuning.h

  writeReg(0xFF, 0x01);
  writeReg(0x00, 0x00);

  writeReg(0xFF, 0x00);
  writeReg(0x09, 0x00);
  writeReg(0x10, 0x00);
  writeReg(0x11, 0x00);

  writeReg(0x24, 0x01);
  writeReg(0x25, 0xFF);
  writeReg(0x75, 0x00);

  writeReg(0xFF, 0x01);
  writeReg(0x4E, 0x2C);
  writeReg(0x48, 0x00);
  writeReg(0x30, 0x20);

  writeReg(0xFF, 0x00);
  writeReg(0x30, 0x09);
  writeReg(0x54, 0x00);
  writeReg(0x31, 0x04);
  writeReg(0x32, 0x03);
  writeReg(0x40, 0x83);
  writeReg(0x46, 0x25);
  writeReg(0x60, 0x00);
  writeReg(0x27, 0x00);
  writeReg(0x50, 0x06);
  writeReg(0x51, 0x00);
  writeReg(0x52, 0x96);
  writeReg(0x56, 0x08);
  writeReg(0x57, 0x30);
  writeReg(0x61, 0x00);
  writeReg(0x62, 0x00);
  writeReg(0x64, 0x00);
  writeReg(0x65, 0x00);
  writeReg(0x66, 0xA0);

  writeReg(0xFF, 0x01);
  writeReg(0x22, 0x32);
  writeReg(0x47, 0x14);
  writeReg(0x49, 0xFF);
  writeReg(0x4A, 0x00);

  writeReg(0xFF, 0x00);
  writeReg(0x7A, 0x0A);
  writeReg(0x7B, 0x00);
  writeReg(0x78, 0x21);

  writeReg(0xFF, 0x01);
  writeReg(0x23, 0x34);
  writeReg(0x42, 0x00);
  writeReg(0x44, 0xFF);
  writeReg(0x45, 0x26);
  writeReg(0x46, 0x05);
  writeReg(0x40, 0x40);
  writeReg(0x0E, 0x06);
  writeReg(0x20, 0x1A);
  writeReg(0x43, 0x40);

  writeReg(0xFF, 0x00);
  writeReg(0x34, 0x03);
  writeReg(0x35, 0x44);

  writeReg(0xFF, 0x01);
  writeReg(0x31, 0x04);
  writeReg(0x4B, 0x09);
  writeReg(0x4C, 0x05);
  writeReg(0x4D, 0x04);

  writeReg(0xFF, 0x00);
  writeReg(0x44, 0x00);
  writeReg(0x45, 0x20);
  writeReg(0x47, 0x08);
  writeReg(0x48, 0x28);
  writeReg(0x67, 0x00);
  writeReg(0x70, 0x04);
  writeReg(0x71, 0x01);
  writeReg(0x72, 0xFE);
  writeReg(0x76, 0x00);
  writeReg(0x77, 0x00);

  writeReg(0xFF, 0x01);
  writeReg(0x0D, 0x01);

  writeReg(0xFF, 0x00);
  writeReg(0x80, 0x01);
  writeReg(0x01, 0xF8);

  writeReg(0xFF, 0x01);
  writeReg(0x8E, 0x01);
  writeReg(0x00, 0x01);
  writeReg(0xFF, 0x00);
  writeReg(0x80, 0x00);

  // -- VL53L0X_load_tuning_settings() end

  // "Set interrupt config to new sample ready"
  // -- VL53L0X_SetGpioConfig() begin

  writeReg(SYSTEM_INTERRUPT_CONFIG_GPIO, 0x04);
  writeReg(GPIO_HV_MUX_ACTIVE_HIGH, readReg(GPIO_HV_MUX_ACTIVE_HIGH) & ~0x10); // active low
  writeReg(SYSTEM_INTERRUPT_CLEAR, 0x01);

  // -- VL53L0X_SetGpioConfig() end

  measurement_timing_budget_us = getMeasurementTimingBudget();

  // "Disable MSRC and TCC by default"
  // MSRC = Minimum Signal Rate Check
  // TCC = Target CentreCheck
  // -- VL53L0X_SetSequenceStepEnable() begin

  writeReg(SYSTEM_SEQUENCE_CONFIG, 0xE8);

  // -- VL53L0X_SetSequenceStepEnable() end

  // "Recalculate timing budget"
  setMeasurementTimingBudget(measurement_timing_budget_us);

  // VL53L0X_StaticInit() end

  // VL53L0X_PerformRefCalibration() begin (VL53L0X_perform_ref_calibration())

  // -- VL53L0X_perform_vhv_calibration() begin

  writeReg(SYSTEM_SEQUENCE_CONFIG, 0x01);
  if (!performSingleRefCalibration(0x40)) { return false; }

  // -- VL53L0X_perform_vhv_calibration() end

  // -- VL53L0X_perform_phase_calibration() begin

  writeReg(SYSTEM_SEQUENCE_CONFIG, 0x02);
  if (!performSingleRefCalibration(0x00)) { return false; }

  // -- VL53L0X_perform_phase_calibration() end

  // "restore the previous Sequence Config"
  writeReg(SYSTEM_SEQUENCE_CONFIG, 0xE8);

  // VL53L0X_PerformRefCalibration() end

  return true;
}

// Write an 8-bit register
void VL53L0X::writeReg(uint8_t reg, uint8_t value)
{
  bus->beginTransmission(address);
  bus->write(reg);
  bus->write(value);
  last_status = bus->endTransmission();
}

// Write a 16-bit register
void VL53L0X::writeReg16Bit(uint8_t reg, uint16_t value)
{
  bus->beginTransmission(address);
  bus->write(reg);
  bus->write((value >> 8) & 0xFF); // value high byte
  bus->write( value       & 0xFF); // value low byte
  last_status = bus->endTransmission();
}

// Write a 32-bit register
void VL53L0X::writeReg32Bit(uint8_t reg, uint32_t value)
{
  bus->beginTransmission(address);
  bus->write(reg);
  bus->write((value >> 24) & 0xFF); // value highest byte
  bus->write((value >> 16) & 0xFF);
  bus->write((value >>  8) & 0xFF);
  bus->write( value        & 0xFF); // value lowest byte
  last_status = bus->endTransmission();
}

// Read an 8-bit register
uint8_t VL53L0X::readReg(uint8_t reg)
{
  uint8_t value;

  bus->beginTransmission(address);
  bus->write(reg);
  last_status = bus->endTransmission();

  bus->requestFrom(address, (uint8_t)1);
  value = bus->read();

  return value;
}

// Read a 16-bit register
uint16_t VL53L0X::readReg16Bit(uint8_t reg)
{
  uint16_t value;

  bus->beginTransmission(address);
  bus->write(reg);
  last_status = bus->endTransmission();

  bus->requestFrom(address, (uint8_t)2);
  value  = (uint16_t)bus->read() << 8; // value high byte
  value |=           bus->read();      // value low byte

  return value;
}

// Read a 32-bit register
uint32_t VL53L0X::readReg32Bit(uint8_t reg)
{
  uint32_t value;

  bus->beginTransmission(address);
  bus->write(reg);
  last_status = bus->endTransmission();

  bus->requestFrom(address, (uint8_t)4);
  value  = (uint32_t)bus->read() << 24; // value highest byte
  value |= (uint32_t)bus->read() << 16;
  value |= (uint16_t)bus->read() <<  8;
  value |=           bus->read();       // value lowest byte

  return value;
}

// Write an arbitrary number of bytes from the given array to the sensor,
// starting at the given register
void VL53L0X::writeMulti(uint8_t reg, uint8_t const * src, uint8_t count)
{
  bus->beginTransmission(address);
  bus->write(reg);

  while (count-- > 0)
  {
    bus->write(*(src++));
  }

  last_status = bus->endTransmission();
}

// Read an arbitrary number of bytes from the sensor, starting at the given
// register, into the given array
void VL53L0X::readMulti(uint8_t reg, uint8_t * dst, uint8_t count)
{
  bus->beginTransmission(address);
  bus->write(reg);
  last_status = bus->endTransmission();

  bus->requestFrom(address, count);

  while (count-- > 0)
  {
    *(dst++) = bus->read();
  }
}

// Set the return signal rate limit check value in units of MCPS (mega counts
// per second). "This represents the amplitude of the signal reflected from the
// target and detected by the device"; setting this limit presumably determines
// the minimum measurement necessary for the sensor to report a valid reading.
// Setting a lower limit increases the potential range of the sensor but also
// seems to increase the likelihood of getting an inaccurate reading because of
// unwanted reflections from objects other than the intended target.
// Defaults to 0.25 MCPS as initialized by the ST API and this library.
bool VL53L0X::setSignalRateLimit(float limit_Mcps)
{
  if (limit_Mcps < 0 || limit_Mcps > 511.99) { return false; }

  // Q9.7 fixed point format (9 integer bits, 7 fractional bits)
  writeReg16Bit(FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT, limit_Mcps * (1 << 7));
  return true;
}

// Get the return signal rate limit check value in MCPS
float VL53L0X::getSignalRateLimit()
{
  return (float)readReg16Bit(FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT) / (1 << 7);
}

// Set the measurement timing budget in microseconds, which is the time allowed
// for one measurement; the ST API and this library take care of splitting the
// timing budget among the sub-steps in the ranging sequence. A longer timing
// budget allows for more accurate measurements. Increasing the budget by a
// factor of N decreases the range measurement standard deviation by a factor of
// sqrt(N). Defaults to about 33 milliseconds; the minimum is 20 ms.
// based on VL53L0X_set_measurement_timing_budget_micro_seconds()
bool VL53L0X::setMeasurementTimingBudget(uint32_t budget_us)
{
  SequenceStepEnables enables;
  SequenceStepTimeouts timeouts;

  uint16_t const StartOverhead     = 1910;
  uint16_t const EndOverhead        = 960;
  uint16_t const MsrcOverhead       = 660;
  uint16_t const TccOverhead        = 590;
  uint16_t const DssOverhead        = 690;
  uint16_t const PreRangeOverhead   = 660;
  uint16_t const FinalRangeOverhead = 550;

  uint32_t const MinTimingBudget = 20000;

  if (budget_us < MinTimingBudget) { return false; }

  uint32_t used_budget_us = StartOverhead + EndOverhead;

  getSequenceStepEnables(&enables);
  getSequenceStepTimeouts(&enables, &timeouts);

  if (enables.tcc)
  {
    used_budget_us += (timeouts.msrc_dss_tcc_us + TccOverhead);
  }

  if (enables.dss)
  {
    used_budget_us += 2 * (timeouts.msrc_dss_tcc_us + DssOverhead);
  }
  else if (enables.msrc)
  {
    used_budget_us += (timeouts.msrc_dss_tcc_us + MsrcOverhead);
  }

  if (enables.pre_range)
  {
    used_budget_us += (timeouts.pre_range_us + PreRangeOverhead);
  }

  if (enables.final_range)
  {
    used_budget_us += FinalRangeOverhead;

    // "Note that the final range timeout is determined by the timing
    // budget and the sum of all other timeouts within the sequence.
    // If there is no room for the final range timeout, then an error
    // will be set. Otherwise the remaining time will be applied to
    // the final range."

    if (used_budget_us > budget_us)
    {
      // "Requested timeout too big."
      return false;
    }

    uint32_t final_range_timeout_us = budget_us - used_budget_us;

    // set_sequence_step_timeout() begin
    // (SequenceStepId == VL53L0X_SEQUENCESTEP_FINAL_RANGE)

    // "For the final range timeout, the pre-range timeout
    //  must be added. To do this both final and pre-range
    //  timeouts must be expressed in macro periods MClks
    //  because they have different vcsel periods."

    uint32_t final_range_timeout_mclks =
      timeoutMicrosecondsToMclks(final_range_timeout_us,
                                 timeouts.final_range_vcsel_period_pclks);

    if (enables.pre_range)
    {
      final_range_timeout_mclks += timeouts.pre_range_mclks;
    }

    writeReg16Bit(FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI,
      encodeTimeout(final_range_timeout_mclks));

    // set_sequence_step_timeout() end

    measurement_timing_budget_us = budget_us; // store for internal reuse
  }
  return true;
}

// Get the measurement timing budget in microseconds
// based on VL53L0X_get_measurement_timing_budget_micro_seconds()
// in us
uint32_t VL53L0X::getMeasurementTimingBudget()
{
  SequenceStepEnables enables;
  SequenceStepTimeouts timeouts;

  uint16_t const StartOverhead     = 1910;
  uint16_t const EndOverhead        = 960;
  uint16_t const MsrcOverhead       = 660;
  uint16_t const TccOverhead        = 590;
  uint16_t const DssOverhead        = 690;
  uint16_t const PreRangeOverhead   = 660;
  uint16_t const FinalRangeOverhead = 550;

  // "Start and end overhead times always present"
  uint32_t budget_us = StartOverhead + EndOverhead;

  getSequenceStepEnables(&enables);
  getSequenceStepTimeouts(&enables, &timeouts);

  if (enables.tcc)
  {
    budget_us += (timeouts.msrc_dss_tcc_us + TccOverhead);
  }

  if (enables.dss)
  {
    budget_us += 2 * (timeouts.msrc_dss_tcc_us + DssOverhead);
  }
  else if (enables.msrc)
  {
    budget_us += (timeouts.msrc_dss_tcc_us + MsrcOverhead);
  }

  if (enables.pre_range)
  {
    budget_us += (timeouts.pre_range_us + PreRangeOverhead);
  }

  if (enables.final_range)
  {
    budget_us += (timeouts.final_range_us + FinalRangeOverhead);
  }

  measurement_timing_budget_us = budget_us; // store for internal reuse
  return budget_us;
}

// Set the VCSEL (vertical cavity surface emitting laser) pulse period for the
// given period type (pre-range or final range) to the given value in PCLKs.
// Longer periods seem to increase the potential range of the sensor.
// Valid values are (even numbers only):
//  pre:  12 to 18 (initialized default: 14)
//  final: 8 to 14 (initialized default: 10)
// based on VL53L0X_set_vcsel_pulse_period()
bool VL53L0X::setVcselPulsePeriod(vcselPeriodType type, uint8_t period_pclks)
{
  uint8_t vcsel_period_reg = encodeVcselPeriod(period_pclks);

  SequenceStepEnables enables;
  SequenceStepTimeouts timeouts;

  getSequenceStepEnables(&enables);
  getSequenceStepTimeouts(&enables, &timeouts);

  // "Apply specific settings for the requested clock period"
  // "Re-calculate and apply timeouts, in macro periods"

  // "When the VCSEL period for the pre or final range is changed,
  // the corresponding timeout must be read from the device using
  // the current VCSEL period, then the new VCSEL period can be
  // applied. The timeout then must be written back to the device
  // using the new VCSEL period.
  //
  // For the MSRC timeout, the same applies - this timeout being
  // dependant on the pre-range vcsel period."


  if (type == VcselPeriodPreRange)
  {
    // "Set phase check limits"
    switch (period_pclks)
    {
      case 12:
        writeReg(PRE_RANGE_CONFIG_VALID_PHASE_HIGH, 0x18);
        break;

      case 14:
        writeReg(PRE_RANGE_CONFIG_VALID_PHASE_HIGH, 0x30);
        break;

      case 16:
        writeReg(PRE_RANGE_CONFIG_VALID_PHASE_HIGH, 0x40);
        break;

      case 18:
        writeReg(PRE_RANGE_CONFIG_VALID_PHASE_HIGH, 0x50);
        break;

      default:
        // invalid period
        return false;
    }
    writeReg(PRE_RANGE_CONFIG_VALID_PHASE_LOW, 0x08);

    // apply new VCSEL period
    writeReg(PRE_RANGE_CONFIG_VCSEL_PERIOD, vcsel_period_reg);

    // update timeouts

    // set_sequence_step_timeout() begin
    // (SequenceStepId == VL53L0X_SEQUENCESTEP_PRE_RANGE)

    uint16_t new_pre_range_timeout_mclks =
      timeoutMicrosecondsToMclks(timeouts.pre_range_us, period_pclks);

    writeReg16Bit(PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI,
      encodeTimeout(new_pre_range_timeout_mclks));

    // set_sequence_step_timeout() end

    // set_sequence_step_timeout() begin
    // (SequenceStepId == VL53L0X_SEQUENCESTEP_MSRC)

    uint16_t new_msrc_timeout_mclks =
      timeoutMicrosecondsToMclks(timeouts.msrc_dss_tcc_us, period_pclks);

    writeReg(MSRC_CONFIG_TIMEOUT_MACROP,
      (new_msrc_timeout_mclks > 256) ? 255 : (new_msrc_timeout_mclks - 1));

    // set_sequence_step_timeout() end
  }
  else if (type == VcselPeriodFinalRange)
  {
    switch (period_pclks)
    {
      case 8:
        writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, 0x10);
        writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_LOW,  0x08);
        writeReg(GLOBAL_CONFIG_VCSEL_WIDTH, 0x02);
        writeReg(ALGO_PHASECAL_CONFIG_TIMEOUT, 0x0C);
        writeReg(0xFF, 0x01);
        writeReg(ALGO_PHASECAL_LIM, 0x30);
        writeReg(0xFF, 0x00);
        break;

      case 10:
        writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, 0x28);
        writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_LOW,  0x08);
        writeReg(GLOBAL_CONFIG_VCSEL_WIDTH, 0x03);
        writeReg(ALGO_PHASECAL_CONFIG_TIMEOUT, 0x09);
        writeReg(0xFF, 0x01);
        writeReg(ALGO_PHASECAL_LIM, 0x20);
        writeReg(0xFF, 0x00);
        break;

      case 12:
        writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, 0x38);
        writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_LOW,  0x08);
        writeReg(GLOBAL_CONFIG_VCSEL_WIDTH, 0x03);
        writeReg(ALGO_PHASECAL_CONFIG_TIMEOUT, 0x08);
        writeReg(0xFF, 0x01);
        writeReg(ALGO_PHASECAL_LIM, 0x20);
        writeReg(0xFF, 0x00);
        break;

      case 14:
        writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, 0x48);
        writeReg(FINAL_RANGE_CONFIG_VALID_PHASE_LOW,  0x08);
        writeReg(GLOBAL_CONFIG_VCSEL_WIDTH, 0x03);
        writeReg(ALGO_PHASECAL_CONFIG_TIMEOUT, 0x07);
        writeReg(0xFF, 0x01);
        writeReg(ALGO_PHASECAL_LIM, 0x20);
        writeReg(0xFF, 0x00);
        break;

      default:
        // invalid period
        return false;
    }

    // apply new VCSEL period
    writeReg(FINAL_RANGE_CONFIG_VCSEL_PERIOD, vcsel_period_reg);

    // update timeouts

    // set_sequence_step_timeout() begin
    // (SequenceStepId == VL53L0X_SEQUENCESTEP_FINAL_RANGE)

    // "For the final range timeout, the pre-range timeout
    //  must be added. To do this both final and pre-range
    //  timeouts must be expressed in macro periods MClks
    //  because they have different vcsel periods."

    uint16_t new_final_range_timeout_mclks =
      timeoutMicrosecondsToMclks(timeouts.final_range_us, period_pclks);

    if (enables.pre_range)
    {
      new_final_range_timeout_mclks += timeouts.pre_range_mclks;
    }

    writeReg16Bit(FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI,
      encodeTimeout(new_final_range_timeout_mclks));

    // set_sequence_step_timeout end
  }
  else
  {
    // invalid type
    return false;
  }

  // "Finally, the timing budget must be re-applied"

  setMeasurementTimingBudget(measurement_timing_budget_us);

  // "Perform the phase calibration. This is needed after changing on vcsel period."
  // VL53L0X_perform_phase_calibration() begin

  uint8_t sequence_config = readReg(SYSTEM_SEQUENCE_CONFIG);
  writeReg(SYSTEM_SEQUENCE_CONFIG, 0x02);
  performSingleRefCalibration(0x0);
  writeReg(SYSTEM_SEQUENCE_CONFIG, sequence_config);

  // VL53L0X_perform_phase_calibration() end

  return true;
}

// Get the VCSEL pulse period in PCLKs for the given period type.
// based on VL53L0X_get_vcsel_pulse_period()
uint8_t VL53L0X::getVcselPulsePeriod(vcselPeriodType type)
{
  if (type == VcselPeriodPreRange)
  {
    return decodeVcselPeriod(readReg(PRE_RANGE_CONFIG_VCSEL_PERIOD));
  }
  else if (type == VcselPeriodFinalRange)
  {
    return decodeVcselPeriod(readReg(FINAL_RANGE_CONFIG_VCSEL_PERIOD));
  }
  else { return 255; }
}

// Start continuous ranging measurements. If period_ms (optional) is 0 or not
// given, continuous back-to-back mode is used (the sensor takes measurements as
// often as possible); otherwise, continuous timed mode is used, with the given
// inter-measurement period in milliseconds determining how often the sensor
// takes a measurement.
// based on VL53L0X_StartMeasurement()
void VL53L0X::startContinuous(uint32_t period_ms)
{
  writeReg(0x80, 0x01);
  writeReg(0xFF, 0x01);
  writeReg(0x00, 0x00);
  writeReg(0x91, stop_variable);
  writeReg(0x00, 0x01);
  writeReg(0xFF, 0x00);
  writeReg(0x80, 0x00);

  if (period_ms != 0)
  {
    // continuous timed mode

    // VL53L0X_SetInterMeasurementPeriodMilliSeconds() begin

    uint16_t osc_calibrate_val = readReg16Bit(OSC_CALIBRATE_VAL);

    if (osc_calibrate_val != 0)
    {
      period_ms *= osc_calibrate_val;
    }

    writeReg32Bit(SYSTEM_INTERMEASUREMENT_PERIOD, period_ms);

    // VL53L0X_SetInterMeasurementPeriodMilliSeconds() end

    writeReg(SYSRANGE_START, 0x04); // VL53L0X_REG_SYSRANGE_MODE_TIMED
  }
  else
  {
    // continuous back-to-back mode
    writeReg(SYSRANGE_START, 0x02); // VL53L0X_REG_SYSRANGE_MODE_BACKTOBACK
  }
}

// Stop continuous measurements
// based on VL53L0X_StopMeasurement()
void VL53L0X::stopContinuous()
{
  writeReg(SYSRANGE_START, 0x01); // VL53L0X_REG_SYSRANGE_MODE_SINGLESHOT

  writeReg(0xFF, 0x01);
  writeReg(0x00, 0x00);
  writeReg(0x91, 0x00);
  writeReg(0x00, 0x01);
  writeReg(0xFF, 0x00);
}

// Returns a range reading in millimeters when continuous mode is active
// (readRangeSingleMillimeters() also calls this function after starting a
// single-shot range measurement)
uint16_t VL53L0X::readRangeContinuousMillimeters()
{
  startTimeout();
  while ((readReg(RESULT_INTERRUPT_STATUS) & 0x07) == 0)
  {
    if (checkTimeoutExpired())
    {
      did_timeout = true;
      return 65535;
    }
  }

  // assumptions: Linearity Corrective Gain is 1000 (default);
  // fractional ranging is not enabled
  uint16_t range = readReg16Bit(RESULT_RANGE_STATUS + 10);

  writeReg(SYSTEM_INTERRUPT_CLEAR, 0x01);

  return range;
}

// Performs a single-shot range measurement and returns the reading in
// millimeters
// based on VL53L0X_PerformSingleRangingMeasurement()
uint16_t VL53L0X::readRangeSingleMillimeters()
{
  writeReg(0x80, 0x01);
  writeReg(0xFF, 0x01);
  writeReg(0x00, 0x00);
  writeReg(0x91, stop_variable);
  writeReg(0x00, 0x01);
  writeReg(0xFF, 0x00);
  writeReg(0x80, 0x00);

  writeReg(SYSRANGE_START, 0x01);

  // "Wait until start bit has been cleared"
  startTimeout();
  while (readReg(SYSRANGE_START) & 0x01)
  {
    if (checkTimeoutExpired())
    {
      did_timeout = true;
      return 65535;
    }
  }

  return readRangeContinuousMillimeters();
}

// Did a timeout occur in one of the read functions since the last call to
// timeoutOccurred()?
bool VL53L0X::timeoutOccurred()
{
  bool tmp = did_timeout;
  did_timeout = false;
  return tmp;
}

// Private Methods /////////////////////////////////////////////////////////////

// Get reference SPAD (single photon avalanche diode) count and type
// based on VL53L0X_get_info_from_device(),
// but only gets reference SPAD count and type
bool VL53L0X::getSpadInfo(uint8_t * count, bool * type_is_aperture)
{
  uint8_t tmp;

  writeReg(0x80, 0x01);
  writeReg(0xFF, 0x01);
  writeReg(0x00, 0x00);

  writeReg(0xFF, 0x06);
  writeReg(0x83, readReg(0x83) | 0x04);
  writeReg(0xFF, 0x07);
  writeReg(0x81, 0x01);

  writeReg(0x80, 0x01);

  writeReg(0x94, 0x6b);
  writeReg(0x83, 0x00);
  startTimeout();
  while (readReg(0x83) == 0x00)
  {
    if (checkTimeoutExpired()) { return false; }
  }
  writeReg(0x83, 0x01);
  tmp = readReg(0x92);

  *count = tmp & 0x7f;
  *type_is_aperture = (tmp >> 7) & 0x01;

  writeReg(0x81, 0x00);
  writeReg(0xFF, 0x06);
  writeReg(0x83, readReg(0x83)  & ~0x04);
  writeReg(0xFF, 0x01);
  writeReg(0x00, 0x01);

  writeReg(0xFF, 0x00);
  writeReg(0x80, 0x00);

  return true;
}

// Get sequence step enables
// based on VL53L0X_GetSequenceStepEnables()
void VL53L0X::getSequenceStepEnables(SequenceStepEnables * enables)
{
  uint8_t sequence_config = readReg(SYSTEM_SEQUENCE_CONFIG);

  enables->tcc          = (sequence_config >> 4) & 0x1;
  enables->dss          = (sequence_config >> 3) & 0x1;
  enables->msrc         = (sequence_config >> 2) & 0x1;
  enables->pre_range    = (sequence_config >> 6) & 0x1;
  enables->final_range  = (sequence_config >> 7) & 0x1;
}

// Get sequence step timeouts
// based on get_sequence_step_timeout(),
// but gets all timeouts instead of just the requested one, and also stores
// intermediate values
void VL53L0X::getSequenceStepTimeouts(SequenceStepEnables const * enables, SequenceStepTimeouts * timeouts)
{
  timeouts->pre_range_vcsel_period_pclks = getVcselPulsePeriod(VcselPeriodPreRange);

  timeouts->msrc_dss_tcc_mclks = readReg(MSRC_CONFIG_TIMEOUT_MACROP) + 1;
  timeouts->msrc_dss_tcc_us =
    timeoutMclksToMicroseconds(timeouts->msrc_dss_tcc_mclks,
                               timeouts->pre_range_vcsel_period_pclks);

  timeouts->pre_range_mclks =
    decodeTimeout(readReg16Bit(PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI));
  timeouts->pre_range_us =
    timeoutMclksToMicroseconds(timeouts->pre_range_mclks,
                               timeouts->pre_range_vcsel_period_pclks);

  timeouts->final_range_vcsel_period_pclks = getVcselPulsePeriod(VcselPeriodFinalRange);

  timeouts->final_range_mclks =
    decodeTimeout(readReg16Bit(FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI));

  if (enables->pre_range)
  {
    timeouts->final_range_mclks -= timeouts->pre_range_mclks;
  }

  timeouts->final_range_us =
    timeoutMclksToMicroseconds(timeouts->final_range_mclks,
                               timeouts->final_range_vcsel_period_pclks);
}

// Decode sequence step timeout in MCLKs from register value
// based on VL53L0X_decode_timeout()
// Note: the original function returned a uint32_t, but the return value is
// always stored in a uint16_t.
uint16_t VL53L0X::decodeTimeout(uint16_t reg_val)
{
  // format: "(LSByte * 2^MSByte) + 1"
  return (uint16_t)((reg_val & 0x00FF) <<
         (uint16_t)((reg_val & 0xFF00) >> 8)) + 1;
}

// Encode sequence step timeout register value from timeout in MCLKs
// based on VL53L0X_encode_timeout()
uint16_t VL53L0X::encodeTimeout(uint32_t timeout_mclks)
{
  // format: "(LSByte * 2^MSByte) + 1"

  uint32_t ls_byte = 0;
  uint16_t ms_byte = 0;

  if (timeout_mclks > 0)
  {
    ls_byte = timeout_mclks - 1;

    while ((ls_byte & 0xFFFFFF00) > 0)
    {
      ls_byte >>= 1;
      ms_byte++;
    }

    return (ms_byte << 8) | (ls_byte & 0xFF);
  }
  else { return 0; }
}

// Convert sequence step timeout from MCLKs to microseconds with given VCSEL period in PCLKs
// based on VL53L0X_calc_timeout_us()
uint32_t VL53L0X::timeoutMclksToMicroseconds(uint16_t timeout_period_mclks, uint8_t vcsel_period_pclks)
{
  uint32_t macro_period_ns = calcMacroPeriod(vcsel_period_pclks);

  return ((timeout_period_mclks * macro_period_ns) + 500) / 1000;
}

// Convert sequence step timeout from microseconds to MCLKs with given VCSEL period in PCLKs
// based on VL53L0X_calc_timeout_mclks()
uint32_t VL53L0X::timeoutMicrosecondsToMclks(uint32_t timeout_period_us, uint8_t vcsel_period_pclks)
{
  uint32_t macro_period_ns = calcMacroPeriod(vcsel_period_pclks);

  return (((timeout_period_us * 1000) + (macro_period_ns / 2)) / macro_period_ns);
}


// based on VL53L0X_perform_single_ref_calibration()
bool VL53L0X::performSingleRefCalibration(uint8_t vhv_init_byte)
{
  writeReg(SYSRANGE_START, 0x01 | vhv_init_byte); // VL53L0X_REG_SYSRANGE_MODE_START_STOP

  startTimeout();
  while ((readReg(RESULT_INTERRUPT_STATUS) & 0x07) == 0)
  {
    if (checkTimeoutExpired()) { return false; }
  }

  writeReg(SYSTEM_INTERRUPT_CLEAR, 0x01);

  writeReg(SYSRANGE_START, 0x00);

  return true;
}

... e se non serve anche a qualcos'altro modificare questa??

// Read a 16-bit register
uint16_t VL53L0X::readReg16Bit(uint8_t reg)
{
  uint16_t value;

  bus->beginTransmission(address);
  bus->write(reg);
  last_status = bus->endTransmission();

  bus->requestFrom(address, (uint8_t)2);
  value  = (uint16_t)bus->read() << 8; // value high byte
  value |=           bus->read();      // value low byte

  return value;
}

posso postare nel forum galattico?

mi sembra di capire che vinca il piu vicono!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.