UART data communication to arduino board question?

General discussion about topics related to OpenMV.
star
Posts: 4
Joined: Fri Feb 16, 2018 6:11 am

UART data communication to arduino board question?

Postby star » Fri Feb 16, 2018 7:50 am

Hello there Openmv ,
I'm having problems trying to read color blob data coming into my Arduino board,
I'm trying to read and serial print same blob data from openmv
I don't know whats wrong, I'm new to openmv cam and I hope
bellow is the picture.I see 49,57 instead of 16 ...please I appreciate your help, as I have spent days trying to solve the problem ...thanks
Attachments
2239.png
2239.png (143.23 KiB) Viewed 15844 times
User avatar
iabdalkader
Posts: 460
Joined: Sun May 24, 2015 3:53 pm

Re: UART data communication to arduino board question?

Postby iabdalkader » Fri Feb 16, 2018 9:55 am

Hi, can you test with a serial terminal and see if you're getting the expected data ?
User avatar
kwagyeman
Posts: 1920
Joined: Sun May 24, 2015 2:10 pm

Re: UART data communication to arduino board question?

Postby kwagyeman » Fri Feb 16, 2018 11:51 am

Please post your OpenMV Cam code and Arduino code.
Nyamekye,
star
Posts: 4
Joined: Fri Feb 16, 2018 6:11 am

Re: UART data communication to arduino board question?

Postby star » Fri Feb 16, 2018 12:24 pm

OK ,Here's the openmv code

Code: Select all

# Single Color RGB565 Blob Tracking Example
import sensor, image, time
import time
from pyb import UART
uart = UART(3,9600, timeout_char = 1000)

threshold_index = 0 # 0 for red, 1 for green, 2 for blue
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
              (30, 100, -64, -8, -32, 32), # generic_green_thresholds
              (0, 30, 0, 64, -128, 0)] # generic_blue_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[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
    uart.write("%d"%blob.cx())   

    print(blob.cx())





and here's the arduino code

Code: Select all

int indata;
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600); 
}
void loop() {
indata = mySerial.read();


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

Re: UART data communication to arduino board question?

Postby kwagyeman » Sat Feb 17, 2018 1:35 am

Try this code:

Code: Select all

# Single Color RGB565 Blob Tracking Example
import sensor, image, time
import time
from pyb import UART
uart = UART(3,9600, timeout_char = 1000)

threshold_index = 0 # 0 for red, 1 for green, 2 for blue
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
              (30, 100, -64, -8, -32, 32), # generic_green_thresholds
              (0, 30, 0, 64, -128, 0)] # generic_blue_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[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        uart.write("%d\n"%blob.cx())   
        print("%d\n"%blob.cx(), end='')
You had the wrong indentation in python. Also, you can't use "blob" outside of the loop. Whatever value you were using was a stale value.
Nyamekye,
User avatar
kwagyeman
Posts: 1920
Joined: Sun May 24, 2015 2:10 pm

Re: UART data communication to arduino board question?

Postby kwagyeman » Sat Feb 17, 2018 1:42 am

On the Arduino side:

https://www.google.com/search?q=arduino ... e&ie=UTF-8

You need a method to read a line of text. I.e. keep reading text until you see '/n'.
Nyamekye,
star
Posts: 4
Joined: Fri Feb 16, 2018 6:11 am

Re: UART data communication to arduino board question?

Postby star » Sat Feb 17, 2018 11:47 am

Thanks so much for your reply Sir,
now I'm 80% done, I have used the openmv code u sent and I have adjusted the Arduino code, the results are like this
I can receive the data from openmv but I need to reset before I can get a new data , that means I have to reset every time to get real-time data from openmv
here is the result ...Any help to resolve this i will really appreciate .thanks in advance
Image

Image
the arduino code

Code: Select all


String str;
int ds = 4;
#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);
   }

Attachments
serial20180218001310.png
serial20180218001310.png (186.08 KiB) Viewed 11197 times
serial44420180218001452.png
serial44420180218001452.png (193.89 KiB) Viewed 11197 times
User avatar
kwagyeman
Posts: 1920
Joined: Sun May 24, 2015 2:10 pm

Re: UART data communication to arduino board question?

Postby kwagyeman » Sat Feb 17, 2018 2:19 pm

Try the attached. You might see some compile errors.
Attachments
sketch_feb17a.zip
(449 Bytes) Downloaded 46 times
Nyamekye,
star
Posts: 4
Joined: Fri Feb 16, 2018 6:11 am

Re: UART data communication to arduino board question?

Postby star » Sun Feb 25, 2018 7:46 am

Thanks Sir , Everything worked well finally made a simple robot here https://www.youtube.com/watch?v=1enFgfF ... e=youtu.be
User avatar
kwagyeman
Posts: 1920
Joined: Sun May 24, 2015 2:10 pm

Re: UART data communication to arduino board question?

Postby kwagyeman » Sun Feb 25, 2018 11:48 am

Good job! Yo may wish to turn your PID gains down a bit.
Nyamekye,

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 1 guest