def main(): # usb object usb = USB_VCP() i = 0 j = 0 emailNum = 0 while (True): clock.tick() img = sensor.snapshot(pixformat=sensor.GRAYSCALE) blobs = img.find_blobs(threshold_list, pixels_threshold=0, area_threshold=0, merge=True) cmd = usb.recv(4, timeout=1) if (cmd == b'snap'): i = 0 emailNum = 0 if (i == 0): usb.send(244) if (blobs): i = 1 if (i == 1): fire_blob = max(blobs, key=lambda x: x.density()) img.draw_rectangle(fire_blob.rect()) img.draw_cross(fire_blob.cx(), fire_blob.cy()) # For pan control xPos = fire_blob.cx() xErr = 20 - xPos # For tilt control yPos = fire_blob.cy() yErr = 15 - yPos #img.draw_string(fire_blob.x(), fire_blob.y() - 10, "Pan Error: %.2f pixels" % xPosErr, mono_space=False) usb.send(xPos) usb.send(yPos) if ((xErr <= 1 and xErr >= -1) and (yErr <= 1 and yErr >= -1) and emailNum == 0): bigPic() # Convert to better resolution for email photo img2 = sensor.snapshot(pixformat=sensor.GRAYSCALE) blobs = img2.find_blobs(threshold_list, pixels_threshold=0, area_threshold=0, merge=True) fire_blob = max(blobs, key=lambda x: x.density()) img2.draw_rectangle(fire_blob.rect()) img2.draw_cross(fire_blob.cx(), fire_blob.cy()) img2.to_rainbow( color_palette=sensor.PALETTE_IRONBOW) # color it imgUSB = img2.compress() usb.send(ustruct.pack("<L", imgUSB.size())) usb.send(imgUSB) emailNum = 1 smallPic() # Convert back to low-res imaging for control
# Sensor Initialization sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time=2000) sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking # USB Initialization usb = USB_VCP() # Send X,Y while (True): img = sensor.snapshot() for blob in img.find_blobs(thresholds, pixels_threshold=25, area_threshold=25, merge=True): # Filter non-circle blobs if blob.roundness() > 0.6: buf = str(blob.cx()) + ' ' + str(blob.cy()) usb.send(buf) # Draw red circle around ball # radius = 9 # img.draw_circle(x, y, radius, color = (255, 0, 0), thickness = 4, fill = False) #print(clock.fps())
return "DATABAR_EXP" if (code.type() == image.CODABAR): return "CODABAR" if (code.type() == image.CODE39): return "CODE39" if (code.type() == image.PDF417): return "PDF417" if (code.type() == image.CODE93): return "CODE93" if (code.type() == image.CODE128): return "CODE128" while (True): clock.tick() blue_led.on() img = sensor.snapshot() codes = img.find_barcodes() for code in codes: img.draw_rectangle(code.rect()) if (temp != code.payload()): temp = code.payload() result = code.payload() green_led.on() time.sleep(100) green_led.off() usb.send(result + '\r\n') time.sleep(1000) if not codes: pass
''' 串口通信 ''' import sensor, image, time, ustruct from pyb import USB_VCP usb = USB_VCP() sensor.reset() # 复位并初始化感光元件。 sensor.set_pixformat(sensor.RGB565) # 设置像素格式为RGB565(或GRAYSCALE) sensor.set_framesize(sensor.QVGA) # 将图像大小设置为QVGA (320x240) sensor.skip_frames(time=2000) # 等待设置生效。 while (True): test = 'hi\r\n' usb.send(test) time.sleep(1000)
# xonxoff=False, rtscts=False, stopbits=serial.STOPBITS_ONE, timeout=None, dsrdtr=True) # sp.write("snap") # sp.flush() # size = struct.unpack('<L', sp.read(4))[0] # img = sp.read(size) # sp.close() # # with open("img.jpg", "w") as f: # f.write(img) import sensor, image, time, ustruct from pyb import USB_VCP usb = USB_VCP() sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat( sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) #sensor.set_auto_gain(True) # Automatically set the gain #sensor.__write_reg(0x6B, 0x22) # Enable Advanced AWB #sensor.set_auto_whitebal(True) # Automatically set the white balance #sensor.set_auto_exposure(True) # Automatically set the exposure sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) sensor.skip_frames(time=2000) # Wait for settings take effect. while (True): cmd = usb.recv(4, timeout=5000) if (cmd == b'snap'): img = sensor.snapshot().compress() usb.send(ustruct.pack("<L", img.size())) usb.send(img)
img.draw_rectangle(ball.rect(), (0, 0, 255)) # \/ \/ USB CODE \/ \/ AND THE LLLL's are attacking! ticks = time.ticks() if stream: img.copy(x_scale=.25, y_scale=.25, copy_to_fb=True) img.compress(88) #90 cmd = usb.recv( 2, timeout=1000) # Change this to match the number of commands received if not cmd: continue if cmd[0] == b's'[0] and stream: usb.send( ustruct.pack(">llll", use_the_force_luke, ball_angle, ball_distance, img.size())) usb.send(img) else: usb.send( ustruct.pack(">llll", use_the_force_luke, ball_angle, ball_distance, 0)) if cmd[0] == b's'[0]: stream = True else: stream = False if cmd[1] == b'r'[0]: pyb.LED(1).toggle() if cmd[1] == b'g'[0]: pyb.LED(2).toggle()
# #!/usr/bin/env python2.7 # import sys, serial, struct # port = '/dev/ttyACM0' # sp = serial.Serial(port, baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, # xonxoff=False, rtscts=False, stopbits=serial.STOPBITS_ONE, timeout=None, dsrdtr=True) # sp.setDTR(True) # dsrdtr is ignored on Windows. # sp.write("snap") # sp.flush() # size = struct.unpack('<L', sp.read(4))[0] # img = sp.read(size) # sp.close() # # with open("img.jpg", "w") as f: # f.write(img) import sensor, image, time, ustruct from pyb import USB_VCP usb = USB_VCP() sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) sensor.skip_frames(time = 2000) # Wait for settings take effect. while(True): cmd = usb.recv(4, timeout=5000) if (cmd == b'snap'): img = sensor.snapshot().compress() usb.send(ustruct.pack("<L", img.size())) usb.send(img)
else: return 0 while(True): clock.tick() img = sensor.snapshot() numb = 0 rateOfBlue = 0 rateOfRed = 0 rateOfGreen = 0 #查看图片中是否存在该种颜色(占图片中颜色5%) calculateRateOfColor(img,hsv_low_red,hsv_high_red,rateOfRed) calculateRateOfColor(img,hsv_low_green,hsv_high_green,rateOfGreen) calculateRateOfColor(img,hsv_low_blue,hsv_high_blue,rateOfBlue) #numb 值为1 所需颜色出现在图片中线 为0不在中线 if rateOfRed > 0.05: numb = calculateRange(img,img,hsv_low_red,hsv_high_red) elif rateOfBlue > 0.05: numb = calculateRange(img,img,hsv_low_blue,hsv_high_blue) elif rateOfGreen > 0.05: numb = calculateRange(img,img,hsv_low_green,hsv_high_green) else: numb = 0 #图片中有颜色返回True 无该颜色返回False if numb == 1: USB_VCP.send(True) else: USB_VCP.send(False) print(clock.fps())
blue_led = LED(3) ir_led = LED(4) #usb.send('Camera initialization...') blue_led.on() usb = USB_VCP() sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat( sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) sensor.skip_frames(time=2000) # Wait for settings take effect clock = time.clock() # Create a clock object to track the FPS. # sensor.set_gainceiling(16) blue_led.off() usb.send('Done') contrast = 0 brightness = 0 saturation = 0 vertical_flip = False horizontal_flip = False watchdog_led = True while (True): cmd = usb.recv(5, timeout=2000) if (cmd == b'photo'): clock.tick() img = sensor.snapshot().compress() usb.send(ustruct.pack('<L', img.size())) usb.send(img)
def beam(values): # function that shines the LED on the camera if (((values[3] >= -5) and (values[3] <= 5)) and (values[3] != -1)): green.on() elif (values != [-1, -1, -1, -1, -1, -1]): blue.on() elif (values == [-1, -1, -1, -1, -1, -1]): red.on() while (True): img = sensor.snapshot() # params: width actual of target and height actual of target # returns: centerX, centerY, distance, angleX, angleY, blob width pixels values = getUnfilteredValues(TARGET_WIDTH, TARGET_HEIGHT, img) if (values == None): values = [-1, -1, -1, -1, -1, -1] # makes values this when there is no blob detected beam(values) if (COMMS_METHOD == "print"): print(values) elif (COMMS_METHOD == "usb" ): # sending the data via USB serial to the robot # values = memoryview(values) usb.send( ustruct.pack("d", values[0], values[1], values[2], values[3], values[4], values[5])) elif (COMMS_METHOD == "can"): pass
from pyb import USB_VCP # importing the library for USb VCP(Virtual Comm Port). usb = USB_VCP() # makes USB object while (True): # making a loop to continously send data usb.send("hi123") # send function to send script. ''' NOTE usb.send() fucntion send the data character by character. for ex: usb.send("hi123") send the "hi123" string as "h", "i", "1", "2", "3". '''
imgMs = time.ticks() - ticks # \/ \/ USB CODE \/ \/ ticks = time.ticks() if stream: img.copy(x_scale=.25, y_scale=.25, copy_to_fb=True) img.compress(88) #90 cmd = usb.recv( 2, timeout=1000) # Change this to match the number of commands received if not cmd: continue if cmd[0] == b's'[0] and stream: usb.send(ustruct.pack(">lll", imgMs, usbMs, img.size())) usb.send(img) else: usb.send(ustruct.pack(">lll", imgMs, usbMs, 0)) if cmd[0] == b's'[0]: stream = True else: stream = False usbMs = time.ticks() - ticks if cmd[1] == b'r'[0]: pyb.LED(1).toggle() if cmd[1] == b'g'[0]: pyb.LED(2).toggle() if cmd[1] == b'b'[0]: pyb.LED(3).toggle()