(SKU:SEN0314) 小MU视觉传感器

来自DFRobot Product Wiki
跳转至: 导航搜索

目录

概述

小MU视觉传感器是一款内置的深度学习引擎的图像识别传感器,采用30W广角镜头,含有多种视觉算法,能够实现机器人自主智能,让机器人面对环境中的随机事件时自主触发适合的行为。所有算法均在本地处理,处理速度更快,识别率更高。无须联网即可进行图像识别,不受网络状况所限制,无需担忧隐私泄露。可以识别多种目标物体,例如颜色检测,球体检测,人体检测,卡片识别等。检测结果可以通过UART或I2C方式进行输出,产品体积小巧,功耗低,可广泛应用于智能玩具、人工智能教具、创客产品等产品或领域。
该款传感器使用起来非常方便,采用2.54mm间距引脚设计的模块化设计,通过杜邦线可以很方便的与Arduino、树莓派等开发板连接,通过板载USB串口可以方便实现传感器模块的参数设定和固件更新,支持UART,I2C,WIFI通讯方式,适用于Arduino,Mixly以及Microbit嵌入式平台,同时我们提供官方库文件,可开源下载应用案例。
通过小MU视觉传感器可完成一些职能化应用,比如智能打印扫描小车、智能无人车、寻球投篮机器人等,可广泛应用于智能玩具、人工智能教具、创客产品等产品或领域。

特性

小MU视觉传感器具有如下优点:

  1. 能够做出丰富多样的视觉课程应用
    1. 智能打印扫描小车
    2. 智能无人车
    3. 寻球投篮机器人等
  2. 体积小巧,功耗极低
  3. 易于使用:支持UART,I2C,WIFI通讯方式,适用于Arduino,Mixly以及Microbit嵌入式平台
  4. 应用广泛
  5. 安全可靠,无需网络:所有视觉识别算法都于本地进行处理,无须联网,不受网络状况所限制,无需担忧隐私泄露

产品参数

  • 处理器:双核,240MHz
  • 摄像头:Omnivision ov7725
  • 摄像头分辨率:640x480
  • 视场角:90°(对角线)
  • 辅助灯:2个LED辅助灯
  • 尺寸:3.2cm*3.2cm*1.2cm
  • 数据输出接口:UART/I2C
  • 供电电压:5V
  • 可识别目标
    • 人(依据上半身轮廓)
    • 球(乒乓球与网球)
    • 定制的卡片20张(3组-----5张交通卡片、5张形状、10张数字)
    • 颜色(视野内指定位置输出指定颜色,指定颜色输出在视野内的位置)
  • 可提供软件库
    • milxy的库函数与参考程序
    • arduino的库函数
    • microbit的库函数
  • 开发中功能
    • wifi图传(模块做为AP)
    • 运动轨迹检测(检测手部上下左右运动轨迹,非手势检测)
    • 人脸的记忆与识别
    • 二维码识别

引脚说明


MU.png
编号 功能描述
G 地(GND)
V 电源输入(兼容3.3V和5V)
TX 串口发送端
RX 串口接收端
SCL 控制线
SDA 数据线


输出模式及地址选择

传感器有UART和I2C两种输出模式,不同模式下需通过拨码开关进行调整,开关下方标有Output的为输出模式拨码开关
传感器有4种地址可选择,地址范围为0x60-0x63,该范围内的地址在两种模式下通用,不同地址需通过拨码开关进行调整,开关下方标有Address的为地址拨码开关

输出模式选择

采用 2 位拨码开关来选择信号输出类型,定义见下表:

拨码1 wifi开关 拨码2 信号输出类型
0 禁止使用Wifi功能* 0 UART通讯方式
1 允许使用Wifi功能* 1 I2C通讯方式

注1:*所示功能,当前版本尚不具备
注2:向上拨动为1,向下拨动为0

地址选择

采用 2 位拨码开关来选择信号输出类型,定义见下表:

拨码1 拨码2 设备地址
0 0 0x60
0 1 0x61
1 0 0x62
1 1 0x63

