Skip to content
Snippets Groups Projects
Select Git revision
  • 38d3784f1870af98c9a537bf0a14269b386f6fe5
  • develop default protected
  • 5.5
  • 5.1
  • master protected
  • deprecated/4-22
6 results

UniversalLogging.cpp

Blame
  • BITs_tof_array.h 3.39 KiB
    /*
     * Klasse: BITsTimeOfFlight
     *
     * Zur Erzeugung eines Arrays gefüllt mit Objekten für die Ansteuerung von mehreren VL53L0X Time of Flight Sensoren
     *
     * O.-T. Altan, Januar 2022
     *
     */
    
    /*
    * Auslagerung in source Datei wünschenswert.
    */
    
    #ifndef BITS_TOF_ARRAY_H
    #define BITS_TOF_ARRAY_H
    
    #include "Adafruit_VL53L0X.h"
    
    #ifdef DEBUG_TOF
    #define _println_tof_(x) Serial.println(x)
    #define _print_tof_(x) Serial.print(x)
    #else
    #define _print_tof_(x)
    #define _println_tof_(x)
    #endif
    
    const int TOFS = 5;                                             // Anzahl der zu Verwendenden Time of Flight Sensoren
    const int TOF_XSHUTS[TOFS] = {0, 9, 11, 31, A10};               // XShut Pins der einzelnen Time of Flight Sensoren
    const int TOF_ADDRESSES[TOFS] = {0x30, 0x31, 0x32, 0x33, 0x34}; // Adresse der einzelnen Time of Flight Sensoren
    
    class BITsTimeOfFlight
    {
    public:
      void init()
      {
        for (int i = 0; i < COUNT_SENSORS; i++)
        {
          pinMode(sensors[i].shutdown_pin, OUTPUT);
          digitalWrite(sensors[i].shutdown_pin, LOW);
        }
    
        bool found_any_sensors = false;
        for (int i = 0; i < COUNT_SENSORS; i++)
          digitalWrite(sensors[i].shutdown_pin, LOW);
        delay(10);
    
        for (int i = 0; i < COUNT_SENSORS; i++)
        {
          digitalWrite(sensors[i].shutdown_pin, HIGH);
          delay(100);
          if (sensors[i].psensor->begin(sensors[i].id, false, sensors[i].pwire,
                                        sensors[i].sensor_config))
          {
            found_any_sensors = true;
          }
          else
          {
            _print_tof_(i);
            _print_tof_(F(": failed to start\n"));
          }
        }
        if (!found_any_sensors)
        {
          _println_tof_("No valid sensors found");
          while (1)
            ;
        }
      };
    
      int get_distance(int i)
      {
        VL53L0X_RangingMeasurementData_t measure;
    
        sensors[i].psensor->rangingTest(&measure, false); // 'true' um debug Informationen auszugeben
    
        if (measure.RangeStatus != 4)
        { // phase failures have incorrect data
          tof_dist = measure.RangeMilliMeter / 10;
          _print_tof_(i);
          _print_tof_(" Sensor Entfernung (cm): ");
          _println_tof_(tof_dist);
        }
        else
        {
          _println_tof_(" Ausserhalb des Messbereichs ");
          tof_dist = 200;
        }
        return tof_dist;
      };
    
    private:
      int tof_dist;
    
      typedef struct
      {
        Adafruit_VL53L0X *psensor; // Zeiger zum Objekt
        TwoWire *pwire;
        int id;           // Adresse des Sensors
        int shutdown_pin; // XShut Pin des Sensors
        Adafruit_VL53L0X::VL53L0X_Sense_config_t
            sensor_config; // Option wie der Sensor verwendet werden soll
        uint16_t range;    // Wertebereich für den continuous mode
        uint8_t sensor_status;
      } sensorList_t;
    
      Adafruit_VL53L0X tof1;
      Adafruit_VL53L0X tof2;
      Adafruit_VL53L0X tof3;
      Adafruit_VL53L0X tof4;
      Adafruit_VL53L0X tof5;
    
      sensorList_t sensors[TOFS] =
          {
              {&tof1, &Wire, TOF_ADDRESSES[0], TOF_XSHUTS[0], Adafruit_VL53L0X::VL53L0X_SENSE_HIGH_SPEED, 0, 0},
              {&tof2, &Wire, TOF_ADDRESSES[1], TOF_XSHUTS[1], Adafruit_VL53L0X::VL53L0X_SENSE_HIGH_SPEED, 0, 0},
              {&tof3, &Wire, TOF_ADDRESSES[2], TOF_XSHUTS[2], Adafruit_VL53L0X::VL53L0X_SENSE_HIGH_SPEED, 0, 0},
              {&tof4, &Wire, TOF_ADDRESSES[3], TOF_XSHUTS[3], Adafruit_VL53L0X::VL53L0X_SENSE_HIGH_SPEED, 0, 0},
              {&tof5, &Wire, TOF_ADDRESSES[4], TOF_XSHUTS[4], Adafruit_VL53L0X::VL53L0X_SENSE_HIGH_SPEED, 0, 0}};
    
      const int COUNT_SENSORS = sizeof(sensors) / sizeof(sensors[0]);
    };
    
    #endif