No announcement yet.

LinearOpMode Explanation

  • Filter
  • Time
  • Show
Clear All
new posts

  • LinearOpMode Explanation

    So I've been piecing together the new OpMode (Linear) but the examples both have loops - hence, I have no idea how to program the robot without a loop (and if it is any different from with a loop). Could someone who understands the LinearOpMode either:

    1. Go through a step by step explanation of the K9LinearTeleOp
    2. Tell me how to use the LinearTeleOp mode to:
    Go forward for 2 rotations
    Output some data to telemetry


  • #2
    An Explanation

    LinearOpMode attempts to emulated the Linear style of programming that was used in RobotC.

    From the advanced documentation ( Decompiled code - ):
    LinearOpMode derives off OpMode and adds a new abstract method runOpMode() that you override rather than Loop().
    What it does internally is to create a separate synchronized thread that runs runOpMode() while it's implementation of Loop() continuously loops.
    The result (should be) that when you set a motor object from runOpMode() it will (almost) immediately take affect because Loop() is running in parallel to pass all the motor data to the motors. Well, in reality I suspect the motors don't actually get updated until you call sleep() to allow Loop() to run.

    I can't tell you how to do the "2 rotations" thing because
    1. I haven't used that feature yet
    2. From all I've read, it's complicated depending on whether you have new or legacy controllers

    I can tell you how to move forward at 100% for 2sec the stop:
    telemetry.addData("Motors", "Running");
    telemetry.addData("Motors", "Stopping");
    .... just like the old RobotC days

    Regarding the loop, I believe the reason LinearK9TeleOp has a loop in its runOpMode is because it is a TeleOp mode; so, it needs to run 'forever'.
    In the case of a limited autonomous mode, I expect you can just return from runOpMode when you are done. What the code will do when you do that is not clear to me; but, it may just cause it to throw an Exception and thus exit the OpMode (which is what would happen in RobotC).
    Mark Hancock
    Tigard Team Mentor


    • #3
      The Linear K9 TeleOp code begins with a waitForStart(), similar to how robotC had one in the beginning of the code. I believe this is waiting for the user to select "start" on the drivers station app, and then the code will execute.

      float throttle = -gamepad1.left_stick_y;
      float direction = gamepad1.left_stick_x;
      float right = throttle - direction;
      float left = throttle + direction;

      The range of the values on the joystick go from -1 to 1, 0 being inactive. For the y-axis, -1 is full up and 1 is full down. This is why float throttle = -gamepad1.left_stick_y; to make sure 1 is forward and -1 is backwards. For the x-axis, -1 is full left and 1 is full right.

      The value received from the left joystick along the y axis is placed in throttle, and the value received from the left joystick along the x axis is placed in direction. These values are added and subtracted and placed in new float values (left and right) and are set as motor powers.

      For example, if you just have tilt the joystick forward on the y axis, throttle is set to 1 and direction is set to 0. Left and right are both set to 1, so the robot moves forward.
      If you have the joystick tilted all the way to the right, throttle is 0 and direction is 1. Right is -1 and Left is 1, causing the robot to turn.

      if (gamepad1.y) {
      neckPosition -= neckDelta;
      This states that if button Y on gamepad 1 is pressed, move this servo.

      For linear teleop, there is a "sleep()" method, where you input milliseconds into the parentheses, so you might have

      which would allow the robot to move forward for 2 seconds.

      telemetry.addData(" left motor", motorLeft.getPower());
      telemetry.addData("right motor", motorRight.getPower());
      This displays the motor power on the screen of the app. You can replace the motorLeft.getPower() with a sensor type also to display sensor readings.
      Not sure if this answers the question, hope it helps!