Advertisement
jmcdonne

Arcade Mode 2 Motor With Telemetry

Oct 28th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.64 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 Arcade 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="Arcade Drive 2 Motor", group="Linear Opmode")  // @Autonomous(...) is the other common choice
  55. public class ArcadeDrive2MotorOneJoystick extends LinearOpMode
  56. {
  57.     /* Declare OpMode members. */
  58.     private ElapsedTime runtime = new ElapsedTime();
  59.     DcMotor leftMotor = null;
  60.     DcMotor rightMotor = null;
  61.  
  62.     @Override
  63.     public void runOpMode() throws InterruptedException
  64.     {
  65.         telemetry.addData("Status", "Initialized");
  66.         telemetry.update();
  67.  
  68.         /* eg: Initialize the hardware variables. Note that the strings used here as parameters
  69.          * to 'get' must correspond to the names assigned during the robot configuration
  70.          * step (using the FTC Robot Controller app on the phone).
  71.          */
  72.         leftMotor  = hardwareMap.dcMotor.get("left motor");
  73.         rightMotor = hardwareMap.dcMotor.get("right motor");
  74.  
  75.         // eg: Set the drive motor directions:
  76.         // "Reverse" the motor that runs backwards when connected directly to the battery
  77.         leftMotor.setDirection(DcMotor.Direction.REVERSE); // Set to REVERSE if using AndyMark motors
  78.         rightMotor.setDirection(DcMotor.Direction.FORWARD);// Set to FORWARD if using AndyMark motors
  79.  
  80.         // Wait for the game to start (driver presses PLAY)
  81.         waitForStart();
  82.         runtime.reset();
  83.  
  84.         // run until the end of the match (driver presses STOP)
  85.         while (opModeIsActive())
  86.         {
  87.             telemetry.addData("Status", "Run Time: " + runtime.toString());
  88.  
  89.             float throttle = -gamepad1.left_stick_y;
  90.             float direction = -gamepad1.left_stick_x;
  91.  
  92.             float leftPower = scaleInput(throttle + direction);
  93.             float rightPower = scaleInput(throttle - direction);
  94.  
  95.             leftMotor.setPower(leftPower);
  96.             rightMotor.setPower(rightPower);
  97.  
  98.             telemetry.addData("Left",leftPower);
  99.             telemetry.addData("Right",rightPower);
  100.  
  101.             // Show joystick information as some other illustrative data
  102.             telemetry.addLine("left joystick | ")
  103.                     .addData("x", gamepad1.left_stick_x)
  104.                     .addData("y", gamepad1.left_stick_y);
  105.             telemetry.addLine("right joystick | ")
  106.                     .addData("x", gamepad1.right_stick_x)
  107.                     .addData("y", gamepad1.right_stick_y);
  108.  
  109.             telemetry.update();
  110.  
  111.             idle(); // Always call idle() at the bottom of your while(opModeIsActive()) loop
  112.         }
  113.     }
  114.  
  115.     /** Scales the input so the commanded power will be within a certain range. Avoids stalls and robot moving too fast. */
  116.     private float scaleInput(float input)
  117.     {
  118.         final float MIN_INPUT = 0.15f; //15% power
  119.         final float MAX_INPUT = 0.75f; //75% power
  120.         float retVal = 0.0f;
  121.  
  122.         if (Math.abs(input) >= MIN_INPUT)
  123.         {
  124.             retVal = input >= 0 ? Range.clip(input, MIN_INPUT, MAX_INPUT) : Range.clip(input, -MAX_INPUT, -MIN_INPUT) ;
  125.         }
  126.         return retVal;
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement