Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Centering Camera and Robot (http://www.chiefdelphi.com/forums/showthread.php?t=112256)

barre006 28-01-2013 17:17

Centering Camera and Robot
 
We need to figure out how to get our camera to center itself on rectangular objects.

Basically, we can get the camera to take a picture and recognize the rectangle as a target. But the rectangle in the image isn't centered. We want the robot and camera to move until the rectangle is in the dead center of the camera and the image, that way we can aim correctly.

Thank you in advance for any assistance you might be able to offer us, we're really looking forward to finally using vision targeting this year.

2472Programer 28-01-2013 17:48

Re: Centering Camera and Robot
 
I faced this problem a few weeks ago and here is my solution

Code:

private void centerShooter() {
       
        ColorImage image = null;
       
        BinaryImage thresholdImage = null;
       
        BinaryImage bigObjectsImage = null;
       
        BinaryImage convexHullImage = null;
       
        BinaryImage filteredImage = null;
        int biggestindex = 99;
        int widest = 0;
        int highest = 0;
        ParticleAnalysisReport[] reports = null;
       

        try{
           
            image = camera.getImage();
           
           
            thresholdImage = image.thresholdRGB(0, 25, 20, 225, 0, 225);
           
          // thresholdImage.
           
            bigObjectsImage = thresholdImage.removeSmallObjects(false, 2);
           
            convexHullImage =  bigObjectsImage.convexHull(false);
           
            filteredImage = convexHullImage.particleFilter(cc);
           
            reports = filteredImage.getOrderedParticleAnalysisReports();
            for (int i = 0; i < reports.length + 1; i++ ){
               
                           
                if ((reports[i].imageHeight > highest) && (reports[i].imageWidth > widest))
                {
                    widest = reports[i].imageWidth;
                    highest = reports[i].imageHeight;
                    biggestindex = i;
                   
                    System.out.println("Problem's not here");
                }
           
           
       
                System.out.println("Test");
       
                SmartDashboard.putString("Filtered Image", filteredImage.toString());
               
                outputstring = "Height = " + reports[biggestindex].toString() + "Width = " + reports[biggestindex].toString();
                System.out.println (outputstring);
               
                SmartDashboard.putString("Dimensions: ", outputstring);
               
                SmartDashboard.putInt ("Targets Found", reports.length);
               
                SmartDashboard.putInt("height of Target", reports[biggestindex].imageHeight);
               
                SmartDashboard.putInt("Width of Target", reports[biggestindex].imageWidth);     
               
           
                if(reports[biggestindex].center_mass_y>centerdown){
                 
                    increaseShooterAngle();
                   
                    Timer.delay(.3);
                   
                    shooterAngle.set(Relay.Value.kOff);
                   
                    System.out.println("Increase");
                   
                }else if(reports[biggestindex].center_mass_y<centerup){
                   
                    decreaseShooterAngle();
                   
                    Timer.delay(.3);
                   
                    shooterAngle.set(Relay.Value.kOff);
                   
                    System.out.println("Decrease");

                   
                }else{
                   
                    System.out.println("You're Good");
                   
                }
                if(reports[biggestindex].center_mass_x>centerleft){
                   
                    toTheLeft();
                   
                }else if(reports[biggestindex].center_mass_x<centerright){
                   
                    toTheRight();
                   
                }else{
                   
                    tank.tankDrive(0, 0);
                }
               
                   
           
            }
           
                   
        }catch(Exception ex){
            //System.out.println("Failed");
           
            SmartDashboard.putString("Error", ex.getMessage());
           
        }finally{
           
        }
       
        try{
           
            filteredImage.free();
           
            convexHullImage.free();
           
            bigObjectsImage.free();
           
            thresholdImage.free();
           
            image.free();
           
        }catch(NIVisionException ex){
           
        }
    }

This still isn't perfect, as most times the robot completely misses the vision target, a problem that I must face today, and this code likes to also track white light... no explanation needed:yikes: and in terms of the Y axis tracking, you may want to have a sensor that can tell you the angle of the shooter and have the shooter move until it is at the right angle.

divixsoft 29-01-2013 00:49

Re: Centering Camera and Robot
 
why not calculate the angle, than use a gyro with a pid loop to turn?

jesusrambo 29-01-2013 03:43

Re: Centering Camera and Robot
 
Hey, try checking out my guide. It explains how to do the vision processing parts of what you're trying to do.

As for the rest, check out my code for automatic turret tracking. RobotMap.top[0] is the reported x coordinate of the center of the detected target. If you're doing your own vision processing, just pass it that value as such. CameraXOffset is just 160, the x value of the center of the camera's vision. HorizontalTurretAxis.rotate() is a method I added to HorizontalTurretAxis, but replace that with whatever you want the PID output to be of course.

If you need more explanation on how this works please ask.

TL;DR image processing sends x,y coordinates of detected target back to a command that runs a PID controller using that coordinate as input and your motors as output to align itself.


All times are GMT -5. The time now is 10:09.

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