Go to Post step it up, stop trying to validate mediocrity - Aren_Hill [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
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 30-01-2013, 00:49
Nymph808's Avatar
Nymph808 Nymph808 is offline
Registered User
FRC #2443
 
Join Date: Jan 2011
Location: Maui
Posts: 11
Nymph808 is an unknown quantity at this point
Camera Tracking Code

This is currently our fifth year competing in an FRC regional, and this year we decided to attempt camera tracking again. We haven't been successful with this in the past, but we decided to revisit it this year. This code is supposed to see the low, medium, or high goal, and center the robot according to the center mass x. So far we were able to get the robot to see the high goal using the light ring, and I was able to get the Jaguar motors to move according to the center mass x.

The two issues:
* The tracking only works in the dark
* The center mass x barely changes even if the camera can see the reflective tape

Anyone can help?


Code:
 
package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.Joystick;
//import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.camera.AxisCamera;
import edu.wpi.first.wpilibj.camera.AxisCameraException;
import edu.wpi.first.wpilibj.image.BinaryImage;
import edu.wpi.first.wpilibj.image.ColorImage;
import edu.wpi.first.wpilibj.image.CriteriaCollection;
import edu.wpi.first.wpilibj.image.NIVision.MeasurementType;
import edu.wpi.first.wpilibj.image.NIVisionException;
import edu.wpi.first.wpilibj.image.ParticleAnalysisReport;
import edu.wpi.first.wpilibj.image.RGBImage;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.DriverStationLCD;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.IterativeRobot;

/**
 * Sample program to use NIVision to find rectangles in the scene that are
 * illuminated by a red ring light (similar to the model from FIRSTChoice). The
 * camera sensitivity is set very low so as to only show light sources and
 * remove any distracting parts of the image.
 *
 * The CriteriaCollection is the set of criteria that is used to filter the set
 * of rectangles that are detected. In this example we're looking for rectangles
 * with a minimum width of 30 pixels and maximum of 400 pixels. Similar for
 * height (see the addCriteria() methods below.
 *
 * The algorithm first does a color threshold operation that only takes objects
 * in the scene that have a significant red color component. Then removes small
 * objects that might be caused by red reflection scattered from other parts of
 * the scene. Then a convex hull operation fills all the rectangle outlines
 * (even the partially occluded ones). Finally a particle filter looks for all
 * the shapes that meet the requirements specified in the criteria collection.
 *
 * Look in the VisionImages directory inside the project that is created for the
 * sample images as well as the NI Vision Assistant file that contains the
 * vision command chain (open it with the Vision Assistant)
 */
public class CameraTest extends IterativeRobot {

    AxisCamera camera;          // the axis camera object (connected to the switch)
    CriteriaCollection cc;      // the criteria for doing the particle filter operation
    //Joystick joystick = new Joystick(1);
    Joystick joystick;
    Joystick gamepad;
    Relay spikeRelay;
    DriverStationLCD b_LCD;
    Drive robotDrive;
    Jaguar jagTL;
    Jaguar jagTR;
    Jaguar jagBL;
    Jaguar jagBR;

    public void robotInit() {
        camera = AxisCamera.getInstance("10.24.43.11");  // get an instance ofthe camera
        cc = new CriteriaCollection();      // create the criteria for the particle filter
        cc.addCriteria(MeasurementType.IMAQ_MT_BOUNDING_RECT_WIDTH, 30, 400, false);
        cc.addCriteria(MeasurementType.IMAQ_MT_BOUNDING_RECT_HEIGHT, 40, 400, false);
        spikeRelay = new Relay(5);
        b_LCD = DriverStationLCD.getInstance();
        jagTL = new Jaguar(3);
        jagTR = new Jaguar(1);
        jagBL = new Jaguar(4);
        jagBR = new Jaguar(2);
        joystick = new Joystick(1);
        gamepad = new Joystick(2);
        robotDrive = new Drive(jagTR, jagBR, jagTL, jagBL);
        b_LCD.updateLCD();
    }

    public void autonomousPeriodic() {
    }

    /**
     * This function is called once each time the robot enters operator control.
     */
    public void teleopPeriodic() {

        if (joystick.getRawButton(1)) {
            spikeRelay.set(Relay.Value.kForward);
            try {
                /**
                 * Do the image capture with the camera and apply the algorithm
                 * described above. This sample will either get images from the
                 * camera or from an image file stored in the top level
                 * directory in the flash memory on the cRIO. The file name in
                 * this case is "10ft2.jpg"
                 *
                 */
                ColorImage image = camera.getImage();         // comment if using stored images
//                ColorImage image;                           // next 2 lines read image from flash on cRIO
//                image =  new RGBImage("/10ft2.jpg");
                //BinaryImage thresholdImage = image.thresholdRGB(25, 255, 0, 45, 0, 47);   // keep only red objects
                BinaryImage thresholdImage = image.thresholdRGB(0, 45, 25, 225, 0, 45); //Michelle changed for green
                BinaryImage bigObjectsImage = thresholdImage.removeSmallObjects(false, 2);  // remove small artifacts
                BinaryImage convexHullImage = bigObjectsImage.convexHull(false);          // fill in occluded rectangles
                BinaryImage filteredImage = convexHullImage.particleFilter(cc);           // find filled in rectangles

                ParticleAnalysisReport[] reports = filteredImage.getOrderedParticleAnalysisReports();  // get list of results
                for (int i = 0; i < reports.length; i++) {                                // print results
                    ParticleAnalysisReport r = reports[i];
                    //System.out.println("Particle: " + i + ":  Center of mass x: " + r.center_mass_x);
                    //int centerMass = r.center_mass_x;
                    b_LCD.println(DriverStationLCD.Line.kMain6, 1, "Particle: " + i);
                    b_LCD.println(DriverStationLCD.Line.kUser5, 1, "Centermass x: " + r.center_mass_x);
                    b_LCD.updateLCD();

                    if (r.center_mass_x < 200) {
                        jagTL.set(0.4);
                        jagBL.set(0.4);
                        jagTR.set(0.4);
                        jagBR.set(0.4);
                    } else {
                        jagTL.set(-0.4);
                        jagBL.set(-0.4);
                        jagTR.set(-0.4);
                        jagBR.set(-0.4);
                    }

                }
                System.out.println(filteredImage.getNumberParticles() + "  " + Timer.getFPGATimestamp());

                SmartDashboard.putInt("Number of Particles: ", filteredImage.getNumberParticles());
                SmartDashboard.putInt("Height:  ", filteredImage.getHeight());
                SmartDashboard.putInt("Width:  ", filteredImage.getWidth());

                /**
                 * all images in Java must be freed after they are used since
                 * they are allocated out of C data structures. Not calling
                 * free() will cause the memory to accumulate over each pass of
                 * this loop.
                 */
                filteredImage.free();
                convexHullImage.free();
                bigObjectsImage.free();
                thresholdImage.free();
                image.free();

            } catch (AxisCameraException ex) {        // this is needed if the camera.getImage() is called
                ex.printStackTrace();
            } catch (NIVisionException ex) {
                ex.printStackTrace();
            }
        } else {
            robotDrive.run(gamepad);
            spikeRelay.set(Relay.Value.kOff);
        }

    }

    public void testPeriodic() {


    }
}
Reply With Quote
  #2   Spotlight this post!  
Unread 30-01-2013, 12:54
yelk11 yelk11 is offline
Registered User
FRC #3414
 
Join Date: Jan 2012
Location: farmington
Posts: 27
yelk11 is an unknown quantity at this point
Re: Camera Tracking Code

we are also having trouble with camera. it keeps giving us an error which tells us that the crio has ran out of memory, is there a way to free the memory and how?
Reply With Quote
  #3   Spotlight this post!  
Unread 30-01-2013, 16:24
apples000's Avatar
apples000 apples000 is offline
Registered User
no team
 
Join Date: Mar 2012
Rookie Year: 2012
Location: United States
Posts: 222
apples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant future
Re: Camera Tracking Code

If you are seeing an out of memory error, this usually means that you are creating more images than you are freeing. At the end of the image processing loop, after you've done all the filters and math that you think that you will need, you MUST call the free() method on ALL of the images you've created.
Reply With Quote
  #4   Spotlight this post!  
Unread 02-02-2013, 14:20
jds2001 jds2001 is offline
Registered User
AKA: Jon Stanley
FRC #4263 (CyberDrgaon)
Team Role: Mentor
 
Join Date: Nov 2012
Rookie Year: 2013
Location: United States
Posts: 160
jds2001 has much to be proud ofjds2001 has much to be proud ofjds2001 has much to be proud ofjds2001 has much to be proud ofjds2001 has much to be proud ofjds2001 has much to be proud ofjds2001 has much to be proud ofjds2001 has much to be proud ofjds2001 has much to be proud of
Re: Camera Tracking Code

Keep in mind that the images are stored in C data structures, so you must explicitly call free() on them in order for them to be discarded. Like the next poster said, make sure that you do that or you WILL run out of memory on the cRIO.
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 22:16.

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