Camera communication with Arduino Mega

General discussion about topics related to OpenMV.
finnleh
Posts: 36
Joined: Sun Apr 14, 2019 12:12 am

Camera communication with Arduino Mega

Postby finnleh » Tue May 28, 2019 2:58 pm

Hello!
I want to send a signal to my Arduino Mega as soon as I detect something. It worked fine with 5v, but for some reason, my camera doesn't work with 5v anymore. So, I switched to 3.3v. The camera worked and it even sent the signal to the board. The arduino board did what it was supposed to do and sent back a signal, when it was done, but the camera didn't receive anything...

Is that, because the communication only works with 5v? Or is it a problem with my code?


Here my OpenMV code:

Code: Select all

	w = min(max(int(blob.w() * 1.2), 10), 160) # Not too small, not too big.
        h = min(max(int(blob.h() * 1.2), 10), 160) # Not too small, not too big.
        x = min(max(int(blob.x() + (blob.w()/4) - (w * 0.1)), 0), img.width()-1)
        y = min(max(int(blob.y() + (blob.h()/4) - (h * 0.1)), 0), img.height()-1)
        out = net.forward(img.binary([(100, 255)]), softmax=True, roi=(x,y,w,h))
        max_idx = out.index(max(out))
        score = int(out[max_idx]*100)
        if (score < 50):
            score_str = "??:??%"
        else:
            score_str = "%s:%d%% "%(labels[max_idx], score)
        img.draw_string(0, 0, score_str, color=(0, 255, 0))
        print(labels[max_idx])
        if (score > 70):
            if (labels[max_idx] == "H"):
                print("HHHHH")
                uart.write("h\r")
                answerH = False
                while (answerH == False):
                    if uart.any():
                        nachrichtVonArduino = uart.readchar()
                        nachrichtenKontrolle = 209 #ID for letter h
                        if (id(nachrichtVonArduino) == nachrichtenKontrolle):
                            print("H ist korrekt!")
                            answerH = True;
            elif (labels[max_idx] == "S") or (labels[max_idx] == "s"): # or (labels[max_idx] == "5"):
                print("SSSSS")
                uart.write("s\r")
                answerS = False
                while (answerS == False):
                    if uart.any():
                        nachrichtVonArduino = uart.readchar()
                        nachrichtenKontrolle = 231 #ID for letter S
                        if (id(nachrichtVonArduino) == nachrichtenKontrolle):
                            print("S ist korrekt!")
                            answerS = True;
            elif (labels[max_idx] == "U") or (labels[max_idx] == "u"): # or (labels[max_idx] == "v"):
                print("UUUUUU")
                uart.write("u\r")
                answerU = False
                while (answerU == False):
                    if uart.any():
                        nachrichtVonArduino = uart.readchar()
                        nachrichtenKontrolle = 235 #ID for letter U
                        if (id(nachrichtVonArduino) == nachrichtenKontrolle):
                            print("U ist korrekt!")
                            answerU = True;

    print(clock.fps())             	# Note: OpenMV Cam runs about half as fast when connected
                                   		# to the IDE. The FPS should increase once disconnected.
Here my arduino code:

Code: Select all

// 104: ID for h
// 115: ID for s
// 117: ID for u

// white: TX1
// black: RX1

// yellow: GND
// red: 5v/3.3v

#include <Wire.h>

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Auf gehts!");
  pinMode(9, OUTPUT);         //LED 
}

void loop() {
  // read from port 1, send to port 0:
  // from Camera to Arduino
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
    if (inByte == 104)
    {
      for(int i = 0; i < 5; i++)
      {
        digitalWrite(9, HIGH);
        Serial.println("ON");
        delay(500);
        digitalWrite(9, LOW);
        Serial.println("OFF");
        delay(500);
      }
      // h detected, deploying 2 rescue kits
      Serial.println("");
      Serial.println("H detected! Rescue procedure started...");          
      Serial1.write("h");
    }
    else if (inByte == 115)
    {
      for(int i = 0; i < 5; i++)
      {
        digitalWrite(9, HIGH);
        Serial.println("ON");
        delay(500);
        digitalWrite(9, LOW);
        Serial.println("OFF");
        delay(500);
      }
      // s detected, deploying 1 rescue kits
      Serial.println("");
      Serial.println("S detected! Rescue procedure started...");
      Serial1.write("s");
    }
    else if (inByte == 117)
    {
      for(int i = 0; i < 5; i++)
      {
        digitalWrite(9, HIGH);
        Serial.println("ON");
        delay(500);
        digitalWrite(9, LOW);
        Serial.println("OFF");
        delay(500);
      }
      // u detected, deploying 0 rescue kits
      Serial.println("");
      Serial.println("U detected! Rescue procedure started...");
      Serial1.write("u");
    }
  }
}
The camera I use is OpenMV H7, the Arduino board I use is Mega 2560. If you know it is my code, or if you know it is the camera (because it only works with 5v?), then please tell me and help me.

Thank you very much,
Finn
User avatar
iabdalkader
Posts: 951
Joined: Sun May 24, 2015 3:53 pm

Re: Camera communication with Arduino Mega

