Page 1 of 1

Saving x, y and rotation as txt file to sd card?

Posted: Sun Apr 19, 2020 10:35 am
by TechRep
Hello, I'm a newly in all OpenMV.
Have been working on a 6 axis robot arm project and did like to integrate OpenMV as vision system to detect parts orientation at random. The Robot controller program is looking in a txt file located on the C drive for the coordinates, X, Y, and rotation.
So far I found the Single Color Code Tracking Example quite helpful, from there I need to send or save the x, y, rotation to a txt file that the robot software reads and then is able to pick the part accordingly, it could also be save to the sd card inside the camera.
I have not been able to find any example that would show me how I would save or write the txt file to sd or external drive. Any help would be very appreciated. See code bellow:

Code: Select all

# Single Color Code Tracking Example
# This example shows off single color code tracking using the OpenMV Cam.
# A color code is a blob composed of two or more colors. The example below will
# only track colored objects which have both the colors below in them.

import sensor, image, time
from pyb import LED

# 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 = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds -> index is 0 so code == (1 << 0)
#              (30, 100, -64, -8, -32, 32)] # generic_green_thresholds -> index is 1 so code == (1 << 1)

thresholds = [(55, 100,-24, 11, 32, 86),     #1#yellow
              (27, 100, 42, 80, 30, 64),     #2#red
              (39, 100,-51,-12, 10, 57)]      #4#green
# Codes are or'ed together when "merge=True" for "find_blobs".

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()

object_x_old = 0
object_y_old = 0

code = 2 ## 1:yellow   2:red    4:green
buf = "00"
# 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. "merge=True" must be set to merge overlapping color blobs for color codes.


    img = sensor.snapshot()
    for blob in img.find_blobs(thresholds, pixels_threshold=100, area_threshold=100, merge=False):

#check if there is object with right color
        if blob.code() == code:


#make sure the detected object is stable and print the coordinates
#first it detect if the coordinates of blob is available
#second compared with the last position to make sure if the object is not moving
#third reduce the affect of anbience
            if!=None and (
                abs(object_x_old - int( < 8 and
                abs(object_y_old - int( < 8) and (
                blob.w()>35 and
               if buf[1]==ord('S') :

                    #clear the flag
                    buf = "00"

Re: Saving x, y and rotation as txt file to sd card?

Posted: Sun Apr 19, 2020 1:04 pm
by kwagyeman
Hi, the camera runs python.

So you literally open a file in Python and write to that file.

Code: Select all

with open("./filename.txt", 'w') as file:

Re: Saving x, y and rotation as txt file to sd card?

Posted: Sun Apr 19, 2020 3:29 pm
by TechRep
Thank you for the fast response!
And if I like to save the list on the main drive, on my mac, is that possible?

Re: Saving x, y and rotation as txt file to sd card?

Posted: Sun Apr 19, 2020 4:11 pm
by kwagyeman
Not yet, I'm working on an interface library that makes this possible however. It's tricky to do currently. Once I release the interface library this will be trivial. Actually, I already have working code if you want it.

Re: Saving x, y and rotation as txt file to sd card?

Posted: Sun Apr 19, 2020 5:01 pm
by TechRep
That would be very helpful if you are able to give me a hand in implementing it. Thank you!

Re: Saving x, y and rotation as txt file to sd card?

Posted: Sun Apr 19, 2020 5:31 pm
by kwagyeman
Copy this file onto your OpenMV Cam: ... ies/

Set this as ...


On your pc download this: ... ols/ ...


Alternative examples:

For the OpenMV Cam: ...

For your PC: ...


I'm still working on this code. Small data transfers work well. Still debugging issues with larger transfers.

Note, I'm not providing help support for desktop python on this code. If you are getting errors about libs not being installed you should know how to fix that on your PC. Make sure the script for the OpenMV Cam is saved as and that it actually got saved correctly.

On the desktop side just run the desktop script and it should work.

If you have a WiFi shield its far easier to use that over VCP which is harder to get working as there's no way to debug while the protocol is operating.