Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.47 KB | None | 0 0
  1. package org.firstinspires.ftc.robotcontroller.external.samples;
  2. import com.qualcomm.robotcore.eventloop.opmode.OpMode;
  3. import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
  4. import com.qualcomm.robotcore.hardware.CRServo;
  5. import com.qualcomm.robotcore.hardware.HardwareMap;
  6. import com.qualcomm.robotcore.util.Hardware;
  7. import com.qualcomm.robotcore.util.Range;
  8. import com.qualcomm.robotcore.hardware.CRServoImplEx;
  9. import com.qualcomm.robotcore.hardware.CRServoImpl;
  10. import com.qualcomm.robotcore.hardware.PwmControl
  11.  
  12.  
  13.  
  14. @TeleOp(name="MecanumDriveCode2", group="Mecanum")  // @Autonomous(...) is the other common choice
  15.  
  16. public class ExampleMecanumWheelCode2 extends OpMode
  17. {
  18.     /* Declare OpMode members. */
  19.  
  20.  
  21.  
  22.     CRServo leftdrive;
  23.     CRServo Leftback;
  24.     CRServo reallyrightback;
  25.     CRServo rightdrive;
  26.  
  27.  
  28.  
  29.  
  30.  
  31.     /*
  32.      * Code to run ONCE when the driver hits INIT
  33.      */
  34.     @Override
  35.     public void init() {
  36.  
  37.         /* eg: Initialize the hardware variables. Note that the strings used here as parameters
  38.          * to 'get' must correspond to the names assigned during the robot configuration
  39.          * step (using the FTC Robot Controller app on the phone).
  40.          */
  41.         // leftMotor  = hardwareMap.dcMotor.get("left motor");
  42.         // rightMotor = hardwareMap.dcMotor.get("right motor");
  43.  
  44.  
  45.         // eg: Set the drive motor directions:
  46.         // Reverse the motor that runs backwards when connected directly to the batteryYou mat need to tune these based on how your motors are mounted.
  47.  
  48.  
  49.         telemetry.addData("Status", "Initialized");
  50.  
  51.  
  52.  
  53.     }
  54.  
  55.     /*
  56.      * Code to run REPEATEDLY after the driver hits INIT, but before they hit PLAY
  57.      */
  58.     @Override
  59.     public void init_loop() {
  60.     }
  61.  
  62.     /*
  63.      * Code to run ONCE when the driver hits PLAY
  64.      */
  65.  
  66.  
  67.     /*
  68.      * Code to run REPEATEDLY after the driver hits PLAY but before they hit STOP
  69.      */
  70.     @Override
  71.     public void loop() {
  72.  
  73.         // Sets Motors in Brake Mode which prevents the robot from being easily pushed while stationary
  74.  
  75.  
  76.         // Sets joysticks as variables to simply equation
  77.         float Ch1 = -(gamepad1.right_stick_x);
  78.         float Ch3 = -(gamepad1.left_stick_y);
  79.         float Ch4 = (gamepad1.left_stick_x);
  80.  
  81.  
  82.         // note that if y equal -1 then joystick is pushed all of the way forward.
  83.         float rightfront = Ch3 + Ch1 - Ch4;
  84.         float rightback = Ch3 + Ch1 + Ch4;
  85.         float leftfront = Ch3 - Ch1 + Ch4;
  86.         float leftback = Ch3 - Ch1 - Ch4;
  87.  
  88. // Equation for Drive, Left Stick when pushed forward sends robot forward, Left Stick when sideways controls strafing, Right stick when pushed left controls turning
  89.  
  90.  
  91.  
  92.  
  93.  
  94.         // clip the right/left values so that the values never exceed +/- 1
  95.         rightback = Range.clip(rightback, -1, 1);
  96.         leftback = Range.clip(leftback, -1, 1);
  97.         rightfront = Range.clip(rightfront, -1, 1);
  98.         leftfront = Range.clip(leftfront, -1, 1);
  99.  
  100.  
  101.  
  102.         // scale the joystick value to make it easier to control
  103.         // the robot more precisely at slower speeds.
  104.         rightfront = (float)scaleInput(rightfront);
  105.         leftfront =  (float)scaleInput(leftfront);
  106.         rightback = (float)scaleInput(rightback);
  107.         leftback =  (float)scaleInput(leftback);
  108.  
  109.  
  110.  
  111.  
  112.  
  113.         // write the values to the motors
  114.         rightdrive.setPower(rightfront);
  115.         leftdrive.setPower(leftfront);
  116.         reallyrightback.setPower(rightback);
  117.         Leftback.setPower(leftback);
  118.  
  119.  
  120.  
  121.         // Sends back values to drive station
  122.     }
  123.  
  124.     /*
  125.      * Code to run ONCE after the driver hits STOP
  126.      */
  127.     @Override
  128.     public void stop() {
  129.     }
  130.     double scaleInput(double dVal)  {
  131.         double[] scaleArray = { 0.0, 0.05, 0.09, 0.10, 0.12, 0.15, 0.18, 0.24,
  132.                 0.30, 0.36, 0.43, 0.50, 0.60, 0.72, 0.85, 1.00, 1.00 };
  133.  
  134.         // get the corresponding index for the scaleInput array.
  135.         int index = (int) (dVal * 16.0);
  136.  
  137.         // index should be positive.
  138.         if (index < 0) {
  139.             index = -index;
  140.         }
  141.  
  142.         // index cannot exceed size of array minus 1.
  143.         if (index > 16) {
  144.             index = 16;
  145.         }
  146.  
  147.         // get value from the array.
  148.         double dScale = 0.0;
  149.         if (dVal < 0) {
  150.             dScale = -scaleArray[index];
  151.         } else {
  152.             dScale = scaleArray[index];
  153.         }
  154.  
  155.         // return scaled value.
  156.         return dScale;
  157.     }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement