驱动FTF屏幕代码
openmv寻找最大色块
openmv和arduino通信的基础代码
效果:
工程文件:
arduino代码:
#include <SPI.h>
#include "Ucglib.h"
volatile int flag;
String shuju;
bool stringComplete = false; //字符串是否完成
int CenterX, lcd_CenterX, last_lcd_CenterX;
int CenterY, lcd_CenterY, last_lcd_CenterY;
/*
Hardware SPI Pins:
Arduino Uno sclk=13, data=11
Arduino Due sclk=76, data=75
Arduino Mega sclk=52, data=51
*/
Ucglib_ST7735_18x128x160_SWSPI ucg(/*sclk=*/ 13, /*data=*/ 11, /*cd=*/ 9 , /*cs=*/ 10, /*reset=*/ 8);
void setup(void)
{
flag = 0;
shuju = "0";
Serial.begin(9600);
delay(1000);
// ucg.begin(UCG_FONT_MODE_TRANSPARENT); //字体模式透明
ucg.begin(UCG_FONT_MODE_SOLID); //立体字,有底字
ucg.clearScreen(); //清除屏幕
ucg.setRotate90(); //旋转
ucg.setColor(255, 255, 255); //白色
ucg.setFont(ucg_font_ncenR24_tr); //字体
}
void loop(void)
{
if (stringComplete) {
// Serial.print(shuju);
dataProcessing();
ucg.setColor(0, 0, 0); //黑色
ucg.setPrintPos(last_lcd_CenterX, last_lcd_CenterY);
ucg.print("+"); //达到清屏的目的
last_lcd_CenterX = lcd_CenterX;
last_lcd_CenterY = lcd_CenterY; //记录上次的值
ucg.setColor(255, 255, 255); //白色
ucg.setPrintPos(lcd_CenterX, lcd_CenterY);
ucg.print("+");
shuju = "";
stringComplete = false;
}
}
int foundStr(char Str) //找字符的位置
{
int founddata;
founddata = shuju.indexOf(Str); //'{'所在的位置
return founddata;
}
String shujuduan(int first, int last) //定义 数据段 函数
{
String Strdata;
Strdata = String(shuju).substring(first, last); //赋值Strdata是first到last的字符串
return Strdata;
}
void dataProcessing() //数据处理
{
int X_location; //X的位置
int Y_location; //Y的位置
int B_location; //Y的位置
String X_Str;
String Y_Str;
X_location = foundStr('X');
Y_location = foundStr('Y');
X_Str = shujuduan(X_location + 1, Y_location); //X到Y的位置
X_location = foundStr('Y');
B_location = foundStr('B');
Y_Str = shujuduan(Y_location + 1, B_location); //Y到B的位置
/*
Serial.print("X_Str:");
Serial.print(X_Str);
Serial.print(" Y_Str:");
Serial.println(Y_Str);
*/
CenterX = X_Str.toInt();
CenterY = Y_Str.toInt(); //转成可以用的整型
/*
Serial.print("CenterX:");
Serial.print(CenterX);
Serial.print(" CenterY:");
Serial.println(CenterY);
*/
lcd_CenterX = map(CenterX, -80, 80, 0, 160);
lcd_CenterY = map(CenterY, -60, 60, 0, 128);
/*
Serial.print("lcd_CenterX:");
Serial.print(lcd_CenterX);
Serial.print(" lcd_CenterY:");
Serial.println(lcd_CenterY);
*/
}
void serialEvent() {
while (Serial.available()) {
//获取新的字节
char inChar = (char)Serial.read();
shuju += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
openmv代码:
import sensor, image, time
from pid import PID
from pyb import Servo
from pyb import UART
uart = UART(3, 9600) #P4 TX P5 RX
green_threshold = (35, 60, 76, 15, 5, 70)
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565. 128*160
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
return max_blob
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([green_threshold])
if blobs:
max_blob = find_max(blobs)
img.draw_rectangle(max_blob.rect()) # rect
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
#print("X:",max_blob.cx(),"Y:",max_blob.cy())
X_error = max_blob.cx()-img.width()/2 #底的误差为最大色块所在的X轴-宽(宽固定=160 /2=80)我理解为取XY中心点
Y_error = max_blob.cy()-img.height()/2
X_error = int(X_error)
Y_error = int(Y_error)
print("X:",X_error,"Y:",Y_error)
#uart.write("X:",X_error,"Y:",Y_error+'\r\n')
'''
图像大小为160*120
所以中心点是(80,60)
160/2-现在的位置=X
120/2-现在的位置=Y
'''
uart.write("A")
uart.write("X"+str(X_error))
uart.write("Y"+str(Y_error))
uart.write("B")
uart.write("\r\n")