Go to Post Oh man... 20% skinnier, 40% dorkier, ... if that's what turns into one of our best mentors, I REALLY need to find a new hobby. ;) - Rich Kressly [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 20-02-2012, 17:11
Stonemotmot Stonemotmot is offline
Registered User
FRC #0486
 
Join Date: Sep 2011
Location: United States, Pa
Posts: 53
Stonemotmot is an unknown quantity at this point
Camera Tracking Delay

Hello, My team has finally gotten camera targeting to work in c++. The only issue is there is about a 3-5 sec wait between when the function is called and when it actually begins to auto correct to the target. My code is full of "printf" commands and I was wondering if this could cause the delay by using up more resources or is 3-5 sec the best speeds we are going to get. I dont have the code on me to post sry.
Reply With Quote
  #2   Spotlight this post!  
Unread 20-02-2012, 17:40
Travis Hoffman's Avatar Unsung FIRST Hero
Travis Hoffman Travis Hoffman is offline
O-H
FRC #0048 (Delphi E.L.I.T.E.)
Team Role: Engineer
 
Join Date: Sep 2001
Rookie Year: 2001
Location: Warren, Ohio USA
Posts: 4,045
Travis Hoffman has a reputation beyond reputeTravis Hoffman has a reputation beyond reputeTravis Hoffman has a reputation beyond reputeTravis Hoffman has a reputation beyond reputeTravis Hoffman has a reputation beyond reputeTravis Hoffman has a reputation beyond reputeTravis Hoffman has a reputation beyond reputeTravis Hoffman has a reputation beyond reputeTravis Hoffman has a reputation beyond reputeTravis Hoffman has a reputation beyond reputeTravis Hoffman has a reputation beyond repute
Re: Camera Tracking Delay

Quote:
Originally Posted by Stonemotmot View Post
Hello, My team has finally gotten camera targeting to work in c++. The only issue is there is about a 3-5 sec wait between when the function is called and when it actually begins to auto correct to the target. My code is full of "printf" commands and I was wondering if this could cause the delay by using up more resources or is 3-5 sec the best speeds we are going to get. I dont have the code on me to post sry.
I'm not saying this will fix your problem, but I will definitely say it is good practice to comment out any printf commands in your code you do not need for active debugging. I'd try this and see if it helps.
__________________

Travis Hoffman, Enginerd, FRC Team 48 Delphi E.L.I.T.E.
Encouraging Learning in Technology and Engineering - www.delphielite.com
NEOFRA - Northeast Ohio FIRST Robotics Alliance - www.neofra.com
NEOFRA / Delphi E.L.I.T.E. FLL Regional Partner
Reply With Quote
  #3   Spotlight this post!  
Unread 20-02-2012, 17:46
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: Camera Tracking Delay

Quote:
Originally Posted by Stonemotmot View Post
Hello, My team has finally gotten camera targeting to work in c++. The only issue is there is about a 3-5 sec wait between when the function is called and when it actually begins to auto correct to the target. My code is full of "printf" commands and I was wondering if this could cause the delay by using up more resources or is 3-5 sec the best speeds we are going to get. I dont have the code on me to post sry.
When we started playing with the Vision code, we got a long delay too. There are several factors:
1. Make sure your camera resolution is 320x240. If you had it 640x480, it will take a long time to process the image. At first, we thought we had it set at 320x240 because we have the line m_camera.WriteResolution(AxisCamera::kResolution_3 20x240). But for some reason, we had to go to the web interface of the camera to force it to 320x240 before the performance started to improve.
2. Make sure you filter the image correctly. For example, if your light source is red, make sure your color thresholds are towards red. If not, you may end up with lots of false positive targets that require a long time to process.

In order to diagnose performance issues, it may be helpful to add timing code to check who takes a long time. For example, this is an excerpt of our image processing code based on the sample:
Code:
#define SAFE_DELETE(p)          if ((p) != NULL)    \
                                {                   \
                                    delete (p);     \
                                    (p) = NULL;     \
                                }
#define GetMsecTime()           (GetFPGATime()/1000)
    void
    ProcessImage(
        void
        )
    {
        if (m_camera->IsFreshImage())
        {
            int err = ERR_SUCCESS;
            BinaryImage *image = NULL;
            BinaryImage *thresholdImage = NULL;
            BinaryImage *bigObjImage = NULL;
            BinaryImage *convexHullImage = NULL;
            BinaryImage *filteredImage = NULL;
#ifdef _CHECK_PERF
            UINT32 totalTime = 0;
            UINT32 startTime;
            UINT32 deltaTime;
#endif
#ifdef _CHECK_PERF
            startTime = GetMsecTime();
#endif
            m_camera->GetImage(&m_cameraImage);
#ifdef _CHECK_PERF
            deltaTime = GetMsecTime() - startTime;
            totalTime += deltaTime;
            printf("AcquireImageTime = %d", deltaTime);
#endif
            //
            // Filter the image by color.
            //
#ifdef _CHECK_PERF
            startTime = GetMsecTime();
#endif
            switch (m_imageType)
            {
            case IMAQ_IMAGE_RGB:
                thresholdImage =
                    m_cameraImage.ThresholdRGB(*m_colorThresholds);
                break;
            case IMAQ_IMAGE_HSL:
                thresholdImage =
                    m_cameraImage.ThresholdHSL(*m_colorThresholds);
                break;
            default:
                printf("Unsupported image type (type=%d).", m_imageType);
                break;
            }
#ifdef _CHECK_PERF
            deltaTime = GetMsecTime() - startTime;
            totalTime += deltaTime;
            printf("ColorThresholdTime = %d", deltaTime);
#endif
            image = thresholdImage;
            if (thresholdImage == NULL)
            {
                err = imaqGetLastError();
                printf("Failed to filter image with thresholds (err=%d).", err);
            }
            else if (m_sizeThreshold > 0)
            {
                //
                // Remove small objects
                //
#ifdef _CHECK_PERF
                startTime = GetMsecTime();
#endif
                bigObjImage = image->RemoveSmallObjects(
                                false, m_sizeThreshold);
#ifdef _CHECK_PERF
                deltaTime = GetMsecTime() - startTime;
                totalTime += deltaTime;
                printf("BigObjFilterTime = %d", deltaTime);
#endif
                image = bigObjImage;
                if (bigObjImage == NULL)
                {
                    err = imaqGetLastError();
                    printf("Failed to filter image with size (err=%d).", err);
                }
            }
            if (err == ERR_SUCCESS)
            {
#ifdef _CHECK_PERF
                startTime = GetMsecTime();
#endif
                convexHullImage = image->ConvexHull(false);
#ifdef _CHECK_PERF
                deltaTime = GetMsecTime() - startTime;
                totalTime += deltaTime;
                printf("ConvexHullTime = %d", deltaTime);
#endif
                image = convexHullImage;
                if (convexHullImage == NULL)
                {
                    err = imaqGetLastError();
                    printf("Failed to generate Convex Hull image (err=%d).", err);
                }
            }
            if ((err == ERR_SUCCESS) && (m_filterCriteria != NULL))
            {
#ifdef _CHECK_PERF
                startTime = GetMsecTime();
#endif
                filteredImage = image->ParticleFilter(m_filterCriteria,
                                                      m_numCriteria);
#ifdef _CHECK_PERF
                deltaTime = GetMsecTime() - startTime;
                totalTime += deltaTime;
                printf("ParticleFilterTime = %d", deltaTime);
#endif
                image = filteredImage;
                if (filteredImage == NULL)
                {
                    err = imaqGetLastError();
                    printf("Failed to filter image based on criteria (err=%d).", err);
                }
            }
            if (err == ERR_SUCCESS)
            {
#ifdef _CHECK_PERF
                startTime = GetMsecTime();
#endif
                vector<ParticleAnalysisReport> *reports =
                        image->GetOrderedParticleAnalysisReports();
#ifdef _CHECK_PERF
                deltaTime = GetMsecTime() - startTime;
                totalTime += deltaTime;
                printf("GetReportTime = %d", deltaTime);
#endif
                if (reports == NULL)
                {
                    err = imaqGetLastError();
                    printf("Failed to get particle analysis reports (err=%d).", err);
                }
                else
                {
#ifdef _DUMP_REPORTS
                    printf("NumParticles = %d", reports->size());
                    for (unsigned i = 0; i < reports->size(); i++)
                    {
                        ParticleAnalysisReport *p = &(reports->at(i));
                        printf("%d: (%d,%d) [%d,%d/%d,%d]: AR=%5.2f,Q=%5.2f,Per=%4.2f",
                               i,
                               p->center_mass_x,
                               p->center_mass_y,
                               p->boundingRect.left,
                               p->boundingRect.top,
                               p->boundingRect.width,
                               p->boundingRect.height,
                               (float)p->boundingRect.width/(float)p->boundingRect.height,
                               p->particleQuality,
                               p->particleToImagePercent);
                    }
#endif
                    Synchronized sync(m_semaphore);
                    SAFE_DELETE(m_targets);
                    m_targets = reports;
                    reports = NULL;
                }
            }
#ifdef _CHECK_PERF
            printf("Total Elapsed Time = %d", totalTime);
#endif
            SAFE_DELETE(filteredImage);
            SAFE_DELETE(convexHullImage);
            SAFE_DELETE(bigObjImage);
            SAFE_DELETE(thresholdImage);
        }
    }   //ProcessImage
