Announcement

Collapse
No announcement yet.

Color Sensor Troubles

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

  • dlevy
    replied
    "if (ColorTest.blue()) ////// This is where we are unable to call upon the color sensor"

    Note: recommend that you use lowercase colorTest for variables.

    colorTest.blue() returns an int value that represents the "B" value in RGB.

    So what you can do is look at your telemetry to see the values of the object under examination:

    Code:
    telemetry.addData ("Red",colorTest.red());
    telemetry.addData ("Green",colorTest.green());
    telemetry.addData ("Blue",colorTest.blue());
    And then pass values into the following test methods that you will define:

    Code:
     /**
         * @param r sensor red reading must >= param
         * @param g sensor green reading must <= param
         * @param b sensor blue reading must be <= param
         * @return boolean
         */
        public boolean isRed(int r, int g, int b) {
    
            return (colorTest.red() >= r ) && (colorTest.green() <= g ) && (colorTest.blue() <= b);
    
        }
    
        /**
         * @param r sensor red reading must <= param
         * @param g sensor green reading must >= param
         * @param b sensor blue reading must be <= param
         * @return boolean
         */
        public boolean isGreen(int r, int g, int b) {
    
            return (colorTest.red() <= r) && (colorTest.green() >= g) && (colorTest.blue() <= b);
    
        }
    
        /**
         * @param r sensor red reading must <= param
         * @param g sensor green reading must <= param
         * @param b sensor blue reading must be >= param
         * @return boolean
         */
        public boolean isBlue(int r, int g, int b) {
    
            return (colorTest.red() <= r) && (colorTest.green() <= g) && (colorTest.blue() >= b);
    
        }

    Leave a comment:


  • Mitch Neal
    started a topic Color Sensor Troubles

    Color Sensor Troubles

    package com.qualcomm.ftcrobotcontroller.opmodes;

    import android.graphics.Color;

    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de;
    import com.qualcomm.robotcore.hardware.ColorSensor;
    import com.qualcomm.robotcore.hardware.DcMotor;
    import com.qualcomm.robotcore.hardware.Servo;

    public class AutonomousColorTest extends LinearOpMode {

    DcMotor left_motor;
    DcMotor right_motor;
    Servo ButtonPress;
    ColorSensor ColorTest;

    final double LEFT_POSITION = 0.0;
    final double CENTER_POSITION = 0.5;
    final double RIGHT_POSITION = 1;


    @Override
    public void runOpMode() throws InterruptedException {
    left_motor = hardwareMap.dcMotor.get("left_motor");
    right_motor = hardwareMap.dcMotor.get("right_motor");
    ButtonPress = hardwareMap.servo.get("ButtonPress");
    ColorTest = hardwareMap.colorSensor.get("ColorTest");


    right_motor.setDirection(DcMotor.Direction.REVERSE );

    // Write the reflectance detected to a variable
    ColorTest.enableLed(true);
    ButtonPress.setPosition(CENTER_POSITION);
    boolean bEnabled = true;

    // If the sensor is on the line
    // only the right motor rotates to move it off the line
    waitForStart();

    float hsvValues[] = {0F,0F,0F};
    final float values[] = hsvValues;

    if (ColorTest.blue()) ////// This is where we are unable to call upon the color sensor
    {
    telemetry.addData ("Triggered","YES"); // change value to red/blue
    ButtonPress.setPosition(LEFT_POSITION);

    }
    // Otherwise (if the sensor is off the line)
    // only the left motor rotates to move it back toward the line
    else
    {
    telemetry.addData ("Triggered","NO");
    ButtonPress.setPosition(RIGHT_POSITION); //Button press mechanism shifts over left
    //Press button then drop climbers
    }

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

    telemetry.addData("Clear", ColorTest.alpha());
    telemetry.addData ("Red",ColorTest.red());
    telemetry.addData ("Green",ColorTest.green());
    telemetry.addData ("Blue",ColorTest.blue());
    telemetry.addData ("Hue",hsvValues [0]);



    telemetry.addData("color",ColorTest);

    }
    }




    This is our code. We just want to read a color, and have a servo turn if it is one color and turn a different way if it is a different color but we are having difficulty calling upon the sensor to read its value. Any and all help would be greatly appreciated.
Working...
X