Executing Main Module Example

General discussion about topics related to OpenMV.
Alchemist
Posts: 4
Joined: Tue Sep 22, 2020 3:31 pm

Executing Main Module Example

Postby Alchemist » Mon Feb 22, 2021 7:41 pm

Hi Guys,

Going back to basic please.

I have read other members in the past struggle with this concept. eg Missing modules when installing on Windows 7 viewtopic.php?t=305.

Using the IDE, the code will flash the correctly the colours when connected and disconnected.

Code: Select all

# Main Module Example
#
# When your OpenMV Cam is disconnected from your computer it will either run the
# main.py script on the SD card (if attached) or the main.py script on
# your OpenMV Cam's internal flash drive.

import time, pyb

led = pyb.LED(3) # Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
usb = pyb.USB_VCP() # This is a serial port object that allows you to
# communciate with your computer. While it is not open the code below runs.

while(not usb.isconnected()):
    led = pyb.LED(1)
    led.on()
    time.sleep_ms(150)
    led.off()
    time.sleep_ms(100)
    led = pyb.LED(3)
    led.on()
    time.sleep_ms(150)
    led.off()
    time.sleep_ms(600)
    print("HELLO DISCONNECTED")

led = pyb.LED(2) # Switch to using the green LED.

while(usb.isconnected()):
    led.on()
    time.sleep_ms(150)
    led.off()
    time.sleep_ms(100)
    led.on()
    time.sleep_ms(150)
    led.off()
    time.sleep_ms(600)
    print("Hello CONNECTED")
Saving the code to main.py via Tools --> Save to main.py and then reset cam....... the cam will flash red and blue - ie disconnected.

A few times, you mentioned "Now on the next power up, the cam will run your code instead of the default". The USB cable is giving power to the cam - hence flash disconnected colours. Without using IDE, how can I make a connection the the cam?

Using python (Atom/Visual Studio Code) to connect to the CAM's com port

Code: Select all

# Windows OS
import serial
import time

ser = serial.Serial('COM7', 115200, timeout=1, dsrdtr=False)
ser.dtr = False
ser.setDTR(False)

print(ser.readline())
print(ser.isOpen)

while True:
    line = ser.readline().strip()
    if line:
        print(line)
By running the python code (windows 7), I would assume main.py script is executed and switch colours to 'connected colours'...but it does not. I would expect to read one of the print statements too but it doesnt.

What am I missing? Have a not setup my python environment properly?

As you know, my first goal is to read the CAM's print() statement in python.

Thank you in advance.
User avatar
kwagyeman
Posts: 5001
Joined: Sun May 24, 2015 2:10 pm

Re: Executing Main Module Example

Postby kwagyeman » Mon Feb 22, 2021 8:59 pm

Hi, I replied to you via the private messages about this and showed that the code works fine for me. I don't know how to help you given standard tooling works with them system for what you are trying to do.
Nyamekye,
User avatar
kwagyeman
Posts: 5001
Joined: Sun May 24, 2015 2:10 pm

Re: Executing Main Module Example

Postby kwagyeman » Mon Feb 22, 2021 9:00 pm

After you updated the code on the camera please verify the file was written. You should be able to see the main.py being updated with your script.
Nyamekye,
User avatar
iabdalkader
Posts: 1326
Joined: Sun May 24, 2015 3:53 pm

Re: Executing Main Module Example

Postby iabdalkader » Mon Feb 22, 2021 11:11 pm

This is the config for a serial port in Python that works with the cam you need these exact flags, I see you turn of DTR/DSR might be the issue.

Code: Select all

sp = serial.Serial(port, baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE,
            xonxoff=False, rtscts=False, stopbits=serial.STOPBITS_ONE, timeout=None, dsrdtr=True)
Alchemist
Posts: 4
Joined: Tue Sep 22, 2020 3:31 pm

Re: Executing Main Module Example

Postby Alchemist » Tue Feb 23, 2021 4:27 pm

Yes Kwabena, we have been communicating privately and your help is much appreciated.

Ibrahim's reply to suggested that I should post here to share knowledge etc.

Yes, the code is saved properly. Red light confirms saving and IDLE confirms the code has been updated.

I don't understand why the code posted works for you and not me. Is my python environment not properly configured? Windows has something disabled?

Is there another test I could try? For example a python code that can change the LED colours on the CAM?

Any other suggestion to try?
User avatar
kwagyeman
Posts: 5001
Joined: Sun May 24, 2015 2:10 pm

Re: Executing Main Module Example

Postby kwagyeman » Tue Feb 23, 2021 4:58 pm

Um, well, maybe add a while loop around both loops. Your script exits if any process opens the port. It's quite common for apps to be on a PC that open and close serial ports on plugin for any UART device.
Nyamekye,

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 1 guest