Advertisement
Guest User

autonomie

a guest
Feb 22nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. /* Copyright (c) 2018 FIRST. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without modification,
  4. * are permitted (subject to the limitations in the disclaimer below) provided that
  5. * the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice, this list
  8. * of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. * list of conditions and the following disclaimer in the documentation and/or
  12. * other materials provided with the distribution.
  13. *
  14. * Neither the name of FIRST nor the names of its contributors may be used to endorse or
  15. * promote products derived from this software without specific prior written permission.
  16. *
  17. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
  18. * LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29.  
  30. package org.firstinspires.ftc.teamcode;
  31.  
  32. import com.qualcomm.robotcore.eventloop.opmode.Disabled;
  33. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  34. import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
  35. import com.qualcomm.robotcore.hardware.DcMotor;
  36. import com.qualcomm.robotcore.hardware.DcMotorSimple;
  37. import com.qualcomm.robotcore.hardware.Servo;
  38.  
  39. import org.firstinspires.ftc.robotcore.external.ClassFactory;
  40. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
  41. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer.CameraDirection;
  42. import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
  43. import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
  44.  
  45. import java.util.List;
  46.  
  47. /**
  48. * This 2018-2019 OpMode illustrates the basics of using the TensorFlow Object Detection API to
  49. * determine the position of the gold and silver minerals.
  50. *
  51. * Use Android Studio to Copy this Class, and Paste it into your team's code folder with a new name.
  52. * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list.
  53. *
  54. * IMPORTANT: In order to use this OpMode, you need to obtain your own Vuforia license key as
  55. * is explained below.
  56. */
  57. @TeleOp(name = "Autonomous_2018_2019", group = "Concept")
  58. //@Disabled
  59.  
  60. public class Autonomie_2018_2019 extends LinearOpMode {
  61. private static final String TFOD_MODEL_ASSET = "RoverRuckus.tflite";
  62. private static final String LABEL_GOLD_MINERAL = "Gold Mineral";
  63. private static final String LABEL_SILVER_MINERAL = "Silver Mineral";
  64.  
  65.  
  66. DcMotor stangafata;
  67. DcMotor stangaspate;
  68. DcMotor dreaptafata;
  69. DcMotor dreaptaspate;
  70. DcMotor motorridicare;
  71.  
  72.  
  73.  
  74.  
  75. /*
  76. * IMPORTANT: You need to obtain your own license key to use Vuforia. The string below with which
  77. * 'parameters.vuforiaLicenseKey' is initialized is for illustration only, and will not function.
  78. * A Vuforia 'Development' license key, can be obtained free of charge from the Vuforia developer
  79. * web site at https://developer.vuforia.com/license-manager.
  80. *
  81. * Vuforia license keys are always 380 characters long, and look as if they contain mostly
  82. * random data. As an example, here is a example of a fragment of a valid key:
  83. * ... yIgIzTqZ4mWjk9wd3cZO9T1axEqzuhxoGlfOOI2dRzKS4T0hQ8kT ...
  84. * Once you've obtained a license key, copy the string from the Vuforia web site
  85. * and paste it in to your code on the next line, between the double quotes.
  86. */
  87. private static final String VUFORIA_KEY = "AZMtsN7/////AAAAGaO7fMjTHkBzuK+Q5+sJ8GODX9t0JUpPTa42RrZjmqooU4jdyDeaR+DLBis3OmcP/hw3ApMB/69HhTJlZF/LDgPLJ3kmAFo1BM6pn0ECyo/V/JZbo9aCbxZHoAtEfCl8oiwwf9ysbKkHoSeyA7V+IvyRzttPVXfbKNn7ZNqpzJK0CmFdHiM9gQfJ1vpYcAeelXjaSLjLDzBIsn+Gci75VzaAifiL6hgkKIhJ8d7DYJLAvydiykY2dWMRtyK5MWCP8ehG0zXKKZXQYoMMGULar0gxzh10BULScmzludez0PO/+OiIe4oz4bD+6w/czItvj0euyH5jon+s0WZrgrR9VZDcauhBOAVv/yqCBP16qXIc\n";
  88.  
  89. /**
  90. * {@link #vuforia} is the variable we will use to store our instance of the Vuforia
  91. * localization engine.
  92. */
  93. private VuforiaLocalizer vuforia;
  94.  
  95. /**
  96. * {@link #tfod} is the variable we will use to store our instance of the Tensor Flow Object
  97. * Detection engine.
  98. */
  99. private TFObjectDetector tfod;
  100.  
  101. @Override
  102. public void runOpMode() {
  103.  
  104.  
  105.  
  106. stangafata= hardwareMap.get(DcMotor.class,"stangafata");
  107. stangaspate= hardwareMap.get(DcMotor.class,"stangaspate");
  108. dreaptafata= hardwareMap.get(DcMotor.class, "dreaptafata");
  109. dreaptaspate=hardwareMap.get(DcMotor.class, "dreaptaspate");
  110. motorridicare=hardwareMap.get(DcMotor.class,"motorridicare");
  111.  
  112. stangaspate.setDirection(DcMotorSimple.Direction.REVERSE);
  113. stangafata.setDirection(DcMotorSimple.Direction.REVERSE);
  114.  
  115.  
  116.  
  117. // The TFObjectDetector uses the camera frames from the VuforiaLocalizer, so we create that
  118. // first.
  119. initVuforia();
  120.  
  121. if (ClassFactory.getInstance().canCreateTFObjectDetector()) {
  122. initTfod();
  123. } else {
  124. telemetry.addData("Sorry!", "This device is not compatible with TFOD");
  125. }
  126.  
  127. /** Wait for the game to begin */
  128. telemetry.addData(">", "Press Play to start tracking");
  129. telemetry.update();
  130. waitForStart();
  131.  
  132. if (opModeIsActive()) {
  133. /** Activate Tensor Flow Object Detection. */
  134. if (tfod != null) {
  135. tfod.activate();
  136. }
  137.  
  138. while (opModeIsActive()) {
  139. if (tfod != null) {
  140. // getUpdatedRecognitions() will return null if no new information is available since
  141. // the last time that call was made.
  142. List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();
  143. if (updatedRecognitions != null) {
  144. telemetry.addData("# Object Detected", updatedRecognitions.size());
  145. if (updatedRecognitions.size() == 3) {
  146. int goldMineralX = -1;
  147. int silverMineral1X = -1;
  148. int silverMineral2X = -1;
  149. for (Recognition recognition : updatedRecognitions) {
  150. if (recognition.getLabel().equals(LABEL_GOLD_MINERAL)) {
  151. goldMineralX = (int) recognition.getLeft();
  152. } else if (silverMineral1X == -1) {
  153. silverMineral1X = (int) recognition.getLeft();
  154. } else {
  155. silverMineral2X = (int) recognition.getLeft();
  156. }
  157. }
  158. if (goldMineralX != -1 && silverMineral1X != -1 && silverMineral2X != -1) {
  159. if (goldMineralX < silverMineral1X && goldMineralX < silverMineral2X) {
  160. telemetry.addData("Gold Mineral Position", "Left");
  161. } else if (goldMineralX > silverMineral1X && goldMineralX > silverMineral2X) {
  162. telemetry.addData("Gold Mineral Position", "Right");
  163. } else {
  164. telemetry.addData("Gold Mineral Position", "Center");
  165. }
  166. }
  167. }
  168. telemetry.update();
  169. }
  170. }
  171. }
  172. }
  173.  
  174. if (tfod != null) {
  175. tfod.shutdown();
  176. }
  177. }
  178.  
  179. /**
  180. * Initialize the Vuforia localization engine.
  181. */
  182. private void initVuforia() {
  183. /*
  184. * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
  185. */
  186. VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
  187.  
  188. parameters.vuforiaLicenseKey = VUFORIA_KEY;
  189. parameters.cameraDirection = CameraDirection.BACK;
  190.  
  191. // Instantiate the Vuforia engine
  192. vuforia = ClassFactory.getInstance().createVuforia(parameters);
  193.  
  194. // Loading trackables is not necessary for the Tensor Flow Object Detection engine.
  195. }
  196.  
  197. /**
  198. * Initialize the Tensor Flow Object Detection engine.
  199. */
  200. private void initTfod() {
  201. int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifier(
  202. "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName());
  203. TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId);
  204. tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia);
  205. tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_GOLD_MINERAL, LABEL_SILVER_MINERAL);
  206. }
  207.  
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement