UART communication with Teensy (Arduino)

General discussion about topics related to OpenMV.
hcrobo
Posts: 1
Joined: Mon Apr 01, 2019 4:00 am

UART communication with Teensy (Arduino)

Postby hcrobo » Mon Apr 01, 2019 4:58 am

Hi all,
im trying to send over blob.cx() using colour tracking but the communication doesn't seem to work and the Teensy side doesn't read any receiving bytes on Serial.available
Here is my OpenMV M7 code:

Code: Select all

import sensor, image, time, math,struct
from pyb import UART

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max), declare variables
bluethresh = [(8, 35, -19, 25, -50, 20)] #to set pre-match
yellowthresh = [(54, 95, -10, 20, 20, 57)]
goalthresh = 0
goalX = 0
blueX = 0
yellowX = 0
goalcolour = 0




uart = UART(3, 115200, timeout_char=100) #(only UART 1 or 3 available,baud rate,delay b/w frames)
uart.init(115200, bits=8, parity=None, stop=1, timeout_char=10)

clock = time.clock()
print("Waiting for Arduino...")
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) #for 160x120 resolution
sensor.skip_frames(time = 2000)
sensor.set_auto_exposure(3, 8750)
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()
img=sensor.snapshot()

for blob in img.find_blobs(yellowthresh, pixels_threshold=700, area_threshold=200):
    img=sensor.snapshot()
    img.draw_rectangle(blob.rect())
    img.draw_cross(blob.cx(), blob.cy())
    yellowX = blob.cx()

for blob in img.find_blobs(bluethresh, pixels_threshold=700, area_threshold=200):
    img=sensor.snapshot()
    img.draw_rectangle(blob.rect())
    img.draw_cross(blob.cx(), blob.cy())
    blueX = blob.cx()

if yellowX > blueX: #test for which goal it is facing
    goalthresh = yellowthresh
    goalcolour = 1
else:
    goalthresh = bluethresh
    goalcolour = 2

while(True):
    clock.tick()
    img=sensor.snapshot()
    for blob in img.find_blobs(goalthresh, pixels_threshold=600, area_threshold=200, merge=True): #change area threshold in case localisation marks fail
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        goalX = blob.cx()
    try:
        uart.write(struct.pack("<b",goalX))
        print(goalX)
        print("sent")
    

    except OSError as error:
        pyb.LED(1).on()
        pyb.LED(2).off()
        pyb.LED(3).off()
        print(error)
        time.sleep(3000)
        pass

and this is my Arduino code:

Code: Select all

  void setup() {
  Serial.begin(9600);
  Serial2.begin(115200);
}


void loop() {
  int goalX=0;
  while(Serial2.available()<1);
    goalX=Serial2.read();
    Serial.print(goalX);
    Serial.print(" ");
    Serial.println();
}
Any help would be appreciated, thank you!
User avatar
kwagyeman
Posts: 3209
Joined: Sun May 24, 2015 2:10 pm

Re: UART communication with Teensy (Arduino)

Postby kwagyeman » Mon Apr 01, 2019 11:04 am

Hi, I can debug this tonight. It looks pretty right however.

It's probably something really simple.
Nyamekye,
User avatar
iabdalkader
Posts: 920
Joined: Sun May 24, 2015 3:53 pm

Re: UART communication with Teensy (Arduino)

Postby iabdalkader » Tue Apr 02, 2019 1:03 pm

How do you connect the two boards ? You should connect TX->RX, RX->TX and ground.

Return to “General Topics”

Who is online

Users browsing this forum: Bing [Bot] and 4 guests