openmv第一次调试

2018-09-19  20:14:51

import sensor, image, time
import car
import json
import time
from pyb import UART
from pid import PID

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(30) # Let new settings take affect.
sensor.set_auto_whitebal(False) # 追踪颜色需要关闭白平衡
sensor.set_auto_gain(False)#扫码需要关闭自动增益
clock = time.clock() # Tracks FPS.
red_threshold  = (13, 49, 18, 61, 6, 47)  #红色阈值
size_threshold = 2000
x_pid = PID(p=0.5, i=1, imax=100)
h_pid = PID(p=0.05, i=0.1, imax=50)
uart = UART(3, 115200)
ma_flag = 0
ma = []
'''
#串口通信:
测试json数据    {(1,22),(-3,33),(22222,0),(9999,12),(0,0)}
data=[]
data_out = json.dumps(set(data))
uart.write(data_out +'\n')
uart.write("Hello World!\r")
'''
######################
'''
#扫码反馈信息
img.lens_corr(1.8) # 缩小视野
    for code in img.find_qrcodes():
        print(code)

'''
def find_ma():
    global ma_flag
    if ma_flag :
        print("已完成扫码。")
    else :
        ma = img.find_qrcodes()
        if ma:
            print("发现二维码即将停车进行扫码")
            saoma()
            ma_flag = 1
        else:
            print("正在寻找二维码。。。")

def saoma():
    img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.
    for code in img.find_qrcodes():
        print(code)

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()
    find_ma()


    '''
    blobs = img.find_blobs([red_threshold])
    if blobs:
        max_blob = find_max(blobs)
        x_error = max_blob[5]-img.width()/2
        h_error = max_blob[2]*max_blob[3]-size_threshold
        # print("x error: ", x_error)
        print("测试与arduino通信")
        ###下面代码为测试与arduino通信发送坐标####
        data=[]
        for b in blobs:
            # Draw a rect around the blob.
            img.draw_rectangle(b[0:4]) # rect
            img.draw_cross(b[5], b[6]) # cx, cy
            data.append((b.cx(),b.cy()))
            data_out = json.dumps(set(data))
            uart.write(data_out +'\n')
        ###################################
        img.draw_rectangle(max_blob[0:4]) # rect
        img.draw_cross(max_blob[5], max_blob[6]) # cx, cy
        x_output=x_pid.get_pid(x_error,1)
        h_output=h_pid.get_pid(h_error,1)
        print("h_output",h_output)
        car.run(-h_output-x_output,-h_output+x_output)
    else:
        car.run(10,-10)  #旋转寻找小球
        '''

 

转载于:https://www.cnblogs.com/pengwenzheng/p/9676919.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值