Openmv sending command to arduino

Discussion related to "under the hood" OpenMV topics.
elliotmil659
Posts: 4
Joined: Tue Jun 19, 2018 3:06 pm

Openmv sending command to arduino

Postby elliotmil659 » Tue Jun 19, 2018 3:19 pm

Hi, I am currently building a small robot car that detects the arrows on the wall using openmv. Im trying to make openmv to send a data to arduino uno so that when the camera detects the right or left arrow, it will turn according to the direction of the arrow. I did make the open mv cam to detect the arrows but I am still having problem sending the command to turn the car to the left or right according to the direction of the arrow using uart. I am new to sending commands and openmv and it is very confusing. It feels like the camera is not sending any data to the arduino at all because the car only rotates left and right. How should I write it so that the car turns left or right when it detects the arrow on the wall? (Dector is the openmv code i have come up with and robot car is the arduino ide code).
Attachments
robot car.txt
(4.05 KiB) Downloaded 12 times
detector.txt
(1.51 KiB) Downloaded 11 times
User avatar
kwagyeman
Posts: 2202
Joined: Sun May 24, 2015 2:10 pm

Re: Openmv sending command to arduino

Postby kwagyeman » Tue Jun 19, 2018 4:35 pm

Hi, it looks like your UART code is wrong...

Code: Select all

import time, sensor, image
from pyb import UART
from image import SEARCH_EX, SEARCH_DS
ser = UART(3,115200,timeout_char=1000)
# Reset sensor
sensor.reset()

# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# Max resolution for template matching with SEARCH_EX is QQVGA
sensor.set_framesize(sensor.QQCIF)
# You can set windowing to reduce the search image.
#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)

# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
template = image.Image("/Right.pgm")
template = image.Image("/Left.pgm")
clock = time.clock()

# Run template matching
while (True):
    clock.tick()
    img = sensor.snapshot()

    # find_template(template, threshold, [roi, step, search])
    # ROI: The region of interest tuple (x, y, w, h).
    # Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
    # Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
    #
    # Note1: ROI has to be smaller than the image and bigger than the template.
    # Note2: In diamond search, step and ROI are both ignored.
    r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    l = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
    if r:
        img.draw_rectangle(r,5)
        ser.write(0x01)
    if l:
        img.draw_rectangle(l,5)
        ser.write(0x02)
    print(clock.fps())
The above code should output binary 0x01 or 0x02 on the serial port when it sees an arrow. I noticed in your Arduino code you're looking for a multibyte number. Change that to just look for the value 0x01 or 0x02.
Nyamekye,
User avatar
kwagyeman
Posts: 2202
Joined: Sun May 24, 2015 2:10 pm

Re: Openmv sending command to arduino

Postby kwagyeman » Tue Jun 19, 2018 4:35 pm

Note, the previous code shouldn't have run. It looked like there were compile issues in it.
Nyamekye,
elliotmil659
Posts: 4
Joined: Tue Jun 19, 2018 3:06 pm

Re: Openmv sending command to arduino

Postby elliotmil659 » Wed Jun 20, 2018 1:34 pm

kwagyeman wrote:
Tue Jun 19, 2018 4:35 pm
Hi, it looks like your UART code is wrong...

Code: Select all

import time, sensor, image
from pyb import UART
from image import SEARCH_EX, SEARCH_DS
ser = UART(3,115200,timeout_char=1000)
# Reset sensor
sensor.reset()

# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# Max resolution for template matching with SEARCH_EX is QQVGA
sensor.set_framesize(sensor.QQCIF)
# You can set windowing to reduce the search image.
#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)

# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
template = image.Image("/Right.pgm")
template = image.Image("/Left.pgm")
clock = time.clock()

# Run template matching
while (True):
    clock.tick()
    img = sensor.snapshot()

    # find_template(template, threshold, [roi, step, search])
    # ROI: The region of interest tuple (x, y, w, h).
    # Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
    # Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
    #
    # Note1: ROI has to be smaller than the image and bigger than the template.
    # Note2: In diamond search, step and ROI are both ignored.
    r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    l = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
    if r:
        img.draw_rectangle(r,5)
        ser.write(0x01)
    if l:
        img.draw_rectangle(l,5)
        ser.write(0x02)
    print(clock.fps())
The above code should output binary 0x01 or 0x02 on the serial port when it sees an arrow. I noticed in your Arduino code you're looking for a multibyte number. Change that to just look for the value 0x01 or 0x02.
I did change such way and now it says the it is out of memory. When I used pgm that is smaller in size than the one i previously used the point of interest is smaller than template. Im thinking compress() is the way to solve this but I don't know how to use it. Can you help me out with this one please?
User avatar
kwagyeman
Posts: 2202
Joined: Sun May 24, 2015 2:10 pm

Re: Openmv sending command to arduino

Postby kwagyeman » Wed Jun 20, 2018 1:58 pm

Hi, I need the PGM files you're using. Also, why are you using templates exactly? Have you seen our AprilTag feature? It is much better for this type of activity. The tags aren't exactly human readable but they are much easier for the robot to find. https://www.youtube.com/watch?v=keb0B11zj5g

Anyway, if you'd like me to debug the script I need the pgm files. I'd watch the AprilTag video and just use the tags however.
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 5 guests