logo

测距加舵机控制的程序定义了length=0后摄像头会变卡有没有什么解决方案呀谢谢啦(代码在下面)

2021-08-13 15:46
2
udfl
import sensor, image, time
from pyb import Servo
yellow_threshold   = (100, 56, -13, -58, 3, 61)
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(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.

K=350#the value should be measured
length=0
while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.

    blobs = img.find_blobs([yellow_threshold])
    if len(blobs) == 1:
        # Draw a rect around the blob.
        b = blobs[0]
        img.draw_rectangle(b[0:4]) # rect
        img.draw_cross(b[5], b[6]) # cx, cy
        Lm = (b[2]+b[3])/2
        length = K/Lm
        print(length)
        #print(Lm)
    if length<10:
        s1 = Servo(1)
        s2 = Servo(2)   
        while(True):
                for i in range(1000):
                    s1.pulse_width(1600 - i)
                    s2.pulse_width(1600 - i)
                    time.sleep_ms(600)
                for i in range(1000):
                    s1.pulse_width(1600 - i)
                    s2.pulse_width(1600 - i)
                    time.sleep_ms(600)
    else:
        s1 = Servo(1)
        s2 = Servo(2)
        while(True):
                for i in range(1000):
                    s1.pulse_width(2999 - i)
                    s2.pulse_width(2999 - i)
                    time.sleep_ms(600)
                for i in range(1000):
                    s1.pulse_width(2999 - i)
                    s2.pulse_width(2999 - i)
                    time.sleep_ms(600)
2021-08-13 19:27
2
udfl

会出现上电的瞬间可以识别出来距离并且控制电机转动,之后画面就定住了也没有数据从串口端出现。可以烧录进去并脱机使用但是脱机的时候和用ide编译的时候一样只能刚上电的时候识别一次

2021-08-13 21:03
I
网红小智智 星瞳科技

你第30行,和42行,都是死循环,永远不会跳出的。

2021-08-14 22:20
2
udfl

@kidswong999 大佬有没有什么办法可以让他不卡死啊求赐教呀,初学不太懂(需求就是控制两个舵机在识别到一个颜色且距离小于一定范围时可以同时旋转)谢谢大佬

2021-08-16 16:37
I
网红小智智 星瞳科技

https://book.openmv.cc/project/pan-tilt.html

参考一下这个。

2021-08-21 17:09
2
udfl

@kidswong999 谢谢大佬非常感谢我去好好学习一下