我想实现openmv识别到矩形后,舵机开始转动,请问这样的代码如何实现?
-
控制舵机
# Servo Shield Example. # # This example demonstrates the servo shield. Please follow these steps: # # 1. Connect a servo to any PWM output. # 2. Connect a 3.7v battery (or 5V source) to VIN and GND. # 3. Copy pca9685.py and servo.py to OpenMV and reset it. # 4. Connect and run this script in the IDE. import time from servo import Servos from machine import I2C, Pin i2c = I2C(sda=Pin('P5'), scl=Pin('P4')) servo = Servos(i2c, address=0x40, freq=50, min_us=500, max_us=2500, degrees=180) while True: for i in range(0, 8): servo.position(i, 0) time.sleep_ms(500) for i in range(0, 8): servo.position(i, 180) time.sleep_ms(500)
矩形识别
# Find Rects Example # # 这个例子展示了如何使用april标签代码中的四元检测代码在图像中找到矩形。 四元检测算法以非常稳健的方式检测矩形,并且比基于Hough变换的方法好得多。 例如,即使镜头失真导致这些矩形看起来弯曲,它仍然可以检测到矩形。 圆角矩形是没有问题的! # (但是,这个代码也会检测小半径的圆)... import sensor, image, time sensor.reset() sensor.set_pixformat(sensor.RGB565) # 灰度更快(160x120 max on OpenMV-M7) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(time = 2000) clock = time.clock() while(True): clock.tick() img = sensor.snapshot() # 下面的`threshold`应设置为足够高的值,以滤除在图像中检测到的具有 # 低边缘幅度的噪声矩形。最适用与背景形成鲜明对比的矩形。 for r in img.find_rects(threshold = 10000): img.draw_rectangle(r.rect(), color = (255, 0, 0)) for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0)) print(r) print("FPS %f" % clock.fps())
-
矩形识别到后会返回几个值,是不是把控制舵机的while true改成while什么就可以?
-
img.find_rects 返回的是列表。for r in 就是遍历所有的列表。
如果只是动,而不是控制角度的话。
rects = img.find_rects(threshold = 10000) if rects: 控制舵机
-
@kidswong999 识别到矩形后,舵机也没有转动,这是全部代码,两个功能分别都可以实现,但合起来无法联动,帮我看一下哪有问题?
import sensor, image, time sensor.reset() sensor.set_pixformat(sensor.RGB565) # 灰度更快(160x120 max on OpenMV-M7) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(time = 2000) clock = time.clock() while(True): clock.tick() img = sensor.snapshot() # 下面的`threshold`应设置为足够高的值,以滤除在图像中检测到的具有 # 低边缘幅度的噪声矩形。最适用与背景形成鲜明对比的矩形。 for r in img.find_rects(threshold = 10000): img.draw_rectangle(r.rect(), color = (255, 0, 0)) for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0)) print(r) print("FPS %f" % clock.fps()) import time from servo import Servos from machine import I2C, Pin i2c = I2C(sda=Pin('P5'), scl=Pin('P4')) servo = Servos(i2c, address=0x40, freq=50, min_us=500, max_us=2500, degrees=180) rects = img.find_rects(threshold = 10000) if rects: servo.position(0, 0) time.sleep_ms(500) servo.position(0, 180) time.sleep_ms(500)
-
import sensor, image, time import time from servo import Servos from machine import I2C, Pin sensor.reset() sensor.set_pixformat(sensor.RGB565) # 灰度更快(160x120 max on OpenMV-M7) sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(time = 2000) clock = time.clock() i2c = I2C(sda=Pin('P5'), scl=Pin('P4')) servo = Servos(i2c, address=0x40, freq=50, min_us=500, max_us=2500, degrees=180) rects = img.find_rects(threshold = 10000) while(True): clock.tick() img = sensor.snapshot() # 下面的`threshold`应设置为足够高的值,以滤除在图像中检测到的具有 # 低边缘幅度的噪声矩形。最适用与背景形成鲜明对比的矩形。 rects = img.find_rects(threshold = 10000): for f in rects: img.draw_rectangle(r.rect(), color = (255, 0, 0)) for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0)) print(r) if rects: servo.position(0, 0) time.sleep_ms(500) servo.position(0, 180) time.sleep_ms(500) print("FPS %f" % clock.fps())
-
@kidswong999 在 我想实现openmv识别到矩形后,舵机开始转动,请问这样的代码如何实现? 中说:
import sensor, image, time
import time
from servo import Servos
from machine import I2C, Pinsensor.reset()
sensor.set_pixformat(sensor.RGB565) # 灰度更快(160x120 max on OpenMV-M7)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()i2c = I2C(sda=Pin('P5'), scl=Pin('P4'))
servo = Servos(i2c, address=0x40, freq=50, min_us=500, max_us=2500, degrees=180)
rects = img.find_rects(threshold = 10000)while(True):
clock.tick()
img = sensor.snapshot()# 下面的`threshold`应设置为足够高的值,以滤除在图像中检测到的具有 # 低边缘幅度的噪声矩形。最适用与背景形成鲜明对比的矩形。 rects = img.find_rects(threshold = 10000): for f in rects: img.draw_rectangle(r.rect(), color = (255, 0, 0)) for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0)) print(r) if rects: servo.position(0, 0) time.sleep_ms(500) servo.position(0, 180) time.sleep_ms(500) print("FPS %f" % clock.fps())
还是运行不了啊,再帮我们看一下吧
-
@kidswong999
你的代码还是报错了,再帮我们看一下吧