Embedded_game/002_B_Car/len_B_test_循迹2.py
2025-01-02 12:48:11 +08:00

45 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding:utf-8 -*-
# @Author len
# @Create 2023/12/4 19:25
import sensor
import image
import lcd
import time
from machine import Timer, PWM, UART, Timer
def Servo(angle):
'''
说明:舵机控制函数
功能180度舵机angle:-90至90 表示相应的角度
360连续旋转度舵机angle:-90至90 旋转方向和速度值。
【duty】占空比值0-100
'''
#PWM通过定时器配置接到IO17引脚
tim_pwm = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
S1 = PWM(tim_pwm, freq=50, duty=0, pin=17)
S1.duty((angle+90)/180*10+2.5)
Servo(-75)
THRESHOLD = (5, 70, -23, 15, -57, 0)
clock = time.clock()
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
#sensor.set_vflip(1) #开启图像反转
sensor.set_vflip(0) #关闭图像反转
sensor.run(1)
sensor.skip_frames(30)
min_degree = 0
max_degree = 179
while True:
clock.tick()
img = sensor.snapshot()
for l in img.find_lines(threshold = 2000, theta_margin = 25, rho_margin = 25):
if (min_degree <= l.theta()) and (l.theta() <= max_degree):
img.draw_line(l.line(), color = (255, 0, 0))
lcd.display(img)