Postby iabdalkader » Tue May 28, 2019 6:02 pm

It's hard to tell by just looking at all the code, I could test it tomorrow and get back to you. In the meantime, you say it used to work I'd double check the wiring, also if you search the forums you'll find many working Arduino examples try some of them and see if they work.
finnleh
Posts: 36
Joined: Sun Apr 14, 2019 12:12 am

Re: Camera communication with Arduino Mega

Postby finnleh » Tue May 28, 2019 10:34 pm

Yeah, I would appreciate it if you could test it. The connections were like this:

OpenMV to Arduino:
GND to GND
5v to 5v
P4 to RX1
P5 to TX1

The camera should not be connected to any other thing that supplies it with electricity, but the 5v from the arduino board. Also, the camera needs to work without a computer connected (but that shouldn't be a problem, because it is starting it automatically if the code is saved on the camera and it is turned on through the arduino board, right?)

if you could test my code, maybe with an easier version of just sending letters or numbers to each other, that would be awesome. Because if the wiring and the same algorithm works on your camera and board, I know that either the board or my camera is broken...

Thank you very much,
Finn
finnleh
Posts: 36
Joined: Sun Apr 14, 2019 12:12 am

Re: Camera communication with Arduino Mega

Postby finnleh » Wed May 29, 2019 9:13 am

Have you already had the chance to test it?


Finn
User avatar
iabdalkader
Posts: 951
Joined: Sun May 24, 2015 3:53 pm

Re: Camera communication with Arduino Mega

Postby iabdalkader » Wed May 29, 2019 12:06 pm

if you could test my code, maybe with an easier version of just sending letters or numbers to each other
I haven't had the time yet, it would be helpful if you could provide an simpler example that shows the same issue
finnleh
Posts: 36
Joined: Sun Apr 14, 2019 12:12 am

Re: Camera communication with Arduino Mega

Postby finnleh » Wed May 29, 2019 1:47 pm

Ok, here is an example. As my camera doesn't work with the communication to Arduino, I can't check if it is correct. But if it works, then I know, my camera is broken.

OpenMV code:

Code: Select all

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

clock = time.clock()

while(True):
    clock.tick()
    
    if (labels[max_idx] == "H"):
        print("Send signal to Arduino")
        uart.write("h\r")
        answerH = False
        while (answerH == False):
            if uart.any():
                print("Received some signal");
                messageReceived = uart.readchar()
                messageCheck = 209 #ID for letter h
                if (id(messageReceived) == messageCheck):
                    print("Received answer!")
                    answerH = True;

    img = sensor.snapshot()
    print(clock.fps())
And here is the Arduino code:

Code: Select all

#include <Wire.h>

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("Auf gehts!");
  pinMode(9, OUTPUT);         //LED 
}

void loop() {
  // read from port 1, send to port 0:
  // from Camera to Arduino
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
    if (inByte == 104)
    {
      for(int i = 0; i < 5; i++)
      {
        digitalWrite(9, HIGH);
        Serial.println("ON");
        delay(500);
        digitalWrite(9, LOW);
        Serial.println("OFF");
        delay(500);
      }
      Serial.println("");
      Serial.println("H detected! Rescue procedure started...");          
      Serial1.write("h");
    }
  }	
}

Ok, what should happen is the following: The camera sends a signal ('h') to the Arduino board. The Arduino board receives it as an ID (104 == h). Then, it would turn on the LED that is connected to pin 9 on and off for 5 seconds. After that, it sends back a signal to the camera ('h' again). The camera should also receive it as an ID, but a different one (209). It should then start again. If it ends up in an infinite loop (always sending signals to each other), then it works. If not, the communication might fail because of the code or the wiring.

The wiring should be exactly the same as I explained yesterday.

Thanks for your help,
Finn
User avatar
iabdalkader
Posts: 951
Joined: Sun May 24, 2015 3:53 pm

Re: Camera communication with Arduino Mega

Postby iabdalkader » Wed May 29, 2019 4:10 pm

Hi, what is labels, where is it defined and where it's written to ? And this part:

Code: Select all

                messageCheck = 209 #ID for letter h
                if (id(messageReceived) == messageCheck):
Do you compare the object id to 209 ? Why would the object ID equal 209 ?
https://docs.python.org/2/library/functions.html#id
finnleh
Posts: 36
Joined: Sun Apr 14, 2019 12:12 am

Re: Camera communication with Arduino Mega

Postby finnleh » Wed May 29, 2019 4:20 pm

I don't know, but I tried it.

How I came to the result that I would have to use this, is because I printed whatever I received from the Arduino board. This was (for the letter h) 209, for some reason. So I just took this number and in case the received message equals 209, the camera is allowed to go on.

But I mean, you should at least get the message: "Received something", if the communication works. Do you receive that message when you try the code(s) with the wiring I gave you?


Thanks,
Finn
User avatar
iabdalkader
Posts: 951
Joined: Sun May 24, 2015 3:53 pm

Re: Camera communication with Arduino Mega

Postby iabdalkader » Wed May 29, 2019 4:45 pm

