-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamReader.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; // 超时或没有数据 | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...s/MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamReader.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef SMART_CAM_READER_H // 防止头文件重复包含 | ||
#define SMART_CAM_READER_H | ||
|
||
#include <Arduino.h> | ||
|
||
// 函数声明 | ||
int SmartCamReader(unsigned int *data, unsigned int timeout = 500); // timeout设置默认值为500ms | ||
|
||
#endif |
36 changes: 36 additions & 0 deletions
36
...MVision_Resources/mPython Lib(for MATRIX or Arduino)/Arduino Side Code/SmartCamSimple.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
26 changes: 26 additions & 0 deletions
26
Sensors/MVision_Resources/mPython Lib(for MATRIX or Arduino)/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.