شِفر (کود) جستجو

درس ۷۶-۱: استفاده از یک حساس(حس کننده) فاصله لیزری VL6180 با آردوینو

درس ۷۶-۱: استفاده از یک حساس(حس کننده) فاصله لیزری VL6180 با آردوینو

درس ۷۶-۱: استفاده از یک حساس(حس کننده) فاصلهسنج لیزری VL6180 با آردوینو

در این آموزش، نحوه استفاده از حساس(حس کننده) فاصلهسنج لیزری VL6180X با برد آردوینو را یاد خواهیم گرفت. این حساس(حس کننده) جمعوجور میتواند فاصلهها را با دقت بالا تا ۲۰ سانتیمتر اندازهگیری کند و آن را برای کاربردهای مختلف در رباتیک و تشخیص اشیا مناسب میسازد. در پایان این آموزش، درک روشنی از نحوه سیمکشی حساس(حس کننده)، نصب کتابخانه لازم و پیادهسازی شِفر (کود) برای دریافت خوانشهای فاصله خواهید داشت.

VL6080X Sensor top view

حساس(حس کننده) VL6180X با استفاده از اصل زمان پرواز (ToF) کار میکند، به این صورت که یک لیزر منتشر کرده و زمان بازگشت نور پس از برخورد به یک جسم را اندازهگیری میکند. این روش امکان اندازهگیری دقیق فاصله را فراهم میکند و به طور گسترده در دستگاههای تلفن همراه و رباتیک استفاده میشود. در بخشهای بعدی، اجزای سختافزاری، دستورالعملهای سیمکشی و پیادهسازی شِفر (کود) را مورد بحث قرار خواهیم داد.

توضیح سختافزار

جزء اصلی این پروژه حساس(حس کننده) فاصلهسنج لیزری VL6180X است. این حساس(حس کننده) دارای رابط I2C است که اتصال آن به آردوینو را آسان میکند. این حساس(حس کننده) چندین پایه از جمله پینهای برق، زمین و خطوط داده دارد. حساس(حس کننده) در محدوده ولتاژ ۲.۶ تا ۳.۶ ولت کار میکند و میتواند فاصلهها را با دقت بالا اندازهگیری کند.

علاوه بر این، برد آردوینو به عنوان میکروکنترلری عمل میکند که دادهها را از حساس(حس کننده) میخواند و پردازش میکند. راهاندازی به اجزای الکترونیکی پایه مانند سیمهای جامپر و بردبورد برای اتصالات نیاز دارد. درک نحوه تعامل هر جزء برای اجرای موفقیتآمیز بسیار مهم است.

جزئیات دیتاشیت

سازندهSTMicroelectronics
شماره قطعهVL6180X
ولتاژ منطق/IO۲.۶ - ۳.۶ ولت
ولتاژ تغذیه۲.۶ - ۳.۶ ولت
جریان خروجی۱.۷ میلیآمپر (معمولی)
حداکثر جریان۱۰ میلیآمپر (حداکثر)
راهنمای فرکانس PWMنامعتبر
آستانههای منطق ورودینامعتبر
افت ولتاژ / RDS(on) / اشباعنامعتبر
محدودیتهای حرارتی-۲۰ تا ۷۰ درجه سانتیگراد
بسته۴.۸ × ۲.۸ × ۱ میلیمتر
یادداشتها / انواعحساس(حس کننده) مجاورت و فاصله

  • اطمینان حاصل کنید که منبع تغذیه در محدوده مشخص شده است تا از آسیب به حساس(حس کننده) جلوگیری شود.
  • در صورت نیاز برای ارتباط مطمئن، از مقاومتهای pull-up در خطوط I2C استفاده کنید.
  • حساس(حس کننده) را از نور مستقیم خورشید دور نگه دارید تا خوانشهای دقیق داشته باشید.
  • برای اتصالات و تنظیمات آسان، استفاده از بردبورد را در نظر بگیرید.
  • مستندات کتابخانه را برای استفاده خاص از توابع و قابلیتها بررسی کنید.

دستورالعملهای سیمکشی

Wiring VL6180 Obstacle Avoidance with Arduino
اتصال VL6180 جلوگیری از موانع با آردوینو — Wiring VL6180 Obstacle Avoidance with Arduino
VL6180_dual_sensor_bb

