Advertisement
jmcdonne

8782 Teleop

Dec 4th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.52 KB | None | 0 0
  1. /* Copyright (c) 2014 Qualcomm Technologies Inc
  2.  
  3. All rights reserved.
  4.  
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted (subject to the limitations in the disclaimer below) provided that
  7. the following conditions are met:
  8.  
  9. Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11.  
  12. Redistributions in binary form must reproduce the above copyright notice, this
  13. list of conditions and the following disclaimer in the documentation and/or
  14. other materials provided with the distribution.
  15.  
  16. Neither the name of Qualcomm Technologies Inc nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19.  
  20. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
  21. LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  25. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
  31.  
  32. package com.qualcomm.ftcrobotcontroller.opmodes;
  33.  
  34. import com.qualcomm.robotcore.eventloop.opmode.OpMode;
  35. import com.qualcomm.robotcore.hardware.DcMotor;
  36. import com.qualcomm.robotcore.hardware.Servo;
  37. import com.qualcomm.robotcore.util.Range;
  38.  
  39. /**
  40.  * TeleOp Mode
  41.  * <p>
  42.  * Enables control of the robot via the gamepad
  43.  */
  44. public class FourWheelTankDriveAndArm8782 extends OpMode {
  45.  
  46.     DcMotor motorRightFront;
  47.     DcMotor motorLeftFront;
  48.     DcMotor motorRightRear;
  49.     DcMotor motorLeftRear;
  50.     Servo arm;
  51.  
  52.     final static double ARM_MIN_RANGE  = 0.00;
  53.     final static double ARM_MAX_RANGE  = 1.00;
  54.  
  55.     // starting position of the arm servo.
  56.     double armPosition = 0.5;
  57.  
  58.     // amount to change the arm servo position.
  59.     double armDelta = 0.1;
  60.  
  61.     /**
  62.      * Constructor
  63.      */
  64.     public FourWheelTankDriveAndArm8782() {
  65.  
  66.     }
  67.  
  68.     /*
  69.      * Code to run when the op mode is first enabled goes here
  70.      *
  71.      * @see com.qualcomm.robotcore.eventloop.opmode.OpMode#start()
  72.      */
  73.     @Override
  74.     public void init() {
  75.   /*
  76.    * Use the hardwareMap to get the dc motors by name. Note
  77.    * that the names of the devices must match the names used when you
  78.    * configured your robot and created the configuration file.
  79.    *
  80.    * For this bot we assume there are four motors:
  81.    *   "1" is on the left front corner of the bot.
  82.    *   "3" is on the left rear corner of the bot.
  83.    *   "2" is on the right front corner of the bot.
  84.    *   "4" is on the right rear corner of the bot.
  85.    */
  86.     motorLeftFront = hardwareMap.dcMotor.get("FtcDcMotor1");
  87.     motorLeftRear = hardwareMap.dcMotor.get("FtcDcMotor3");
  88.         motorRightFront = hardwareMap.dcMotor.get("FtcDcMotor2");
  89.         motorRightRear = hardwareMap.dcMotor.get("FtcDcMotor4");
  90.         motorLeftFront.setDirection(DcMotor.Direction.REVERSE);
  91.         motorLeftRear.setDirection(DcMotor.Direction.REVERSE);
  92.  
  93.         arm = hardwareMap.servo.get("FtcServo1");
  94.  
  95.     }
  96.  
  97.     /*
  98.      * This method will be called repeatedly in a loop
  99.      *
  100.      * @see com.qualcomm.robotcore.eventloop.opmode.OpMode#run()
  101.      */
  102.     @Override
  103.     public void loop() {
  104.  
  105.     /*
  106.      * Gamepad 1
  107.      *
  108.      * Gamepad 1 controls the motors via the left stick
  109.      */
  110.  
  111.         // tank drive
  112.         // note that if y equal -1 then joystick is pushed all of the way forward.
  113.         float left = -gamepad1.left_stick_y;
  114.         float right = -gamepad1.right_stick_y;
  115.  
  116.         // clip the right/left values so that the values never exceed +/- 1
  117.     left = Range.clip(left, -1, 1);
  118.         right = Range.clip(right, -1, 1);
  119.  
  120.         // scale the joystick value to make it easier to control
  121.         // the robot more precisely at slower speeds.
  122.     left =  (float)scaleInput(left);
  123.         right = (float)scaleInput(right);
  124.  
  125.         // write the values to the motors
  126.     motorLeftFront.setPower(left);
  127.     motorLeftRear.setPower(left);
  128.         motorRightFront.setPower(right);
  129.         motorRightRear.setPower(right);
  130.  
  131.         // update the position of the arm.
  132.         if (gamepad1.y) {
  133.             // if the Y button is pushed on gamepad1, decrease the position of
  134.             // the arm servo.
  135.             armPosition += armDelta;
  136.         }
  137.  
  138.         if (gamepad1.a) {
  139.             // if the A button is pushed on gamepad1, increment the position of
  140.             // the arm servo.
  141.             armPosition -= armDelta;
  142.         }
  143.  
  144.         armPosition = Range.clip(armPosition, ARM_MIN_RANGE, ARM_MAX_RANGE);
  145.  
  146.         // write position values to the wrist and claw servo
  147.         arm.setPosition(armPosition);
  148.  
  149.         /*
  150.          * Send telemetry data back to driver station. Note that if we are using
  151.          * a legacy NXT-compatible motor controller, then the getPower() method
  152.          * will return a null value. The legacy NXT-compatible motor controllers
  153.          * are currently write only.
  154.          */
  155.  
  156.         telemetry.addData("Text", "*** Robot Data***");
  157.         telemetry.addData("left tgt pwr",  "left  pwr: " + String.format("%.2f", left));
  158.         telemetry.addData("right tgt pwr", "right pwr: " + String.format("%.2f", right));
  159.     telemetry.addData("arm pos"      , "arm pos: "   + String.format("%.2d", armPosition));
  160.     }
  161.  
  162.     /*
  163.      * Code to run when the op mode is first disabled goes here
  164.      *
  165.      * @see com.qualcomm.robotcore.eventloop.opmode.OpMode#stop()
  166.      */
  167.     @Override
  168.     public void stop() {
  169.  
  170.     }
  171.    
  172.     /*
  173.      * This method scales the joystick input so for low joystick values, the
  174.      * scaled value is less than linear.  This is to make it easier to drive
  175.      * the robot more precisely at slower speeds.
  176.      */
  177.     double scaleInput(double dVal)  {
  178.         double[] scaleArray = { 0.0, 0.05, 0.09, 0.10, 0.12, 0.15, 0.18, 0.24,
  179.                 0.30, 0.36, 0.43, 0.50, 0.60, 0.72, 0.85, 1.00, 1.00 };
  180.        
  181.         // get the corresponding index for the scaleInput array.
  182.         int index = (int) (dVal * 16.0);
  183.        
  184.         // index should be positive.
  185.         if (index < 0) {
  186.             index = -index;
  187.         }
  188.  
  189.         // index cannot exceed size of array minus 1.
  190.         if (index > 16) {
  191.             index = 16;
  192.         }
  193.  
  194.         // get value from the array.
  195.         double dScale = 0.0;
  196.         if (dVal < 0) {
  197.             dScale = -scaleArray[index];
  198.         } else {
  199.             dScale = scaleArray[index];
  200.         }
  201.  
  202.         // return scaled value.
  203.         return dScale;
  204.     }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement