Open mv camera freazing no matter what i do

I am trying to create a pan tilt camera with the pan tilt board on the openmv camera I’ve gotten the color detection script working for the most part but as soon as I try anything relating to servos the camera freezes and stops detecting anything


Here is my current code

That code looks fine. Also, post the code and not a jpeg of it if possible.

What camera board are you using? The H7 Plus or RT1062?

Also, manually processing pixels in python is very slow. Please use the functions in the image module. There’s a method called find_blobs() which does what you are trying to do but 100x faster.

I edited the code based of an already working script from the examples page here’s what I have going on now

import sensor, time, math

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
thresholds = [
    (30, 100, 15, 127, 15, 127),  # generic_red_thresholds
]

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
clock = time.clock()

while True:
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):
        if blob.elongation() > 0.5:
            img.draw_edges(blob.min_corners(), color=(255, 0, 0))
            img.draw_line(blob.major_axis_line(), color=(0, 255, 0))
            img.draw_line(blob.minor_axis_line(), color=(0, 0, 255))
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        img.draw_keypoints(
            [(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20
        )
    print(clock.fps())

The blob detection is working great now id just like to hook up some servos to be able to point at the blob itself do you know any good place to start?

And im using the standard h7 with the pan and tilt board attatched

Use the machine module: class PWM – pulse width modulation — MicroPython 1.23 documentation (openmv.io)

You just specify the servo PWM pulse lengths in 1000us to 2000us values with 1500us being the servo center.

-90 → 1000us, 90+ ->2000us etc.

The pyb module is deprecated by MicroPython. Avoid using it.

Alright thank you im trying something different now and your suggestion

I’m still having some issues with integrating servos the frame buffer seems to freeze seconds after running the program. Do you have an example of some code for pan/tilt that I could look at?

Can you share your code, with the servo control?

Also, there’s probably code on the forums or online for this. People have been doing this for years.

Heres my code as of right now "import sensor, time, math, pyb

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
thresholds = [
    (30, 100, 15, 127, 15, 127),  # Red (generic_red_thresholds)
    (20, 100, -10, 10, 10, 127),  # Orange
    (0, 30, -10, 10, 10, 127)     # Yellow
]

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
clock = time.clock()

# Initialize the servos using pyb.Servo
pan_servo = pyb.Servo(1)  # Pan (left/right)
tilt_servo = pyb.Servo(4) # Tilt (up/down)

# Servo movement parameters
pan_angle = 0
tilt_angle = 0
step = 2  # Step size for smoother movement

while True:
    clock.tick()
    img = sensor.snapshot()
    blobs = img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200)

    if blobs:
        largest_blob = max(blobs, key=lambda b: b.pixels())

        # Calculate the error from the center of the image
        error_x = largest_blob.cx() - img.width() // 2
        error_y = largest_blob.cy() - img.height() // 2

        # Adjust the pan and tilt angles based on the blob's position
        if abs(error_x) > 10:  # Threshold to prevent jitter
            pan_angle -= step if error_x > 0 else -step

        if abs(error_y) > 10:  # Threshold to prevent jitter
            tilt_angle += step if error_y > 0 else -step

        # Limit the angles to avoid over-rotation
        pan_angle = max(min(pan_angle, 90), -90)
        tilt_angle = max(min(tilt_angle, 90), -90)

        # Move the servos
        pan_servo.angle(pan_angle)
        tilt_servo.angle(tilt_angle)

        # Optionally, draw the blob on the image for debugging
        img.draw_rectangle(largest_blob.rect())
        img.draw_cross(largest_blob.cx(), largest_blob.cy())

    print("FPS:", clock.fps())

Hi, I ran your code on the H7, and it doesn’t appear to crash:

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
thresholds = [
    (30, 100, 15, 127, 15, 127),  # Red (generic_red_thresholds)
    (20, 100, -10, 10, 10, 127),  # Orange
    (0, 30, -10, 10, 10, 127)     # Yellow
]

import sensor, pyb, image, time

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
clock = time.clock()

# Initialize the servos using pyb.Servo
pan_servo = pyb.Servo(1)  # Pan (left/right)
tilt_servo = pyb.Servo(3) # Tilt (up/down)

# Servo movement parameters
pan_angle = 0
tilt_angle = 0
step = 2  # Step size for smoother movement

while True:
    clock.tick()
    img = sensor.snapshot()
    blobs = img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200)

    if blobs:
        largest_blob = max(blobs, key=lambda b: b.pixels())

        # Calculate the error from the center of the image
        error_x = largest_blob.cx() - img.width() // 2
        error_y = largest_blob.cy() - img.height() // 2

        # Adjust the pan and tilt angles based on the blob's position
        if abs(error_x) > 10:  # Threshold to prevent jitter
            pan_angle -= step if error_x > 0 else -step

        if abs(error_y) > 10:  # Threshold to prevent jitter
            tilt_angle += step if error_y > 0 else -step

        # Limit the angles to avoid over-rotation
        pan_angle = max(min(pan_angle, 90), -90)
        tilt_angle = max(min(tilt_angle, 90), -90)

        # Move the servos
        pan_servo.angle(pan_angle)
        tilt_servo.angle(tilt_angle)

        # Optionally, draw the blob on the image for debugging
        img.draw_rectangle(largest_blob.rect())
        img.draw_cross(largest_blob.cx(), largest_blob.cy())

    print("FPS:", clock.fps())

Note that Servo 4 is not defined. Don’t use it. However, having that in your script shouldn’t crash anything.

It may be a system brown out. Can your powersupply handle the OpenMV Cam power draw and the servos at the same time?

All right, thank you for letting me know. For right now I’m only trying to run one servo since I don’t have an adequate power supply to use both yet