برای سیمکشی حساس(حس کننده) VL6180X به آردوینو، ابتدا پایه VCC حساس(حس کننده) را به پایه 5V آردوینو وصل کنید. سپس پایه GND حساس(حس کننده) را به پایه GND آردوینو متصل کنید. پایه SCL حساس(حس کننده) باید به A5 و پایه SDA به A4 آردوینو متصل شود.

مطمئن شوید که حساس(حس کننده) را به این ترتیب وصل میکنید: VCC به 5V، GND به GND، SDA به A4 و SCL به A5. اگر قصد استفاده از چند حساس(حس کننده) را دارید، اطمینان حاصل کنید که هر کدام آدرس I2C منحصر به فردی دارند و پینهای خاموشسازی به طور مناسب مدیریت میشوند.

مثالهای شِفر (کود) و راهنمای گام به گام

در شِفر (کود)، با شامل کردن کتابخانههای لازم و مقداردهی اولیه نمونه حساس(حس کننده) شروع میکنیم. بخش زیر نحوه راهاندازی ارتباط I2C و بررسی وجود حساس(حس کننده) را نشان میدهد:

void setup() {
  Serial.begin(115200);
  while (!Serial) { delay(1); }
  
  if (! vl.begin()) {
    Serial.println("Failed to find sensor");
    while (1);
  }
  Serial.println("Sensor found!");
}

این شِفر (کود) نمایشگر مسلسل را مقداردهی اولیه میکند و سعی در شروع ارتباط با حساس(حس کننده) VL6180X دارد. اگر حساس(حس کننده) پیدا نشود، پیام خطا چاپ شده و برنامه متوقف میشود.

در ادامه، فاصله را از حساس(حس کننده) میخوانیم و خطاهای احتمالی را بررسی میکنیم:

void loop() {
  uint8_t range = vl.readRange();
  uint8_t status = vl.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.print(range);
    Serial.println("mm");
  }
}

این بخش فاصله اندازهگیریشده توسط حساس(حس کننده) را میخواند و آن را در نمایشگر مسلسل چاپ میکند. اگر خطایی وجود داشته باشد، پیامهای خطای مربوطه بر اساس وضعیت برگشتی قابل چاپ هستند.

نمایش / انتظار چه چیزی را داشته باشیم

پس از تکمیل سیمکشی و بارگذاری شِفر (کود) روی آردوینو، باید خوانشهای فاصله را در نمایشگر مسلسل مشاهده کنید. وقتی یک جسم در محدوده حساس(حس کننده) قرار میگیرد، فاصله بر حسب میلیمتر نمایش داده میشود. اگر جسم خیلی دور یا خیلی نزدیک باشد، حساس(حس کننده) پیام خطا برمیگرداند. این رفتار را میتوانید در ویدیوی نمایشی تأیید کنید (در ویدیو در زمان ۱۹:۳۰).

زمانبندیهای ویدیو

  • ۰۰:۰۰ شروع
  • ۰۰:۵۶ مقدمه
  • ۰۴:۳۰ مشاهده دیتاشیت VL6080V
  • ۰۶:۵۸ نمایش سیمکشی
  • ۰۷:۳۲ سیمکشی برای دو یا چند حساس(حس کننده)
  • ۰۹:۲۲ نصب کتابخانه
  • ۱۰:۳۰ توضیح شِفر (کود) آردوینو (۱ حساس(حس کننده))
  • ۱۲:۵۷ شِفر (کود) برای ۲ یا چند حساس(حس کننده)
  • ۱۹:۳۵ نمایش با ۱ حساس(حس کننده)
  • ۲۲:۰۵ نمایش با ۲ حساس(حس کننده)

تصاویر

