Sending changing values over Serial to Arduino

Hi there,
currently I’m working on a robotics project where I want to detect colored Blocks and send the angle of the block to my Teensy. The Angle is also always changing. But on Arduino are no values which I can read out he just tells me there’s nothing. I also already checked the cabeling. But I’m quite new in phyton so it could also be a programming issue.
Thanks for help, Theo.

Here’s my code in OpenMV IDE:

# This work is licensed under the MIT license.
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Multi Color Blob Tracking Example
#
# This example shows off multi color blob tracking using the OpenMV Cam.

import sensor
import time
import math

import time
from pyb import UART



# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green things. You may wish to tune them...
thresholds = [
(50, 60, 10, 127, 21, 85),  # generic_red_thresholds
]  # generic_blue_thresholds
# You may pass up to 16 thresholds above. However, it's not really possible to segment any
# scene with 16 thresholds before color thresholds start to overlap heavily.

uart = UART(3, 19200)

sensor.reset()
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.set_transpose(False)
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()

# 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. Don't set "merge=True" because that will merge blobs which we don't want here.
data = 6
while True:
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):
        # These values depend on the blob not being circular - otherwise they will be shaky.
        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))
        # These values are stable all the time.
        img.draw_rectangle(blob.rect())
        #img.draw_cross(blob.cx(), blob.cy())
        # Note - the blob rotation is unique to 0-180 only.
        img.draw_keypoints(
            [(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20
        )

        XStandort = 240;
        YStandort = 0;

        XTor = blob.cx()
        YTor = blob.cy()

        XEnde = XTor - XStandort
        YEnde = YTor - YStandort

        TorRichtung = (((math.atan2(XEnde, YEnde)) * 180) / math.pi) + 50


    # UART 3, and baudrate.
    uart.write(str(TorRichtung))
    time.sleep(1)

And here’s my Arduino code running on my Teensy 4.1:

char Tor;

void setup() {
  Serial.println("Starting ");
 Serial.begin(115200);
 Serial5.begin(19200);

}

void loop() {
  if(Serial5.available()){
    Tor = Serial5.read();
  }
  else{
    Tor = 1;
  }
  Serial.println(Tor);
}

The code looks right to me.

uart = UART(3, 19200)

That line should have created and initialized the uart. Documentation specifies that by adding the second baudrate parameter uart should also be initialized. I would split that into two statements though, creating the object and then running uart.init() just for readability and making sure that this isn’t your issue.

uart.write(str(TorRichtung))

This looks right to me.

Did you remember to connect the camera RX to board TX and board TX to camera RX?

Also, UART(3) creates the object on P4 and P5 for the H7 if that’s what you’re using, so make sure you didn’t mistakenly connect to P1 and P0.

1 Like

Hi, thanks for help. But my problem isn’t solved yet. Has anybody another idea.
And sorry for answering so late I was on holidays.

Do you know if your camera is actually detecting the colored blocks? It looks like this line only gets executed if blob is non-empty, meaning that img.find_blobs() successfully detected a blob or blobs:

I’d suggest adding a line right above the while loop to initialize TorRichtung to some value. That way you will be able to tell if your serial communications are working or not, based on whether your Teensy successfully receives the initialized value or not.

I tried It and it still doesn’t work. The Teensy tells my it got contact with the OpenMV but doesn’t get any values. Any Ideas?

If you post the updated code that you’re using to test with, I would be happy to take a look at it and double-check it. Otherwise it’s a little hard for me to provide specific suggestions and help you narrow down why your Teensy isn’t receiving any values.

Also, what OpenMV board are you using? (e.g. “OpenMV Cam H7 Plus”)

Here is my uptated code. I’m using a OpenMV Cam H7.

# This work is licensed under the MIT license.
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Multi Color Blob Tracking Example
#
# This example shows off multi color blob tracking using the OpenMV Cam.

import sensor
import time
import math



# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green things. You may wish to tune them...
thresholds = [
(50, 60, 10, 127, 21, 85),  # generic_red_thresholds
]  # generic_blue_thresholds
# You may pass up to 16 thresholds above. However, it's not really possible to segment any
# scene with 16 thresholds before color thresholds start to overlap heavily.


sensor.reset()
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.set_transpose(False)
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()

# 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. Don't set "merge=True" because that will merge blobs which we don't want here.
TorRichtung = 15
while True:
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):
         These values depend on the blob not being circular - otherwise they will be shaky.
        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))
         These values are stable all the time.
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
         Note - the blob rotation is unique to 0-180 only.
        img.draw_keypoints(
            [(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20
        )

    XStandort = 240;
    YStandort = 0;

    XTor = blob.cx()
    YTor = blob.cy()
    XEnde = XTor - XStandort
    YEnde = YTor - YStandort

    TorRichtung = (((math.atan2(XEnde, YEnde)) * 180) / math.pi) + 50+'


    # UART 3, and baudrate.
    uart.write(str(TorRichtung))
    time.sleep(1)

Thank you. I’ll see what I can do to help you troubleshoot this.

First, I tried out the script that you posted on July 9th. I do not have my OpenMV Cam connected to a separate microcontroller via UART, but I don’t think that matters.

To try out your code, I am using an OpenMV Cam H7 Plus, Firmware Version 4.3.2, with OpenMV IDE Version 4.2.0; additionally, I left the lens cap on, to ensure that the find_blobs() method does not return any blobs.

When I ran it, it produced an error message that said “NameError: name ‘TorRichtung’ isn’t defined”, which is what I expected to happen because TorRichtung was not initialized because the code inside the for loop didn’t run because find_blobs() didn’t return any blobs.

I could not run the code that you posted today, as it does not appear to have copy-pasted correctly; several of the comment lines are missing the # and at least one line has incorrect indentation. So instead, please try running the following code on your OpenMV Cam while it is connected to your Teensy 4.1, then report back with answers to the following questions:

  1. Did your Teensy 4.1 successfully send the message "Starting " to its serial connection?
  2. Did your Teensy 4.1 successfully send any other messages to its serial connection, and if so what were they?
  3. Did your Teensy 4.1 successfully receive any values from your OpenMV Cam, and if so what were they?
  4. Did the LED on your OpenMV Cam blink on and off?
# Test code for theo

import time
from pyb import UART
from pyb import LED

red_led = LED(1)
green_led = LED(2)
blue_led = LED(3)

uart = UART(3, 19200)

TorRichtung = 3000

while True:
    # UART 3, and baudrate.
    uart.write(str(TorRichtung))
#    print(str(TorRichtung))
    red_led.on()
    green_led.on()
    blue_led.on()
    time.sleep(1)
    red_led.off()
    green_led.off()
    blue_led.off()
    time.sleep(1)

Do you know if I have to convert the string value into an int value on my teensy