Skip to content

Commit

Permalink
Upload MVision res
Browse files Browse the repository at this point in the history
  • Loading branch information
a10036gt committed Nov 21, 2024
1 parent bd01990 commit 67b93da
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Sensors/MVision_Resources/Python Lib/fill_light.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pyb import Pin, Timer

tim = Timer(4, freq=80000)
light_pin = tim.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width_percent=10)

light_brightness = 30

def on():
global light_brightness
light_pin.pulse_width(light_brightness)

def off():
light_pin.pulse_width(0)

def brightness(n):
global light_brightness
if n < 0:
n = 0
elif n > 100:
n = 100
light_brightness = round(n *0.4)
light_pin.pulse_width(light_brightness)

18 changes: 18 additions & 0 deletions Sensors/MVision_Resources/Python Lib/fill_light_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import sensor, image, time
import fill_light


fill_light.on()
time.sleep(1)
fill_light.off()
time.sleep(1)

while(True):
for n in range(100):
fill_light.brightness(n)
time.sleep(0.01)
for n in range(100,0,-1):
fill_light.brightness(n)
time.sleep(0.01)


45 changes: 45 additions & 0 deletions Sensors/MVision_Resources/Python Lib/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sensor, image, time # Import image sensor and time modules
from matrix_mini import send_data # Import the send_data function from the matrix_mini module

# Initialize the sensor
sensor.reset() # Reset the sensor
sensor.set_pixformat(sensor.RGB565) # Set the pixel format to RGB565
sensor.set_framesize(sensor.QVGA) # Set the image resolution to QVGA (320x240)
sensor.skip_frames(time=2000) # Wait for 2 seconds to stabilize the sensor

# Set image flipping
sensor.set_vflip(True) # Vertically flip the image
sensor.set_hmirror(True) # Horizontally mirror the image

# Define color threshold range (HSV), suitable for detecting the target color
threshold = (0, 0, 0, 0, 0, 0) # Hue, Saturation, and Value range

clock = time.clock() # Create a clock to calculate frames per second (FPS) of image processing

while True:
clock.tick() # Start timing
img = sensor.snapshot() # Capture an image

print(clock.fps()) # Output the current FPS

# Find blobs (color regions) in the image that match the threshold
blobs = img.find_blobs([threshold], pixels_threshold=200, area_threshold=200)
if blobs: # If blobs are found
# Find the largest blob by area
max_blob = max(blobs, key=lambda b: b[2] * b[3])

img.draw_rectangle(max_blob.rect()) # Draw a rectangle around the largest blob

# Calculate the center coordinates of the blob
x_center = max_blob.cx()
y_center = max_blob.cy()

# Calculate the blob's area and round it to an integer
blob_area = round(max_blob.area() / 2)

# Send the center coordinates and area to an external system
send_data([x_center, y_center, blob_area])

# Mark the center point and coordinates text on the image
img.draw_cross(x_center, y_center) # Draw a cross at the center
img.draw_string(0, 0, str(x_center) + ", " + str(y_center)) # Display the coordinates
42 changes: 42 additions & 0 deletions Sensors/MVision_Resources/Python Lib/matrix_mini.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from pyb import UART
from pyb import LED
from pyb import Pin

uart = UART(3, 115200)
pin0 = Pin('P4', Pin.IN, Pin.PULL_NONE)
is_enabled = False
green_led = LED(2)
green_led.on()

def enable_if_start_received():
global is_enabled
if uart.any() >= 5:
start_signal = uart.read(5)
if start_signal == b'start':
is_enabled = True
green_led.off()
uart1 = UART(3, 115200)

def send_data(send_buffer):
global is_enabled

# 首次检查是否收到 'start' 指令
if not is_enabled:
enable_if_start_received()
return

length = len(send_buffer)
if length > 20:
raise Exception("Too much data, number of data should not exceed 20.")
uart.writechar(0xaa)
uart.writechar(length)
check = 0xaa ^ length
for n in send_buffer:
if n < 0 or n > 65535:
raise Exception("Value range error, value should be between 0 and 65535.")
n = round(n)
uart.writechar(n & 0xff)
uart.writechar((n >> 8) & 0xff)
check = check ^ (n & 0xff)
check = check ^ ((n >> 8) & 0xff)
uart.writechar(check)

0 comments on commit 67b93da

Please sign in to comment.