I just realized that you don't even initialize UART. Please try the built-in Examples or search the forums for examples and if it doesn't work let me know and I'll test it.
Note id() returns the ID of an object (think of it like a memory address) it's a unique ID for the object, you should compare the char directly it should equal 104 for 'h' and 72 for 'H'.
finnleh
Posts: 36
Joined: Sun Apr 14, 2019 12:12 am

Re: Camera communication with Arduino Mega

Postby finnleh » Wed May 29, 2019 5:11 pm

from pyb import UART

Do you mean this line?

I have it in my other, bigger code. Can you insert this line into the code snippet I sent you earlier today, and then test it for me, please?

As I said, I don't have another camera. It worked before, but I am not sure if it worked because I was lucky (and it should not have worked). Right now, it doesn't work, and I am not sure if my camera is broken. That is why I want you to test it with your camera, please.

Please, test it, and tell me if it worked or if it didn't. Also, insert this line:
import sensor, image, time, os, nn, math

Might be the reason why the upper code doesn't work.


Thank you for helping me out,
Finn
User avatar
iabdalkader
Posts: 951
Joined: Sun May 24, 2015 3:53 pm

Re: Camera communication with Arduino Mega

Postby iabdalkader » Wed May 29, 2019 6:15 pm

Do you initialize the serial port and set the baudrate to 9600 ? Like this:

Code: Select all

uart = UART(3, 9600, timeout_char = 1000)
I can't test your code unless you send a working example. Please understand that I have limited time.
finnleh
Posts: 36
Joined: Sun Apr 14, 2019 12:12 am

Re: Camera communication with Arduino Mega

Postby finnleh » Wed May 29, 2019 6:41 pm

Yes, I do (see the first code in my first message).

Code: Select all

# Always pass UART 3 for the UART number for your OpenMV Cam.
# The second argument is the UART baud rate. For a more advanced UART control
# example see the BLE-Shield driver.
uart = UART(3, 9600, timeout_char = 1000)
Insert that above clock = time.clock():

Code: Select all

import sensor, image, time, os, nn, math
from pyb import UART

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

# Always pass UART 3 for the UART number for your OpenMV Cam.
# The second argument is the UART baud rate. For a more advanced UART control
# example see the BLE-Shield driver.
uart = UART(3, 9600, timeout_char = 1000)

clock = time.clock()

while(True):
    clock.tick()
    
    if (labels[max_idx] == "H"):
        print("Send signal to Arduino")
        uart.write("h\r")
        answerH = False
        while (answerH == False):
            if uart.any():
                print("Received some signal");
                messageReceived = uart.readchar()
                messageCheck = 209 #ID for letter h
                if (id(messageReceived) == messageCheck):
                    print("Received answer!")
                    answerH = True;

    img = sensor.snapshot()
    print(clock.fps())
   
Does it work now? It should, right?


Sincerely,
Finn
User avatar
iabdalkader
Posts: 951
Joined: Sun May 24, 2015 3:53 pm

Re: Camera communication with Arduino Mega

Postby iabdalkader » Wed May 29, 2019 7:47 pm

No it doesn't, this isn't defined:

Code: Select all

labels[max_idx]
labels and max_idx.

At least run the code before you send it.
finnleh
Posts: 36
Joined: Sun Apr 14, 2019 12:12 am

Re: Camera communication with Arduino Mega

Postby finnleh » Wed May 29, 2019 8:54 pm

OK. Here is the new code. I didn't have a camera with me, so I couldn't run the code. But it doesn't take much time for you to run the code...so everything's fine, right? Thank you anyway, that you test it for me.

Code: Select all

import sensor, image, time, os, nn, math
from pyb import UART

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

# Always pass UART 3 for the UART number for your OpenMV Cam.
# The second argument is the UART baud rate. For a more advanced UART control
# example see the BLE-Shield driver.
uart = UART(3, 9600, timeout_char = 1000)

clock = time.clock()

while(True):
    clock.tick()
    print("Send signal to Arduino")
    uart.write("h\r")
    answerH = False
    while (answerH == False):
        if uart.any():
            print("Received some signal")
            messageReceived = uart.readchar()
            messageCheck = 209 #ID for letter h
            if (id(messageReceived) == messageCheck):
                print("Received answer!")
                answerH = True

    img = sensor.snapshot()
    print(clock.fps())
I still don't have a camera with me, so if you could run the code and test it, that would be great. If it works, please test if the communication with the Arduino Mega works. Even if the ID isn't correct, you should at least get "Received some signal". If it gives you that, the communication works.

Thanks for your help,
Finn
User avatar
iabdalkader
Posts: 951
Joined: Sun May 24, 2015 3:53 pm

Re: Camera communication with Arduino Mega

Postby iabdalkader » Wed May 29, 2019 9:39 pm

It looks like it's working
Screenshot from 2019-05-30 03-39-13.png
You should double check your wiring and test the same example, if it still doesn't work maybe you have damaged your board after all.
finnleh
Posts: 36
Joined: Sun Apr 14, 2019 12:12 am

Re: Camera communication with Arduino Mega

Postby finnleh » Wed May 29, 2019 10:49 pm

OK, thank you very much for your help. I appreciate it!

All the best,
Finn

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 4 guests