Announcement

Collapse
No announcement yet.

Color Sensor HELP

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

  • Color Sensor HELP

    We are trying to use the color sensor to find the sky stone and we can't seem to get the code right at the (if) statements. Here is our code.

    /* Copyright (c) 2017 FIRST. All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without modification,
    * are permitted (subject to the limitations in the disclaimer below) provided that
    * the following conditions are met:
    *
    * Redistributions of source code must retain the above copyright notice, this list
    * of conditions and the following disclaimer.
    *
    * Redistributions in binary form must reproduce the above copyright notice, this
    * list of conditions and the following disclaimer in the documentation and/or
    * other materials provided with the distribution.
    *
    * Neither the name of FIRST nor the names of its contributors may be used to endorse or
    * promote products derived from this software without specific prior written permission.
    *
    * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
    * LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
    * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    */

    package org.firstinspires.ftc.teamcode;

    import android.app.Activity;
    import com.qualcomm.robotcore.eventloop.opmode.Autonomous ;
    import android.graphics.Color;
    import android.view.View;

    import com.qualcomm.robotcore.eventloop.opmode.Disabled;
    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de;
    import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
    import com.qualcomm.robotcore.hardware.NormalizedColorSen sor;
    import com.qualcomm.robotcore.hardware.NormalizedRGBA;
    import com.qualcomm.robotcore.hardware.SwitchableLight;

    import com.qualcomm.robotcore.util.Hardware;
    import com.qualcomm.robotcore.hardware.DcMotor;
    import com.qualcomm.robotcore.hardware.DistanceSensor;
    import com.qualcomm.robotcore.util.ElapsedTime;

    /*
    * This is an example LinearOpMode that shows how to use a color sensor in a generic
    * way, insensitive which particular make or model of color sensor is used. The opmode
    * assumes that the color sensor is configured with a name of "sensor_color".
    *
    * If the color sensor has a light which is controllable, you can use the X button on
    * the gamepad to toggle the light on and off.
    *
    * Use Android Studio to Copy this Class, and Paste it into your team's code folder with a new name.
    * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
    */
    @Autonomous(name = "Tom", group = "Pushbot")

    public class Tom extends LinearOpMode {

    HardwarePushbot robot = new HardwarePushbot();
    private ElapsedTime runtime = new ElapsedTime();

    NormalizedColorSensor colorSensor;
    View relativeLayout;

    /**
    * The runOpMode() method is the root of this LinearOpMode, as it is in all linear opModes.
    * Our implementation here, though is a bit unusual: we've decided to put all the actual work
    * in the main() method rather than directly in runOpMode() itself. The reason we do that is that
    * in this sample we're changing the background color of the robot controller screen as the
    * opmode runs, and we want to be able to *guarantee* that we restore it to something reasonable
    * and palatable when the opMode ends. The simplest way to do that is to use a try...finally
    * block around the main, core logic, and an easy way to make that all clear was to separate
    * the former from the latter in separate methods.
    */
    @Override
    public void runOpMode() throws InterruptedException {

    robot.init(hardwareMap);

    // Send telemetry message to signify robot waiting;
    telemetry.addData("Status", "Ready to run");
    telemetry.update();
    // Wait for the game to start (driver presses PLAY)
    while (!opModeIsActive()&&!isStopRequested())
    {telemetry.addData("Status","Still Waiting in Init");
    telemetry.update();}

    //Set encoder distances
    int initLeftCount = robot.left_drive.getCurrentPosition();
    int tgLeftCount = initLeftCount + 89;
    int initRightCount = robot.right_drive.getCurrentPosition();
    int tgRightCount = initRightCount + 89;

    sleep (1000);

    telemetry.addData("Status", "step Two Done");
    telemetry.update();

    robot.left_drive.setPower(0);
    robot.right_drive.setPower(0);

    robot.left_drive.setMode(DcMotor.RunMode.STOP_AND_ RESET_ENCODER);
    robot.right_drive.setMode(DcMotor.RunMode.STOP_AND _RESET_ENCODER);

    telemetry.addData("Status", "step Three Done");
    telemetry.update();

    robot.left_drive.setMode(DcMotor.RunMode.RUN_USING _ENCODER);
    robot.right_drive.setMode(DcMotor.RunMode.RUN_USIN G_ENCODER);

    robot.left_drive.setPower(0.3);
    robot.right_drive.setPower(0.3);

    while (opModeIsActive() && (robot.left_drive.getCurrentPosition() < tgLeftCount * 15)) ;
    while (opModeIsActive() && (robot.right_drive.getCurrentPosition() < tgRightCount * 15 )) ;

    robot.left_drive.setMode(DcMotor.RunMode.STOP_AND_ RESET_ENCODER);
    robot.right_drive.setMode(DcMotor.RunMode.STOP_AND _RESET_ENCODER);

    robot.left_drive.setPower(0);
    robot.right_drive.setPower(0);

    telemetry.addData("Status", "step Four Done");
    telemetry.update();

    sleep(1000);



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

    try {
    runSample(); // actually execute the sample
    } finally {

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

    protected void runSample() throws InterruptedException {

    // values is a reference to the hsvValues array.
    float[] hsvValues = new float[3];
    final float values[] = hsvValues;

    // bPrevState and bCurrState keep track of the previous and current state of the button
    boolean bPrevState = false;
    boolean bCurrState = false;

    // Get a reference to our sensor object.
    colorSensor = hardwareMap.get(NormalizedColorSensor.class, "sensor_color");

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


    waitForStart();

    while (opModeIsActive()) {

    bCurrState = gamepad1.x;


    if (bCurrState != bPrevState) {
    if (bCurrState) {
    if (colorSensor instanceof SwitchableLight) {
    SwitchableLight light = (SwitchableLight)colorSensor;
    light.enableLight(!light.isLightOn());
    }
    }
    }
    bPrevState = bCurrState;

    // Read the sensor
    NormalizedRGBA colors = colorSensor.getNormalizedColors();

    Color.colorToHSV(colors.toColor(), hsvValues);

    telemetry.addLine()
    //.addData("a", "%.3f", colors.alpha)
    //.addData("r", "%.3f", colors.red)
    //.addData("g", "%.3f", colors.green)
    .addData("b", "%.3f", colors.blue);

    int color = colors.toColor();

    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("a", "%02x", Color.alpha(color))
    //.addData("r", "%02x", Color.red(color))
    //.addData("g", "%02x", Color.green(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 (colorSensor = 0)
    {robot.left_servo.setPosition(0.7);}
    sleep(1000);

    if (colorSensor > 0)
    robot.left_drive.setPower(0);
    robot.right_drive.setPower(0);

    robot.left_drive.setMode(DcMotor.RunMode.STOP_AND_ RESET_ENCODER);
    robot.right_drive.setMode(DcMotor.RunMode.STOP_AND _RESET_ENCODER);

    telemetry.addData("Status", "step Three Done");
    telemetry.update();

    robot.left_drive.setMode(DcMotor.RunMode.RUN_USING _ENCODER);
    robot.right_drive.setMode(DcMotor.RunMode.RUN_USIN G_ENCODER);

    robot.left_drive.setPower(-0.3);
    robot.right_drive.setPower(-0.3);

    while (opModeIsActive() && (robot.left_drive.getCurrentPosition() > tgLeftCount * -2)) ;
    while (opModeIsActive() && (robot.right_drive.getCurrentPosition() > tgRightCount * -2 )) ;

    robot.left_drive.setMode(DcMotor.RunMode.STOP_AND_ RESET_ENCODER);
    robot.right_drive.setMode(DcMotor.RunMode.STOP_AND _RESET_ENCODER);

    robot.left_drive.setPower(0);
    robot.right_drive.setPower(0);}



    }


    Here are the errors it is saying we have.

    org/firstinspires/ftc/teamcode/Tom.java line 219, column 26: ERROR: incompatible types: int cannot be converted to com.qualcomm.robotcore.hardware.NormalizedColorSen sor org/firstinspires/ftc/teamcode/Tom.java line 219, column 23: ERROR: incompatible types: com.qualcomm.robotcore.hardware.NormalizedColorSen sor cannot be converted to boolean org/firstinspires/ftc/teamcode/Tom.java line 223, column 22: ERROR: bad operand types for binary operator '>' first type: com.qualcomm.robotcore.hardware.NormalizedColorSen sor second type: int org/firstinspires/ftc/teamcode/Tom.java line 239, column 77: ERROR: cannot find symbol symbol: variable tgLeftCount location: class org.firstinspires.ftc.teamcode.Tom org/firstinspires/ftc/teamcode/Tom.java line 240, column 78: ERROR: cannot find symbol symbol: variable tgRightCount location: class org.firstinspires.ftc.teamcode.Tom

  • #2
    if (colorSensor = 0)
    {robot.left_servo.setPosition(0.7);}
    sleep(1000);

    if (colorSensor > 0)
    robot.left_drive.setPower(0);
    robot.right_drive.setPower(0);
    From what I can see colorSensor is a NormalizedColorSensor, not an int, so you can't just check if it's equal to 0 or greater than 0. Even if it was, you accidentally did a single equals sign in the if instead of a double equals sign for comparisons.

    Comment

    Working...
    X