I2C Communication

Discussion related to "under the hood" OpenMV topics.
Ju_Mrcn
Posts: 3
Joined: Tue Nov 19, 2019 10:57 am

I2C Communication

Postby Ju_Mrcn » Tue Nov 19, 2019 11:27 am

Hi everybody,
I am on a project where I would like follow a micro-line with a robot and I have a big problem on an I2C communication between an arduino mega(SLAVE) and the OpenMV cam M7 (MASTER).
Problem :
The communication run perfectly when the arduino and the cam are connected at the computer. When I unplug the cam, it's running but if I restart them, the communication does not start.

OpenMV code :

Code: Select all

enable_lens_corr = False # turn on for straighter lines...

from pyb import LED
import sensor, image, time, pyb
from pyb import DAC
from  pyb import I2C

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) # grayscale is faster
sensor.set_framesize(sensor.VGA) #
sensor.skip_frames(time = 2000)

i2c = pyb.I2C(2,I2C.MASTER)
#i2c.scan()
clock = time.clock()
dac = DAC("P6")

Yun=0
Ydeux=0
min_degree = 1
max_degree = 179
teensy = 8
angle = 0
code1 = 90

while(True):
    clock.tick()
    clock.fps()

    img = sensor.snapshot()
    #img.draw_line(0,240,640,240,color=(0,0,255),thickness=2)
    #img.draw_keypoints(1,color = (255,0,0))
    for l in img.find_lines(threshold = 4500, theta_margin = 25, rho_margin = 25):
        if (min_degree <= l.theta()) and (l.theta() <= max_degree):
            img.draw_line(l.line(), color = (255, 0, 0))
        dac.write(l.theta())
        #print("Theta %f" % l.theta())
        Yun=l.y1()
        Ydeux=l.y2()
        print("Y1= %f" %Yun)
        print("Y2= %f" %Ydeux)
        Yc=Ydeux-((Yun-Ydeux)/2)
        print("Yc    = %.4f" % Yc)
        code1 = int(l.theta())
        #print("Theta = %i" % code1)
        if(i2c.is_ready(teensy)):
            i2c.send(90 ,addr = 8, timeout=9999999)
        time.sleep(10)

Arduino code :

Code: Select all

#include <Wire.h>

void setup() {
  
  Wire.begin(8);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);
}

void loop() {
  delay(1000);
}

void receiveEvent(int x){
  while(Wire.available()){
  int c = Wire.read();
  Serial.print("receveid: ");
  Serial.print(c);
  Serial.println();
  }
}

I'm sorry for my english, i'm french student and it's difficult :mrgreen: .
User avatar
iabdalkader
Posts: 1143
Joined: Sun May 24, 2015 3:53 pm

Re: I2C Communication

Postby iabdalkader » Tue Nov 19, 2019 2:09 pm

I don't understand the issue, do you save the code as main.py ?
Ju_Mrcn
Posts: 3
Joined: Tue Nov 19, 2019 10:57 am

Re: I2C Communication

Postby Ju_Mrcn » Tue Nov 19, 2019 5:00 pm

Hum... When I used the computer, i click on the green arrow, to put the code on the cam. I don't know what is the main.py... you have mat be the solution... I will test that tomorrow because it's the night in France... Thank you a lot!
User avatar
iabdalkader
Posts: 1143
Joined: Sun May 24, 2015 3:53 pm

Re: I2C Communication

Postby iabdalkader » Tue Nov 19, 2019 7:43 pm

Ju_Mrcn wrote:
Tue Nov 19, 2019 5:00 pm
Hum... When I used the computer, i click on the green arrow, to put the code on the cam. I don't know what is the main.py... you have mat be the solution... I will test that tomorrow because it's the night in France... Thank you a lot!
You need to save the script on the camera so it runs when you disconnect it. It should be saved as main.py. The IDE can save the script from Tools->save open script...
Ju_Mrcn
Posts: 3
Joined: Tue Nov 19, 2019 10:57 am

Re: I2C Communication

Postby Ju_Mrcn » Wed Nov 20, 2019 1:55 am

iabdalkader wrote:
Tue Nov 19, 2019 7:43 pm
Ju_Mrcn wrote:
Tue Nov 19, 2019 5:00 pm
Hum... When I used the computer, i click on the green arrow, to put the code on the cam. I don't know what is the main.py... you have mat be the solution... I will test that tomorrow because it's the night in France... Thank you a lot!
You need to save the script on the camera so it runs when you disconnect it. It should be saved as main.py. The IDE can save the script from Tools->save open script...
Okay I test that this morning. It's strange because before this problem, when I just put the code as I say before and it ran. It's a memory problem? It's an I2C constraint?

Thanks a lot!!
User avatar
kwagyeman
Posts: 3829
Joined: Sun May 24, 2015 2:10 pm

Re: I2C Communication

Postby kwagyeman » Wed Nov 20, 2019 2:12 am

The camera will continue to execute whatever script it is running when disconnected from the IDE until it loses power. Then on startup if not connected to the IDE it will run main.py.
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: MSN [Bot] and 7 guests