立创·梁山派开发板-21年电赛F题-送药小车-K210功能实现代码讲解

本文详细讲解了基于立创梁山派开发板的21年电赛F题智能送药小车的K210功能实现代码,包括初始化LCD、摄像头、UART通信及中断按钮。代码实现了寻红线和数字识别,通过用户按键或远程控制切换模式。介绍了关键的图像处理和ROI设置,以及如何检测色块、绘制标记、发送数据到立创梁山派。同时,阐述了数字识别的配置和主循环中根据模式选择执行相应功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

送药小车代码仓库:基于立创梁山派的21年电赛F题智能送药小车: 基于立创梁山派的21年电赛F题-智能送药小车

更好的观看体验请去:https://lceda001.feishu.cn/wiki/ZDYbwqDfCiwVlckUEcScF0KSnRh

送药小车立创开源平台资料:【电子竞赛】21年电赛F题-智能送药小车 - 嘉立创EDA开源硬件平台

K210功能实现代码讲解

具体实现代码如下(在2_Code->application->sensor->k210->pyconde->main.py):

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

import sensor, image, time, lcd, struct, ustruct
import gc

from machine import UART,Timer,PWM
from board import board_info
from fpioa_manager import fm
import KPU as kpu
from Maix import GPIO,utils
import gc
import machine

lcd.init()                          # Init lcd display
lcd.clear(lcd.RED)                  # Clear lcd screen.

sensor.reset(freq=22000000, dual_buff=1)                # 设置摄像头频率 24M 开启双缓冲模式 会提高帧率 但内存占用增加
sensor.set_auto_exposure(0)         # 设置自动曝光
sensor.set_auto_gain(False) # 颜色跟踪必须关闭自动增益
sensor.set_auto_whitebal(False) # 颜色跟踪必须关闭白平衡
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)

#kpu = KPU()
#kpu.load_kmodel("/sd/KPU/mnist/uint8_mnist_cnn_model.kmodel")
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.
sensor.set_auto_exposure(0)         # 设置自动曝光

sensor.set_vflip(1)
#______________________________________________________________________________________________________________________________
#打印内存分配情况
print(utils.gc_heap_size())
print("stack mem:"+str(gc.mem_free() / 1024)) # stack mem
print("heap mem:"+str(utils.heap_free() / 1024)) # heap mem

#第一次用执行一次下面这两个语句
#utils.gc_heap_size(800*1024)
#machine.reset()

#______________________________________________________________________________________________________________________________
#程序运行选择
is_need_debug = 0
is_patrol_line = 0
is_upacker_debug = 0
is_stack_heap_mem_debug = 0
is_upacker_recive_debug = 1
is_findflob_debug = 0

#______________________________________________________________________________________________________________________________
#程序运行状态
work_mode = 0  #0是巡线模式,1是数字识别模式

#______________________________________________________________________________________________________________________________
#串口配置区

fm.register(6, fm.fpioa.UART1_TX, force=True)
fm.register(7, fm.fpioa.UART1_RX, force=True)

k210_uart = UART(UART.UART1, 115200, 8, 0, 0, timeout=1000, read_buf_len=4096)

uart_test_write_str = '1260808878'

#______________________________________________________________________________________________________________________________
#按键蜂鸣器配置区
#注册IO,注意高速GPIO口才有中断
fm.register(35, fm.fpioa.GPIO0)
fm.register(16, fm.fpioa.GPIOHS0)
#构建案件对象
KEY=GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_UP)

#PWM通过定时器配置,接到IO15引脚
tim = Timer(Timer.TIMER1, Timer.CHANNEL0, mode=Timer.MODE_PWM)
beep = PWM(tim, freq=1000, duty=0, pin=9)

#按键标志位
key_node = 0
key_press_long = 0

#中断回调函数
def fun(KEY):
    global work_mode,key_node,key_press_long
    temp_count = 0

    time.sleep_ms(10) #消除抖动
    while KEY.value()== 0:
        key_node = 1
        time.sleep_ms(10) #长按延时
        #长按检测计数
        temp_count=temp_count+1
        print(temp_count)
    if temp_count >= 50:

        beep.duty(50)
        time.sleep_ms(500)
        beep.duty(0)
        time.sleep_ms(100)

        print(temp_count)
        key_node = 0
        key_press_long = 1

#开启中断,下降沿触发
KEY.irq(fun, GPIO.IRQ_FALLING)

#______________________________________________________________________________________________________________________________
#要传给梁山派的数据

#work_mode:0是巡线模式,1是数字识别模式
#recognition:0是未知(巡线状态下),1是药房门口区域(停车线),十字路口和T字路口由

class uart_send_data_t:
   def __init__( self, work_mode=0, recognition=0,top_block_offset=0,center_block_offset=0,left_block_offset=0,right_block_offset=0,left_number = 0,right_number = 0):
      self.work_mode = work_mode
      self.recognition = recognition
      self.top_block_offset = top_block_offset
      self.center_block_offset = center_block_offset
      self.left_block_offset = left_block_offset
      self.right_block_offset = right_block_offset
      self.left_number = left_number
      self.right_number = right_number


global_uart_send_data = uart_send_data_t()

#__________________________________________________________
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值