当前位置:   article > 正文

OpenMV——串口通信+发送中心位置_openmv串口发送坐标

openmv串口发送坐标

串口通信

OpenMV本质还是一个单片机,可以通过调用pyb中的UART使用串口通信,注意发送的数据类型为字符串,可以通过json.dumps()进行字符串转换

from pyb import UART

uart = UART(3, 9600)
uart.write('hello')
uart.read(5) # read up to 5 bytes
  • 1
  • 2
  • 3
  • 4
  • 5

数据类型转换

blob色块的各类方法:http://book.openmv.cc/image/blob.html
通过方法得到的数据类型为int,需要对其进行一系列操作
1.对其进行0拓展,保证每个数据4位数,方便单片机进行解析
2.进行0拓展的时候,先利用json.dumps()将坐标值转换为字符串,然后利用list将字符串转换为列表,利用列表的insert()方法在头部进行插入
3.完成0拓展之后,将list转换为字符串,利用’’.join()

附上全部代码

功能描述:找到最大的红色色块的中心,通过串口发送其中心坐标数据

# Hello World Example
#
# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!

import sensor, image, time
import json
from pyb import UART

sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.

#对list进行拓展,高位补0
def Expand_String(str):
    while(len(str)<4):
        str.insert(0,'0')
    return str

#所需要寻找的颜色的LAB范围
red_range=(43, 64, 27, 127, -128, 127)
#开串口3,波特率为115200
uart=UART(3,115200)

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.

    #寻找红色色块
    blobs=img.find_blobs([red_range])

    #画出所有红色色块
    for blob in blobs:
        #print(blob.rect())
        img.draw_rectangle(blob.rect())

    #找出面积最大的色块,放到blob_max
    if blobs!=[]:
        blob_max=blobs[0]
        for blob in blobs:
            if blob_max.area()<blob.area():
                blob_max=blob

        #画出blob_max的中心
        img.draw_cross(blob_max.cx(),blob_max.cy(),size=5,color=(0,255,0))

        #将int型的blob_max的中心转换成json形式(字符形式),再转换为list
        centre_x=list(json.dumps(blob_max.cx()))
        centre_y=list(json.dumps(blob_max.cy()))

        #此时用list存放的中心坐标做0拓展
        Expand_String(centre_x)
        Expand_String(centre_y)

        #将list转换为字符串形式
        centre_x=''.join(centre_x)
        centre_y=''.join(centre_y)

        #利用串口发出中心坐标值
        uart.write(centre_x+' '+centre_y+'\r\n')

        print(centre_x+' '+centre_y+'\n')
    else:
        #如果没有找到的话,返回
        print('0000'+' '+'0000'+'\n')

    #print(clock.fps())              # Note: OpenMV Cam runs about half as fast when connected
                                    # to the IDE. The FPS should increase once disconnected.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/474147
推荐阅读
相关标签
  

闽ICP备14008679号