حساس(حس کننده) VL6080X
حساس(حس کننده) VL6080X
حساس(حس کننده) VL6080X در پشت PCB
حساس(حس کننده) VL6080X در پشت PCB
نمای بالا از حساس(حس کننده) VL6080X
نمای بالا از حساس(حس کننده) VL6080X
حساس(حس کننده) عمودی VL6080X
حساس(حس کننده) عمودی VL6080X
اتصال VL6180 جلوگیری از موانع با آردوینو
اتصال VL6180 جلوگیری از موانع با آردوینو
VL6180_wiring_new
VL6180_wiring_new
VL6180_dual_sensor_bb
VL6180_dual_sensor_bb
361-Arduino code for VL6180X 20cm Time-of-Flight proximity sensor
زبان: C++
/*
 *  Arduino code 
Using a single VL6180X 20cm Time-of-Flight proximity sensor with Arduino

View code for using a single VL6180X sensor: https://robojax.com?vid=robojax_VL6180X_laser2

 * Original code and library by https://github.com/adafruit/Adafruit_VL6180X
 * 
 * Written/updated by Ahmad Shamshiri for Robojax Robojax.com
 * on Mar 10, 2021  in Ajax, Ontario, Canada
Watch the video instruction for this sketch: https://youtu.be/_H9D0czQpSI
 


If you found this tutorial helpful, please support me so I can continue creating 
content like this. 
or make a donation using PayPal http://robojax.com/L/?id=64

* 
 * Code is available at http://robojax.com/learn/arduino

 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. 

*/
#include <Wire.h>
#include "Adafruit_VL6180X.h"

Adafruit_VL6180X vl = Adafruit_VL6180X();

void setup() {
  Serial.begin(115200);

  // wait for serial port to open on native usb devices
  while (!Serial) {
    delay(1);
  }
  
  Serial.println("Adafruit VL6180x test!");
  if (! vl.begin()) {
    Serial.println("Failed to find sensor");
    while (1);
  }
  Serial.println("Sensor found!");
}

void loop() {

  uint8_t range = vl.readRange();
  uint8_t status = vl.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.print(range);
    Serial.println("mm");
  }
//  if(range <=76)
//  {
//    //do something
//  }

  // Some error occurred, print it out!
  
  if  ((status >= VL6180X_ERROR_SYSERR_1) && (status <= VL6180X_ERROR_SYSERR_5)) {
    Serial.println("System error");
  }
  else if (status == VL6180X_ERROR_ECEFAIL) {
    Serial.println("ECE failure");
  }
  else if (status == VL6180X_ERROR_NOCONVERGE) {
    Serial.println("No convergence");
  }
  else if (status == VL6180X_ERROR_RANGEIGNORE) {
    Serial.println("Ignoring range");
  }
  else if (status == VL6180X_ERROR_SNR) {
    Serial.println("Signal/Noise error");
  }
  else if (status == VL6180X_ERROR_RAWUFLOW) {
    Serial.println("Raw reading underflow");
  }
  else if (status == VL6180X_ERROR_RAWOFLOW) {
    Serial.println("Raw reading overflow");
  }
  else if (status == VL6180X_ERROR_RANGEUFLOW) {
    Serial.println("Range reading underflow");
  }
  else if (status == VL6180X_ERROR_RANGEOFLOW) {
    Serial.println("Range reading overflow");
  }
  delay(50);
}
449-Lesson 76: Using VL6180 62cm laser distance sensors with Arduino
زبان: C++
/*
* Lesson 76-1: Using one VL6180 laser distance sensor with Arduino 
 * Adafruit code modified for this tutorial
 * by Ahmad Shamshiri at 22:22 on 
 * March 10-11, 2021 in Ajax, ON, Canada
 Please watch video instruction of this code: https://youtu.be/_H9D0czQpSI
 
View code for using two or more VL6180X sensors: https://robojax.com/course1/lecture76

  This video is part of the Arduino Step by Step Course, which starts here: https://youtu.be/-6qSrDUA5a8

 

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon: http://robojax.com/L/?id=63

or make a donation using PayPal: http://robojax.com/L/?id=64
  
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact. * 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. 
 */
#include <Wire.h>
#include "Adafruit_VL6180X.h"

Adafruit_VL6180X vl = Adafruit_VL6180X();

void setup() {
  Serial.begin(115200);

  // wait for serial port to open on native usb devices
  while (!Serial) {
    delay(1);
  }
  
  Serial.println("Adafruit VL6180x test!");
  if (! vl.begin()) {
    Serial.println("Failed to find sensor");
    while (1);
  }
  Serial.println("Sensor found!");
}

void loop() {

  uint8_t range = vl.readRange();
  uint8_t status = vl.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.print(range);
    Serial.println("mm");
  }
