Announcement

Collapse
No announcement yet.

How to use a color sensor in an Autonomous Program?

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

  • How to use a color sensor in an Autonomous Program?

    Hi Everyone!
    I am the head programmer for the team Fantom-234, and I've never used color sensors before in autonomous programming. I have been researching and asked people for help but I am at a standstill since I've supposedly set the color sensor to pick up either blue or red and I used an if / else statement to make the robot move forwards or backward in accordance with whatever color it picks up. But, it's not able to pick up a color for some reason and I do not understand why, can anyone please check my code with what isn't working? Thanks!
    The program is supposed to put an arm down, pick up a color, move accordingly and then stop
    package org.firstinspires.ftc.teamcode; 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; import com.qualcomm.robotcore.hardware.DcMotorSimple; import com.qualcomm.robotcore.hardware.Servo; import com.qualcomm.robotcore.util.ElapsedTime; /** * Created by Dravid-C on 1/14/2018. */ @Autonomous(name= "colorsensor 2") public class colorsensor_2 extends LinearOpMode{ public DcMotor leftDrive, rightDrive; public ColorSensor colorSensor; public Servo leftJewel, rightJewel, leftServo, rightServo; @Override public void runOpMode() { telemetry.addData("Status", "Initialized"); telemetry.update(); leftDrive = hardwareMap.dcMotor.get("left_drive"); rightDrive = hardwareMap.dcMotor.get("right_drive"); leftServo = hardwareMap.servo.get("left_arm"); rightServo = hardwareMap.servo.get("right_arm"); leftJewel = hardwareMap.servo.get("left_jewel"); rightJewel = hardwareMap.servo.get("right_jewel"); colorSensor = hardwareMap.colorSensor.get("color_sensor"); leftDrive.setDirection(DcMotorSimple.Direction.FOR WARD); rightDrive.setDirection(DcMotorSimple.Direction.RE VERSE); int blue = colorSensor.blue(); int red = colorSensor.red(); waitForStart(); leftDrive.setPower(0.0); rightDrive.setPower(0.0); leftServo.setPosition(0.0); rightServo.setPosition(1.0); leftJewel.setPosition(1.0); rightJewel.setPosition(0.0); ElapsedTime eTime = new ElapsedTime(); eTime.reset(); while (eTime.time() < 0.0) { leftDrive.setPower(0.0); rightDrive.setPower(0.0); leftServo.setPosition(0.0); rightServo.setPosition(1.0); leftJewel.setPosition(1.0); rightJewel.setPosition(0.0); } while(eTime.time()<1.0) { leftDrive.setPower(0.0); rightDrive.setPower(0.0); leftServo.setPosition(0.0); rightServo.setPosition(1.0); leftJewel.setPosition(1.0); rightJewel.setPosition(0.0); } while(eTime.time()<2.0) { if (blue > red ) { leftDrive.setPower(1.0); rightDrive.setPower(1.0); } else if (red > blue) { leftDrive.setPower(-1.0); rightDrive.setPower(-1.0); } } while(eTime.time() < 3.0) { leftDrive.setPower(0.0); rightDrive.setPower(0.0); leftServo.setPosition(0.0); rightServo.setPosition(1.0); leftJewel.setPosition(1.0); rightJewel.setPosition(1.0); } } } Thanks again!

  • #2
    I guess the code wasn't applied in correctly so this is it
    Code:
     package org.firstinspires.ftc.teamcode;  import com.qualcomm.robotcore.eventloop.opmode.Autonomous;  import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;  import com.qualcomm.robotcore.hardware.ColorSensor;  import com.qualcomm.robotcore.hardware.DcMotor;  import com.qualcomm.robotcore.hardware.DcMotorSimple;  import com.qualcomm.robotcore.hardware.Servo;  import com.qualcomm.robotcore.util.ElapsedTime;   @Autonomous(name= "colorsensor 2")  public class colorsensor_2 extends LinearOpMode{  public DcMotor leftDrive, rightDrive; public ColorSensor colorSensor;  public Servo leftJewel, rightJewel, leftServo, rightServo;   @Override public void runOpMode() {  telemetry.addData("Status", "Initialized");  telemetry.update();  leftDrive = hardwareMap.dcMotor.get("left_drive");  rightDrive = hardwareMap.dcMotor.get("right_drive");  leftServo = hardwareMap.servo.get("left_arm");  rightServo = hardwareMap.servo.get("right_arm");  leftJewel = hardwareMap.servo.get("left_jewel");  rightJewel = hardwareMap.servo.get("right_jewel");  colorSensor = hardwareMap.colorSensor.get("color_sensor");  leftDrive.setDirection(DcMotorSimple.Direction.FORWARD);  rightDrive.setDirection(DcMotorSimple.Direction.REVERSE);   int blue = colorSensor.blue();  int red = colorSensor.red();   waitForStart();  leftDrive.setPower(0.0);  rightDrive.setPower(0.0);  leftServo.setPosition(0.0);  rightServo.setPosition(1.0);  leftJewel.setPosition(1.0);  rightJewel.setPosition(0.0);  ElapsedTime eTime = new ElapsedTime();  eTime.reset();   while (eTime.time() < 0.0) {  leftDrive.setPower(0.0);  rightDrive.setPower(0.0);  leftServo.setPosition(0.0);  rightServo.setPosition(1.0);  leftJewel.setPosition(1.0);  rightJewel.setPosition(0.0);  }   while(eTime.time()<1.0) {  leftDrive.setPower(0.0);  rightDrive.setPower(0.0);  leftServo.setPosition(0.0);  rightServo.setPosition(1.0);  leftJewel.setPosition(1.0);  rightJewel.setPosition(0.0);  }   while(eTime.time()<2.0) {  if (blue > red ) {  leftDrive.setPower(1.0);  rightDrive.setPower(1.0);  } else if (red > blue) {  leftDrive.setPower(-1.0); rightDrive.setPower(-1.0);      }  }  while(eTime.time() < 3.0) {  leftDrive.setPower(0.0);  rightDrive.setPower(0.0);  leftServo.setPosition(0.0);  rightServo.setPosition(1.0);  leftJewel.setPosition(1.0);  rightJewel.setPosition(1.0);  }  }  }

    Comment


    • #3
      Ahhh I don't know how to put the code correctly but that's it

      Comment


      • #4
        Paste your code into wordpad then copy it here.

        You read the color sensor once before wait for start, so red and blue contain whatever the color sensor saw at that time. You need to read it when the color sensor is in position near the jewel.


        Code:
         public DcMotor leftDrive, rightDrive; 
        public ColorSensor colorSensor; 
        public Servo leftJewel, rightJewel, leftServo, rightServo; 
        @Override public void runOpMode() { 
        telemetry.addData("Status", "Initialized"); 
        telemetry.update(); 
        leftDrive = hardwareMap.dcMotor.get("left_drive"); 
        rightDrive = hardwareMap.dcMotor.get("right_drive"); 
        leftServo = hardwareMap.servo.get("left_arm"); 
        rightServo = hardwareMap.servo.get("right_arm"); 
        leftJewel = hardwareMap.servo.get("left_jewel"); 
        rightJewel = hardwareMap.servo.get("right_jewel"); 
        colorSensor = hardwareMap.colorSensor.get("color_sensor"); leftDrive.setDirection(DcMotorSimple.Direction.FOR WARD); rightDrive.setDirection(DcMotorSimple.Direction.REVERSE); 
        int blue = colorSensor.blue(); 
        int red = colorSensor.red(); 
        waitForStart(); 
        leftDrive.setPower(0.0); 
        rightDrive.setPower(0.0); 
        leftServo.setPosition(0.0); 
        rightServo.setPosition(1.0); 
        leftJewel.setPosition(1.0);
         rightJewel.setPosition(0.0); 
        ElapsedTime eTime = new ElapsedTime(); eTime.reset(); 
        while (eTime.time() < 0.0) { 
        leftDrive.setPower(0.0); 
        rightDrive.setPower(0.0); 
        leftServo.setPosition(0.0); 
        rightServo.setPosition(1.0); 
        leftJewel.setPosition(1.0); 
        rightJewel.setPosition(0.0);
         } 
        while(eTime.time()<1.0) { 
        leftDrive.setPower(0.0); 
        rightDrive.setPower(0.0); 
        leftServo.setPosition(0.0); 
        rightServo.setPosition(1.0); 
        leftJewel.setPosition(1.0); 
        rightJewel.setPosition(0.0); 
        } 
        while(eTime.time()<2.0) { 
        if (blue > red ) { 
        leftDrive.setPower(1.0); 
        rightDrive.setPower(1.0); 
        } else if (red > blue) { 
        leftDrive.setPower(-1.0); 
        rightDrive.setPower(-1.0);
         } } 
        while(eTime.time() < 3.0) { 
        leftDrive.setPower(0.0); 
        rightDrive.setPower(0.0); 
        leftServo.setPosition(0.0); 
        rightServo.setPosition(1.0); 
        leftJewel.setPosition(1.0); 
        rightJewel.setPosition(1.0); } }

        Comment


        • #5
          How is the color sensor taking a reading before waitforStart?

          Comment


          • #6

            These are the statements that acquire data from the color sensor -
            int blue = colorSensor.blue(); int red = colorSensor.red(); You need to run these each time you want a fresh reading from the color sensor.

            Comment


            • #7
              Ohhhhhh okay that makes sense, so should i put it in the while loop or in each if/else statement?

              Comment


              • #8
                Not for the way your program works. If you put it into the while loop once the robot starts to move and gets far enough away from a jewel what it reads could change and it could then move in the other direction knocking the other jewel off. You want to take a reading just before outside of the while loop. Then it won't change in the loop and the robot will continue in the chosen direction.

                Comment


                • #9
                  Oh okay, I will test that out. Thank you so much!

                  Comment


                  • #10
                    Okay so I don't understand if the sensor itself isn't working because I tested it in a teleop program and it was fine but in autonomous, the arm will go down, nothing will happen, and then it will just stop as the program is meant to do.

                    Comment


                    • #11
                      Put in some telemetry addata and update statements in each while loop. Identify which loop you're in, what the color sensor reads in the ifs.

                      Comment


                      • #12
                        How do you add in telemetry statements for the color sensor, sorry this is my first year working with FTC.

                        Comment


                        • #13
                          Actually you want to know what blue and red are -
                          In each true block of the ifs -

                          telemetry.addData("red ", red);
                          telemetry.addData("blue ", blue);
                          telemetry.update();

                          Comment

                          Working...
                          X