目录
1. 色块识别设计思路
2. OpenMV代码
3. Arduino代码
4.Verilog代码
对于色块识别功能,我想让OpenMV检测色块,在数码管上显示。OpenMV的通信也是串口通信,实现比较耗时。由于之前的红外遥控已经外接了Arduino,因此我依然借助Arduino对红绿蓝进行编码,使用两个IO实现信息传递,在FPGA上进行译码,最后根据对应的信息实现数码管显示。
流程如下:
import sensor, image, time
from pyb import UART
uart = UART(3, 115200)
red_threshold=(1, 21, 5, 50, -20, 36) #red
green_threshold=(7, 22, -33, -14, 9, 27)#green
blue_threshold=(6, 42, -8, 16, -10, -79)#blue
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
while True:
clock.tick()
img = sensor.snapshot()
red_blobs = img.find_blobs([red_threshold], pixels_threshold=200, area_threshold=200, merge=True)
green_blobs = img.find_blobs([green_threshold], pixels_threshold=200, area_threshold=200, merge=True)
blue_blobs = img.find_blobs([blue_threshold], pixels_threshold=200, area_threshold=200, merge=True)
if red_blobs and not green_blobs and not blue_blobs:
uart.write("2") # 发送给Arduino的红色标识
print("红色")
elif green_blobs and not red_blobs and not blue_blobs:
uart.write("3") # 发送给Arduino的绿色标识
print("绿色")
elif blue_blobs and not red_blobs and not green_blobs:
uart.write("1") # 发送给Arduino的蓝色标识
print("蓝色")
else:
uart.write("0") # 未识别到任何颜色的标识
print("未识别到任何颜色")
print(clock.fps()) # 打印帧率
#include
int data;
SoftwareSerial BT(8, 11); // 引脚2是arduino的RX端
//无-48 蓝-49 红-50 绿-51
void setup() {
BT.begin(115200); //设置arduino的串口波特率与蓝牙模块的默认值相同为9600
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
if (BT.available()>0)
{
data=BT.read(); //读取串口字符串
// 根据接收到的数据进行相应的处理
//00-无 01-红 10-绿 11-蓝
if (data == 49) {
Serial.println("Blue"); // 打印蓝色
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
} else if (data == 50) {
Serial.println("Red"); // 打印红色
digitalWrite(6,LOW);
digitalWrite(9, HIGH);
} else if (data == 51) {
Serial.println("Green"); // 打印绿色
digitalWrite(6,HIGH);
digitalWrite(9, LOW);
}
else
{
digitalWrite(6,LOW);
digitalWrite(9,LOW);
}
}
}
//遥控模式
else if(data==8'b11100000) begin
case(openmv_in)
2'b00: led1_reg<=SEG_NUM3;
2'b01: led1_reg<=SEG_NUM1;//red
2'b10: led1_reg<=SEG_NUM2;//green
2'b11: led1_reg<=SEG_NUM0;//blue
endcase
ledout<=4'b0111;//亮三个LED
mode<=0;//默认状态停止
Mode_Control<=1;//遥控模式标志符置高
end