//  if(range <=76)
//  {
//    //do something
//  }

  // Some error occurred, print it out!
  
  if  ((status >= VL6180X_ERROR_SYSERR_1) && (status <= VL6180X_ERROR_SYSERR_5)) {
    Serial.println("System error");
  }
  else if (status == VL6180X_ERROR_ECEFAIL) {
    Serial.println("ECE failure");
  }
  else if (status == VL6180X_ERROR_NOCONVERGE) {
    Serial.println("No convergence");
  }
  else if (status == VL6180X_ERROR_RANGEIGNORE) {
    Serial.println("Ignoring range");
  }
  else if (status == VL6180X_ERROR_SNR) {
    Serial.println("Signal/Noise error");
  }
  else if (status == VL6180X_ERROR_RAWUFLOW) {
    Serial.println("Raw reading underflow");
  }
  else if (status == VL6180X_ERROR_RAWOFLOW) {
    Serial.println("Raw reading overflow");
  }
  else if (status == VL6180X_ERROR_RANGEUFLOW) {
    Serial.println("Range reading underflow");
  }
  else if (status == VL6180X_ERROR_RANGEOFLOW) {
    Serial.println("Range reading overflow");
  }
  delay(50);
}
450-Lesson 76: Using 2 ore more VL6180X 62cm Laser Distance Sensors with Arduino
زبان: C++
/*
 *  Arduino code 
 * Lesson 76-2: Using two or more VL6180 Laser Distance Sensors with Arduino 
 * Adafruit code modified for this tutorial 
 * Using two or more VL6180X 20cm Time-of-Flight proximity sensors with Arduino
 *
 * View code for using single VL6180X sensors: https://robojax.com/course1/lecture76
 *
 * Original code and library by https://github.com/adafruit/Adafruit_VL6180X
 * 
 * Written/updated by Ahmad Shamshiri for Robojax Robojax.com
 * on Mar 12, 2021  in Ajax, Ontario, Canada
 * Watch the video instruction for this sketch: https://youtu.be/_H9D0czQpSI
 *
 *
 * Please watch video instruction for this code: https://youtu.be/_H9D0czQpSI
 *
 *  This video is part of Arduino Step by Step Course which starts here: https://youtu.be/-6qSrDUA5a8
 *
 *
 * If you found this tutorial helpful, please support me so I can continue creating 
 * content like this. 
 *
 * or make a donation using PayPal http://robojax.com/L/?id=64
 *  
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
 * This code has been downloaded from Robojax.com
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>. 
 *
 */

#include <Adafruit_VL6180X.h>

// address we will assign if dual sensor is present
#define LOX1_ADDRESS 0x30
#define LOX2_ADDRESS 0x31



// set the pins to shutdown
#define SHT_LOX1 7
#define SHT_LOX2 6


// Optional define GPIO pins to check to see if complete
#define GPIO_LOX1 4
#define GPIO_LOX2 3


#define TIMING_PIN 13

// objects for the VL6180X
Adafruit_VL6180X lox1 = Adafruit_VL6180X();
Adafruit_VL6180X lox2 = Adafruit_VL6180X();


// Setup mode for doing reads
typedef enum {RUN_MODE_DEFAULT, RUN_MODE_TIMED, RUN_MODE_ASYNC, RUN_MODE_GPIO, RUN_MODE_CONT} runmode_t;

runmode_t run_mode = RUN_MODE_DEFAULT;
uint8_t show_command_list = 1;

//==========================================================================
// Define some globals used in the continuous range mode
// Note: going to start table drive this part, may back up and do the rest later
Adafruit_VL6180X *sensors[] = {&lox1, &lox2};
const uint8_t COUNT_SENSORS = sizeof(sensors) / sizeof(sensors[0]);
const int sensor_gpios[COUNT_SENSORS] = {GPIO_LOX1, GPIO_LOX2}; // if any are < 0 will poll instead
uint8_t  tempRange;
uint8_t         sensor_ranges[COUNT_SENSORS];
uint8_t         sensor_status[COUNT_SENSORS];
// Could do with uint8_t for 8 sensors, but just in case...
const uint16_t  ALL_SENSORS_PENDING = ((1 << COUNT_SENSORS) - 1);
uint16_t        sensors_pending = ALL_SENSORS_PENDING;
uint32_t        sensor_last_cycle_time;


