Advertisement
Guest User

BlueAuto

a guest
Dec 6th, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. public class AUTOMATION extends LinearOpMode {
  2.  
  3. private DcMotor motor4;
  4. private DcMotor motor3;
  5. private DcMotor motor1;
  6. private DcMotor motor2;
  7. private DcMotor lift;
  8. private CRServo pivot;
  9. private Servo grabber;
  10.  
  11. /*
  12. * Specify the source for the Tensor Flow Model.
  13. * If the TensorFlowLite object model is included in the Robot Controller App as an "asset",
  14. * the OpMode must to load it using loadModelFromAsset(). However, if a team generated model
  15. * has been downloaded to the Robot Controller's SD FLASH memory, it must to be loaded using loadModelFromFile()
  16. * Here we assume it's an Asset. Also see method initTfod() below .
  17. */
  18. private static final String TFOD_MODEL_ASSET = "PowerPlay.tflite";
  19. private static final String blueCone = "/sdcard/FIRST/tflitemodels/BlueCone.tflite";
  20. // private static final String TFOD_MODEL_FILE = "/sdcard/FIRST/tflitemodels/CustomTeamModel.tflite";
  21.  
  22.  
  23. private static final String[] LABELS = {
  24. "Bolt",
  25. "Bulb",
  26. "Panel"
  27. };
  28.  
  29. private static final String[] coneLabels = {
  30. "bCone",
  31. };
  32.  
  33. private static final String VUFORIA_KEY =
  34. "Ad4M7GP/////AAABmeZxPeyoWU0vk45dz6VmWoFZtZ3Beei2zVQN6SQjXKbzyNWwHkloibWVtf4m7s4WMhM6+3JUvKVBmBk0tPHahpeDFQi8gZ+x3/44X4M9xBxSoycd6WNwAst5YNIh8fN11Ml7v3tyxEtX7olacEfty/hSqWQm6GfwZBpgqXiJKJkjYy0K8XOutYdsKxVajNc7I636HDd970RxlvB73DgnsiFEtlX5CG/f4UFI1w99II6RKCj8fgFsaihHm1v2iWddGGOF24fcQF8bBejtwa4Hi9rs4ohh4t7yr6hpNraG6avcvtzMQzXhIbe1aWkaFSViHAMMpUDHoV/yIjFop6G7pQ2D7ysHtKidCRrM5Tq6mLaL";
  35.  
  36. /**
  37. * {@link #vuforia} is the variable we will use to store our instance of the Vuforia
  38. * localization engine.
  39. */
  40. private VuforiaLocalizer vuforia;
  41.  
  42. /**
  43. * {@link #tfod} is the variable we will use to store our instance of the TensorFlow Object
  44. * Detection engine.
  45. */
  46. private TFObjectDetector tfod;
  47.  
  48. @Override
  49. public void runOpMode() {
  50. // The TFObjectDetector uses the camera frames from the VuforiaLocalizer, so we create that
  51. // first.
  52. initVuforia();
  53. initTfod();
  54.  
  55. motor4 = hardwareMap.get(DcMotor.class, "motor 4");
  56. motor3 = hardwareMap.get(DcMotor.class, "motor 3");
  57. motor1 = hardwareMap.get(DcMotor.class, "motor 1");
  58. motor2 = hardwareMap.get(DcMotor.class, "motor 2");
  59. lift = hardwareMap.get(DcMotor.class, "lift");
  60. pivot = hardwareMap.get(CRServo.class, "pivot");
  61. grabber = hardwareMap.get(Servo.class, "grabber");
  62.  
  63.  
  64. /**
  65. * Activate TensorFlow Object Detection before we wait for the start command.
  66. * Do it here so that the Camera Stream window will have the TensorFlow annotations visible.
  67. **/
  68. if (tfod != null) {
  69. tfod.activate();
  70.  
  71. // The TensorFlow software will scale the input images from the camera to a lower resolution.
  72. // This can result in lower detection accuracy at longer distances (> 55cm or 22").
  73. // If your target is at distance greater than 50 cm (20") you can increase the magnification value
  74. // to artificially zoom in to the center of image. For best results, the "aspectRatio" argument
  75. // should be set to the value of the images used to create the TensorFlow Object Detection model
  76. // (typically 16/9).
  77. tfod.setZoom(1.0, 16.0/9.0);
  78. }
  79.  
  80. /** Wait for the game to begin */
  81. telemetry.addData(">", "Press Play to start op mode");
  82. telemetry.update();
  83. waitForStart();
  84.  
  85. if (opModeIsActive()) {
  86. while (opModeIsActive()) {
  87. if (tfod != null) {
  88. // getUpdatedRecognitions() will return null if no new information is available since
  89. // the last time that call was made.
  90. List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();
  91.  
  92. if (updatedRecognitions != null && updatedRecognitions.size() > 0) {
  93. telemetry.addData("# Objects Detected", updatedRecognitions.size());
  94. telemetry.addData(updatedRecognitions.get(0).getLabel(), " ");
  95. if (updatedRecognitions.get(0).getLabel() == "Bolt"){
  96. motor1.setPower(1);
  97. motor2.setPower(1);
  98. motor3.setPower(-1);
  99. motor4.setPower(-1);
  100. sleep(1150);
  101. motor1.setPower(-1);
  102. motor2.setPower(1);
  103. motor3.setPower(-1);
  104. motor4.setPower(1);
  105. sleep(1300);
  106. motor1.setPower(1);
  107. motor2.setPower(1);
  108. motor3.setPower(-1);
  109. motor4.setPower(-1);
  110. sleep(200);
  111. motor1.setPower(0);
  112. motor2.setPower(0);
  113. motor3.setPower(0);
  114. motor4.setPower(0);
  115.  
  116. }else if(updatedRecognitions.get(0).getLabel() == "Bulb"){
  117. motor1.setPower(1);
  118. motor2.setPower(1);
  119. motor3.setPower(-1);
  120. motor4.setPower(-1);
  121. sleep(1150);
  122. motor1.setPower(0);
  123. motor2.setPower(0);
  124. motor3.setPower(0);
  125. motor4.setPower(0);
  126. }else if(updatedRecognitions.get(0).getLabel() == "Panel"){
  127. motor1.setPower(1);
  128. motor2.setPower(1);
  129. motor3.setPower(-1);
  130. motor4.setPower(-1);
  131. sleep(1000);
  132. motor1.setPower(1);
  133. motor2.setPower(-1);
  134. motor3.setPower(1);
  135. motor4.setPower(-1);
  136. sleep(1400);
  137. motor1.setPower(1);
  138. motor2.setPower(1);
  139. motor3.setPower(-1);
  140. motor4.setPower(-1);
  141. sleep(100);
  142. motor1.setPower(0);
  143. motor2.setPower(0);
  144. motor3.setPower(0);
  145. motor4.setPower(0);
  146. }
  147. // step through the list of recognitions and display image position/size information for each one
  148. // Note: "Image number" refers to the randomized image orientation/number
  149. }
  150. }
  151. telemetry.update();
  152. }
  153. }
  154. }
  155.  
  156. /**
  157. * Initialize the Vuforia localization engine.
  158. */
  159. private void initVuforia() {
  160. /*
  161. * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
  162. */
  163. VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
  164.  
  165. parameters.vuforiaLicenseKey = VUFORIA_KEY;
  166. parameters.cameraName = hardwareMap.get(WebcamName.class, "Eye");
  167.  
  168. // Instantiate the Vuforia engine
  169. vuforia = ClassFactory.getInstance().createVuforia(parameters);
  170. }
  171.  
  172. /**
  173. * Initialize the TensorFlow Object Detection engine.
  174. */
  175. private void initTfod() {
  176. int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifier(
  177. "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName());
  178. TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId);
  179. tfodParameters.minResultConfidence = 0.75f;
  180. tfodParameters.isModelTensorFlow2 = true;
  181. tfodParameters.inputSize = 300;
  182. tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia);
  183.  
  184. // Use loadModelFromAsset() if the TF Model is built in as an asset by Android Studio
  185. // Use loadModelFromFile() if you have downloaded a custom team model to the Robot Controller's FLASH.
  186. tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABELS);
  187. tfod.loadModelFromFile(blueCone, coneLabels);
  188.  
  189. // tfod.loadModelFromFile(TFOD_MODEL_FILE, LABELS);
  190. }
  191. }
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement