Announcement

Collapse
No announcement yet.

Trying new TeleOp... Programming questions

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

  • Trying new TeleOp... Programming questions

    Hi! I am trying a slightly different type of TeleOp from the given K9 teleop on Android Studios. I'm just trying some things out, because our team has some plans with our teleop, and I feel this sort of logic will be easier to manipulate when we get there. This is the main portion of the code:


    public void loop() {
    double right = 0.0;
    double left = 0.0;

    double LeftYValue = -gamepad1.left_stick_y;
    double RightXValue = gamepad1.right_stick_x;

    while(LeftYValue >= -1 && LeftYValue <= 1) {
    left = LeftYValue;
    right = LeftYValue;
    break;
    }

    while(RightXValue >= -1 && RightXValue <= 1) {
    left = RightXValue;
    right = -RightXValue;
    break;
    }

    right = Range.clip(right, -1, 1);
    left = Range.clip(left, -1, 1);
    right = (float)scaleInput(right);
    left = (float)scaleInput(left);

    motorRight.setPower(left);
    motorLeft.setPower(right);

    telemetry.addData("Text", "*** Robot Data***");

    telemetry.addData("left tgt pwr", "left pwr: " + String.format("%.2f", left));
    telemetry.addData("right tgt pwr", "right pwr: " + String.format("%.2f", right));

    }


    So basically, the robot should just move forward and turn. When it is executed, however, the robot only runs the second while loop. The robot only turns, and does not move straight. Each loop runs individually, but when put together, only one works. I tried many versions of this, including if/else and not having a conditional statement at all, but none seem to work. We used code similar to this in RobotC last year, and it worked perfectly fine. If anyone has any suggestions, please let me know! Thanks!

  • #2
    First off, the stick values are always between -1 and 1, so the while statements are always true. (Aside: Why are you using while structures with breaks in them? They should just be if statements if you want the lines inside the braces {} to occur once.) What are you trying to accomplish with the while statements?

    So your code basically does this:

    [CODE]
    double right = 0.0;
    double left = 0.0;

    double LeftYValue = -gamepad1.left_stick_y;
    double RightXValue = gamepad1.right_stick_x;

    // "while" always called
    left = LeftYValue;
    right = LeftYValue;

    // "while" always called
    left = RightXValue;
    right = -RightXValue;


    right = Range.clip(right, -1, 1);
    left = Range.clip(left, -1, 1);
    right = (float)scaleInput(right);
    left = (float)scaleInput(left);

    motorRight.setPower(left);
    motorLeft.setPower(right);
    [\CODE]

    If you look, the second "while" just overwrites what you wrote in the first "while" so you only get the turning behavior.

    I think you probably want the lines in the second "while" to be replaced with the following.
    [CODE]
    left = left + RightXValue; // or use +=
    right = right - RightXValue; // or use -=
    [\CODE]

    Comment


    • #3
      I recognized that the conditions were always true, but never made the connection that the values for left and right would be over written.
      Wouldn't this still occur with the replaced lines? Also, would it help if I added "&& !=0" In the condition?
      Thanks for the help! I'm new to Java and Android Studios.

      Comment


      • #4
        left = RightXValue;

        The above line just blows away whatever was in left and replaces it with RightXValue;


        left = left + RightXValue;

        The above line replaces left with "left + RightXValue" so it essentially adds RightXValue to what was in left before. It's not the same.


        BTW, you could probably just replace the two different lines setting left with the single line:

        left = LeftXValue + RightYValue;

        (and similar statements hold for the lines related to right)

        Comment


        • #5
          Our goal was to add a third "condition" to the previous code which would allow the robot to use mecanum wheels, and move side to side. Any ideas on how to do that with this format? This is similar to the throttle/direction from the K9 teleop.

          Comment


          • #6
            If I correctly read the following document: http://thinktank.wpi.edu/resources/3...canumDrive.pdf. Then Mec Drive code would look something like:
            Code:
            double speed = gamepad1.left_stick_y;
            double rotation = gamepad1.left_stick_x * Math.PI;
            double changeDirection = gamepad1.right_stick_x;
            
            v[0].setPower(speed * Math.sin(rotation + Math.PI / 4) + changeDirection);
            v[1].setPower(speed * Math.cos(rotation + Math.PI / 4) - changeDirection);
            v[2].setPower(speed * Math.sin(rotation + Math.PI / 4) + changeDirection);
            v[3].setPower(speed * Math.cos(rotation + Math.PI / 4) - changeDirection);
            Just replace the v[0 - 3] with the names of the motors in the correct order (v[0] corresponds to forward-left, v[1] - forward right, v[2] - rear left, v[3] corresponds to rear right.

            Comment


            • #7
              Just want to make sure you understand that contents of the loop() method just sets what the motors are to be set to at end of the method.
              The hardware is not set until AFTER loop() completes. This is very different from how RobotC worked.
              For an OpMode that allows you to program using the "old" RobotC linear model look at the OpModes that derive off LinearOpMode (Ex: LinearIrExample)
              While this model may seem easier (especially for Autonomous), the non-linear event-based is a much better choice.
              Mark Hancock
              Tigard Team Mentor

              Comment


              • #8
                Hi! Thank you all for the help!
                I was not aware that the motors set at the end of that method. I think that the loop() method is what repeats during the running time of the robot, and I thought the motors get set with motorRight.setPower(right); etc. Is this incorrect? Thanks!

                Comment


                • #9
                  Originally posted by SanjnaR View Post
                  Hi! Thank you all for the help!
                  I was not aware that the motors set at the end of that method. I think that the loop() method is what repeats during the running time of the robot, and I thought the motors get set with motorRight.setPower(right); etc. Is this incorrect? Thanks!
                  You are mostly correct. motorRight.setPower(right) queues the write; the writes (or speed changes) are not executed until sometime after the end of the loop method.

                  Comment


                  • #10
                    Originally posted by dmssargent View Post
                    You are mostly correct. motorRight.setPower(right) queues the write; the writes (or speed changes) are not executed until sometime after the end of the loop method.
                    I think this is correct for the new usb controllers, but not
                    for anything connected through the legacy module.

                    Comment

                    Working...
                    X