def __init__(self): # private self.__crc_16 = self.__def_crc_16 if omv.board_type() == "H7": stm.mem32[stm.RCC + stm.RCC_AHB4ENR] = stm.mem32[stm.RCC + stm.RCC_AHB4ENR] | (1 << 19) stm.mem32[stm.CRC + stm.CRC_POL] = 0x1021 self.__crc_16 = self.__stm_crc_16 elif omv.board_type() == "F7": stm.mem32[stm.RCC + stm.RCC_AHB1ENR] = stm.mem32[stm.RCC + stm.RCC_AHB1ENR] | (1 << 12) stm.mem32[stm.CRC + stm.CRC_POL] = 0x1021 self.__crc_16 = self.__stm_crc_16 self._stream_writer_queue_depth_max = 255
def __get_can_settings(bit_rate, sampling_point): clk = 48000000 if omv.board_type() == "H7" else pyb.freq()[2] for prescaler in range(8): for bs1 in range(16): for bs2 in range(8): if bit_rate == ((clk >> prescaler) // (1 + bs1 + bs2)) and (sampling_point * 10) == (((1 + bs1) * 1000) // (1 + bs1 + bs2)): return (1 << prescaler, bs1, bs2) raise ValueError("Invalid bit_rate and/or sampling_point!")
def get_frame_buffer_call_back(pixformat, framesize, cutthrough, silent): if not silent: print("Getting Remote Frame...") result = interface.call("raw_image_snapshot", struct.pack("<II", pixformat, framesize)) if result is not None: w, h, pixformat, size = struct.unpack("<IIII", result) img = image.Image(w, h, pixformat, copy_to_fb=True) # Alloc cleared frame buffer. if cutthrough: # Fast cutthrough data transfer with no error checking. # Before starting the cut through data transfer we need to sync both the master and the # slave device. On return both devices are in sync. result = interface.call("raw_image_read") if result is not None: # GET BYTES NEEDS TO EXECUTE NEXT IMMEDIATELY WITH LITTLE DELAY NEXT. # Read all the image data in one very large transfer. interface.get_bytes(img.bytearray(), 5000) # timeout else: # Slower data transfer with error checking. # Transfer 32/8 KB chunks. chunk_size = (1 << 15) if omv.board_type() == "H7" else (1 << 13) if not silent: print("Reading %d bytes..." % size) for i in range(0, size, chunk_size): ok = False for j in range(3): # Try up to 3 times. result = interface.call("raw_image_read", struct.pack("<II", i, chunk_size)) if result is not None: img.bytearray( )[i:i + chunk_size] = result # Write the image data. if not silent: print("%.2f%%" % ((i * 100) / size)) ok = True break if not silent: print("Retrying... %d/2" % (j + 1)) if not ok: if not silent: print("Error!") return None return img else: if not silent: print("Failed to get Remote Frame!") return None
def __init__(self, devid): self.can = CAN(2, CAN.NORMAL) # Device id [0-63] self.devid = devid # Mode of the device self.mode = 0 # Configuration data self.config_simple_targets = 0 self.config_line_segments = 0 self.config_color_detect = 0 self.config_advanced_targets = 0 self.frame_counter = 0 # Buffer for receiving data in our callback. self.read_buffer = bytearray(8) self.read_data = [0, 0, 0, memoryview(self.read_buffer)] # Initialize CAN based on which type of board we're on if omv.board_type() == "H7": print("H7 CAN Interface") self.can.init(CAN.NORMAL, extframe=True, baudrate=1000000, sampling_point=75) # 1000Kbps H7 #self.can.init(CAN.NORMAL, extframe=True, prescaler=4, sjw=1, bs1=8, bs2=3) elif omv.board_type() == "M7": self.can.init(CAN.NORMAL, extframe=True, prescaler=3, sjw=1, bs1=10, bs2=7) # 1000Kbps on M7 self.can.setfilter(0, CAN.LIST32, 0, [ self.my_arb_id(self.api_id(1, 3)), self.my_arb_id(self.api_id(1, 4)) ]) print("M7 CAN Interface") else: print("CAN INTERFACE NOT INITIALIZED!") self.can.restart()
def __init__(self, message_id=0x7FF, bit_rate=250000, sample_point=75, can_bus=2): self.__message_id = message_id self.__can = pyb.CAN(can_bus, pyb.CAN.NORMAL, baudrate=bit_rate, sample_point=sample_point, auto_restart=True) self.__can.setfilter( 0, pyb.CAN.DUAL if omv.board_type() == "H7" else pyb.CAN.LIST32, 0, [message_id, message_id]) rpc_slave.__init__(self)
from pyb import CAN # NOTE: Set to False on receiving node. TRANSMITTER = True can = CAN(2, CAN.NORMAL, baudrate=125_000, sample_point=75) # NOTE: uncomment to set bit timing manually, for example: #can.init(CAN.NORMAL, prescaler=32, sjw=1, bs1=8, bs2=3) can.restart() if (TRANSMITTER): while (True): # Send message with id 1 can.send('Hello', 1) time.sleep_ms(1000) else: # Runs on the receiving node. if (omv.board_type() == 'H7'): # FDCAN # Set a filter to receive messages with id=1 -> 4 # Filter index, mode (RANGE, DUAL or MASK), FIFO (0 or 1), params can.setfilter(0, CAN.RANGE, 0, (1, 4)) else: # Set a filter to receive messages with id=1, 2, 3 and 4 # Filter index, mode (LIST16, etc..), FIFO (0 or 1), params can.setfilter(0, CAN.LIST16, 0, (1, 2, 3, 4)) while (True): # Receive messages on FIFO 0 print(can.recv(0, timeout=10000))
return "TAG25H7" if(tag.family() == image.TAG25H9): return "TAG25H9" if(tag.family() == image.TAG36H10): return "TAG36H10" if(tag.family() == image.TAG36H11): return "TAG36H11" if(tag.family() == image.ARTOOLKIT): return "ARTOOLKIT" sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) sensor.set_framesize(sensor.VGA) # we run out of memory if the resolution is much bigger... # AprilTags works on a maximum of < 64K pixels. if omv.board_type() == "H7": sensor.set_windowing((240, 240)) elif omv.board_type() == "M7": sensor.set_windowing((200, 200)) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) # must turn this off to prevent image washout... sensor.set_auto_whitebal(False) # must turn this off to prevent image washout... # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the # camera resolution. "merge=True" merges all overlapping blobs in the image. #, thresholds[2], thresholds[3] iteration = 0 blobThreshold = thresholds[3] recognitionMode = "AprilTags"#Recognize color by default lastAprilTag = None
import sensor, image, time, math, omv #adding UART capability import pyb, ustruct uart = pyb.UART(3, 113200, timeout_char=50) #clocks are off => slow down a smidgen #say hello uart.write("hej, verden!!\n") # Set the thresholds to find a white object (i.e. tag border) thresholds = (160, 255) sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) if omv.board_type() == "H7": sensor.set_framesize(sensor.VGA) elif omv.board_type() == "M7": sensor.set_framesize(sensor.VGA) else: raise Exception("You need a more powerful OpenMV Cam to run this script") sensor.skip_frames( time=100) # increase this to let the auto methods run for longer sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking clock = time.clock() # The apriltag code supports up to 6 tag families which can be processed at the same time. # Returned tag objects will have their tag family and id within the tag family. tag_families = 0 tag_families |= image.TAG16H5 # comment out to disable this family #tag_families |= image.TAG25H7 # comment out to disable this family #tag_families |= image.TAG25H9 # comment out to disable this family #tag_families |= image.TAG36H10 # comment out to disable this family
def __init__(self, message_id=0x7FF, bit_rate=250000, sampling_point=75): self.__message_id = message_id can_prescaler, can_bs1, can_bs2 = __get_can_settings(bit_rate, sampling_point) self.__can = pyb.CAN(2, pyb.CAN.NORMAL, prescaler=can_prescaler, bs1=can_bs1, bs2=can_bs2, auto_restart=True) self.__can.setfilter(0, pyb.CAN.DUAL if omv.board_type() == "H7" else pyb.CAN.LIST32, 0, [message_id, message_id]) rpc_slave.__init__(self)
# AprilTags Max Res Example # # This example shows the power of the OpenMV Cam to detect April Tags # on the OpenMV Cam M7. The M4 versions cannot detect April Tags. import sensor, image, time, math, omv sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) sensor.set_framesize(sensor.VGA) # we run out of memory if the resolution is much bigger... # AprilTags works on a maximum of < 64K pixels. if omv.board_type() == "H7": sensor.set_windowing((240, 240)) elif omv.board_type() == "M7": sensor.set_windowing((200, 200)) else: raise Exception("You need a more powerful OpenMV Cam to run this script") sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) # must turn this off to prevent image washout... sensor.set_auto_whitebal(False) # must turn this off to prevent image washout... clock = time.clock() # Note! Unlike find_qrcodes the find_apriltags method does not need lens correction on the image to work. # The apriltag code supports up to 6 tag families which can be processed at the same time. # Returned tag objects will have their tag family and id within the tag family. tag_families = 0 tag_families |= image.TAG16H5 # comment out to disable this family tag_families |= image.TAG25H7 # comment out to disable this family tag_families |= image.TAG25H9 # comment out to disable this family tag_families |= image.TAG36H10 # comment out to disable this family tag_families |= image.TAG36H11 # comment out to disable this family (default family) tag_families |= image.ARTOOLKIT # comment out to disable this family
# finding Apriltags in the image using blob tracking to find the # area of where the tag is first and then calling find_apriltags # on that blob. # Note, this script works well assuming most parts of the image do not # pass the thresholding test... otherwise, you don't get a distance # benefit. import sensor, image, time, math, omv # Set the thresholds to find a white object (i.e. tag border) thresholds = (150, 255) sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) if omv.board_type() == "H7": sensor.set_framesize(sensor.VGA) elif omv.board_type() == "M7": sensor.set_framesize(sensor.QVGA) else: raise Exception("You need a more powerful OpenMV Cam to run this script") sensor.skip_frames(time = 200) # increase this to let the auto methods run for longer sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking clock = time.clock() # The apriltag code supports up to 6 tag families which can be processed at the same time. # Returned tag objects will have their tag family and id within the tag family. tag_families = 0 tag_families |= image.TAG16H5 # comment out to disable this family tag_families |= image.TAG25H7 # comment out to disable this family tag_families |= image.TAG25H9 # comment out to disable this family tag_families |= image.TAG36H10 # comment out to disable this family tag_families |= image.TAG36H11 # comment out to disable this family (default family)