备战电赛K210篇(第四天)

今天实现了和32的传输数据PID等一系列操作,可以说可以正常上路了,可以寻迹了。明天的话去学习如何识别数字之类的。

当然用了K210端的定时器,发现比32简单不少

import time
import sensor
import lcd, image
from machine import UART
from fpioa_manager import fm
from machine import Timer

lcd.init()
lcd.init(freq = 15000000)
lcd.init(type=2)
lcd.rotation(2)

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)#灰度
sensor.set_framesize(sensor.QVGA)#分辨率240×320
sensor.set_auto_gain(False)#自动增益
sensor.set_auto_whitebal(False)#白平衡
sensor.skip_frames(time=100)#跳过3000张图片
sensor.run(1)

z=0
x=0
y=0
#########################################
Range1 = (0,100,320,16)
Range2 = (0,180,320,8)
black_my=(42, 22, -9, 10, 10, -18)
Flag=0        #判断开始
##########################################################配置 9600 八位 不校验 一位停止位 串口接收超时时间 串口接收缓存

fm.register( 10,fm.fpioa.UART1_TX, force=True )
uart_A = UART(UART.UART1,115200, 8,0,1, timeout = 1000, read_buf_len=4096)

##########发送
def sending_data(num,x,y):
    FH= bytearray([0xa3,0xb3,num,x,y,0xc3])
    uart_A.write(FH);

##########################################################


class timer_property():
    cnt     = 0                                             # 定时器计数值
    cnt_max = 0                                             # 定时器计数值上限
    period  = 0                                             # 定时器周期
    freq    = 0                                             # 定时器频率

# 定时器0 配置_______________________________________________________
# 定时器0 实例化类
timer0 = timer_property()                                   # 实例化定时器属性类 timer_property() 为 timer0
timer0.cnt_max = 5                                          # 设定 定时器0 的计数值上限为 5
timer0.period = 100                                         # 设定 定时器0 的周期为 100

def timer0_back(tim0):
    if timer0.cnt < timer0.cnt_max:                         # 若 定时器0 的计数值小于 定时器0 的计数值上限
        timer0.cnt = timer0.cnt + 1                         # 计数值自增
    else:
        timer0.cnt = 0                                      # 超出计数值上限 则计数值重置为0
# 定时器0 初始化
tim0 = Timer(Timer.TIMER0,                                  # 定时器编号 定时器0
            Timer.CHANNEL0,                                 # 定时器通道 通道0
            mode = Timer.MODE_PERIODIC,                     # 定时器模式 周期性
            unit = Timer.UNIT_MS,                           # 定时器周期单位 ms
            period = timer0.period,                         # 定时器周期 timer0.period 若 unit 为 Timer.UNIT_MS 则周期为 timer0.period ms
            callback = timer0_back)                         # 定时器触发中断后执行的回调函数 timer0_back

###########################################################
x=0
num=0
y=0
count=0
c=0
print("发送OK================")
while True:
    img=sensor.snapshot()

    are1 = img.find_blobs([black_my],roi=Range1,area_threshold=200,merge=True)
    #are2 = img.find_blobs([black_my],roi=Range2,area_threshold=120,merge=True,margin=120)
    #are1寻迹  are2判断路口
    if are1:
        for blob in are1:
           #blob[0]_颜色外框
           #blob[1]_颜色外框Y
           #blob[2]_颜色外框W宽度
           #blob[3]_颜色外框H高度
           #blob[4]_像素数量
           #blob[5]_中心坐标X
           #blob[6]_中心坐标Y
           tmp=img.draw_rectangle(blob[0:4])#画框
           tmp=img.draw_cross(blob[5],blob[6])#十字
           c=img.get_pixel(blob[5],blob[6])
           print(blob.cx(),blob.cy())
           if timer0.cnt == 0:#定时器

               x=blob.cx()//100
               y=(blob.cx()//10)%10
               z=blob.cx()%10
               sending_data(x,y,z)
               print("x:",x,"y:",y,"z:",z)


    lcd.display(img)





发送的是x的坐标和中心位置x形成位置pid的闭环。

学习之路的艰辛,无非就是外界的干扰啊 

这一阶段的学习可以搞好了,k210的寻迹,这只是基础。还得更加努力加油加油加油!!!

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值