Advertisement
Guest User

vl53l1x calibration

a guest
Oct 12th, 2022
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <SparkFun_VL53L1X.h>
  3. #include <Wire.h>
  4.  
  5. /* I2C-1 standard pins: PB7(sda) PB6(scl) */
  6.  
  7.  
  8. /*
  9. // Default pin used for generic 'Serial' instance
  10. // Mandatory for Firmata
  11. #ifndef PIN_SERIAL_RX
  12.   #define PIN_SERIAL_RX         PA3
  13. #endif
  14. #ifndef PIN_SERIAL_TX
  15.   #define PIN_SERIAL_TX         PA2
  16. #endif
  17.  */
  18.  
  19. #define OFFSET_CALIBRATION_DISTANCE 140    // mm
  20. #define XTALK_CALIBRATION_DISTANCE 500    // mm
  21. int xtalkCalibrationDistance = XTALK_CALIBRATION_DISTANCE;
  22. #define SHUTDOWN_PIN PB7
  23. #define INTERRUPT_PIN PB6
  24. SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);
  25.  
  26. #define Button_Pin PA0
  27. unsigned long oneButtonPeriod = 100;    // the debounce time; increase if the output flickers
  28. int buttonState;             // the current reading from the input pin
  29. int lastButtonState = LOW;   // the previous reading from the input pin
  30.  
  31. // VL53L1X* ranger = distanceSensor._device;
  32.  
  33. void readDistance();
  34. void calibrateXTalk();
  35. void calibrateOffset();
  36.  
  37. void setup()
  38. {
  39.     pinMode(LED_BUILTIN,OUTPUT);
  40.     digitalWrite(LED_BUILTIN, LOW);
  41.     pinMode(Button_Pin, INPUT);
  42.  
  43.     Serial.setTx(PB10);
  44.     Serial.setRx(PB11);
  45.     Serial.begin(115200);
  46.     Wire.setSDA(PB9);
  47.     Wire.setSCL(PB8);
  48.     Wire.setClock(10000U);
  49.     Wire.begin();
  50.    
  51.     Serial.println("VL53L1X Qwiic Test");
  52.  
  53.     if (distanceSensor.begin() != 0) // Begin returns 0 on a good init
  54.     {
  55.         Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
  56.         while (1)
  57.             ;
  58.     }
  59.     Serial.println("Sensor online!");
  60.     distanceSensor.setDistanceModeShort();
  61.     distanceSensor.setTimingBudgetInMs(100);
  62.     distanceSensor.setOffset((int16_t)2034);
  63.     // ranger.VL53L1X_SensorInit();
  64.     // ranger.VL53L1X_StartRanging();
  65.     delay(100);
  66.     digitalWrite(LED_BUILTIN, HIGH);
  67. }
  68.  
  69. void loop()
  70. {
  71.     static uint32_t prevmillis = 0;
  72.     static int pressedButton = 0;
  73.     if (millis() - prevmillis >= oneButtonPeriod)
  74.     {
  75.         bool buttonState = digitalRead(Button_Pin);
  76.         pressedButton += buttonState;
  77.         if (pressedButton >= 1 && buttonState == LOW /* Unpressed */)
  78.         {
  79.             Serial.printf("Debounced button\n");
  80.             if (pressedButton > 10 /* Long press */)
  81.             {
  82.                 calibrateXTalk();
  83.             }
  84.             else
  85.             {
  86.                 calibrateOffset();
  87.             }
  88.             pressedButton = 0;
  89.         }
  90.         prevmillis = millis();
  91.     }
  92.     // readDistance();
  93.     // delay(1000);
  94.  
  95.     static uint32_t prevmillis_Distance;
  96.     if (millis() - prevmillis_Distance >= 500)
  97.     {
  98.         readDistance();
  99.         prevmillis_Distance = millis();
  100.         if (Serial.available())
  101.         {
  102.             auto i = Serial.parseInt();
  103.             if (i)
  104.             {
  105.                 Serial.printf("The xTalk distance is set to %d\n", i);
  106.                 xtalkCalibrationDistance = i;
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112. void calibrateXTalk()
  113. {
  114.     auto currentXtalk = distanceSensor.getXTalk();
  115.     distanceSensor.calibrateXTalk(xtalkCalibrationDistance);
  116.     auto calibratedXtalk = distanceSensor.getXTalk();
  117.  
  118.     Serial.printf("\tcurrentXtalk=%u\tcalibratedXtalk=%u\n", currentXtalk, calibratedXtalk);
  119. }
  120. void calibrateOffset()
  121. {
  122.     auto currentOffset = distanceSensor.getOffset();
  123.     distanceSensor.calibrateOffset(OFFSET_CALIBRATION_DISTANCE);
  124.     auto calibratedOffset = distanceSensor.getOffset();
  125.     Serial.printf("\tcurrentOffset=%u\tcalibratedOffset=%u\n", currentOffset, calibratedOffset);
  126.  
  127. }
  128. void readDistance()
  129. {
  130.     distanceSensor.startOneshotRanging(); // Write configuration bytes to initiate measurement
  131.     while (!distanceSensor.checkForDataReady())
  132.     {
  133.         delay(1);
  134.     }
  135.     int distance = distanceSensor.getDistance(); // Get the result of the measurement from the sensor
  136.     distanceSensor.clearInterrupt();
  137.     distanceSensor.stopRanging();
  138.  
  139.     Serial.printf("Distance(mm): %d\n", distance);
  140.  
  141.     // float distanceInches = distance * 0.0393701;
  142.     // float distanceFeet = distanceInches / 12.0;
  143.  
  144.     // Serial.print("\tDistance(ft): ");
  145.     // Serial.print(distanceFeet, 2);
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement