diff --git a/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamReader.cpp b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamReader.cpp new file mode 100644 index 0000000..fc7e4de --- /dev/null +++ b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamReader.cpp @@ -0,0 +1,52 @@ +#include "SmartCamReader.h" + +bool hasReceivedValidData = false; // 记录是否收到过有效数据 + +// 函数定义 +int SmartCamReader(unsigned int *data, unsigned int timeout) { + byte length, check; // 声明局部变量 + byte data_buffer[40]; // 数据缓冲区 + unsigned int system_time; // 系统时间 + + system_time = millis(); // 获取当前系统时间 + // 等待至少两个字节的数据或超时 + while ((millis() - system_time < timeout) && (Serial.available() < 2)); + + if (Serial.available() >= 2) { // 如果至少有两个字节的数据 + if (Serial.read() == 0xAA) { // 检查头字节 + length = Serial.read(); // 读取数据长度 + check = 0xAA ^ length; // 初始化校验位 + + system_time = millis(); // 重置系统时间 + // 等待足够多的数据或超时 + while ((millis() - system_time < timeout) && (Serial.available() < length * 2 + 1)) {} + + if (Serial.available() >= length * 2 + 1) { // 如果数据足够 + for (unsigned char n = 0; n < length * 2; n++) { + data_buffer[n] = Serial.read(); // 读取数据到缓冲区 + check = check ^ data_buffer[n]; // 更新校验位 + } + if (check == Serial.read()) { // 如果校验成功 + while (Serial.available()) { // 清空缓冲区 + Serial.read(); + } + for (unsigned char n = 0; n < length; n++) { // 解码数据 + data[n] = (data_buffer[n * 2 + 1] << 8) + data_buffer[n * 2]; + } + hasReceivedValidData = true; + return length; // 返回数据长度 + } else { + hasReceivedValidData = true; + return -3; // 校验失败 + } + } else { + hasReceivedValidData = true; + return -2; // 数据不完整 + } + } + } else { + if (!hasReceivedValidData) { + Serial.write("start");} + return -1; // 超时或没有数据 + } +} diff --git a/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamReader.h b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamReader.h new file mode 100644 index 0000000..2a7420e --- /dev/null +++ b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamReader.h @@ -0,0 +1,9 @@ +#ifndef SMART_CAM_READER_H // 防止头文件重复包含 +#define SMART_CAM_READER_H + +#include + +// 函数声明 +int SmartCamReader(unsigned int *data, unsigned int timeout = 500); // timeout设置默认值为500ms + +#endif diff --git a/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamSimple.ino b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamSimple.ino new file mode 100644 index 0000000..466bb8e --- /dev/null +++ b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamSimple.ino @@ -0,0 +1,36 @@ +#include "SmartCamReader.h" // Include the header file + +unsigned int data[20]; // Array to store data received from OpenMV +unsigned int cX, cY, AREA; // Variables to store received X, Y, and AREA values + +void setup() { + Serial.begin(115200); // Initialize Serial communication with a baud rate of 115200 + Serial1.begin(115200); // Initialize Serial1 for data output with a baud rate of 115200 + delay(1000); +} + +void loop() { + // Use the SmartCamReader function to check and read data + int result = SmartCamReader(data); // Use the default timeout of 500ms + + // Handle the data or error based on the return value + if (result > 0) { + // Data read successfully, `result` indicates the length of the data + cX = data[0]; + cY = data[1]; + AREA = data[2]; + + // Output the received data to Serial1 + Serial1.print("X: "); + Serial1.println(cX); + Serial1.print("Y: "); + Serial1.println(cY); + Serial1.print("AREA: "); + Serial1.println(AREA); + } else { + // Data read failed, output error message to Serial1 + Serial1.println("Error: Failed to read data."); + } + + delay(100); // Add a delay to avoid excessively frequent outputs +} \ No newline at end of file diff --git a/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/README.md b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/README.md new file mode 100644 index 0000000..7651ad0 --- /dev/null +++ b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/README.md @@ -0,0 +1,26 @@ +# mVision-Python Lib for Arduino +An example for mVision Camera connect to MCU. Include MATRIX Mini (R3, R4). + +## How it works +A MicroPython library from ceeoinnovations allow mVision (OpenMV) connect to other controller via UART Protocol, and we wrote a sample program to let mVision detect objects by color and mark them with IDs on the screen (IDE view) and send the information to other controller. + +## Hardware +You can use SPIKE Ultrasonic sensor breakout board, or cut a wire to connect to SPIKE Hub, and connnect the wire as: + + mVision Pins <------> Other MCU Pins (UART) + Pin 1 (BLACK, GND ) <------> GND + Pin 2 (RED, VIN ) <------> 3V3 + Pin 3 (WHITE, Tx ) <------> Rx + Pin 4 (BLUE, Rx ) <------> Tx + +For MATRIX User, can using the JST cable conneect direcly. + +## Software - Camera side +Using OpenMV IDE or mVision IDE with Python mode, open "main.py" and copy the "matrix_mini.py" & "fill_light.py" to OpenMV disk drive, then Download the program to OpenMV, connect OpenMV to controller, and OpenMV IDE screen will show the object ID with square, controller will receive the largest object information(You can change the code to send diffrent information to controller). + +You can modify the send_data() content to custom the information you want to sent to controller, just make sure the Max size of send_data() is 20 items, and each items max value is 65535. + +## Software - Controller side + +For Arduino User, using the code in "Arduino Side" folder. +For MATRIX User, using the code in "MATRIX Side" folder. diff --git a/Sensors/MVision_Resources/mPython Lib/fill_light.py b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/fill_light.py similarity index 100% rename from Sensors/MVision_Resources/mPython Lib/fill_light.py rename to Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/fill_light.py diff --git a/Sensors/MVision_Resources/mPython Lib/fill_light_example.py b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/fill_light_example.py similarity index 100% rename from Sensors/MVision_Resources/mPython Lib/fill_light_example.py rename to Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/fill_light_example.py diff --git a/Sensors/MVision_Resources/mPython Lib/main.py b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/main.py similarity index 100% rename from Sensors/MVision_Resources/mPython Lib/main.py rename to Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/main.py diff --git a/Sensors/MVision_Resources/mPython Lib/matrix_mini.py b/Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/matrix_mini.py similarity index 100% rename from Sensors/MVision_Resources/mPython Lib/matrix_mini.py rename to Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/matrix_mini.py