天天看點

openmv和arduino的夢幻關聯,在arduino驅動的FTF屏上顯示圖像坐标點效果:工程檔案:arduino代碼:openmv代碼:

目錄

  • 效果:
  • 工程檔案:
  • arduino代碼:
  • openmv代碼:

驅動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")