M5Stack新品测评 —— ToF-HAT(VL53L0X)
在许多应用场景中经常会用到测距模块,常用的传感器模块有超声波,红外,激光测距和毫米波雷达,常用的是前三者,他们各自有各自的特点,也有不同的应用场合,简单对比如下:模块优势不足场景
ToF测距探测速度快,抗干扰能力强恶劣天气易受影响,针对黑色高亮物体量程受限汽车辅助驾驶、无人机定高、物位计、工业安防
超声波测距稳定性强,不怕腐蚀性液体,对液体和固体穿透力强抗干扰能力差,精度低,不利于测量高速运动物体机器人避障、液位检测
红外测距性价比高,操作简单受光线、物体材料影响大,距离非线性,最小距离偏大室内测量机器人避障工业计数
可以看出,如果希望获得较好的测量精度,ToF是最佳选择。今天进行测试的就是集成了VL53L0X的Unit,目前性价比最高的一款激光测距模块。根据实际测试,它的最小输出距离为20mm,最大测量距离为2000mm,超过2000为无效距离。由于是HAT,因此需要与M5StickC一起搭配使用,G26为SCL,G0为SDA,直接插入M5StickC的顶部就可以使用。受内部空间影响,厚度会略微高出一点。模块I2C地址为0x29,既然是测距模块,最简单的应用就是测量距离,但是今天借助测距模块做流水线的计数检测,测距是垂直摆放,而计数需要被测物体与ToF相交摆放,根据实际体验,需要将传感器垫高桌面一些,测量会更加灵敏。先来看运行效果:https://m5stack.oss-cn-shenzhen.aliyuncs.com/video/Product_example_video/HAT/ToF_HAT.mp4整个计数流程比较简单,定义一个比较阈值reference,假如在阈值范围内则视为有效数据,超出阈值视为无效。已知ToF的测量距离为20mm-2000mm,因此取值要在测量范围内。传感器初始化后,由ToF返回测量结果,ToF的结果在20~reference mm之间说明有物体通过,物体是有长度的,因此这段时间传感器的数据只会在20~reference之间波动,一旦物体离开,距离必定会大于reference,这个时候就可以计数+1。
#include <M5StickC.h>
#include <Wire.h>
//the original code by Ted Meyers
//posted here: https://groups.google.com/d/msg/diyrovers/lc7NUZYuJOg/ICPrYNJGBgAJ
//if your tof have some problem, please see https://docs.m5stack.com/#/en/HAT/tof
#define VL53L0X_REG_IDENTIFICATION_MODEL_ID 0xc0
#define VL53L0X_REG_IDENTIFICATION_REVISION_ID 0xc2
#define VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD 0x50
#define VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD 0x70
#define VL53L0X_REG_SYSRANGE_START 0x00
#define VL53L0X_REG_RESULT_INTERRUPT_STATUS 0x13
#define VL53L0X_REG_RESULT_RANGE_STATUS 0x14
#define address 0x29
byte gbuf;
int dist = 0;
int count = 0;
int Reference = 300;
uint16_t bswap(byte b[]) {
// Big Endian unsigned short to little endian unsigned short
uint16_t val = ((b << 8) & b);
return val;
}
uint16_t makeuint16(int lsb, int msb) {
return ((msb & 0xFF) << 8) | (lsb & 0xFF);
}
void write_byte_data(byte data) {
Wire.beginTransmission(address);
Wire.write(data);
Wire.endTransmission();
}
void write_byte_data_at(byte reg, byte data) {
// write data word at address and register
Wire.beginTransmission(address);
Wire.write(reg);
Wire.write(data);
Wire.endTransmission();
}
void write_word_data_at(byte reg, uint16_t data) {
// write data word at address and register
byte b0 = (data &0xFF);
byte b1 = ((data >> 8) && 0xFF);
Wire.beginTransmission(address);
Wire.write(reg);
Wire.write(b0);
Wire.write(b1);
Wire.endTransmission();
}
byte read_byte_data() {
Wire.requestFrom(address, 1);
while (Wire.available() < 1) delay(1);
byte b = Wire.read();
return b;
}
byte read_byte_data_at(byte reg) {
//write_byte_data((byte)0x00);
write_byte_data(reg);
Wire.requestFrom(address, 1);
while (Wire.available() < 1) delay(1);
byte b = Wire.read();
return b;
}
uint16_t read_word_data_at(byte reg) {
write_byte_data(reg);
Wire.requestFrom(address, 2);
while (Wire.available() < 2) delay(1);
gbuf = Wire.read();
gbuf = Wire.read();
return bswap(gbuf);
}
void read_block_data_at(byte reg, int sz) {
int i = 0;
write_byte_data(reg);
Wire.requestFrom(address, sz);
for (i=0; i<sz; i++) {
while (Wire.available() < 1) delay(1);
gbuf = Wire.read();
}
}
uint16_t VL53L0X_decode_vcsel_period(short vcsel_period_reg) {
// Converts the encoded VCSEL period register value into the real
// period in PLL clocks
uint16_t vcsel_period_pclks = (vcsel_period_reg + 1) << 1;
return vcsel_period_pclks;
}
int test() {
byte val1 = read_byte_data_at(VL53L0X_REG_IDENTIFICATION_REVISION_ID);
Serial.print("Revision ID: "); Serial.println(val1);
val1 = read_byte_data_at(VL53L0X_REG_IDENTIFICATION_MODEL_ID);
Serial.print("Device ID: "); Serial.println(val1);
val1 = read_byte_data_at(VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD);
Serial.print("PRE_RANGE_CONFIG_VCSEL_PERIOD="); Serial.println(val1);
Serial.print(" decode: "); Serial.println(VL53L0X_decode_vcsel_period(val1));
val1 = read_byte_data_at(VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD);
Serial.print("FINAL_RANGE_CONFIG_VCSEL_PERIOD="); Serial.println(val1);
Serial.print(" decode: "); Serial.println(VL53L0X_decode_vcsel_period(val1));
write_byte_data_at(VL53L0X_REG_SYSRANGE_START, 0x01);
byte val = 0;
int cnt = 0;
while (cnt < 10) { // 1 second waiting time max
delay(10);
val = read_byte_data_at(VL53L0X_REG_RESULT_RANGE_STATUS);
if (val & 0x01) break;
cnt++;
}
if (val & 0x01) Serial.println("ready"); else Serial.println("not ready");
read_block_data_at(0x14, 12);
uint16_t acnt = makeuint16(gbuf, gbuf);
uint16_t scnt = makeuint16(gbuf, gbuf);
uint16_t dist = makeuint16(gbuf, gbuf);
byte DeviceRangeStatusInternal = ((gbuf & 0x78) >> 3);
Serial.print("ambient count: "); Serial.println(acnt);
Serial.print("signal count: ");Serial.println(scnt);
Serial.print("distance "); Serial.println(dist);
Serial.print("status: "); Serial.println(DeviceRangeStatusInternal);
return dist;
}
void setup() {
// put your setup code here, to run once:
Wire.begin(0, 26, 400000); // join i2c bus (address optional for master)
Serial.begin(115200);// start serial for output
pinMode(M5_BUTTON_HOME,INPUT_PULLUP);
Serial.println("VLX53LOX test started.");
//---osmar
M5.begin();
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextColor(YELLOW);
M5.Lcd.setCursor(35, 0, 2);
M5.Lcd.println(("Objects Counter"));
M5.Lcd.setTextColor(GREEN);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString("0", 80, 50, 7);
//---osmar
}
void loop() {
dist = test();
if (dist > 20 && dist < Reference){
while(test()<Reference);
count++;
String num = String(count);
M5.Lcd.fillRect(0, 17, 160, 60 , BLACK);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString(num, 80, 50, 7);
}
if(digitalRead(M5_BUTTON_HOME) == LOW){
ESP.restart();
}
delay(50);
}
页:
[1]