Advertisement
CyanBlob

3005_StarterBot_Base_2022

Oct 8th, 2022 (edited)
1,166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.76 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.hardware.DcMotorSimple;
  6. import com.qualcomm.robotcore.hardware.DcMotorEx;
  7. import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
  8. import com.qualcomm.robotcore.hardware.CRServo;
  9. import com.qualcomm.robotcore.hardware.DcMotor;
  10. import com.qualcomm.robotcore.util.ElapsedTime;
  11. import com.qualcomm.robotcore.util.Range;
  12.  
  13. // Assign this teleop program a specific name so that we can choose it on the driver station
  14. @TeleOp(name="StarterBot Base", group="Linear Opmode")
  15.  
  16. public class StarterBot_Base extends LinearOpMode {
  17.  
  18.     // This is a timer. It's effectively a stopwatch that we can use to check
  19.     // how long the robot has been running. This is useful for defining movements
  20.     // such as "Move forward for 1 second"
  21.     private ElapsedTime runtime = new ElapsedTime();
  22.  
  23.     // These variables represent the physical motors/servos on our robot. In order to adjust
  24.     // things like motor speed or servo position we have to use these variables. These
  25.     // are set to `null` by default because we need to ask the Control Hub to actually find
  26.     // the hardware before we can use it
  27.     private DcMotor leftFront  = null;
  28.     private DcMotor rightFront = null;
  29.     private DcMotor leftRear   = null;
  30.     private DcMotor rightRear  = null;
  31.    
  32.     private DcMotorEx arm1   = null;
  33.     private DcMotorEx arm2  = null;
  34.  
  35.     private CRServo intake1    = null;
  36.     private CRServo intake2    = null;
  37.    
  38.     private int targetArmPosition = 0;
  39.    
  40.     private final int ARM_DOWN   = 0;
  41.     private final int ARM_INTAKE = 0;
  42.     private final int ARM_GROUND = 100;
  43.     private final int ARM_LOW    = 225;
  44.     private final int ARM_MID    = 375;
  45.     private final int ARM_HIGH   = 600;
  46.     private final int ARM_MAX    = 800;
  47.  
  48.     // This function contains the core of our program. When the teleop is selected and started,
  49.     // it automatically calls this function. This is where our gameplay logic will go
  50.     @Override
  51.     public void runOpMode() {
  52.         // This adds data to our driver station dashboard. This is purely informational, it does
  53.         // not have any impact on the robot
  54.         telemetry.addData("Status", "Initialized");
  55.         telemetry.update();
  56.  
  57.         // This is where we find the physical motors and actually save them into our
  58.         // variables that are declared above. On the driver station we assign a name
  59.         // to each physical port on the control hub + expansion hub. Then we use
  60.         // those names in our program to find each motor/servo
  61.         leftFront  = hardwareMap.get(DcMotor.class, "frontLeft");
  62.         rightFront = hardwareMap.get(DcMotor.class, "frontRight");
  63.         leftRear   = hardwareMap.get(DcMotor.class, "rearLeft");
  64.         rightRear  = hardwareMap.get(DcMotor.class, "rearRight");
  65.        
  66.         arm1       = hardwareMap.get(DcMotorEx.class, "armRight");
  67.         arm2       = hardwareMap.get(DcMotorEx.class, "armLeft");
  68.        
  69.         //arm1.setDirection(DcMotor.Direction.REVERSE);
  70.        
  71.  
  72.         //intake1  = hardwareMap.get(CRServo.class, "intake1");
  73.         //intake2  = hardwareMap.get(CRServo.class, "intake2");
  74.  
  75.         // Here we set the direction of each of our motors. Because the motors are mirrored
  76.         // on the right vs. the left of the robot, they're effectively backwards. This is
  77.         // an easy way to account for that
  78.         leftFront.setDirection(DcMotor.Direction.FORWARD);
  79.         leftRear.setDirection(DcMotor.Direction.FORWARD);
  80.         rightRear.setDirection(DcMotor.Direction.REVERSE);
  81.         rightFront.setDirection(DcMotor.Direction.REVERSE);
  82.  
  83.         // Wait for the game to start (driver presses PLAY)
  84.         waitForStart();
  85.  
  86.         // Reset the timer (stopwatch) because we only care about time since the game
  87.         // actually starts
  88.         runtime.reset();
  89.        
  90.         arm1.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  91.         arm2.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  92.        
  93.         targetArmPosition = ARM_DOWN;
  94.  
  95.         // This `while` loop runs until the end of the game. All of the code inside the loop
  96.         // gets run over and over in order to control our robot. We do things such as
  97.         // reading the controller input and setting motor speeds inside this loop because we
  98.         // need to do those things constantly while we control our robot
  99.         while (opModeIsActive()) {
  100.             stopArmIfTargetReached();
  101.  
  102.             // Reads the controller inputs and stores the values into variables representing
  103.             // how the robot will move
  104.             double drive  = -gamepad1.left_stick_y;  // forwards and backwards movement
  105.             double strafe =  gamepad1.left_stick_x;  // side to side movement
  106.             double turn   =  gamepad1.right_stick_x; // rotation
  107.  
  108.             // Mecanum is weird. Because of the physics of how mecanum wheels actually move
  109.             // the robot we have to do some weird math in order to get our robot to move how
  110.             // we want it to. How this math works isn't very important. If you want more
  111.             // information, ask Orion
  112.             leftFront.setPower(Range.clip(  drive + strafe + turn, -1.0, 1.0));
  113.             rightFront.setPower(Range.clip( drive - strafe - turn, -1.0, 1.0));
  114.             leftRear.setPower(Range.clip(   drive - strafe + turn, -1.0, 1.0));
  115.             rightRear.setPower(Range.clip(  drive + strafe - turn, -1.0, 1.0));
  116.  
  117.             // reads the controller inputs and stores the values into variables representing
  118.             // how we want to control intake servos
  119.             boolean intakeServoIn  = gamepad1.right_bumper;
  120.             boolean intakeServoOut = gamepad1.left_bumper;
  121.  
  122.             // Adds the data for how our robot is moving to the driver station dashboard so
  123.             // we can verify that controller input is being used properly
  124.             telemetry.addData("Drive", drive);
  125.             telemetry.addData("Strafe", strafe);
  126.             telemetry.addData("Turn", turn);
  127.  
  128.             // This variable stores the value that we're going to use to control our intake servo.
  129.             // We default it to 0 because we want the intake servos to remain still if
  130.             // we're not pushing any buttons
  131.             double intakePower = 0.0;
  132.  
  133.             // If the "intake in" button is pressed, set the intake servos to full speed forward
  134.             if (intakeServoIn == true)
  135.             {
  136.                 intakePower = 1;
  137.             }
  138.             // Otherwise, if the "intake in" button is pressed, set the intake servos to
  139.             //  full speed backward
  140.             // Note: Because we use "else if", this code will never run if the previous
  141.             // condition (if (intakeServoIn == true)) was true. This means that if we push
  142.             // the "intake in" button, pressing the "intake out" button as well does nothing
  143.             else if (intakeServoOut == true)
  144.             {
  145.                 intakePower = -1;
  146.             }
  147.            
  148.             if (gamepad1.y)
  149.             {
  150.                 targetArmPosition = ARM_HIGH;
  151.             }
  152.             else if (gamepad1.b)
  153.             {
  154.                 targetArmPosition = ARM_MID;
  155.             }
  156.             else if (gamepad1.a)
  157.             {
  158.                 targetArmPosition = ARM_LOW;
  159.             }
  160.             else if (gamepad1.x)
  161.             {
  162.                 targetArmPosition = ARM_GROUND;
  163.             }
  164.             else if (gamepad1.right_stick_button)
  165.             {
  166.                 targetArmPosition = ARM_DOWN;
  167.             }
  168.            
  169.             setArmPosition(targetArmPosition);
  170.  
  171.             // This is what actually sends all the telemetry data to the driver station
  172.             telemetry.update();
  173.         }
  174.     }
  175.    
  176.     public void setArmPosition(int position)
  177.     {
  178.         arm1.setTargetPosition(position);
  179.         arm2.setTargetPosition(position);
  180.        
  181.         arm1.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  182.         arm2.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  183.        
  184.         arm1.setVelocity(Range.clip(1500, -1500, 1500));
  185.         arm2.setVelocity(Range.clip(1500, -1500, 1500));
  186.     }
  187.    
  188.     public void stopArmIfTargetReached()
  189.     {
  190.         telemetry.addData("Arm target pos 1", arm1.getTargetPosition());
  191.         telemetry.addData("Arm current pos 1", arm1.getCurrentPosition());
  192.         telemetry.addData("Arm target pos 2", arm2.getTargetPosition());
  193.         telemetry.addData("Arm current pos 2", arm2.getCurrentPosition());
  194.  
  195.         if (!arm1.isBusy() || !arm2.isBusy())
  196.         {
  197.             arm1.setVelocity(0);
  198.             arm2.setVelocity(0);
  199.         }
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement