View Single Post
  #15   Spotlight this post!  
Unread 07-03-2012, 23:42
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 code stops recieving images

I am glad that's fixed. In general, you need to add more "error checking" code and always assume the WPI library calls could fail. For example, all the calls that generate images such as ThresholdRGB, FilterImages etc could fail and return NULL pointers. If you don't check and use them, you will fault. It may be unlikely but it could happen under unusual circumstances. For example, here is an excerpt of our vision processing code. Note that it checked for errors after each WPI library call and at the end when deleting the objects, we called SAFE_DELETE which is a macro that basically check for NULL pointer before it deleted the object.
Code:
int err = ERR_SUCCESS;
BinaryImage *image = NULL;
BinaryImage *thresholdImage = NULL;
BinaryImage *bigObjImage = NULL;
BinaryImage *convexHullImage = NULL;
BinaryImage *filteredImage = NULL;
m_camera->GetImage(&m_cameraImage);
//
// Filter the image by color.
//
thresholdImage = m_cameraImage.ThresholdRGB(*m_colorThresholds);
image = thresholdImage;
if (thresholdImage == NULL)
{
    err = imaqGetLastError();
    TErr(("Failed to filter image with thresholds (err=%d).", err));
}
else if (m_sizeThreshold > 0)
{
    //
    // Remove small objects
    //
    bigObjImage = image->RemoveSmallObjects(false, m_sizeThreshold);
    image = bigObjImage;
    if (bigObjImage == NULL)
    {
        err = imaqGetLastError();
        TErr(("Failed to filter image with size (err=%d).", err));
    }
}
if (err == ERR_SUCCESS)
{
    convexHullImage = image->ConvexHull(false);
    image = convexHullImage;
    if (convexHullImage == NULL)
    {
        err = imaqGetLastError();
        TErr(("Failed to generate Convex Hull image (err=%d).", err));
    }
}
if ((err == ERR_SUCCESS) && (m_filterCriteria != NULL))
{
    filteredImage = image->ParticleFilter(m_filterCriteria, m_numCriteria);
    image = filteredImage;
    if (filteredImage == NULL)
    {
       err = imaqGetLastError();
       TErr(("Failed to filter image based on criteria (err=%d).", err));
    }
}
if (err == ERR_SUCCESS)
{
    vector<ParticleAnalysisReport> *reports =
        image->GetOrderedParticleAnalysisReports();
    if (reports == NULL)
    {
        err = imaqGetLastError();
        TErr(("Failed to get particle analysis reports (err=%d).", err));
    }
    else
    {
        CRITICAL_REGION(m_semaphore)
        {
            SAFE_DELETE(m_targets);
            m_targets = reports;
            reports = NULL;
        }
        END_REGION;
    }
}
SAFE_DELETE(filteredImage);
SAFE_DELETE(convexHullImage);
SAFE_DELETE(bigObjImage);
SAFE_DELETE(thresholdImage);
__________________

Last edited by mikets : 08-03-2012 at 00:00.
Reply With Quote