def main(): # CMD args parser parser = argparse.ArgumentParser(description='openmv stress test') parser.add_argument("-j", "--disable_fb", action = "store_true", help = "Disable FB JPEG compression") parser.add_argument("-p", "--port", action = "store", help = "OpenMV serial port") parser.add_argument("-t", "--time", action = "store", default = 100, help = "Max time before stopping the script") parser.add_argument("-s", "--script", action = "store",\ default="../scripts/examples/01-Basics/helloworld.py", help = "OpenMV script file") # Parse CMD args args = parser.parse_args() # init openmv if (args.port): portname = args.port elif 'darwin' in sys.platform: portname = "/dev/cu.usbmodem14221" else: portname = "/dev/openmvcam" print("\n>>>Reading script: %s\n" %(args.script)) with open(args.script, "r") as f: script = f.read() print("%s\n" %(script)) connected = False for i in range(10): try: # Open serial port. # Set small timeout when connecting pyopenmv.init(portname, baudrate=921600, timeout=0.050) connected = True break except Exception as e: connected = False sleep(0.100) if not connected: print ( "Failed to connect to OpenMV's serial port.\n" "Please install OpenMV's udev rules first:\n" "sudo cp openmv/udev/50-openmv.rules /etc/udev/rules.d/\n" "sudo udevadm control --reload-rules\n\n") sys.exit(1) # Set higher timeout after connecting. pyopenmv.set_timeout(0.500) # Enable/Disable framebuffer compression. print(">>>Enable FB JPEG compression %s" %(str(not args.disable_fb))) pyopenmv.enable_fb(not args.disable_fb) # Interrupt running script. pyopenmv.stop_script() max_timeout = int(args.time) for i in xrange(1000): pyopenmv.exec_script(script) sleep(randint(0, max_timeout)/1000) pyopenmv.stop_script()
def __init__(self, port="/dev/openmvcam"): self.port = port self.connected = False pyopenmv.disconnect() rospy.loginfo("Connecting...") for i in range(10): try: # opens CDC port. # Set small timeout when connecting pyopenmv.init(self.port, baudrate=921600, timeout=0.050) self.connected = True break except Exception as e: self.connected = False sleep(0.100) if not self.connected: rospy.logerr("Failed to connect to OpenMV's serial port.\n" "Please install OpenMV's udev rules first:\n" "sudo cp openmv/udev/50-openmv.rules /etc/udev/rules.d/\n" "sudo udevadm control --reload-rules\n\n") return rospy.loginfo("Connected!") # Set higher timeout after connecting for lengthy transfers. pyopenmv.set_timeout(1*2) # SD Cards can cause big hicups. pyopenmv.stop_script() pyopenmv.enable_fb(True) pyopenmv.exec_script(script) # init pub self.pub_image = rospy.Publisher("/image", Image, queue_size=10) # init thread self.running = False self.thread = Thread(target=self.run) self.thread.start() rospy.on_shutdown(self.shutdown)
def camera_connect(): """ Camera connection loop. """ connected = False pyopenmv.disconnect() # try and connect for i in range(10): try: # opens CDC port. # Set small timeout when connecting pyopenmv.init(port_name, baudrate=921600, timeout=0.050) connected = True break except Exception as e: connected = False sleep(0.100) if not connected: print("Failed to connect to OpenMV's serial port.\n" "Please install OpenMV's udev rules first:\n" "sudo cp openmv/udev/50-openmv.rules /etc/udev/rules.d/\n" "sudo udevadm control --reload-rules\n\n") sys.exit(1) # Set higher timeout after connecting for lengthy transfers. pyopenmv.set_timeout(1 * 2) # SD Cards can cause big hicups. pyopenmv.stop_script() pyopenmv.enable_fb(True) pyopenmv.exec_script(blob_script) # init screen running = True Clock = pygame.time.Clock() font = pygame.font.SysFont("monospace", 15) return running, Clock, font
connected = True break except Exception as e: connected = False sleep(0.100) if not connected: print ( "Failed to connect to OpenMV's serial port.\n" "Please install OpenMV's udev rules first:\n" "sudo cp openmv/udev/50-openmv.rules /etc/udev/rules.d/\n" "sudo udevadm control --reload-rules\n\n") sys.exit(1) # Set higher timeout after connecting for lengthy transfers. pyopenmv.set_timeout(1*2) # SD Cards can cause big hicups. pyopenmv.stop_script() pyopenmv.enable_fb(True) pyopenmv.exec_script(script) # init screen running = True Clock = pygame.time.Clock() font = pygame.font.SysFont("monospace", 15) while running: Clock.tick(100) # read framebuffer fb = pyopenmv.fb_dump() if fb != None: # create image from RGB888
def camera_exec(): """ Main camera execution loop that runs until program is aborted. 1. Camera tries to connect continuously until successful 2. Camera outputs frame buffer -> video stream 3. Camera outputs text buffer -> data about target object - Data is passed to robot for evaluation """ pygame.init() locals() plot_num = 0 running, Clock, font = camera_connect() while running: Clock.tick(100) # read framebuffer fb = None while (True): try: fb = pyopenmv.fb_dump() break except Exception as e: # try and reconnect on failure camera_connect() # signal to UArm that camera has connected camera_started.set() if fb is not None: # create image from RGB888 image = pygame.image.frombuffer(fb[2].flat[0:], (fb[0], fb[1]), 'RGB') screen = pygame.display.set_mode((fb[0], fb[1]), pygame.DOUBLEBUF, 32) fps = Clock.get_fps() # blit stuff screen.blit(image, (0, 0)) screen.blit(font.render("FPS %.2f" % (fps), 1, (255, 0, 0)), (0, 0)) # update display pygame.display.flip() # get output from text buffer tx_len = pyopenmv.tx_buf_len() # object was found by camera if there is outputted text if tx_len: ''' if UArm has signaled to the camera to identify the object and the camera has not already assigned values to the global variables associated with the object's location ''' if camera_event.is_set() and (data_ready.is_set() is False): # read the most recent data at index 0 from the text buffer buff = pyopenmv.tx_buf(tx_len).decode() split_buff = str(buff).splitlines() if h_angle_key in split_buff[0]: # Most recent line in buff contains needed information global h_angle, v_angle, is_centered tok = split_buff[0].split() # set angles to corresponding values determined by camera h_angle, v_angle = float(tok[1]), float(tok[3]) if tok[5] == "True": is_centered = True else: is_centered = False # signal that global variables have been set data_ready.set() if plot_ready.is_set(): print("success_rate: ", success_history) plot_distance(distance_history, plot_num) plot_success(success_history, plot_num) plot_num += 1 plot_ready.clear() print("success rate for ", len(success_history), " tests: ", success_history.count(True) / len(success_history)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False if event.key == pygame.K_c: pygame.image.save(image, "capture.png") pygame.quit() pyopenmv.stop_script()