Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.77 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.Autonomous;
  33. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  34. import com.qualcomm.robotcore.hardware.DcMotor;
  35. import com.qualcomm.robotcore.hardware.Servo;
  36.  
  37. import org.firstinspires.ftc.robotcore.external.ClassFactory;
  38. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
  39. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer.CameraDirection;
  40. import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
  41. import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
  42.  
  43. import java.util.List;
  44.  
  45.  
  46. @Autonomous(name = "AutonomiecompletaV3", group = "Concept")
  47. //@Disabled
  48. public class AutonomiecompletaV3 extends LinearOpMode {
  49. private static final String TFOD_MODEL_ASSET = "RoverRuckus.tflite";
  50. private static final String LABEL_GOLD_MINERAL = "Gold Mineral";
  51. private static final String LABEL_SILVER_MINERAL = "Silver Mineral";
  52.  
  53. DcMotor leftMotor;
  54. DcMotor rightMotor;
  55. DcMotor motorlander;
  56. DcMotor motorlandersus;
  57. DcMotor motorbratright,motorbratleft;
  58. Servo servomarker;
  59. double tpower=0.5;
  60. //int pozitie=0;
  61.  
  62.  
  63. private static final String VUFORIA_KEY = "AQavSyD/////AAABmRp+aJs4/0GOvHSzkUoszhAxYckIV6Db8eBIYLJDuXeoiOq0EgfMitokZIH6N2p9ITLl9GNugwEmUlyDo4G+6Oq10VwlFMlSUl+4a1c69ko8Eo215Gdu7TaOIz7Yy53z1VbCoz1TJ7C6L71W74hsmX9Tb9y0XjrRpjvxF8/YWfU0Y+Eil6UTYLqlycZ2MugKt7dK8ZlueYKuJTVEGxAfVIZDWwSDj13WqCVIhONEXN2lfg78k1xxeJR/6aAyCNc2CpQSQHwF2sxp6+7RDBxWwGQKNVNuhlz8HY3sfe9anu5Cu5syyNJlEOuXQKthAlgxWrO6VTswvTLo/gocHvpPbFh2yLs8gOl1exVCVwv9hibU";
  64.  
  65. /**
  66. * {@link #vuforia} is the variable we will use to store our instance of the Vuforia
  67. * localization engine.
  68. */
  69. private VuforiaLocalizer vuforia;
  70.  
  71. /**
  72. * {@link #tfod} is the variable we will use to store our instance of the Tensor Flow Object
  73. * Detection engine.
  74. */
  75. private TFObjectDetector tfod;
  76.  
  77. @Override
  78. public void runOpMode() {
  79. // The TFObjectDetector uses the camera frames from the VuforiaLocalizer, so we create that
  80. // first.
  81. leftMotor=hardwareMap.dcMotor.get("Left_Motor");
  82. rightMotor=hardwareMap.dcMotor.get("Right_Motor");
  83. motorlander=hardwareMap.dcMotor.get("motorlander");
  84. motorlandersus=hardwareMap.dcMotor.get("motorlandersus");
  85. motorbratright=hardwareMap.dcMotor.get("motorbratright");
  86. motorbratleft=hardwareMap.dcMotor.get("motorbratleft");
  87. servomarker=hardwareMap.servo.get("servomarker");
  88. leftMotor.setDirection(DcMotor.Direction.FORWARD);
  89. rightMotor.setDirection(DcMotor.Direction.REVERSE);
  90. motorlandersus.setDirection(DcMotor.Direction.REVERSE);
  91. motorbratleft.setDirection(DcMotor.Direction.REVERSE);
  92. servomarker.setPosition(1.0);
  93.  
  94. initVuforia();
  95.  
  96. if (ClassFactory.getInstance().canCreateTFObjectDetector()) {
  97. initTfod();
  98. } else {
  99. telemetry.addData("Sorry!", "This device is not compatible with TFOD");
  100. }
  101.  
  102. /** Wait for the game to begin */
  103. telemetry.addData(">", "Press Play to start tracking");
  104. telemetry.update();
  105. waitForStart();
  106.  
  107. coboaradepelander(1800);
  108. moveforward(200);
  109. turnright(1200);
  110. moveforward(50);
  111. turnright(450);
  112. movebackward(10);
  113. moveforward(120);
  114. turnleft(350);
  115. moveforward(110);
  116.  
  117. sleep(1000);
  118. if (opModeIsActive()) {
  119. /** Activate Tensor Flow Object Detection. */
  120. if (tfod != null) {
  121. tfod.activate();
  122. }
  123.  
  124. while (opModeIsActive()) {
  125.  
  126. if (tfod != null) {
  127. // getUpdatedRecognitions() will return null if no new information is available since
  128. // the last time that call was made.
  129. List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();
  130. if (updatedRecognitions != null) {
  131. // telemetry.addData("# Object Detected", updatedRecognitions.size());
  132. if (updatedRecognitions.size() >= 1) {
  133. int goldMineralX = -1;
  134. int silverMineral1X = -1;
  135. int silverMineral2X = -1;
  136. int ok=0;
  137. int c=0;
  138. for (Recognition recognition : updatedRecognitions) {
  139. while (ok == 0) {c++;telemetry.addData("# Object Detected", updatedRecognitions.size());
  140. sleep(500);
  141. if (recognition.getLabel().equals(LABEL_GOLD_MINERAL)) {
  142. moveforward(500);
  143. ok = 1;
  144. }
  145. else {
  146. movebackward(100);
  147. turnright(300);
  148. moveforward(100);
  149. sleep(2000);
  150.  
  151. }
  152. }
  153.  
  154. if(c==1) mineralstanga();
  155. else if(c==2) mineralcentru();
  156. else if(c==3) mineraldreapta();
  157. }
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164. /* if (goldMineralX != -1 && silverMineral1X != -1 && silverMineral2X != -1) {
  165. if (goldMineralX < silverMineral1X && goldMineralX < silverMineral2X) {
  166. telemetry.addData("Gold Mineral Position", "Left");
  167. mineralstanga();
  168.  
  169. } else if (goldMineralX > silverMineral1X && goldMineralX > silverMineral2X) {
  170. telemetry.addData("Gold Mineral Position", "Right");
  171.  
  172. mineraldreapta();
  173.  
  174. } else {
  175. telemetry.addData("Gold Mineral Position", "Center");
  176.  
  177. mineralcentru();
  178.  
  179.  
  180.  
  181.  
  182. }
  183.  
  184. }
  185. */
  186. }
  187. telemetry.update();
  188. }
  189. }
  190. }
  191. }
  192.  
  193.  
  194. if (tfod != null) {
  195. tfod.shutdown();
  196. }
  197. }
  198.  
  199. /**
  200. * Initialize the Vuforia localization engine.
  201. */
  202. private void initVuforia() {
  203. /*
  204. * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
  205. */
  206. VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
  207.  
  208. parameters.vuforiaLicenseKey = VUFORIA_KEY;
  209. parameters.cameraDirection = CameraDirection.BACK;
  210.  
  211. // Instantiate the Vuforia engine
  212. vuforia = ClassFactory.getInstance().createVuforia(parameters);
  213.  
  214. // Loading trackables is not necessary for the Tensor Flow Object Detection engine.
  215. }
  216.  
  217. /**
  218. * Initialize the Tensor Flow Object Detection engine.
  219. */
  220. private void initTfod() {
  221. int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifier(
  222. "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName());
  223. TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId);
  224. tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia);
  225. tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_GOLD_MINERAL, LABEL_SILVER_MINERAL);
  226. tfodParameters.minimumConfidence=1.0;
  227. }
  228.  
  229.  
  230. //Functii
  231.  
  232.  
  233. public void movebackward(int time)
  234. {rightMotor.setPower(1.0);
  235. leftMotor.setPower(1.0);
  236. sleep(time);
  237.  
  238. rightMotor.setPower(0.0);
  239. leftMotor.setPower(0.0);
  240. sleep(500);
  241. }
  242.  
  243. public void moveforward(int time)
  244. {rightMotor.setPower(-1.0);
  245. leftMotor.setPower(-1.0);
  246. sleep(time);
  247.  
  248. rightMotor.setPower(0.0);
  249. leftMotor.setPower(0.0);
  250. sleep(500);
  251. }
  252.  
  253. public void turnright(int time)
  254. {leftMotor.setPower(-1.0);
  255. rightMotor.setPower(1.0);
  256. sleep(time);
  257.  
  258. rightMotor.setPower(0.0);
  259. leftMotor.setPower(0.0);
  260. sleep(500);
  261. }
  262.  
  263. public void turnleft(int time)
  264. {rightMotor.setPower(-1.0);
  265. leftMotor.setPower(1.0);
  266. sleep(time);
  267.  
  268. rightMotor.setPower(0.0);
  269. leftMotor.setPower(0.0);
  270. sleep(500);
  271.  
  272. }
  273.  
  274. public void coboaradepelander(int time)
  275. {
  276. motorlander.setPower(0.5);
  277. motorlandersus.setPower(0.5);
  278. sleep(time);
  279.  
  280. motorlander.setPower(0.0);
  281. motorlandersus.setPower(0.0);
  282. sleep(500);
  283. }
  284.  
  285. public void revenireBratRidicare(int time)
  286. {
  287. motorlander.setPower(-0.5);
  288. motorlandersus.setPower(-0.5);
  289. sleep(time);
  290.  
  291. motorlander.setPower(0.0);
  292. motorlandersus.setPower(0.0);
  293. sleep(500);
  294. }
  295.  
  296. public void datjosmarker()
  297. {servomarker.setPosition(0.0);
  298.  
  299. }
  300.  
  301. public void coboarabrat(int time)
  302. {motorbratright.setPower(-1.0);
  303. motorbratleft.setPower(-1.0);
  304. sleep(time);
  305.  
  306. motorbratright.setPower(0.0);
  307. motorbratleft.setPower(0.0);
  308. sleep(500);
  309. }
  310.  
  311. public void mineralstanga()
  312. {
  313. moveforward(500);
  314. turnright(250);
  315. moveforward(500);
  316. /* datjosmarker();
  317. turnleft(2000);
  318. moveforward(5000);
  319. coboarabrat(1500);*/
  320. }
  321.  
  322. public void mineralcentru()
  323. {
  324. moveforward(1000);
  325. /*datjosmarker();
  326. turnleft(400);
  327. moveforward(1000);
  328. turnleft(70);
  329. moveforward(1500);
  330. coboarabrat(1500);*/
  331.  
  332. }
  333.  
  334. public void mineraldreapta()
  335. {
  336. turnright(250);
  337. moveforward(500);
  338. turnleft(250);
  339. moveforward(500);
  340. /*datjosmarker();
  341. turnleft(2000);
  342. moveforward(5000);
  343. coboarabrat(1500);*/
  344. }
  345.  
  346.  
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement