Advertisement
jmcdonne

4 Wheel Tank Drive

Oct 22nd, 2016
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.69 KB | None | 0 0
  1. /*
  2. Copyright (c) 2016 Robert Atkinson
  3.  
  4. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted (subject to the limitations in the disclaimer below) provided that
  8. the following conditions are met:
  9.  
  10. Redistributions of source code must retain the above copyright notice, this list
  11. of conditions and the following disclaimer.
  12.  
  13. Redistributions in binary form must reproduce the above copyright notice, this
  14. list of conditions and the following disclaimer in the documentation and/or
  15. other materials provided with the distribution.
  16.  
  17. Neither the name of Robert Atkinson nor the names of his contributors may be used to
  18. endorse or promote products derived from this software without specific prior
  19. written permission.
  20.  
  21. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
  22. LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESSFOR A PARTICULAR PURPOSE
  25. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  26. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  30. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. package org.firstinspires.ftc.teamcode;
  34.  
  35. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  36. import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
  37. import com.qualcomm.robotcore.hardware.DcMotor;
  38. import com.qualcomm.robotcore.util.ElapsedTime;
  39. import com.qualcomm.robotcore.util.Range;
  40.  
  41. /**
  42.  * This file contains an minimal example of a Linear "OpMode". An OpMode is a 'program' that runs in either
  43.  * the autonomous or the teleop period of an FTC match. The names of OpModes appear on the menu
  44.  * of the FTC Driver Station. When an selection is made from the menu, the corresponding OpMode
  45.  * class is instantiated on the Robot Controller and executed.
  46.  *
  47.  * This particular OpMode just executes a basic Tank Drive Teleop for a PushBot
  48.  * It includes all the skeletal structure that all linear OpModes contain.
  49.  *
  50.  * Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name.
  51.  * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
  52.  */
  53.  
  54. @TeleOp(name="Tank Drive 4 Motor", group="Linear Opmode")  // @Autonomous(...) is the other common choice
  55. public class TankDrive4Motor extends LinearOpMode {
  56.  
  57.     /* Declare OpMode members. */
  58.     private ElapsedTime runtime = new ElapsedTime();
  59.     DcMotor leftMotor = null;
  60.     DcMotor rightMotor = null;
  61.     DcMotor leftBackMotor = null;
  62.     DcMotor rightBackMotor = null;
  63.  
  64.     @Override
  65.     public void runOpMode() throws InterruptedException {
  66.         telemetry.addData("Status", "Initialized");
  67.         telemetry.update();
  68.  
  69.         /* eg: Initialize the hardware variables. Note that the strings used here as parameters
  70.          * to 'get' must correspond to the names assigned during the robot configuration
  71.          * step (using the FTC Robot Controller app on the phone).
  72.          */
  73.         leftMotor  = hardwareMap.dcMotor.get("left motor");
  74.         rightMotor = hardwareMap.dcMotor.get("right motor");
  75.         leftBackMotor  = hardwareMap.dcMotor.get("left back motor");
  76.         rightBackMotor = hardwareMap.dcMotor.get("right back motor");
  77.  
  78.         // eg: Set the drive motor directions:
  79.         // "Reverse" the motor that runs backwards when connected directly to the battery
  80.         leftMotor.setDirection(DcMotor.Direction.REVERSE); // Set to REVERSE if using AndyMark motors
  81.         leftBackMotor.setDirection(DcMotor.Direction.REVERSE); // Set to REVERSE if using AndyMark motors
  82.         rightMotor.setDirection(DcMotor.Direction.FORWARD);// Set to FORWARD if using AndyMark motors
  83.         rightBackMotor.setDirection(DcMotor.Direction.FORWARD);// Set to FORWARD if using AndyMark motors
  84.         // Wait for the game to start (driver presses PLAY)
  85.         waitForStart();
  86.         runtime.reset();
  87.  
  88.         // run until the end of the match (driver presses STOP)
  89.         while (opModeIsActive()) {
  90.             telemetry.addData("Status", "Run Time: " + runtime.toString());
  91.  
  92.             float y_left = scaleInput(-gamepad1.left_stick_y);
  93.             float y_right = scaleInput(-gamepad1.right_stick_y);
  94.  
  95.             // eg: Run wheels in tank mode (note: The joystick goes negative when pushed forwards)
  96.             leftMotor.setPower(y_left);
  97.             rightMotor.setPower(y_right);
  98.             leftBackMotor.setPower(y_left);
  99.             rightBackMotor.setPower(y_right);
  100.  
  101.             telemetry.addData("Left",y_left);
  102.             telemetry.addData("Right",y_right);
  103.             telemetry.update();
  104.             idle(); // Always call idle() at the bottom of your while(opModeIsActive()) loop
  105.         }
  106.     }
  107.  
  108.     /** Scales the input so the commanded power will be within a certain range */
  109.     private float scaleInput(float input)
  110.     {
  111.         final float MIN_INPUT = 0.15f;
  112.         final float MAX_INPUT = 0.75f;
  113.  
  114.         if (Math.abs(input) >= MIN_INPUT) {
  115.             if (input >= 0) {
  116.                 return Range.clip(input, MIN_INPUT, MAX_INPUT);
  117.             } else {
  118.                 return Range.clip(input, -MAX_INPUT, -MIN_INPUT);
  119.             }
  120.         }
  121.         else
  122.         {
  123.             return 0.0f;
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement