Announcement

Collapse
No announcement yet.

Vuforia Crash on Exit

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Vuforia Crash on Exit

    Hi everyone,

    I'm trying to use Vuforia to detect color by sampling an image and determining a range of values for Red, Blue, and other (since I only care about red or blue). I'm currently in the process of implementing a nested-for loop and other methods to find out whether red is on the left or the right. However, one thing that really bugs me at the moment is the fact that the phone crashes and restarts the RC each time I press the stop button. Aside from that, it works as expected. It just didn't crash until I added the code to run through the pixels in the image. Also, I'd appreciate any advice/direction to head to in determining the color. My coach suggested that I use a compression method and count up the number of reds/blues in a column and find where the position is relative to the image and that'll determine the button I should press.

    Thanks,
    FTC8564

    Code:
    package ftc8564opMode;
    
    import android.graphics.Bitmap;
    import android.graphics.Color;
    import android.util.Log;
    
    import com.qualcomm.ftcrobotcontroller.R;
    import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
    import com.vuforia.HINT;
    import com.vuforia.Vuforia;
    
    import ftc8564lib.VuforiaLocalizerImplSubclass;
    import ftc8564lib.pixelObject;
    
    import org.firstinspires.ftc.robotcore.external.matrices.OpenGLMatrix;
    import org.firstinspires.ftc.robotcore.external.matrices.VectorF;
    import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
    import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackable;
    import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackableDefaultListener;
    import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackables;
    
    import java.util.Arrays;
    
    @Autonomous(name = "Vuforia Navigation", group = "Autonomous")
    public class VuforiaNav extends LinearOpMode {
    
        @Override
        public void runOpMode() {
    
            VuforiaLocalizer.Parameters params = new VuforiaLocalizer.Parameters(R.id.cameraMonitorViewId);
            params.cameraDirection = VuforiaLocalizer.CameraDirection.BACK;
            params.vuforiaLicenseKey = "AUgbT0j/////AAAAGTj0axLoDUvmo1PWo4YoBxkeKIG99PQSTrgFoQVymuq7TMc3OGQozLZo6d+dLt0EiQwR87XKW4aXt/7BZr95khAoylxKP02Vh5PmZbp18YDa5g9gWiYUGUvHqLUbNgMKotFhGgE9noRvWbiP2RxgOCy+HoT39NaFXiLiH69cLGbCzpz1tzuvRPce/EVkWBBomcS2yC/hl1hTlBjvzTHN1lKMv59s9gYhC69DNODQeNg2JMOv3ggMlRDTjOpbNZUZAQkHfqS/2w0W8d0+krzVBD129juhL2r6u4mWVhXFq2FOZoUTezbkaFTxKCRadl3v5ot5aPmuEU4mSFtmrw15J6R9XtRAg/U8/I1k7zpRpVTg";
            params.cameraMonitorFeedback = VuforiaLocalizer.Parameters.CameraMonitorFeedback.AXES;
    
            VuforiaLocalizerImplSubclass vuforia = new VuforiaLocalizerImplSubclass(params);
            Vuforia.setHint(HINT.HINT_MAX_SIMULTANEOUS_IMAGE_TARGETS, 4);
            VuforiaTrackables beacons = vuforia.loadTrackablesFromAsset("FTC_2016-17");
            beacons.get(0).setName("Wheels");
            beacons.get(1).setName("Tools");
            beacons.get(2).setName("Lego");
            beacons.get(3).setName("Gears");
    
            waitForStart();
    
            beacons.activate();
    
            while (opModeIsActive()) {
    
                if (vuforia.rgb != null && gamepad1.a) {
                    int color = 0;
                    int red = 0;
                    int green = 0;
                    int blue = 0;
                    int xpos = 0;
                    int count = 0;
    
                    Bitmap bm = Bitmap.createBitmap(vuforia.rgb.getWidth(), vuforia.rgb.getHeight(), Bitmap.Config.RGB_565);
                    bm.copyPixelsFromBuffer(vuforia.rgb.getPixels());
    
                    pixelObject[][] pixel = new pixelObject[vuforia.rgb.getWidth()][vuforia.rgb.getHeight()];
                    pixelObject[][] colorPixel = new pixelObject[vuforia.rgb.getWidth()][vuforia.rgb.getHeight()];
                    String[][] test = new String[vuforia.rgb.getWidth()][vuforia.rgb.getHeight()];
                    pixelObject beaconRed = new pixelObject(0, 0, xpos);
                    pixelObject beaconBlue = new pixelObject(1, 0, xpos);
                    pixelObject beaconOther = new pixelObject(-1, 0, xpos);
    
                    for (int height = 0; height < bm.getHeight(); height++) {
                        for (int width = 0; width < bm.getWidth(); width++) {
                            color = bm.getPixel(width, height);
                            red = Color.red(color);
                            green = Color.green(color);
                            blue = Color.blue(color);
                            test[width][height] = String.valueOf(red) + " " + String.valueOf(green) + " " + String.valueOf(blue);
                            if (red > blue && red > green) {
                                colorPixel[width][height] = beaconRed;
                            } else if (blue > green && blue > red) {
                                colorPixel[width][height] = beaconBlue;
                            } else {
                                colorPixel[width][height] = beaconOther;
                            }
                        }
                    }
    
                    for(int row = 0; row < bm.getHeight(); row++)
                    {
                       Log.i("RGB", Arrays.toString(test[row]));
                    }
    
                    for (int height = 0; height < bm.getHeight(); height++) {
                        xpos = 0;
                        for (int width = 0; width < bm.getWidth(); width++) {
                            int previous = colorPixel[count][height].getColor();
                            if (colorPixel[width][height].getColor() != previous) {
                                pixel[xpos][height] = colorPixel[count][height];
                                pixel[xpos][height].setXpos(xpos);
                                count = width;
                                xpos++;
                            }
                            colorPixel[count][height].addCount();
                        }
                    }
                }
    
                for (VuforiaTrackable beacon : beacons) {
                    OpenGLMatrix pose = ((VuforiaTrackableDefaultListener) beacon.getListener()).getPose();
                    if (pose != null) {
                        VectorF translation = pose.getTranslation();
                        telemetry.addData(beacon.getName() + "-Translation", translation);
                        double degreesToTurn = Math.toDegrees(Math.atan2(translation.get(0), translation.get(1)))+90;
                        telemetry.addData(beacon.getName() + "-Degrees", degreesToTurn);
                        telemetry.addData("Pos", format(pose));
                        double sideC = Math.sqrt(translation.get(2)*translation.get(2) + translation.get(0)*translation.get(0));
                        telemetry.addData(beacon.getName() + "-Distance", sideC);
                    }
                }
                telemetry.update();
                idle();
            }
    
        }
    
        String format(OpenGLMatrix transformationMatrix) {
            return transformationMatrix.formatAsTransform();
        }
    }

  • #2
    Your code isn't providing any way for the App to break out of the pixel scanning loop of the driver presses stop.

    So, I would assume that the code is taking more than 2 seconds to do a full count.
    Remember that you are processing maybe 5000000 pixels, so even if you can execute you inner code in 1 uSec it will take 5 seconds to run.

    You need to check opModeIsActive() or isStopRequested() while you are doing your counts.
    You probably only need to do it each row (eg in the outer loop)

    Code:
    for (int height = 0; height < bm.getHeight(); height++) {
        if (isStopRequested() )
            break;
        for (int width = 0; width < bm.getWidth(); width++) {

    Comment

    Working...
    X