__________________
Reply With Quote
  #4   Spotlight this post!  
Unread 20-02-2012, 22:18
rbmj rbmj is offline
Registered User
FRC #0612 (Chantilly Robotics)
Team Role: Alumni
 
Join Date: Apr 2011
Rookie Year: 2011
Location: DC Area/Fairfax County
Posts: 192
rbmj is a jewel in the roughrbmj is a jewel in the roughrbmj is a jewel in the rough
Re: Camera Tracking Delay

Cutting the resolution to 320x240 should cut the processing time by at least 75%.

Printfs generally aren't so much a bottleneck, as the output is buffered. What'll cause a much bigger slowdown are memory allocations. Make sure you're not copying around gigantic vectors of particle reports (use a const reference/pointer) or a bunch of separate copies of the image.

A FLOP (Floating Point Operation) is also pretty slow on the cRIO, which makes triginometric or exponential calculations pretty slow. So if you find yourself calling std::sin or std:ow a lot, you might see a slowdown.

Most of the speedup you can get though is by decreasing the resolution and making sure your thresholds don't generate a lot of false positives though.

If you want to get rid of the printfs though, one of the best things to do is to make a macro:

Code:
//in "someglobalincludefile.h"
#define DEBUG_FLAG
//change to whatever you like/already have -- comment out to disable
#define STRINGIFY(sym) #sym
//puts quotes around sym

//in a .h file
#include "someglobalincludefile.h"
void print_debug_impl(const char*, const char*);

#ifdef DEBUG_FLAG
#define print_debug(str) print_debug_impl("DEBUG>" __FILE__ "@" STRINGIFY(__LINE__) ": ", str)
#else
//will be disabled
#define print_debug(str) static_cast<void>(0)
#endif

//in a .cpp file
#include <cstdio>
void print_debug_impl(const char * location, const char * message) {
   std::puts(location);
   std::puts(message);
   //or, if you have many threads and don't want them separated, replace with:
   //std::printf("%s%s", location, message);
   //but may be slower
}

//then anywhere else
print_debug_info("Error finding XYZ\n");
Output looks like:
DEBUG>somefile.cpp@123: Error finding XYZ

Might be a small error; don't have the compiler in front of me right now
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 12:57.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi