Advertisement
CyanBlob

Untitled

Sep 24th, 2022 (edited)
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.59 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode;
  2.  
  3. // This `import` statements are used to pull in the FTC code into our program
  4. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  5. import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
  6. import com.qualcomm.robotcore.hardware.CRServo;
  7. import com.qualcomm.robotcore.hardware.DcMotor;
  8. import com.qualcomm.robotcore.util.ElapsedTime;
  9. import com.qualcomm.robotcore.util.Range;
  10.  
  11. // Assign this teleop program a specific name so that we can choose it on the driver station
  12. @TeleOp(name="PowerPlay Base", group="Linear Opmode")
  13.  
  14. public class PowerPlay_Base extends LinearOpMode {
  15.  
  16.     // This is a timer. It's effectively a stopwatch that we can use to check
  17.     // how long the robot has been running. This is useful for defining movements
  18.     // such as "Move forward for 1 second"
  19.     private ElapsedTime runtime = new ElapsedTime();
  20.  
  21.     // These variables represent the physical motors/servos on our robot. In order to adjust
  22.     // things like motor speed or servo position we have to use these variables. These
  23.     // are set to `null` by default because we need to ask the Control Hub to actually find
  24.     // the hardware before we can use it
  25.     private DcMotor leftFront  = null;
  26.     private DcMotor rightFront = null;
  27.     private DcMotor leftRear   = null;
  28.     private DcMotor rightRear  = null;
  29.  
  30.     private CRServo intake1    = null;
  31.     private CRServo intake2    = null;
  32.  
  33.     // This function contains the core of our program. When the teleop is selected and started,
  34.     // it automatically calls this function. This is where our gameplay logic will go
  35.     @Override
  36.     public void runOpMode() {
  37.         // This adds data to our driver station dashboard. This is purely informational, it does
  38.         // not have any impact on the robot
  39.         telemetry.addData("Status", "Initialized");
  40.         telemetry.update();
  41.  
  42.         // This is where we find the physical motors and actually save them into our
  43.         // variables that are declared above. On the driver station we assign a name
  44.         // to each physical port on the control hub + expansion hub. Then we use
  45.         // those names in our program to find each motor/servo
  46.         leftFront  = hardwareMap.get(DcMotor.class, "leftFront");
  47.         rightFront = hardwareMap.get(DcMotor.class, "rightFront");
  48.         leftRear   = hardwareMap.get(DcMotor.class, "leftRear");
  49.         rightRear  = hardwareMap.get(DcMotor.class, "rightRear");
  50.  
  51.         intake1  = hardwareMap.get(CRServo.class, "intake1");
  52.         intake2  = hardwareMap.get(CRServo.class, "intake2");
  53.  
  54.         // Here we set the direction of each of our motors. Because the motors are mirrored
  55.         // on the right vs. the left of the robot, they're effectively backwards. This is
  56.         // an easy way to account for that
  57.         leftFront.setDirection(DcMotor.Direction.FORWARD);
  58.         leftRear.setDirection(DcMotor.Direction.FORWARD);
  59.         rightRear.setDirection(DcMotor.Direction.REVERSE);
  60.         rightFront.setDirection(DcMotor.Direction.REVERSE);
  61.  
  62.         // Wait for the game to start (driver presses PLAY)
  63.         waitForStart();
  64.  
  65.         // Reset the timer (stopwatch) because we only care about time since the game
  66.         // actually starts
  67.         runtime.reset();
  68.  
  69.         // This `while` loop runs until the end of the game. All of the code inside the loop
  70.         // gets run over and over in order to control our robot. We do things such as
  71.         // reading the controller input and setting motor speeds inside this loop because we
  72.         // need to do those things constantly while we control our robot
  73.         while (opModeIsActive()) {
  74.  
  75.             // Reads the controller inputs and stores the values into variables representing
  76.             // how the robot will move
  77.             double drive  = -gamepad1.left_stick_y;  // forwards and backwards movement
  78.             double strafe =  gamepad1.left_stick_x;  // side to side movement
  79.             double turn   =  gamepad1.right_stick_x; // rotation
  80.  
  81.             // Mecanum is weird. Because of the physics of how mecanum wheels actually move
  82.             // the robot we have to do some weird math in order to get our robot to move how
  83.             // we want it to. How this math works isn't very important. If you want more
  84.             // information, ask Orion
  85.             leftFront.setPower(Range.clip(  drive + strafe + turn, -1.0, 1.0));
  86.             rightFront.setPower(Range.clip( drive - strafe - turn, -1.0, 1.0));
  87.             leftRear.setPower(Range.clip(   drive - strafe + turn, -1.0, 1.0));
  88.             rightRear.setPower(Range.clip(  drive + strafe - turn, -1.0, 1.0));
  89.  
  90.             // reads the controller inputs and stores the values into variables representing
  91.             // how we want to control intake servos
  92.             boolean intakeServoIn  = gamepad1.right_bumper;
  93.             boolean intakeServoOut = gamepad1.left_bumper;
  94.  
  95.             // Adds the data for how our robot is moving to the driver station dashboard so
  96.             // we can verify that controller input is being used properly
  97.             telemetry.addData("Drive", drive);
  98.             telemetry.addData("Strafe", strafe);
  99.             telemetry.addData("Turn", turn);
  100.  
  101.             // This variable stores the value that we're going to use to control our intake servo.
  102.             // We default it to 0 because we want the intake servos to remain still if
  103.             // we're not pushing any buttons
  104.             double intakePower = 0.0;
  105.  
  106.             // If the "intake in" button is pressed, set the intake servos to full speed forward
  107.             if (intakeServoIn == true)
  108.             {
  109.                 intakePower = 1;
  110.             }
  111.             // Otherwise, if the "intake in" button is pressed, set the intake servos to
  112.             //  full speed backward
  113.             // Note: Because we use "else if", this code will never run if the previous
  114.             // condition (if (intakeServoIn == true)) was true. This means that if we push
  115.             // the "intake in" button, pressing the "intake out" button as well does nothing
  116.             else if (intakeServoOut == true)
  117.             {
  118.                 intakePower = -1;
  119.             }
  120.  
  121.             // Sets the power of both intake servers.
  122.             intake1.setPower(intakePower);
  123.             // Note: The value of `intakePower` gets multiplied by -1 so that intake2 turns in
  124.             // the opposite direction of intake1
  125.             intake2.setPower(intakePower * -1);
  126.  
  127.             // This is what actually sends all the telemetry data to the driver station
  128.             telemetry.update();
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement