Serial comunication with Arduino

Discussion related to "under the hood" OpenMV topics.
GiusB
Posts: 5
Joined: Wed Dec 18, 2019 3:15 am

Serial comunication with Arduino

Postby GiusB » Fri Jan 24, 2020 5:32 am

Hello!

I need to create a comunication between OpenMV serial and Arduino serial: I want to read data from the first one and print them on the second one.

I did this program on OpenMV (cables connected at P4/P5 of camera):

from pyb import UART
uart = UART(3,9600, timeout_char = 1000)

thresholds_index = 0 #for yellow
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general yellow things.
thresholds = [(50, 100,-20,20, 30, 100)] # generic_yellow_thresholds

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.HQVGA) #240x160
sensor.skip_frames(30)
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()

# Do your regular OpenMV image processing / etc
# 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" must be set to merge overlapping color blobs for color codes.

clock.avg()
img = sensor.snapshot().lens_corr(strength=1.8, zoom=1.0)

for blob in img.find_blobs([thresholds[thresholds_index]], pixels_threshold=50, area_threshold=50, merge=True):

# These values are stable all the time.
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
x= blob.cx()/13.1*2.54
y= blob.cy()/13.1*2.54

uart.write("%d\n"%x)
uart.write("%d\n"%y)

print("x: %d\n"%x, end='')
print("y: %d\n"%y)
#print(clock.avg())

Arduino( MEGA) Code:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}

void loop(){
if (mySerial.available()){
delay(100); // Wait for all data.
while (mySerial.available()) {
char d = mySerial.read();
str.concat(d);
}
}
char t[str.length()+1];
str.toCharArray(t, (sizeof(t)));
int intdata = atoi(t);

Serial.print(intdata);
Serial.print("\n");
delay(100);
}

But I don't have any results.
Thanks for the help!
User avatar
kwagyeman
Posts: 4259
Joined: Sun May 24, 2015 2:10 pm

Re: Serial comunication with Arduino

Postby kwagyeman » Fri Jan 24, 2020 12:38 pm

Can you wrap your code in the code tags the forum has so it's more readable and fix the indentation?
Nyamekye,
GiusB
Posts: 5
Joined: Wed Dec 18, 2019 3:15 am

Re: Serial comunication with Arduino

Postby GiusB » Mon Jan 27, 2020 3:34 am

OpenMV code:

Code: Select all

# Yellow Color Code Tracking

import sensor, image, time, math
from pyb import UART
uart = UART(3,9600, timeout_char = 1000)

thresholds_index = 0 #for yellow
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general yellow things.
thresholds = [(50, 100,-20,20, 30, 100)] # generic_yellow_thresholds

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.HQVGA) #240x160
sensor.skip_frames(30)
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()

    # Do your regular OpenMV image processing / etc
    # 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" must be set to merge overlapping color blobs for color codes.

    clock.avg()
    img = sensor.snapshot().lens_corr(strength=1.8, zoom=1.0)

    for blob in img.find_blobs([thresholds[thresholds_index]], pixels_threshold=50, area_threshold=50, merge=True):

            # These values are stable all the time.
            img.draw_rectangle(blob.rect())
            img.draw_cross(blob.cx(), blob.cy())
            x= blob.cx()/13.1*2.54
            y= blob.cy()/13.1*2.54

            uart.write("%d\n"%x)
            uart.write("%d\n"%y)

            print("%d\n"%x,end="")
            print("%d\n"%y)
            #print(clock.avg())
Arduino code:

Code: Select all

String str
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
Serial.begin(9600);
mySerial.begin(9600); 
}

void loop(){
if (mySerial.available()){
delay(100); // Wait for all data.
while (mySerial.available()) {
char d = mySerial.read();
str.concat(d);
}
}
char t[str.length()+1];
str.toCharArray(t, (sizeof(t)));
int intdata = atoi(t);

Serial.print(intdata);
Serial.print("\n");
delay(100);
}
User avatar
kwagyeman
Posts: 4259
Joined: Sun May 24, 2015 2:10 pm

Re: Serial comunication with Arduino

Postby kwagyeman » Mon Jan 27, 2020 11:52 am

Okay, your program on the OpenMV Cam is fine. The one on the Arduino isn't written in a way that it will receive text easily.

Arduino supports a method called parse_int which can read an int on the serial port and will handle receiving data for you. Can you use that method to read the two unit values you send and then print what parse_int returns?
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 5 guests