UNIT_TOF「M5Stack用ToF測距センサユニット」を動かす
2021/12/30
初版
UNIT_TOF
UNIT_TOF
概要
UNIT_TOF「M5Stack用ToF測距センサユニット」を動かす。
ソースは共通になっており、ターゲットのM5StickCPlus,M5Core2をplatformio.iniの内容で切り替えてビルドする。
ソース
src/main.ino
/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
* Equipped with M5Core2 sample source code
* 配套 M5Core2 示例源代码
* Visit the website for more information:https://docs.m5stack.com/en/core/core2
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2
*
* describe:ToF. 激光测距
* date:2021/8/16
*******************************************************************************
Please connect to Port A,Use ToF Unit to detect distance and display distance data on the screen in real time.
请连接端口A,使用ToF Unit检测距离,并在屏幕上实时显示距离数据。
*/
#ifdef M5C2
#include <M5Core2.h>
#endif
#ifdef M5STICK_CP
#include <M5StickCPlus.h>
#endif
#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 //I2C address
byte gbuf[16];
uint16_t bswap(byte b[]) {
// Big Endian unsigned short to little endian unsigned short
uint16_t val = ((b[0] << 8) & b[1]);
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[0] = Wire.read();
gbuf[1] = 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[i] = 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;
}
void setup() {
// put your setup code here, to run once:
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(115200); // start serial for output
Serial.println("VLX53LOX test started.");
#ifdef M5STICK_CP
M5.begin();
M5.Axp.ScreenBreath(12);
M5.Lcd.setRotation(3);
M5.lcd.setTextSize(1);
//---osmar
M5.Lcd.setCursor(0, 0, 4);
M5.Lcd.println(("VLX53LOX Example"));
//---osmar
#endif
#ifndef M5STICK_CP
M5.begin(); //Init M5Core2. 初始化 M5Core2
M5.lcd.setTextSize(1);
//---osmar
M5.Lcd.setCursor(50, 0, 4);
M5.Lcd.println(("VLX53LOX Example"));
//---osmar
#endif
}
void loop() {
write_byte_data_at(VL53L0X_REG_SYSRANGE_START, 0x01);
byte val = 0;
int cnt = 0;
while (cnt < 100) { // 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[7], gbuf[6]);
uint16_t scnt = makeuint16(gbuf[9], gbuf[8]);
uint16_t dist = makeuint16(gbuf[11], gbuf[10]);
byte DeviceRangeStatusInternal = ((gbuf[0] & 0x78) >> 3);
M5.Lcd.fillRect(0, 35, 319, 239, BLACK);
M5.Lcd.setCursor(0, 35, 4);
M5.Lcd.print("ambient count: "); M5.Lcd.println(acnt);
M5.Lcd.print("signal count: "); M5.Lcd.println(scnt);
M5.Lcd.print("distance: "); M5.Lcd.println(dist);
M5.Lcd.print("status: "); M5.Lcd.println(DeviceRangeStatusInternal);
delay(1000);
}
platformio.ini
M5Core2用
[env:m5core2]
platform = espressif32
board = m5stack-core2
framework = arduino
upload_speed = 2000000
monitor_speed = 115200
board_build.partitions = default_16MB.csv
build_flags =
-DM5C2
-DCORE_DEBUG_LEVEL=4
-DBOARD_HAS_PSRAM
-mfix-esp32-psram-cache-issue
lib_deps =
https://github.com/m5stack/M5Core2.git
https://github.com/FastLED/FastLED
;https://github.com/m5stack/M5Core2/blob/master/examples/core2_for_aws/ArduinoECCX08.zip
lovyan03/LovyanGFX
adafruit/Adafruit BMP280 Library @ ^2.5.0
adafruit/Adafruit Unified Sensor @ ^1.1.4
m5stack/UNIT_ENV @ ^0.0.2
lib_ldf_mode = deep+
M5StickCPlus用
[env:M5StickCPlus]
platform = espressif32
board = m5stick-c
framework = arduino
build_flags = -DM5STICK_CP
monitor_speed = 115200
lib_ldf_mode = deep+
lib_deps =
m5stack/M5StickCPlus @ ^0.0.5
tinyu-zhao/FFT @ ^0.0.1
m5stack/UNIT_ENV @ ^0.0.2
adafruit/Adafruit BMP280 Library @ ^2.5.0
adafruit/Adafruit Unified Sensor @ ^1.1.4
動作
VL53L0Xを搭載したTime-of-Flight方式による距離検出ユニットで検出した距離などをLCDに表示する。
参考情報
terminal関連:
Bootterm – a developer-friendly serial terminal program
platformio関連:
arduinoフレームワーク用platformio.ini集
Building Core2 FactoryDemo in PlatformIO
VSCodeとPlatformIOでM5Stack Core2開発
M5Stack Core2とVSCode + PlatformIOとでM5Stackプログラミングを始めてみた。
Arduino-IDE関連:
Arduino IDE environment - M5Paper
Arduino IDEのインストールと設定 (Windows, Mac, Linux対応)
M5Stackファミリ関連:
M5Core2 Arduino Install
M5Paper Arduino Install
M5CoreInk Arduino Install
M5Stamp-PICO Arduino Install
M5Stamp-C3 Arduino Install
Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(NTP-CLIENT編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(STARWARS編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(MQTT編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(REST-API編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(OSC編)
以上
| 固定リンク
「PlatformIO」カテゴリの記事
- NuEVI/NuRADのビルド(2022.08.18)
- Wio_ExtFlashLoad(WriteSampleMenu.ino)スケッチをplatformioでビルドする(2022.02.03)
- uncannyeyesスケッチをplatformioでビルドする(2022.01.31)
- LovyanGFX-Display ライブラリを使用したスケッチをplatformioでビルドする(2022.01.30)
- Wio-Terminal/M5Core2のWiFiAnallyzer(2022.01.24)
この記事へのコメントは終了しました。


コメント