Announcement

Collapse
No announcement yet.

Colour sensor in Autonomous...

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

  • Colour sensor in Autonomous...

    Hi,so we have an autonomous program but we need to include a bit for the colour sensor, and unfortunately none of us programmers know how to do that. Our program is simple and all the samples we've found are a little too complex for us to completely understand and they all seem to be manual. All we need the sensor to do is to detect blue or red and to move a certain servo based on colour.
    [CODE
    package org.firstinspires.ftc.robotcontroller.external.sam ples;
    import android.app.Activity;
    import android.graphics.Color;
    import android.view.View;

    import com.qualcomm.ftcrobotcontroller.R;
    import com.qualcomm.robotcore.eventloop.opmode.Autonomous ;
    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de;
    import com.qualcomm.robotcore.hardware.ColorSensor;
    import com.qualcomm.robotcore.hardware.DcMotor;



    @Autonomous(name = "Autonomous_Main", group = "Test Programs")
    public class MainAutonomous extends LinearOpMode {
    DcMotor topRight;
    DcMotor topLeft;
    DcMotor botRight;
    DcMotor botLeft;
    DcMotor Ball;
    ColorSensor colorSensor;


    public void hardwareMapping() {
    topRight = hardwareMap.dcMotor.get("top_R");
    topLeft = hardwareMap.dcMotor.get("top_L");
    botRight = hardwareMap.dcMotor.get("bot_R");
    botLeft = hardwareMap.dcMotor.get("bot_L");
    Ball = hardwareMap.dcMotor.get("ball_arm");
    colorSensor = hardwareMap.colorSensor.get("Color");
    topLeft.setDirection(DcMotor.Direction.REVERSE);
    botLeft.setDirection(DcMotor.Direction.REVERSE);
    }

    public void driveForward(long forward) throws InterruptedException {
    topRight.setPower(-1);
    botRight.setPower(-1);
    topLeft.setPower(-1);
    botLeft.setPower(-1);

    sleep(forward * 1000);
    topRight.setPower(0);
    botRight.setPower(0);
    topLeft.setPower(0);
    botLeft.setPower(0);
    }

    public void driveBackward(long backward) throws InterruptedException {
    topRight.setPower(1);
    botRight.setPower(1);
    topLeft.setPower(1);
    botLeft.setPower(1);
    sleep(backward * 1000);
    topRight.setPower(0);
    botRight.setPower(0);
    topLeft.setPower(0);
    botLeft.setPower(0);
    }

    public void turnLeft(long turnLeft) throws InterruptedException {

    topRight.setPower(1);
    botRight.setPower(1);
    topLeft.setPower(0);
    botLeft.setPower(0);
    sleep(turnLeft * 1000);
    topRight.setPower(0);
    botRight.setPower(0);
    topLeft.setPower(0);
    botLeft.setPower(0);
    }

    public void turnRight(long turnRight) throws InterruptedException {

    topRight.setPower(0);
    botRight.setPower(0);
    topLeft.setPower(1);
    botLeft.setPower(1);
    sleep(turnRight * 1000);
    }

    public void stopMotors() {
    topRight.setPower(0);
    botRight.setPower(0);
    topLeft.setPower(0);
    botLeft.setPower(0);
    }

    public void catapult() throws InterruptedException {
    Ball.setPower(1);
    sleep(1000);
    Ball.setPower(0);
    }

    @Override
    public void runOpMode()throws InterruptedException {


    hardwareMapping();
    if (opModeIsActive()) {
    catapult();
    driveForward(1);
    turnLeft(1);
    driveForward(2);
    driveBackward(1);
    catapult();
    driveForward(1);
    driveBackward(3);
    stopMotors();
    }
    stopMotors();
    }
    }



    ] [/CODE]

  • #2
    I believe if you want to , you should do, [code] if(colorSensor.red()) {
    "move servo position here"
    }

    or

    if(ColorSensor = colorsensor.red()){
    "move servo here"
    }
    [code]


    Hope this helps

    Comment


    • #3
      You can view,some simple sample code here: https://github.com/bchay/ftc_app/blo...ensorTest.java

      To use a color sensor, create a ColorSensor object and set it to hardwareMap.colorSensor.get("name"); You can get the RGB values with cs.red() / green / blue. These can be converted to HSV for more accuracy using Color.RGBToHSV(cs.red() * 8, cs.green() * 8, cs.blue() * 8, HSVArray); The hue value can then be used to find the color, as shown in the getColorName() method in the code that I linked above.
      Lead programmer for team 6287, Vertigo

      Comment


      • #4
        The first example "if(colorSensor.red())" won't work. colorSensor.red() returns an integer and what's in the parentheses needs to be a boolean.

        The second example I don't think is legal either.

        The most basic would be

        In the same area where you name your motors put this -
        ColorSensor myNameForColorSensor;

        Put this with other hardware lines -
        myNameForColorSensor = hardwareMap.colorSensor.get("my name in config file for color sensor");

        In your program when you're in front of the beacon -

        if (myNameForColorSensor.red() > 5){
        // servo commands here
        }

        The 5 here is just an example number. You need to determine a value based on what you see the color sensor reports when testing.

        Comment


        • #5
          So we entered that in an If Else statement and there is an error saying the first bracket in the else statement needs to be replaced with a ";"...I checked and the brackets seem to be in place...
          Code:
            public void buttonPusher() throws InterruptedException {
                  if (colorSensor.red() >= 8) {
                      leftButton.setPosition(2);
                  }
                  else (colorSensor.blue() <= 3){
                      rightButton.setPosition(2);
                  }
              }

          Comment


          • #6
            You need an if after the else since you have another condition. Then it does nothing if red <8 and blue >3

            If you just wanted to push the rightButton anytime you don't push the red then

            else {
            rightButton.setPosition(2);
            }

            https://docs.oracle.com/javase/tutor...dbolts/if.html

            Comment


            • #7
              The color values you will get range from 0-255, so you would need to check for much greater numbers than 8 or 3. Pure red has a value of (255, 0, 0) and pure blue is (0, 0, 255). For your second condition, you would need to check if the blue value is greater than some number, and not less than. If you decide to go with the if/else rather than the if/else if statements, testing for just the red value will not work accurately. There are many colors with values for both red and blue that are high, such as white (255, 255, 255). If your color sensor read the white part of the beacon rather than the colored part, it would always return red instead of unknown, which would mess up the autonomous. Using HSV is completely legal, and works much better than only using the RGB values.
              Lead programmer for team 6287, Vertigo

              Comment


              • #8
                Just to chime in: We use a color sensor + servo, and here's the code if you're interested:
                Code:
                if(beacon.red() > 3) {
                            buttonPusher.setPosition(1);
                
                            sleep(2000);
                
                            buttonPusher.setPosition(.5);
                
                            sleep(500);
                        } else {
                            buttonPusher.setPosition(0);
                
                            sleep(2000);
                
                            buttonPusher.setPosition(.5);
                
                            sleep(500);
                        }
                You'll have to write another code for blue side, and you'll have to change the names of your color sensor/servo objects. You may also need to change the value that it decides the beacon color at, ours is greater than three, but depending on distance from the beacon it could be different.

                Comment

                Working...
                X