/*
    Reset all sensors by setting all of their XSHUT pins low for delay(10), then set all XSHUT high to bring out of reset
    Keep sensor #1 awake by keeping XSHUT pin high
    Put all other sensors into shutdown by pulling XSHUT pins low
    Initialize sensor #1 with lox.begin(new_i2c_address) Pick any number but 0x29 and it must be under 0x7F. Going with 0x30 to 0x3F is probably OK.
    Keep sensor #1 awake, and now bring sensor #2 out of reset by setting its XSHUT pin high.
    Initialize sensor #2 with lox.begin(new_i2c_address) Pick any number but 0x29 and whatever you set the first sensor to
*/
void setID() {
  // all reset
  digitalWrite(SHT_LOX1, LOW);
  digitalWrite(SHT_LOX2, LOW);

  delay(10);

  // all unreset
  digitalWrite(SHT_LOX1, HIGH);
  digitalWrite(SHT_LOX2, HIGH);

  delay(10);

  // activating LOX1 and reseting LOX2
  digitalWrite(SHT_LOX1, HIGH);
  digitalWrite(SHT_LOX2, LOW);


  // initing LOX1
  if (!lox1.begin()) {
    Serial.println(F("Failed to boot first VL6180X"));
    while (1);
  }
  lox1.setAddress(LOX1_ADDRESS);
  delay(10);

  // activating LOX2
  digitalWrite(SHT_LOX2, HIGH);
  delay(10);

  //initing LOX2
  if (!lox2.begin()) {
    Serial.println(F("Failed to boot second VL6180X"));
    while (1);
  }
  lox2.setAddress(LOX2_ADDRESS);
  delay(10);

 
}

void readSensor(Adafruit_VL6180X &vl) {

  float lux = vl.readLux(VL6180X_ALS_GAIN_5);

  uint8_t range = vl.readRange();

  uint8_t status = vl.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
      tempRange = range;//save it for the moment
  }

  // Some error occurred, print it out!

  if  ((status >= VL6180X_ERROR_SYSERR_1) && (status <= VL6180X_ERROR_SYSERR_5)) {
    Serial.print("(System error)");
  }
  else if (status == VL6180X_ERROR_ECEFAIL) {
    Serial.print("(ECE failure)");
  }
  else if (status == VL6180X_ERROR_NOCONVERGE) {
    Serial.print("(No convergence)");
  }
  else if (status == VL6180X_ERROR_RANGEIGNORE) {
    Serial.print("(Ignoring range)");
  }
  else if (status == VL6180X_ERROR_SNR) {
    Serial.print("Signal/Noise error");
  }
  else if (status == VL6180X_ERROR_RAWUFLOW) {
    Serial.print("Raw reading underflow");
  }
  else if (status == VL6180X_ERROR_RAWOFLOW) {
    Serial.print("Raw reading overflow");
  }
  else if (status == VL6180X_ERROR_RANGEUFLOW) {
    Serial.print("Range reading underflow");
  }
  else if (status == VL6180X_ERROR_RANGEOFLOW) {
    Serial.print("Range reading overflow");
  }
}

void read_sensors() {
  readSensor(lox1);
  sensor_ranges[0]=tempRange;//save it now

  readSensor(lox2);
  sensor_ranges[1]=tempRange; //save it now 

  Serial.println();
}





//===============================================================
// Setup
//===============================================================
void setup() {
  Serial.begin(115200);

  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }

  pinMode(SHT_LOX1, OUTPUT);
  pinMode(SHT_LOX2, OUTPUT);


  // Enable timing pin so easy to see when pass starts and ends
  pinMode(TIMING_PIN, OUTPUT);

#ifdef GPIO_LOX1
  // If we defined GPIO pins, enable them as PULL UP
  pinMode(GPIO_LOX1, INPUT_PULLUP);
  pinMode(GPIO_LOX2, INPUT_PULLUP);

#endif

  Serial.println("Shutdown pins inited...");

  digitalWrite(SHT_LOX1, LOW);
  digitalWrite(SHT_LOX2, LOW);

  digitalWrite(TIMING_PIN, LOW);
  Serial.println("All in reset mode...(pins are low)");


  Serial.println("Starting...");
  setID();

}

//===============================================================
// Loop
//===============================================================
void loop() {
 read_sensors();
      for(int i=0; i<COUNT_SENSORS; i++)
      {
        Serial.print("Sensor ");
        Serial.print(i);
        Serial.print(" :");        
        Serial.print(sensor_ranges[i]);
        Serial.print("mm");
        Serial.println();
      } 
//      if(sensor_ranges[1] >=76)
//      {
//        //do something here
//      }
  delay(100);
}

مواردی که ممکن است به آن‌ها نیاز داشته باشید

فایل‌ها📁

هیچ فایلی موجود نیست.