NameError:name 'pyb'is not found.这是错误提示。怎样用I2C和arduino进行交流,
-
import sensor, image, time
from pyb import I2C
import ustruct
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # grayscale is faster
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
black_threshold =(100,59,33,-100,-128,30)while(True):
clock.tick()
img = sensor.snapshot()img.draw_cross(160,120) blobs = img.find_blobs([black_threshold]) if blobs: print('sum : %d'% len(blobs)) out_str = '[' for b in blobs: img.draw_rectangle(b[0:4]) # rect img.draw_cross(b[5], b[6]) # cx, cy x = b[5] y = b[6] img.get_pixel(x,y) out_str += '(%d,%d)' % (x,y) out_str += ']' print(out_str) if x<160: text = "right\n" elif x>160: text = "left\n" else: text = "stop\n" bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) bus.deinit() # 完全关闭设备 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) print("Waiting for Arduino...") while(True): try: bus.send(ustruct.pack("<h", len(data)), timeout=10000) # 首先发送长度 (16-bits). try: bus.send(data, timeout=10000) # 然后发送数据 print("Sent Data!") # 没有遇到错误时,会显示 except OSError as err: pass except OSError as err: pass if y<120: text = "up\n" if y<120: text = "down\n" else: text = "stop\n" data = ustruct.pack("<%ds" % len(text), text) # OpenMV上的硬件I2C总线都是2 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) bus.deinit() # 完全关闭设备 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) print("Waiting for Arduino...") while(True): try: bus.send(ustruct.pack("<h", len(data)), timeout=10000) # 首先发送长度 (16-bits). try: bus.send(data, timeout=10000) # 然后发送数据 print("Sent Data!") # 没有遇到错误时,会显示 except OSError as err: pass except OSError as err: pass
while(True):
clock.tick()
img = sensor.snapshot()img.draw_cross(160,120) blobs = img.find_blobs([GRAYSCALE_THRESHOLD]) if blobs: print('sum : %d'% len(blobs)) out_str = '[' for b in blobs: img.draw_rectangle(b[0:4]) # rect img.draw_cross(b[5], b[6]) # cx, cy x = b[5] y = b[6] img.get_pixel(x,y) out_str += '(%d,%d)' % (x,y) out_str += ']' print(out_str) if b[2]>b[3]: a=b[0]+b[1] if 160<a: text = "right\n" elif 160>a: text = "left\n" else: text = "stop\n" else: c=b[1]+b[3] if 120<c: text = "down\n" elif 120>c: text = "up\n" else: text = "stop\n" data = ustruct.pack("<%ds" % len(text), text) # OpenMV上的硬件I2C总线都是2 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) bus.deinit() # 完全关闭设备 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) print("Waiting for Arduino...") while(True): try: bus.send(ustruct.pack("<h", len(data)), timeout=10000) # 首先发送长度 (16-bits). try: bus.send(data, timeout=10000) # 然后发送数据 print("Sent Data!") # 没有遇到错误时,会显示 except OSError as err: pass except OSError as err: pass
请在这里粘贴代码
-
最前面加上
import pyb
-
加上了,是可以的 但是后面用I2C传送数据,由于程序的问题,出了错误,一直图像不更新,信息无法上传,程序运行的时候出现‘I2C’is not defind.并且运行程序后一直显示waiting for arduino...就走不下去了```
请在这里粘贴代码Untitled - By: xuping - 周日 5月 27 2018
import sensor, image, time
import pyb
from pyb import I2C
import ustructsensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QVGA) # use QVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.red_threshold =(0,100,8,127,-128,127)
black_threshold =(100,59,33,-100,-128,30)data = i2c.recv(3) # 建立一个缓存区
#data=1
if data:
while(True): clock.tick() img = sensor.snapshot() img.draw_cross(160,120) blobs = img.find_blobs([red_threshold], area_threshold=128) if blobs: print('sum : %d'% len(blobs)) out_str = '[' for b in blobs: img.draw_rectangle(b[0:4]) # rect img.draw_cross(b[5], b[6]) # cx, cy x = b[5] y = b[6] img.get_pixel(x,y) out_str += '(%d,%d)' % (x,y) out_str += ']' print(out_str) if x<160: text = "right\n" elif x>160: text = "left\n" else: text = "stop\n" data = ustruct.pack("<%ds" % len(text), text) # OpenMV上的硬件I2C总线都是2 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) bus.deinit() # 完全关闭设备 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) print("Waiting for Arduino...") while(True): try: bus.send(ustruct.pack("<h", len(data)), timeout=10000) # 首先发送长度 (16-bits). try: bus.send(data, timeout=10000) # 然后发送数据 print("Sent Data!") # 没有遇到错误时,会显示 except OSError as err: pass # 不用担心遇到错误,会跳过 # 请注意,有3个可能的错误。 超时错误(timeout error), # 通用错误(general purpose error)或繁忙错误 #(busy error)。 “err.arg[0]”的错误代码分别 # 为116,5,16。 except OSError as err: pass if y<120: text = "up\n" if y<120: text = "down\n" else: text = "stop\n" data = ustruct.pack("<%ds" % len(text), text) # OpenMV上的硬件I2C总线都是2 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) bus.deinit() # 完全关闭设备 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) print("Waiting for Arduino...") while(True): try: bus.send(ustruct.pack("<h", len(data)), timeout=10000) # 首先发送长度 (16-bits). try: bus.send(data, timeout=10000) # 然后发送数据 print("Sent Data!") # 没有遇到错误时,会显示 except OSError as err: pass except OSError as err: pass print(out_str)
else:
while(True):
clock.tick()
img = sensor.snapshot()img.draw_cross(160,120) blobs = img.find_blobs(black_threshold) if blobs: print('sum : %d'% len(blobs)) out_str = '[' for b in blobs: img.draw_rectangle(b[0:4]) # rect img.draw_cross(b[5], b[6]) # cx, cy x = b[5] y = b[6] img.get_pixel(x,y) out_str += '(%d,%d)' % (x,y) out_str += ']' print(out_str) if x<160: text = "right\n" elif x>160: text = "left\n" else: text = "stop\n" data = ustruct.pack("<%ds" % len(text), text) # OpenMV上的硬件I2C总线都是2 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) bus.deinit() # 完全关闭设备 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) print("Waiting for Arduino...") while(True): try: bus.send(ustruct.pack("<h", len(data)), timeout=10000) # 首先发送长度 (16-bits). try: bus.send(data, timeout=10000) # 然后发送数据 print("Sent Data!") # 没有遇到错误时,会显示 except OSError as err: pass except OSError as err: pass if y<120: text = "up\n" if y>120: text = "down\n" else: text = "stop\n" data = ustruct.pack("<%ds" % len(text), text) # OpenMV上的硬件I2C总线都是2 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) bus.deinit() # 完全关闭设备 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) print("Waiting for Arduino...") while(True): try: bus.send(ustruct.pack("<h", len(data)), timeout=10000) # 首先发送长度 (16-bits). try: bus.send(data, timeout=10000) # 然后发送数据 print("Sent Data!") # 没有遇到错误时,会显示 except OSError as err: pass except OSError as err: pass while(True): clock.tick() img = sensor.snapshot() img.draw_cross(160,120) blobs = img.find_blobs([GRAYSCALE_THRESHOLD]) if blobs: print('sum : %d'% len(blobs)) out_str = '[' for b in blobs: img.draw_rectangle(b[0:4]) # rect img.draw_cross(b[5], b[6]) # cx, cy x = b[5] y = b[6] img.get_pixel(x,y) out_str += '(%d,%d)' % (x,y) out_str += ']' print(out_str) if b[2]>b[3]: a=b[0]+b[1] if 160<a: text = "right\n" elif 160>a: text = "left\n" else: text = "stop\n" else: c=b[1]+b[3] if 120<c: text = "down\n" elif 120>c: text = "up\n" else: text = "stop\n" data = ustruct.pack("<%ds" % len(text), text) # OpenMV上的硬件I2C总线都是2 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) bus.deinit() # 完全关闭设备 bus = pyb.I2C(2, pyb.I2C.SLAVE, addr=0x12) print("Waiting for Arduino...") while(True): try: bus.send(ustruct.pack("<h", len(data)), timeout=10000) # 首先发送长度 (16-bits). try: bus.send(data, timeout=10000) # 然后发送数据 print("Sent Data!") # 没有遇到错误时,会显示 except OSError as err: pass except OSError as err: pass
-
就是这个图片
-