注1:UART模式下可使用0x00作为广播地址
注2:向上拨动为1,向下拨动为0

模块通讯协议

协议格式

START LEN ADDR CMD DATA CHK END

START:起始码,始终为0xFF
LEN:长度,从起始码到结束码所有的字节数
ADDR:设备地址,范围0x60~0x63,初次使用默认地址为0x60,特别的:0x00为广播地址,所有设备都会接收
CMD:指令码/应答码
DATA:数据,详见各指令码介绍
CHK:校验码,从START至DATA所有字节累加求和
例:FF 08 60 01 20 03 8B ED 其中8B为校验码,0xFF+0x08+0x60+0x01+0x20+0x03 = 0x8B
END:结束码,始终为0xED

应答码

应答正确 应答错误 未知错误 超时错误 校验错误 长度错误 指令错误 地址错误 参数错误 写入错误
0xE0 0xE1 0xE2 0xE3 0xE4 0xE5 0xE6 0xE7 0xE8 0xE9



指令码

写寄存器 读寄存器 数据报文 请求报文
0x01 0x02 0x11 0x12

以上指令均是操作成功返回的代码,若操作失败会返回相对应的失败应答码

UART模式下Arduino使用教程

准备

接线图

MU连线图.png

样例代码

* **************************************************** 
* @brief MU Vision Sensor

 * @copyright	[DFRobot](http://www.dfrobot.com), 2016
 * @copyright	GNU Lesser General Public License

* @author [WWZ](Wenzheng.wang@dfrobot.com)
* @version  V1.0
* @date  2019-03-11
  
* GNU Lesser General Public License.
* All above must be included in any redistribution
* ****************************************************/
#include <Wire.h>
#include "MuVisionSensor.h"
#include <SoftwareSerial.h>

//#define I2C_MODE
#define SERIAL_MODE
#define MU_ADDRESS    0x60
// change vision type here, VISION_TYPE:VISION_COLOR_DETECT
//                                      VISION_COLOR_RECOGNITION
//                                      VISION_BALL_DETECT
//                                      VISION_BODY_DETECT
//                                      VISION_SHAPE_CARD_DETECT
//                                      VISION_TRAFFIC_CARD_DETECT
//                                      VISION_NUM_CARD_DETECT
//#define VISION_TYPE     VISION_BALL_DETECT
//#define VISION_TYPE     VISION_BODY_DETECT
//#define VISION_TYPE     VISION_SHAPE_CARD_DETECT
#define VISION_TYPE     VISION_TRAFFIC_CARD_DETECT
//#define VISION_TYPE     VISION_NUM_CARD_DETECT
//#define VISION_TYPE     VISION_COLOR_DETECT
//#define VISION_TYPE     VISION_COLOR_RECOGNITION

#ifdef SERIAL_MODE
#define TX_PIN 2
#define RX_PIN 3
SoftwareSerial mySerial(RX_PIN, TX_PIN);
#endif
MuVisionSensor Mu(MU_ADDRESS);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(500);

#ifdef I2C_MODE
  Wire.begin();
  Mu.begin(&Wire, kI2CMode);
#elif defined SERIAL_MODE
  mySerial.begin(9600);
  Mu.begin(&mySerial, kSerialMode);
#endif

  Mu.VisionBegin(VISION_TYPE);

  if (VISION_TYPE == VISION_COLOR_DETECT
      || VISION_TYPE == VISION_COLOR_RECOGNITION) {
    Mu.CameraSetAwb(kLockWhiteBalance); // lock AWB
    delay(1000);                        // waiting for AWB lock.
  if (VISION_TYPE == VISION_COLOR_DETECT
      || VISION_TYPE == VISION_COLOR_RECOGNITION) {
      Mu.write(VISION_TYPE, kXValue, 50);
      Mu.write(VISION_TYPE, kYValue, 50);
      Mu.write(VISION_TYPE, kWidthValue, 5);
      Mu.write(VISION_TYPE, kHeightValue, 5);
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  long time_start = millis();

  // read result
  if (Mu.GetValue(VISION_TYPE,kStatus)) {
    Serial.println("vision detected:");
    switch (VISION_TYPE) {
      case VISION_BALL_DETECT:
      case VISION_BODY_DETECT:
      case VISION_SHAPE_CARD_DETECT:
      case VISION_TRAFFIC_CARD_DETECT:
      case VISION_NUM_CARD_DETECT:
      case VISION_COLOR_DETECT:
        Serial.print("x = ");
        Serial.println(Mu.GetValue(VISION_TYPE, kXValue));
        Serial.print("y = ");
        Serial.println(Mu.GetValue(VISION_TYPE, kYValue));
        Serial.print("width = ");
        Serial.println(Mu.GetValue(VISION_TYPE, kWidthValue));
        Serial.print("height = ");
        Serial.println(Mu.GetValue(VISION_TYPE, kHeightValue));
        if (VISION_TYPE != VISION_COLOR_DETECT) {
          Serial.print("label = ");
          Serial.println(Mu.GetValue(VISION_TYPE, kLabel));
        } else {
          Serial.print("color = ");
          Serial.println(Mu.GetValue(VISION_TYPE, kLabel));
        }
        break;
      case VISION_COLOR_RECOGNITION:
        Serial.print("r = ");
        Serial.println(Mu.GetValue(VISION_TYPE, kRValue));
        Serial.print("g = ");
        Serial.println(Mu.GetValue(VISION_TYPE, kGValue));
        Serial.print("b = ");
        Serial.println(Mu.GetValue(VISION_TYPE, kBValue));
        Serial.print("color = ");
        Serial.println(Mu.GetValue(VISION_TYPE, kLabel));
        break;
      default:
        break;
    }
  } else {
    Serial.println("vision undetected.");
  }
  Serial.print("fps = ");
  Serial.println(1000/(millis()-time_start));
  Serial.println();
}

输出结果:识别颜色时可输出颜色的RGB值;识别其它目标时输出其坐标值
上述程序识别的是交通卡片,输出的分别是横、纵坐标值,宽度和长度,一次只能识别一种类型的物体,识别其它类型需在程序#define VISION_TYPE处更换识别种类,方能识别其它物体。
目标物体识别成功时LED1,LED2两个指示灯会红蓝交替闪烁,未识别到物体会闪红灯。

I2C模式下Arduino使用教程

准备

接线图

MU连线图1.png

识别球体样例代码(乒乓球和网球)

* **************************************************** 
* @brief MU Vision Sensor

 * @copyright	[DFRobot](http://www.dfrobot.com), 2016
 * @copyright	GNU Lesser General Public License

* @author [WWZ](Wenzheng.wang@dfrobot.com)
* @version  V1.0
* @date  2019-03-11
  
* GNU Lesser General Public License.
* All above must be included in any redistribution
* ****************************************************/
#include <Wire.h>

#define MU_ADDRESS    0x60
#define PROTOCOL_VER  0x03
#define VISION_ID     0x03      // ball

// register define
#define REG_PROTOCOL_VER  0x01
#define REG_LED1_CONF     0x06
#define REG_LED2_CONF     0x07
#define REG_LED_LEVEL     0x08
#define REG_CAMERA_CONF1  0x10
#define REG_FRAME_CNT     0x1F
#define REG_VISION_ID     0x20
#define REG_VISION_CONF1  0x21
#define REG_PARAM_VALUE5  0x29
#define RESULT_NUM        0x34
#define RESULT_DATA1      0x40
#define RESULT_DATA2      0x41
#define RESULT_DATA3      0x42
#define RESULT_DATA4      0x43
#define RESULT_DATA5      0x44
// color
#define MU_COLOR_BLACK                0x01U
#define MU_COLOR_WHITE                0x02U
#define MU_COLOR_RED                  0x03U
#define MU_COLOR_YELLOW               0x04U
#define MU_COLOR_GREEN                0x05U
#define MU_COLOR_CYAN                 0x06U
#define MU_COLOR_BLUE                 0x07U
#define MU_COLOR_PURPLE               0x08U

int i2c_read8(uint8_t reg) {
  Wire.beginTransmission(MU_ADDRESS);
  Wire.write(reg);
  Wire.endTransmission();

  Wire.requestFrom(MU_ADDRESS, 1);
  return Wire.read();
}
void i2c_write8(const uint8_t reg, const uint8_t value) {
  Wire.beginTransmission(MU_ADDRESS);
  Wire.write(reg);
  Wire.write(value);
  Wire.endTransmission();
}

uint8_t reg[][2] = {
    { REG_VISION_ID,      VISION_ID }, // set vision type = vision_detect
    { REG_VISION_CONF1,   0x21 }, // vision begin
};
uint8_t frame_count_last = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Wire.begin();
  delay(500);

  if (i2c_read8(REG_PROTOCOL_VER) == PROTOCOL_VER) {
    Serial.println("device initialized.");
  } else {
    Serial.println("fail to initialize device! Please check protocol version.");
  }
  for (uint32_t i = 0; i < sizeof(reg)/2; ++i) {
    i2c_write8(reg[i][0], reg[i][1]);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  long time_start = millis();
  int frame_count = 0;
  // waiting for update
  do {
    frame_count = i2c_read8(REG_FRAME_CNT);
  } while(frame_count == frame_count_last);
  frame_count_last = frame_count;

  i2c_write8(REG_VISION_ID, VISION_ID);
  // read result
  if (i2c_read8(RESULT_NUM) > 0) {
    Serial.println("ball detected:");
    Serial.print("x = ");
    Serial.println(i2c_read8(RESULT_DATA1));
    Serial.print("y = ");
    Serial.println(i2c_read8(RESULT_DATA2));
    Serial.print("width = ");
    Serial.println(i2c_read8(RESULT_DATA3));
    Serial.print("height = ");
    Serial.println(i2c_read8(RESULT_DATA4));
    Serial.print("label = ");
    switch(i2c_read8(RESULT_DATA5)) {
      case 1:
        Serial.println("Ping-Pong ball");
        break;
      case 2:
        Serial.println("Tennis");
        break;
      default:
        break;
    }
  } else {
    Serial.println("ball undetected.");
  }
  Serial.print("fps = ");
  Serial.println(1000/(millis()-time_start));
  Serial.println();
}

输出结果:可识别目标球体,通过串口可观察到识别目标后返回的坐标值
目标物体识别成功时LED1,LED2两个指示灯会红蓝交替闪烁,未识别到物体会闪红灯。

识别人体样例代码(依据上半身轮廓)

* **************************************************** 
* @brief MU Vision Sensor

 * @copyright	[DFRobot](http://www.dfrobot.com), 2016
 * @copyright	GNU Lesser General Public License

* @author [WWZ](Wenzheng.wang@dfrobot.com)
* @version  V1.0
* @date  2019-03-11
  
* GNU Lesser General Public License.
* All above must be included in any redistribution
* ****************************************************/
#include <Wire.h>

#define MU_ADDRESS    0x60
#define PROTOCOL_VER  0x03
#define VISION_ID     0x05      // body

// register define
#define REG_PROTOCOL_VER  0x01
#define REG_LED1_CONF     0x06
#define REG_LED2_CONF     0x07
#define REG_LED_LEVEL     0x08
#define REG_CAMERA_CONF1  0x10
#define REG_FRAME_CNT     0x1F
#define REG_VISION_ID     0x20
#define REG_VISION_CONF1  0x21
#define REG_PARAM_VALUE5  0x29
#define RESULT_NUM        0x34
#define RESULT_DATA1      0x40
#define RESULT_DATA2      0x41
#define RESULT_DATA3      0x42
#define RESULT_DATA4      0x43
#define RESULT_DATA5      0x44
// color
#define MU_COLOR_BLACK                0x01U
#define MU_COLOR_WHITE                0x02U
#define MU_COLOR_RED                  0x03U
#define MU_COLOR_YELLOW               0x04U
#define MU_COLOR_GREEN                0x05U
#define MU_COLOR_CYAN                 0x06U
#define MU_COLOR_BLUE                 0x07U
#define MU_COLOR_PURPLE               0x08U

int i2c_read8(uint8_t reg) {
  Wire.beginTransmission(MU_ADDRESS);
  Wire.write(reg);
  Wire.endTransmission();

  Wire.requestFrom(MU_ADDRESS, 1);
  return Wire.read();
}
void i2c_write8(const uint8_t reg, const uint8_t value) {
  Wire.beginTransmission(MU_ADDRESS);
  Wire.write(reg);
  Wire.write(value);
  Wire.endTransmission();
}

uint8_t reg[][2] = {
    { REG_VISION_ID,      VISION_ID }, // set vision type = vision_detect
    { REG_VISION_CONF1,   0x21 }, // vision begin
};
uint8_t frame_count_last = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Wire.begin();
  delay(500);

  if (i2c_read8(REG_PROTOCOL_VER) == PROTOCOL_VER) {
    Serial.println("device initialized.");
  } else {
    Serial.println("fail to initialize device! Please check protocol version.");
  }
  for (uint32_t i = 0; i < sizeof(reg)/2; ++i) {
    i2c_write8(reg[i][0], reg[i][1]);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  long time_start = millis();
  int frame_count = 0;
  // waiting for update
  do {
    frame_count = i2c_read8(REG_FRAME_CNT);
  } while(frame_count == frame_count_last);
  frame_count_last = frame_count;

  i2c_write8(REG_VISION_ID, VISION_ID);
  // read result
  if (i2c_read8(RESULT_NUM) > 0) {
    Serial.println("body detected:");
    Serial.print("x = ");
    Serial.println(i2c_read8(RESULT_DATA1));
    Serial.print("y = ");
    Serial.println(i2c_read8(RESULT_DATA2));
    Serial.print("width = ");
    Serial.println(i2c_read8(RESULT_DATA3));
    Serial.print("height = ");
    Serial.println(i2c_read8(RESULT_DATA4));
  } else {
    Serial.println("body undetected.");
  }
  Serial.print("fps = ");
  Serial.println(1000/(millis()-time_start));
  Serial.println();
}

输出结果:可识别人体,通过串口可观察到识别目标后返回的坐标值
目标物体识别成功时LED1,LED2两个指示灯会红蓝交替闪烁,未识别到物体会闪红灯。

识别颜色样例代码

* **************************************************** 
* @brief MU Vision Sensor

 * @copyright	[DFRobot](http://www.dfrobot.com), 2016
 * @copyright	GNU Lesser General Public License

* @author [WWZ](Wenzheng.wang@dfrobot.com)
* @version  V1.0
* @date  2019-03-11
  
* GNU Lesser General Public License.
* All above must be included in any redistribution
* ****************************************************/
#include <Wire.h>

#define MU_ADDRESS    0x60
#define PROTOCOL_VER  0x03
#define VISION_ID     0x02

// register define
#define REG_PROTOCOL_VER  0x01
#define REG_LED1_CONF     0x06
#define REG_LED2_CONF     0x07
#define REG_LED_LEVEL     0x08
#define REG_CAMERA_CONF1  0x10
#define REG_FRAME_CNT     0x1F
#define REG_VISION_ID     0x20
#define REG_VISION_CONF1  0x21
#define REG_PARAM_VALUE1  0x25
#define REG_PARAM_VALUE2  0x26
#define REG_PARAM_VALUE3  0x27
#define REG_PARAM_VALUE4  0x28
#define RESULT_NUM        0x34
#define RESULT_DATA1      0x40
#define RESULT_DATA2      0x41
#define RESULT_DATA3      0x42
#define RESULT_DATA5      0x44
// color
#define MU_COLOR_BLACK                0x01U
#define MU_COLOR_WHITE                0x02U
#define MU_COLOR_RED                  0x03U
#define MU_COLOR_YELLOW               0x04U
#define MU_COLOR_GREEN                0x05U
#define MU_COLOR_CYAN                 0x06U
#define MU_COLOR_BLUE                 0x07U
#define MU_COLOR_PURPLE               0x08U

int i2c_read8(uint8_t reg) {
  Wire.beginTransmission(MU_ADDRESS);
  Wire.write(reg);
  Wire.endTransmission();

  Wire.requestFrom(MU_ADDRESS, 1);
  return Wire.read();
}
void i2c_write8(const uint8_t reg, const uint8_t value) {
  Wire.beginTransmission(MU_ADDRESS);
  Wire.write(reg);
  Wire.write(value);
  Wire.endTransmission();
}

uint8_t reg[][2] = {
//    { REG_LED_LEVEL,      0x00 }, // LED will be closed automatically in this vision type.
//    { REG_LED1_CONF,      0x00 }, // LED1 color
//    { REG_LED2_CONF,      0x00 }, // LED2 color
    { REG_CAMERA_CONF1,   0x30 }, // lock AWB
    { REG_VISION_ID,      VISION_ID }, // set vision type = vision_recognition
    { REG_PARAM_VALUE1,   50 }, // x
    { REG_PARAM_VALUE2,   50 }, // y
//    { REG_PARAM_VALUE3,   5 }, // width
//    { REG_PARAM_VALUE4,   5 }, // height
    { REG_VISION_CONF1,   0x21 }, // vision begin
};
uint8_t frame_count_last = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Wire.begin();
  delay(500);

  if (i2c_read8(REG_PROTOCOL_VER) == PROTOCOL_VER) {
    Serial.println("device initialized.");
  } else {
    Serial.println("fail to initialize device! Please check protocol version.");
  }
  for (uint32_t i = 0; i < sizeof(reg)/2; ++i) {
    i2c_write8(reg[i][0], reg[i][1]);
  }
  delay(1000); // waiting for AWB lock.
}

void loop() {
  // put your main code here, to run repeatedly:
  long time_start = millis();
  int frame_count = 0;
  // waiting for update
  do {
    frame_count = i2c_read8(REG_FRAME_CNT);
  } while(frame_count == frame_count_last);
  frame_count_last = frame_count;

  i2c_write8(REG_VISION_ID, VISION_ID);
  // read result
  if (i2c_read8(RESULT_NUM) > 0) {
    Serial.println("color detected:");
    Serial.print("color = (");
    Serial.print(i2c_read8(RESULT_DATA1));
    Serial.print(",");
    Serial.print(i2c_read8(RESULT_DATA2));
    Serial.print(",");
    Serial.print(i2c_read8(RESULT_DATA3));
    Serial.print(")\nlabel = ");
    switch (i2c_read8(RESULT_DATA5)) {
      case 0:
        Serial.println("unknow color");
        break;
      case 1:
        Serial.println("black");
        break;
      case 2:
        Serial.println("white");
        break;
      case 3:
        Serial.println("red");
        break;
      case 4:
        Serial.println("yellow");
        break;
      case 5:
        Serial.println("green");
        break;
      case 6:
        Serial.println("cyan");
        break;
      case 7:
        Serial.println("blue");
        break;
      case 8:
        Serial.println("violet");
        break;
      default:
        break;
    }
  } else {
    Serial.println("color undetected.");
  }
  Serial.print("fps = ");
  Serial.println(1000/(millis()-time_start));
  Serial.println();
}

输出结果:可识别相对应的颜色,通过串口可观察到识别到的颜色名称及其RGB值
目标物体识别成功时LED1,LED2两个指示灯会红蓝交替闪烁,未识别到物体会闪红灯。

识别卡片样例代码(仅限20张定制卡片)

* **************************************************** 
* @brief MU Vision Sensor

 * @copyright	[DFRobot](http://www.dfrobot.com), 2016
 * @copyright	GNU Lesser General Public License

* @author [WWZ](Wenzheng.wang@dfrobot.com)
* @version  V1.0
* @date  2019-03-11
  
* GNU Lesser General Public License.
* All above must be included in any redistribution
* ****************************************************/
#include <Wire.h>

#define MU_ADDRESS    0x60
#define PROTOCOL_VER  0x03
#define VISION_ID     0x06      // shape card
//#define VISION_ID     0x07      // traffic card
//#define VISION_ID     0x08      // number card

// register define
#define REG_PROTOCOL_VER  0x01
#define REG_LED1_CONF     0x06
#define REG_LED2_CONF     0x07
#define REG_LED_LEVEL     0x08
#define REG_CAMERA_CONF1  0x10
#define REG_FRAME_CNT     0x1F
#define REG_VISION_ID     0x20
#define REG_VISION_CONF1  0x21
#define REG_PARAM_VALUE5  0x29
#define RESULT_NUM        0x34
#define RESULT_DATA1      0x40
#define RESULT_DATA2      0x41
#define RESULT_DATA3      0x42
#define RESULT_DATA4      0x43
#define RESULT_DATA5      0x44
// color
#define MU_COLOR_BLACK                0x01U
#define MU_COLOR_WHITE                0x02U
#define MU_COLOR_RED                  0x03U
#define MU_COLOR_YELLOW               0x04U
#define MU_COLOR_GREEN                0x05U
#define MU_COLOR_CYAN                 0x06U
#define MU_COLOR_BLUE                 0x07U
#define MU_COLOR_PURPLE               0x08U

int i2c_read8(uint8_t reg) {
  Wire.beginTransmission(MU_ADDRESS);
  Wire.write(reg);
  Wire.endTransmission();

  Wire.requestFrom(MU_ADDRESS, 1);
  return Wire.read();
}
void i2c_write8(const uint8_t reg, const uint8_t value) {
  Wire.beginTransmission(MU_ADDRESS);
  Wire.write(reg);
  Wire.write(value);
  Wire.endTransmission();
}

uint8_t reg[][2] = {
    { REG_VISION_ID,      VISION_ID }, // set vision type = vision_detect
    { REG_VISION_CONF1,   0x21 }, // vision begin
};
uint8_t frame_count_last = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Wire.begin();
  delay(500);

  if (i2c_read8(REG_PROTOCOL_VER) == PROTOCOL_VER) {
    Serial.println("device initialized.");
  } else {
    Serial.println("fail to initialize device! Please check protocol version.");
  }
  for (uint32_t i = 0; i < sizeof(reg)/2; ++i) {
    i2c_write8(reg[i][0], reg[i][1]);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  long time_start = millis();
  int frame_count = 0;
  // waiting for update
  do {
    frame_count = i2c_read8(REG_FRAME_CNT);
  } while(frame_count == frame_count_last);
  frame_count_last = frame_count;

  i2c_write8(REG_VISION_ID, VISION_ID);
  // read result
  if (i2c_read8(RESULT_NUM) > 0) {
    Serial.println("card detected:");
    Serial.print("x = ");
    Serial.println(i2c_read8(RESULT_DATA1));
    Serial.print("y = ");
    Serial.println(i2c_read8(RESULT_DATA2));
    Serial.print("width = ");
    Serial.println(i2c_read8(RESULT_DATA3));
    Serial.print("height = ");
    Serial.println(i2c_read8(RESULT_DATA4));
    Serial.print("label = ");
    Serial.println(i2c_read8(RESULT_DATA5));
  } else {
    Serial.println("card undetected.");
  }
  Serial.print("fps = ");
  Serial.println(1000/(millis()-time_start));
  Serial.println();
}

输出结果:可识别目标卡片,通过串口可观察到识别目标后返回的坐标值
目标物体识别成功时LED1,LED2两个指示灯会红蓝交替闪烁,未识别到物体会闪红灯。

常见问题

还没有客户对此产品有任何问题,欢迎通过qq或者论坛联系我们!


更多问题及有趣的应用,可以 访问论坛 进行查阅或发帖!


更多



个人工具
名字空间

变换
操作
导航
工具箱