Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 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.Autonomous;
  36. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  37. import com.qualcomm.robotcore.eventloop.opmode.OpMode;
  38. import com.qualcomm.robotcore.hardware.DcMotor;
  39. import com.qualcomm.robotcore.util.ElapsedTime;
  40.  
  41.  
  42.  
  43. //import org.firstinspires.ftc.robotcontroller.external.samples.HardwarePushbot;
  44.  
  45. /**
  46. * This file illustrates the concept of driving a path based on encoder counts.
  47. * It uses the common Pushbot hardware class to define the drive on the robot.
  48. * The code is structured as a LinearOpMode
  49. *
  50. * The code REQUIRES that you DO have encoders on the wheels,
  51. * otherwise you would use: PushbotAutoDriveByTime;
  52. *
  53. * This code ALSO requires that the drive Motors have been configured such that a positive
  54. * power command moves them forwards, and causes the encoders to count UP.
  55. *
  56. * The desired path in this example is:
  57. * - Drive forward for 48 inches
  58. * - Spin right for 12 Inches
  59. * - Drive Backwards for 24 inches
  60. * - Stop and close the claw.
  61. *
  62. * The code is written using a method called: encoderDrive(speed, leftInches, rightInches, timeoutS)
  63. * that performs the actual movement.
  64. * This methods assumes that each movement is relative to the last stopping place.
  65. * There are other ways to perform encoder based moves, but this method is probably the simplest.
  66. * This code uses the RUN_TO_POSITION mode to enable the Motor controllers to generate the run profile
  67. *
  68. * Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name.
  69. * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
  70. */
  71.  
  72. @Autonomous(name="RedEncoder_Linear", group="Pushbot")
  73. public class RedBotEncoder_Linear extends LinearOpMode {
  74.  
  75. /* Declare OpMode members. */
  76. TestBotNN robot = new TestBotNN(); // Use a Pushbot's hardware
  77. private ElapsedTime runtime = new ElapsedTime();
  78.  
  79. static final double COUNTS_PER_MOTOR_REV = 1120; // eg: TETRIX Motor Encoder
  80. static final double DRIVE_GEAR_REDUCTION = 1.0 ; // This is < 1.0 if geared UP
  81. static final double WHEEL_DIAMETER_INCHES = 4.0; // For figuring circumference
  82. static final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) /
  83. (WHEEL_DIAMETER_INCHES * 3.1415);
  84. static final double DRIVE_SPEED = 0.4;
  85. static final double TURN_SPEED = 0.2;
  86.  
  87. @Override
  88. public void runOpMode() {
  89.  
  90. /*
  91. * Initialize the drive system variables.
  92. * The init() method of the hardware class does all the work here
  93. */
  94. /* robot.init(hardwareMap);
  95. // Send telemetry message to signify robot waiting;
  96. telemetry.addData("Status", "Resetting Encoders");
  97. telemetry.update();
  98.  
  99. robot.leftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  100. robot.rightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  101. idle();
  102.  
  103. robot.leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  104. robot.rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  105.  
  106. // Send telemetry message to indicate successful Encoder reset
  107. telemetry.addData("Path0", "Starting at %7d :%7d",
  108. robot.leftMotor.getCurrentPosition(),
  109. robot.rightMotor.getCurrentPosition());
  110. telemetry.update();
  111.  
  112. // Wait for the game to start (driver presses PLAY)*/
  113. waitForStart();
  114.  
  115. // Step through each leg of the path,
  116. // Note: Reverse movement is obtained by setting a negative distance (not speed)
  117. /* encoderDrive(DRIVE_SPEED, -48, -48, 5.0);
  118. encoderDrive(DRIVE_SPEED, 24, 24, 5.0);
  119. encoderDrive(0, 0, 0, 0);*/
  120.  
  121. if (robot.colorSensor.red() > robot.colorSensor.blue() && robot.colorSensor.red() > robot.colorSensor.green()) {
  122. telemetry.addData("red", "detected"); // outputs "red" on telemetry when red is detected
  123. } else if(robot.colorSensor.blue() > robot.colorSensor.red() && robot.colorSensor.blue() > robot.colorSensor.green()) {
  124. telemetry.addData("blue", "detected"); // outputs "blue" on telemetry when blue is detected
  125. }
  126.  
  127.  
  128. /*telemetry.addData("Path", "Complete");
  129. telemetry.update();
  130.  
  131. VuNN test = new VuNN(hardwareMap);
  132. test.setActive(VuNN.TargetMode.VUMARKS, true);
  133. while(opModeIsActive()){
  134. VuNN.VuResults results = test.detectVuMarks();
  135. telemetry.addData(results.message, "");
  136. telemetry.update();
  137. }
  138. test.setActive(VuNN.TargetMode.VUMARKS, false);*/
  139. }
  140.  
  141. /*
  142. * Method to perform a relative move, based on encoder counts.
  143. * Encoders are not reset as the move is based on the current position.
  144. * Move will stop if any of three conditions occur:
  145. * 1) Move gets to the desired position
  146. * 2) Move runs out of time
  147. * 3) Driver stops the opmode running.
  148. */
  149. /*public void encoderDrive(double speed,
  150. double leftInches, double rightInches,
  151. double timeoutS) {
  152. int newLeftTarget;
  153. int newRightTarget;
  154.  
  155. // Ensure that the opmode is still active
  156. if (opModeIsActive()) {
  157.  
  158. // Determine new target position, and pass to motor controller
  159. newLeftTarget = robot.leftMotor.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
  160. newRightTarget = robot.rightMotor.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
  161. robot.leftMotor.setTargetPosition(newLeftTarget);
  162. robot.rightMotor.setTargetPosition(newRightTarget);
  163.  
  164. // Turn On RUN_TO_POSITION
  165. robot.leftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  166. robot.rightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  167.  
  168. // reset the timeout time and start motion.
  169. runtime.reset();
  170. robot.leftMotor.setPower(Math.abs(speed));
  171. robot.rightMotor.setPower(Math.abs(speed));
  172.  
  173. // keep looping while we are still active, and there is time left, and both motors are running.
  174. while (opModeIsActive() &&
  175. (runtime.seconds() < timeoutS) &&
  176. (robot.leftMotor.isBusy() && robot.rightMotor.isBusy())) {
  177.  
  178. // Display it for the driver.
  179. telemetry.addData("Path1", "Running to %7d :%7d", newLeftTarget, newRightTarget);
  180. telemetry.addData("Path2", "Running at %7d :%7d",
  181. robot.leftMotor.getCurrentPosition(),
  182. robot.rightMotor.getCurrentPosition());
  183. telemetry.update();
  184. }
  185.  
  186. // Stop all motion;
  187. robot.leftMotor.setPower(0);
  188. robot.rightMotor.setPower(0);
  189.  
  190. // Turn off RUN_TO_POSITION
  191. robot.leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  192. robot.rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  193.  
  194. // sleep(250); // optional pause after each move
  195. }
  196. }*/
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement