Announcement

Collapse
No announcement yet.

ColorSensor and Autonomous integration

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

  • ColorSensor and Autonomous integration

    Hello, our team has been trying to integrate a functioning ColorSensor program (that was adapted from the BasicColorSensor) into out also functioning autonomous mode. However when inserting the ColorSensor program (after making adjustments) and building we are returning with numerous "cannot find symbol:" errors. We have attempted to define all of the unfound symbols. Unfortunately we cannot seem to correct relativeLayout, as it returns "expecting <identifier>". Any Help would be greatly appreciated!

    Below is out current Autonomous program:


    package org.firstinspires.ftc.teamcode;

    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de;
    import com.qualcomm.robotcore.hardware.NormalizedColorSen sor;
    import com.qualcomm.robotcore.hardware.SwitchableLight;
    import com.qualcomm.robotcore.hardware.NormalizedRGBA;
    import com.qualcomm.robotcore.eventloop.opmode.Autonomous ;
    import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
    import com.qualcomm.robotcore.eventloop.opmode.Disabled;
    import com.qualcomm.robotcore.hardware.DcMotor;
    import com.qualcomm.robotcore.util.ElapsedTime;
    import com.qualcomm.robotcore.util.Range;
    import android.app.Activity;
    import android.graphics.Color;
    import android.view.View;

    @Autonomous(name="BasicOpMode_Autonomous", group="Linear Opmode")

    public class BasicOpMode_Autonomous extends LinearOpMode {

    private ElapsedTime runtime = new ElapsedTime();
    private DcMotor left_drive;
    private DcMotor right_drive;
    private NormalizedColorSensor color_sensor;
    private RelativeLayout;


    @Override
    public void runOpMode() {
    telemetry.addData("Status", "Initialized");
    telemetry.update();

    left_drive = hardwareMap.get(DcMotor.class, "left_drive");
    right_drive = hardwareMap.get(DcMotor.class, "right_drive");

    waitForStart();

    left_drive.setPower(1.0);
    right_drive.setPower(-1.0);

    ElapsedTime eTime = new ElapsedTime();

    eTime.reset();

    View relativeLayout;

    int relativeLayoutId = hardwareMap.appContext.getResources().getIdentifie r("RelativeLayout", "id", hardwareMap.appContext.getPackageName());
    relativeLayout = ((Activity) hardwareMap.appContext).findViewById(relativeLayou tId);

    try {
    runSample();
    } finally {
    relativeLayout.post(new Runnable() {
    public void run() {
    relativeLayout.setBackgroundColor(Color.WHITE);
    }
    });
    }
    }

    protected void runSample() throws InterruptedException {

    float[] hsvValues = new float[3];
    final float values[] = hsvValues;

    boolean bPrevState = false;
    boolean bCurrState = false;

    color_sensor = hardwareMap.get(NormalizedColorSensor.class, "color_sensor");

    if (color_sensor instanceof SwitchableLight) {
    ((SwitchableLight)color_sensor).enableLight(true);
    }

    waitForStart();
    while (opModeIsActive()) {
    bCurrState = gamepad1.x;
    if (bCurrState != bPrevState) {
    if (bCurrState) {
    if (color_sensor instanceof SwitchableLight) {
    SwitchableLight light = (SwitchableLight)color_sensor;
    light.enableLight(!light.isLightOn());
    }
    }
    }
    bPrevState = bCurrState;

    NormalizedRGBA colors = color_sensor.getNormalizedColors();

    Color.colorToHSV(colors.toColor(), hsvValues);
    telemetry.addLine()
    .addData("H", "%.3f", hsvValues[0])
    .addData("S", "%.3f", hsvValues[1])
    .addData("V", "%.3f", hsvValues[2]);
    telemetry.addLine()
    .addData("r", "%.3f", colors.red)
    .addData("b", "%.3f", colors.blue);

    int color = colors.toColor();
    telemetry.addLine("raw Android color: ")
    .addData("r", "%02x", Color.red(color))
    .addData("b", "%02x", Color.blue(color));

    float max = Math.max(Math.max(Math.max(colors.red, colors.green), colors.blue), colors.alpha);
    colors.red /= max;
    colors.green /= max;
    colors.blue /= max;
    color = colors.toColor();

    telemetry.addLine("normalized color: ")
    .addData("r", "%02x", Color.red(color))
    .addData("b", "%02x", Color.blue(color));
    telemetry.update();

    Color.RGBToHSV(Color.red(color), Color.green(color), Color.blue(color), hsvValues);

    RelativeLayout.post(new Runnable() {
    public void run() {
    relativeLayout.setBackgroundColor(Color.HSVToColor (0xff, values));
    }
    });
    }

    if(Color.red(color) > Color.blue(color)){
    while(eTime.time() < .5){}
    left_drive.setPower(-1.0);
    right_drive.setPower(1.0);

    eTime.reset();

    while(eTime.time() < 2.5){}

    // Show the elapsed game time and wheel power.
    telemetry.addData("Status", "Run Time: " + runtime.toString());
    telemetry.update();
    }
    }
    }



    Below I've attached the error that returns when building. Thank you!

  • #2
    I'm guessing that line 25 this one: private RelativeLayout; -- This says that you are declaring a class variable of type RelativeLayout, but have not given it a name. I believe you want to delete this line to fix the current error.

    You have a variable named relativeLlayout (lower case "r"), but that is of type View - which is probably what you want.

    Comment


    • #3
      Thank you FLARE, we've addressed the issue and now out last hurdle is reading symbol "eTime".

      Comment


      • #4
        https://gist.githubusercontent.com/f...lorSensor.Java

        this is the most up to date program.

        Comment


        • #5
          I'm not sure exactly what you are trying to do here, but I think it's this:
          - Turn led on when x is pressed
          - if the sensor detects red - drive for 1/2 second
          - show telemetry for 2-1/2 seconds
          - end autonomous routine

          Assuming that is correct, you have a couple of problems.

          - Enclosing all of the code after waitForStart() in a while loop is typically done for TeleOp. But unless you are using state logic you probably don't want it for Autonomous, which you probably want to be performed top down & then be done.
          - So you might want to delete that outer while loop & move your led logic before the waitForStart()

          Notes added below tell what your current robot is probably doing once red is detected:
          Code:
          if(Color.red(color) > Color.blue(color)){
              while(eTime.time() < .5){}    // do nothing for .5 seconds because {} creates an empty block
          
              left_drive.setPower(-1.0);    // turn your motors on
              right_drive.setPower(1.0);
          
              eTime.reset();
              while(eTime.time() < 2.5){}  // again do nothing for 2.5 seconds, though your motors will continue to run
          
              telemetry.addData("Status", "Run Time: " + runtime.toString());
              telemetry.update();
           }
          Since all of this code is in a loop while(opModeIsActive()) & you do not stop your motors, they will continue to run until the end of Autonomous

          There are different ways to do this, but I think instead you might want something like this:
          Code:
           if(Color.red(color) > Color.blue(color)){
              eTime.reset();
              left_drive.setPower(-1.0);   // turn your motors on
              right_drive.setPower(1.0);
          
              while(eTime.seconds() < .5){}    // wait while motors run for .5 seconds 
          
              left_drive.setPower(0);   // stop motors   
              right_drive.setPower(0);
          
              telemetry.addData("Status", "Run Time: " + runtime.toString());
              telemetry.update();
          
              while(eTime.time() < 2.5){}       // again do nothing while telemetry is displayed for 2.5 seconds
           }

          Comment


          • #6
            Oops - you also need to reset your timer before the 2nd while:
            Originally posted by FLARE View Post
            Code:
            if(Color.red(color) > Color.blue(color)){
               eTime.reset();
               left_drive.setPower(-1.0); // turn your motors on
               right_drive.setPower(1.0);
            
               while(eTime.seconds() < .5){} // wait while motors run for .5 seconds
            
               left_drive.setPower(0); // stop motors
               right_drive.setPower(0);
            
               telemetry.addData("Status", "Run Time: " + runtime.toString());
               telemetry.update();
            
               eTime.reset();
               while(eTime.seconds() < 2.5){} // again do nothing while telemetry is displayed for 2.5 seconds
            }

            Comment


            • #7
              A better way to wait for a certain amount of time, for example 2.5 seconds, is to call sleep(2500). When you call while(eTime.seconds() < 2.5){}, the thread is constantly running through the loop for 2.5 seconds. This is an enormous waste of processor cycles and will slow down all the other threads that are running for the duration of the 2.5 seconds. When you call sleep(), it will tell the thread scheduler that the runOpMode() thread doesn't need to be run for 2.5 seconds so the scheduler can spend that time executing other threads instead.

              Comment

              Working...
              X