How do I Post to the Game Q&A Forum?

Welcome to the FTC Game Q&A Forum! If this is your first time here, please refer to the Instructions for Forum Use section before posting.

Thank you!

Posts created to sell a product or service are not permitted and will be deleted!

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: 2 LEGO light sensors

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    68

    Question 2 LEGO light sensors

    We are trying to use 2 light sensors to square up on the mountain before climbing.
    We have a single while loop. Whichever light sensor is first in the commands works perfect. However, the second light sensor does not react to inputs to create outputs. We have communication TOboth sensors since the LED is turning on.
    I don't have the code from my Programming Subteam, but any general suggestions in the logic in AS to run parallel sensor input-output commands? Nest the while loop differently? Use if-then-else instead? ???

    Thank you!

  2. #2
    Member
    Join Date
    Aug 2015
    Location
    Tigard, OR
    Posts
    59
    I have done something similar professionally and it will work.
    Simplest version is top right motor when the right sensor is blocked and left motor until the left sensor is blocked.
    More complicated version: Begin slow decel of the motor on the side where the sensor is to a targeted position then when the 2nd sensor is blocked, calculate the decel needed for that motor to stop in the position as the first based on the remaining decel time for the first motor.

    To understand more, I would need to see the code.
    Mark Hancock
    Tigard Team Mentor

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    68
    Here's the portion of the code:


    We are extending "LinearOpmode":

    LightSensor LightSensor;
    LightSensor LightSensor2;
    double reflection = 0;


    LightSensor = hardwareMap.lightSensor.get("ls1");
    LightSensor2 = hardwareMap.lightSensor.get("ls2");
    LightSensor.enableLed(true);
    LightSensor2.enableLed(true);

    // Wait for the start button to be pressed
    waitForStart();
     
    {
    while (LightSensor2.getLightDetected() > 0.4) ;
    {
    motorRight.setPower(0.0);
    }

    while (LightSensor.getLightDetected() > 0.4) ;
    {
    motorLeft.setPower(0.0);
    }

    waitOneFullHardwareCycle();

    We have been using the pushbotirseek example from the sdk as our base for this.

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    68
    Another team in our League is having a slightly similar problem when trying to use two sensors connected to the Core Device Module (using the i2c interface). The problem is the Core Device Module i2c interface is a single channel, and if two connected devices are using the same address, then it only sees one of the devices. Randomly. Having two of the same sensors from the same vendor guaranteed they would have the same address.

  5. #5
    Senior Member
    Join Date
    Oct 2013
    Location
    Oregon
    Posts
    200
    Be Aware there are some Bugs/issues with the NXT light sensors if your using those...

    http://ftcforum.usfirst.org/showthre...ectected%28%29

    not sure but your logic seems to me to say

    while the right sensor is reading bright turn the right motor power to zero, then
    while the left sensor is reading bright, turn the leftmotor power to zero.

    So assuming you start with power to both motors,
    the right motor should turn off first when you see white then the left when you see white. always tough with just snippets of code.


    you might try something like

    turn on motors
    while left is dark and right is dark
    {
    if left is bright, turn off left motor
    if right is bright, turn off right motor
    }

    turn off both motors (just in case - the loop may exit before the off command inside)

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    68
    korimako, thanks for the reply - I was starting to think it was a logic issue with while vs. if/then/else; I think you may have confirmed my thoughts; we'll try it tonight!

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    68
    OK, so we thought we were getting closer, but it didn't pan out. Here's the full code for "constructive criticisms":


    //incomplete

    package com.qualcomm.ftcrobotcontroller.opmodes;

    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de;
    import com.qualcomm.robotcore.hardware.DcMotor;
    import com.qualcomm.robotcore.hardware.DcMotorController;
    import com.qualcomm.robotcore.hardware.LightSensor;

    public class AutoLightTest extends LinearOpMode {

    DcMotorController.DeviceMode devMode;
    DcMotorController wheelController;
    DcMotor motorLeft;
    DcMotor motorRight;
    DcMotorController wheelController2;
    DcMotor UDMotor;
    LightSensor lightSensor;
    LightSensor lightSensor2;
    double reflection = 0;

    @Override
    public void runOpMode() throws InterruptedException {
    //setup the left and right motors from the configuration file
    wheelController = hardwareMap.dcMotorController.get("mc1");
    wheelController2 = hardwareMap.dcMotorController.get("mc3");
    motorLeft = hardwareMap.dcMotor.get("motor1");
    motorRight = hardwareMap.dcMotor.get("motor2");
    UDMotor = hardwareMap.dcMotor.get("motor3");
    lightSensor = hardwareMap.lightSensor.get("ls1");
    lightSensor2 = hardwareMap.lightSensor.get("ls2");
    lightSensor.enableLed(true);
    lightSensor2.enableLed(true);

    //reverse the right side motor
    motorRight.setDirection(DcMotor.Direction.REVERSE) ;
    //Used for setting NXT to right mode
    devMode = DcMotorController.DeviceMode.WRITE_ONLY;

    // Wait for the start button to be pressed
    waitForStart();




    //Set the UDMotor to move the shaft up c o m p l e t e l y
    //UDMotor.setPower(-0.5);

    //Go for 2.5 seconds
    //sleep(2500);

    //Set the motors to drive the robot forward (r. 2 tiles)
    //motorLeft.setPower(0.5);
    ///motorRight.setPower(0.5);

    //Go fwd for 1.5 seconds
    //sleep(1500);

    //Set the motors to turn the robot left (45 deg.)
    //motorLeft.setPower(-0.5);
    //motorRight.setPower(0.5);

    //Go left for 0.564 seconds
    // sleep(564);

    //Set the motors to drive the robot forward (r. 3 tiles)
    // motorLeft.setPower(0.5);
    // motorRight.setPower(0.5);

    //Go fwd for 2.0 seconds
    //sleep(2000);

    //Set the motors to turn the robot 1.5 (90 deg.)
    //motorLeft.setPower (0.5);
    // motorRight.setPower(-0.5);

    //Turn backwards for 1.02 seconds Original - 1.0, 1.25, 1.18, 1.075, 1.07, 1.067, 1.05 second(s)
    // sleep(1020);

    //Set the motors to back up the robot
    motorLeft.setPower (-0.3);
    motorRight.setPower(-0.3);

    //Go for 1.02 seconds Original - 1.0, 1.25, 1.18, 1.075, 1.07, 1.067, 1.05 second(s)
    //sleep(10000);
    //When light is detected drive fwd for 1 sec
    while (lightSensor.getLightDetected() < 0.6);
    while (lightSensor2.getLightDetected() < 0.6);

    {
    if (lightSensor.getLightDetected() > 0.6);
    motorLeft.setPower(0.0);
    motorRight.setPower(-0.7);

    if (lightSensor2.getLightDetected() > 0.6);
    motorLeft.setPower(-0.7);
    motorRight.setPower(0.0);

    if (lightSensor == lightSensor2 ) ;
    motorLeft.setPower(-0.5);
    motorRight.setPower(-0.5);
    }
    sleep(6000);
    motorLeft.setPower(0.0);
    motorRight.setPower(0.0);
    waitOneFullHardwareCycle();

    //Set the motors to drive backwards for 2 sec
    //motorLeft.setPower (-0.2);
    // motorRight.setPower(-0.2);

    //sleep(7500);


    //Set the motors to drive the robot "backwards" (5 seconds)
    //motorLeft.setPower(-0.5);
    //motorRight.setPower(-0.5);
    //UDMotor.setPower(0.0);

    //////Go up the mountain for 5 seconds
    //sleep(6000);

    //Stop the robot
    //motorLeft.setPower(0);
    //motorRight.setPower(0);

    // NOTE: 1 Tile is about 1 second
    // NOTE: 90 deg. turn is about 1.1 seconds
    // NOTE: 45 deg. turn is about 0.55 second
    }

    }

  8. #8
    Senior Member
    Join Date
    Oct 2013
    Location
    Oregon
    Posts
    200
    Quote Originally Posted by korimako View Post
    turn on motors
    while left is dark and right is dark
    should have been "while left is dark OR right is dark"

    for the logic to work.

    eg while ((sensorL <0.6 ) || (sensorR <0.6)) (I'm not a java syntax expert)

    It seems unlikely that Sensor1 will ever exactly equal Sensor2... If you want to compare them, better to make sure they're close instead.

    After the while loop exits then turn on the motors. The loop should exit when both read bright, this way you shouldn't have to compare them. (assuming that your left motors won't drive the right side forward if they're off.)

  9. #9
    Senior Member
    Join Date
    May 2015
    Location
    Reston, VA
    Posts
    557
    Quote Originally Posted by FTC0839 View Post
    OK, so we thought we were getting closer, but it didn't pan out. Here's the full code for "constructive criticisms":
    Most of it is commented out.

    One thing that stands out is :

    while (lightSensor.getLightDetected() < 0.6);
    while (lightSensor2.getLightDetected() < 0.6);
    {....

  10. #10
    Senior Member
    Join Date
    May 2015
    Location
    Reston, VA
    Posts
    557
    Quote Originally Posted by FTC0839 View Post
    Here's the portion of the code:


    We are extending "LinearOpmode":

    LightSensor LightSensor;
    LightSensor LightSensor2;
    double reflection = 0;


    LightSensor = hardwareMap.lightSensor.get("ls1");
    LightSensor2 = hardwareMap.lightSensor.get("ls2");
    LightSensor.enableLed(true);
    LightSensor2.enableLed(true);

    // Wait for the start button to be pressed
    waitForStart();
     
    {
    while (LightSensor2.getLightDetected() > 0.4) ;
    {
    motorRight.setPower(0.0);
    }

    while (LightSensor.getLightDetected() > 0.4) ;
    {
    motorLeft.setPower(0.0);
    }

    waitOneFullHardwareCycle();

    We have been using the pushbotirseek example from the sdk as our base for this.
    My suggestions for your first posting:

    Code:
    LightSensor LightSensor;
    LightSensor LightSensor2;
    double reflection = 0;
     
     
    LightSensor = hardwareMap.lightSensor.get("ls1");
    LightSensor2 = hardwareMap.lightSensor.get("ls2");
    LightSensor.enableLed(true);
    LightSensor2.enableLed(true);
     
    // Wait for the start button to be pressed
    waitForStart();
     
    while (opModeIsActive()) {
    
       if (LightSensor2.getLightDetected() > 0.4)  {
            motorRight.setPower(0.0);
       }
     
       if (LightSensor.getLightDetected() > 0.4)  {
             motorLeft.setPower(0.0);
       }
     
       waitOneFullHardwareCycle();
    }

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •