View Single Post
  #1   Spotlight this post!  
Unread 18-02-2010, 23:47
adamdb adamdb is offline
Registered User
#1583
 
Join Date: Feb 2005
Location: Parker, CO
Posts: 63
adamdb has a spectacular aura aboutadamdb has a spectacular aura aboutadamdb has a spectacular aura about
Multi-threading issue?

We are trying to run the camera code on a separate thread. We are basically using the Target class as-is from the CircleTrackerDemo program and have pulled out the relevant bits of code from the main program that call it. The code is as follows:
Code:
public class CameraThread extends Thread
{

    private double kScoreThreshold = .01;
    private AxisCamera cam;
    private Target[] targets;
    private boolean targetFound = false;
    private boolean active = false;

    public CameraThread()
    {
        // Setup camera
        cam = AxisCamera.getInstance();
        cam.writeResolution(AxisCamera.ResolutionT.k320x240);
        cam.writeBrightness(0);
    }

    public void run()
    {
        while (true)
        {
            if (active)
            {
                if (cam.freshImage())
                {
                    ColorImage image;
                    try
                    {
                        image = cam.getImage();

                        Thread.yield();
                        targets = Target.findCircularTargets(image);
                        Thread.yield();
                        image.free();
                        if (targets.length == 0 || targets[0].m_score < kScoreThreshold)
                        {
                            this.targetFound = false;
                        }
                        else
                        {
                            this.targetFound = true;
                        }
                    }
                    catch (AxisCameraException ex)
                    {
                        ex.printStackTrace();
                    }
                    catch (NIVisionException ex)
                    {
                        ex.printStackTrace();
                    }
                }
            }
            else
            {
                targetFound = false;

                try
                {
                    Thread.sleep(5);
                }
                catch (InterruptedException ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    }

    public void setActive(boolean active)
    {
        this.active = active;
    }

    public boolean isActive()
    {
        return active;
    }

    public boolean isTargetFound()
    {
        return targetFound;
    }

    public Target[] getTargets()
    {
        return targets;
    }
}
As you can see we set this up so we can tell the thread whether or not to be "active", which just determines whether or not it gets a new image from the camera and goes through the ellipse detection routines.

Our problem is that anytime the "active" flag is true in this routine and it is doing its thing getting images from the camera and detecting targets (which does work), our motor outputs become "jerky" as though the processor is off doing something else and can't feed the motor outputs. The motors actually turn off briefly before resuming their previous speed. We are using all Jaguars for motor control and are using the CAN network. I would think that once we send the command out the CAN network to a motor that the Jaguars would keep the speed the same unless told differently.

We do have the watchdog timeout set at .75 seconds, and we never see the "Watchdog not fed" message. We tried adding frequent "Thread.yield()" statements in this class and in the Target class, but if anything that seemed to make the problem worse. We tried taking all of the Thread.yield() calls out entirely, and tried liberal Thread.sleep(1) calls (which usually works in multi-threaded PC apps) with no change in behavior.

This thread can be running, but with the active flag set to false and we do not see the issue, so it has to be something to do with the camera code itself.

So, does this look like it should work? Is anyone else seeing similar issues using code from the CircleTrackerDemo app? Is anyone else multi-threading the camera code in a similar or different way?

As always any suggestions are appreciated.
__________________
Adam Bryant
Programming Mentor
Team 1583
Ridge View Academy Rambotics
Reply With Quote