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
class USB_Port: def __init__(self): self.usb_serial = USB_VCP() self.recv_buf = bytearray(1) # Disable Control-C on the USB serial port in case one comes in the # data. self.usb_serial.setinterrupt(-1) def read_byte(self): """Reads a byte from the usb serial device.""" if self.usb_serial.any(): bytes_read = self.usb_serial.recv(self.recv_buf) if bytes_read > 0: return self.recv_buf[0] def write(self, data): """Writes an entire packet to the serial port.""" self.usb_serial.write(data)
class USB_Port: """Implements a port which can be used to receive bioloid device commands from a host. """ def __init__(self): self.usb_serial = USB_VCP() self.baud = 0 self.recv_buf = bytearray(1) # Disable Control-C on the USB serail port in case one comes in the # data. self.usb_serial.setinterrupt(-1) def any(self): """Returns a truthy value if characters are available to be read.""" return self.usb_serial.any() def read_byte(self): """Reads a byte from the usb serial device. This function will return None if no character was read within the designated timeout. The max Return Delay time is 254 x 2 usec = 508 usec (the default is 500 usec). This represents the minimum time between receiving a packet and sending a response. """ bytes_read = self.usb_serial.recv(self.recv_buf, timeout=2) if bytes_read > 0: return self.recv_buf[0] def set_baud(self, baud): """Sets the baud rate. Note that for USB Serial, this is essentially a no-op. We store the baud rate that was set, so that """ self.baud = baud def write_packet(self, packet_data): """Writes an entire packet to the serial port.""" self.usb_serial.write(packet_data)
exposure_us = int(current_exposure_time_in_microseconds * EXPOSURE_TIME_SCALE)) clock = time.clock() # Tracks FPS. usb = USB_VCP() #Send standby code (904) until RasPi gives the go ahead #comment out this while loop to start the camera spamming coords by default while (False): red_led.off() green_led.on() blue_led.on() ir_led.off() print('{904$904}') cmd = usb.recv(5, timeout=100) if (cmd == b'start'): cmd = "0" break while (True): #clock.tick() img = sensor.snapshot() blobs = img.find_blobs([(240, 255)], pixels_threshold=1, area_threshold=1, merge=False, margin=50) #red blobs if len(blobs) > 3: raise_error()
dac2 = pyb.DAC(pyb.Pin('X6'), bits=12) pwmGain = pyb.Pin('X1') pwmSlew = pyb.Pin('X2') pwmBias = pyb.Pin('X3') tim2 = pyb.Timer(2, freq=10000) tim5 = pyb.Timer(5, freq=1000000) pwmBiasCh = tim5.channel(3, pyb.Timer.PWM, pin=pwmBias) pwmGainCh = tim5.channel(1, pyb.Timer.PWM, pin=pwmGain) pwmBiasCh.pulse_width_percent(50) pwmSlewCh = tim2.channel(2, pyb.Timer.PWM, pin=pwmSlew) amplitude, Stop = 999, True frequency = 10000 duty = 10 while Stop: cmd = usb.recv(4, timeout=5000) usb.write(cmd) if (cmd == b'strt'): pyb.LED(1).on() pyb.LED(2).off() pyb.LED(3).off() utime.sleep(1) tim2 = pyb.Timer(2, freq=10000) pwmSlewCh.pulse_width_percent(duty) elif (cmd == b'ampl'): data1 = bytes(8) cmd = usb.recv(4, timeout=5000) data1 = ustruct.unpack('L', cmd) dataList1 = list(data1) dataStr1 = [str(i) for i in dataList1] percent = int("".join(dataStr1))
(blob.cx(), blob.cy()) # Note - the blob rotation is unique to 0-180 only. if (ball != None): ball_angle = (angle(ball)) use_the_force_luke = 1 if (stream): 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:
# #!/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)
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) elif (cmd == b'frmps'): usb.send(clock.fps()) elif (cmd == b'ledr0'): red_led.off() elif (cmd == b'ledr1'): red_led.on() elif (cmd == b'ledg0'): green_led.off() elif (cmd == b'ledg1'): green_led.on()
from pyb import USB_VCP u = USB_VCP() while True: data = u.recv(1) ch = data[0] if ch < ord(' ') or ch > ord('~'): ch = '.' print("Rcvd: 0x%x '%c'" % (data[0], ch))
# USB serial example. # # WARNING: # This script should NOT be run from the IDE or command line, it should be saved as main.py # Note the following commented script shows how to receive the image from the host side. import sensor, image, time, ustruct from pyb import USB_VCP, LED usb = USB_VCP() led = LED(1) 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. usb.setinterrupt(3) # input while(True): cmd = usb.recv(1, timeout=5000) if (cmd == b'\x01'): led.toggle() for item in range(65, 91): usb.write(ustruct.pack('<b', item)) else: continue