【初学者OpenMV】 04 串口通信上
众所周知 我是一个爱看文档不爱看视频的人 文档链接 https://book.openmv.cc/
串口介绍
可以看到引脚口是 P4 是连接RX0 P5 是连接TX0 GND 共地
串口初始化程序:
非常简洁,直接几行代码搞定输出
import time
from pyb import UART
uart = UART(3, 19200)
while(True):
uart.write("Hello World!
")
time.sleep_ms(1000)
追踪小车并输出
DEMO:
为了让色彩追踪工作得很好,你最好是在一个非常非常, 非常,可控的环境,那里的光线是恒定的。。。 黄色u阈值=(65、100、-10、6、24、51) 你可能需要调整上面的设置来跟踪绿色的东西。。。 在帧缓冲区中选择一个区域以复制颜色设置。
# 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 : 传感器 初始化摄像头:
sensor.reset() # Initialize the camera sensor.
设置 传感器模式 RGB 565
sensor.set_pixformat(sensor.RGB565) # use RGB565.
使用QVGA加速。
传感器跳帧 10 ,使得新设置生效
sensor.skip_frames(10) # Let new settings take affect.
设置白球自动关闭
sensor.set_auto_whitebal(False) # turn this off.
跟踪FPS
clock = time.clock() # Tracks FPS.
串口初始化 3 波特率115200
uart = UART(3, 115200)
拍照并且返回图像
img = sensor.snapshot() # Take a picture and return the image.
寻找黄色阈值的斑点,把斑点中黄色的阈值赋给blobs
blobs = img.find_blobs([yellow_threshold])
如果看到斑点,输出斑点的长度,并且用json.dumps存储斑点的大小
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
输出字符段 有 you send + output_str
print(you send:,output_str)
uart.write(output_str+
)
else:
print(not found!)
精简数据输出
但是有时候不想传输一大堆的数据。 比如:我只想传输面积最大的色块的x,y中心坐标。 想传输什么数据,就构造一个什么数据。
写一个for循环,再写一个find_max()函数。
