Serial communication I

Video Tutorial 27 - Serial communication sending data:

https://singtown.com/learn/50235/

Video Tutorial 28 - Serial communication receiving data:

https://singtown.com/learn/50240/

Introduction

Why use serial?Because sometimes you need send information to other MCD,serial is simple and universal,basically every MCU has serial.

Serial TTL need three lines at least:TXD,RXD,GND,TXD is sending end,RXD is receiving terminal,and GND is ground wire.while linking,you need to connect RXD of OpenMV with other TXD of MCU,link TXD with RXD.As is shown in the picture:

import time
from machine import UART
#from pyb import UART

# OpenMV4 H7 Plus, OpenMV4 H7, OpenMV3 M7, OpenMV2 M4 的UART(3)是P4-TX P5-RX
uart = UART(3, 19200)   #OpenMV RT 注释掉这一行,用下一行UART(1)
#uart = UART(1, 19200)  #OpenMV RT 用UART(1)这行,注释掉上一行UART(3)
# OpenMV RT 只有串口UART(1),对应P4-TX P5-RX; OpenMV4 H7 Plus, OpenMV4 H7, OpenMV3 M7 的UART(1)是P0-RX P1-TX

while(True):
    uart.write("Hello World!\r")
    time.sleep_ms(1000)

First instantiate a serial port with a baud rate of 19200, and then call the write method.

Note: OpenMV RT only has serial port UART(1), corresponding to P4-TX P5-RX.

The serial port UART(1) of OpenMV4 H7 Plus, OpenMV4 H7, OpenMV3 M7 is P0-RX P1-TX.

The serial port UART(3) of OpenMV4 H7 Plus, OpenMV4 H7, OpenMV3 M7, OpenMV2 M4 is P4-TX P5-RX

Transmitting complex data

In the previous section we talked about json strings.

# Blob Detection and uart transport
import sensor, image, time
#from pyb import UART
from machine import UART
import json
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
yellow_threshold   = (65, 100, -10, 6, 24, 51)
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
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.

# OpenMV4 H7 Plus, OpenMV4 H7, OpenMV3 M7, OpenMV2 M4 的UART(3)是P4-TX P5-RX
uart = UART(3, 115200)   #OpenMV RT 注释掉这一行,用下一行UART(1)
#uart = UART(1, 115200)  #OpenMV RT 用UART(1)这行,注释掉上一行UART(3)
# OpenMV RT 只有串口UART(1),对应P4-TX P5-RX; OpenMV4 H7 Plus, OpenMV4 H7, OpenMV3 M7 的UART(1)是P0-RX P1-TX

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

    blobs = img.find_blobs([yellow_threshold])
    if blobs:
        print('sum :', len(blobs))
        output_str = json.dumps(blobs)
        for b in blobs:
            # Draw a rect around the blob.
            img.draw_rectangle(b.rect()) # rect
            img.draw_cross(b.cx(), b.cy()) # cx, cy

        print('you send:',output_str)
        uart.write(output_str+'\n')
    else:
        print('not found!')

The resulting output is:

sum : 1
you send: [{x:17, y:23, w:37, h:12, pixels:178, cx:40, cy:29, rotation:3.060313, code:1, count:1}]
sum : 2
you send: [{x:34, y:24, w:19, h:13, pixels:149, cx:45, cy:30, rotation:3.120370, code:1, count:1}, {x:23, y:30, w:8, h:2, pixels:17, cx:27, cy:30, rotation:0.046378, code:1, count:1}]

This will send all the blobs.

Streamlining data

But sometimes you don't want to transmit a lot of data. For example, I only want to transmit the x and y center coordinates of the largest color block.\ Construct the data you want to transmit.

Write a for loop and then write a find_max() function.

# Blob Detection and uart transport
import sensor, image, time
#from pyb import UART
from machine import UART
import json
# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
yellow_threshold   = (65, 100, -10, 6, 24, 51)
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
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.

# OpenMV4 H7 Plus, OpenMV4 H7, OpenMV3 M7, OpenMV2 M4 的UART(3)是P4-TX P5-RX
uart = UART(3, 115200)   #OpenMV RT 注释掉这一行,用下一行UART(1)
#uart = UART(1, 115200)  #OpenMV RT 用UART(1)这行,注释掉上一行UART(3)
# OpenMV RT 只有串口UART(1),对应P4-TX P5-RX; OpenMV4 H7 Plus, OpenMV4 H7, OpenMV3 M7 的UART(1)是P0-RX P1-TX

def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob.pixels() > max_size:
            max_blob=blob
            max_size = blob.pixels()
    return max_blob

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

    blobs = img.find_blobs([yellow_threshold])
    if blobs:
        max_blob=find_max(blobs)
        print('sum :', len(blobs))
        img.draw_rectangle(max_blob.rect())
        img.draw_cross(max_blob.cx(), max_blob.cy())

        output_str="[%d,%d]" % (max_blob.cx(),max_blob.cy()) #方式1
        #output_str=json.dumps([max_blob.cx(),max_blob.cy()]) #方式2
        print('you send:',output_str)
        uart.write(output_str+'\r\n')
    else:
        print('not found!')

Result:

sum : 6
you send: [63,45]
sum : 2
you send: [60,50]
sum : 1
you send: [61,51]

In the above code,

output_str="[%d,%d]" % (max_blob.cx(),max_blob.cy()) #方式1

and

output_str=json.dumps([max_blob.cx(),max_blob.cy()]) #方式2

The result is the same, because the structure is simple, you can use Python's string formatting function or JSON's conversion function.

In the result, even if multiple color blocks are found, only the coordinates of the largest color block are sent.

results matching ""

    No results matching ""