openmv和arduino的梦幻联动,在arduino驱动的FTF屏上显示图像坐标点

29 篇文章 10 订阅
5 篇文章 1 订阅

驱动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")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

创客阿蛋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值