Advertisement
Guest User

Muie

a guest
Feb 28th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.98 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode;
  2.  
  3.  
  4. import com.acmerobotics.dashboard.FtcDashboard;
  5. import com.acmerobotics.dashboard.config.Config;
  6. import com.disnodeteam.dogecv.DogeCV;
  7. import com.disnodeteam.dogecv.detectors.skystone.SkystoneDetector;
  8. import com.disnodeteam.dogecv.detectors.skystone.StoneDetector;
  9. import com.disnodeteam.dogecv.filters.GrayscaleFilter;
  10. import com.disnodeteam.dogecv.filters.LeviColorFilter;
  11. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  12. import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
  13. import com.qualcomm.robotcore.util.ElapsedTime;
  14.  
  15. import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
  16. import org.firstinspires.ftc.teamcode.drive.mecanum.SampleMecanumDriveREVOptimized;
  17. import org.opencv.core.Point;
  18. import org.openftc.easyopencv.OpenCvCamera;
  19. import org.openftc.easyopencv.OpenCvCameraRotation;
  20. import org.openftc.easyopencv.OpenCvInternalCamera;
  21. import org.openftc.easyopencv.OpenCvWebcam;
  22.  
  23.  
  24. import java.util.List;
  25. import java.util.Locale;
  26.  
  27. /*
  28.  * Thanks to EasyOpenCV for the great API (and most of the example)
  29.  *
  30.  * Original Work Copright(c) 2019 OpenFTC Team
  31.  * Derived Work Copyright(c) 2019 DogeDevs
  32.  */
  33. @Config
  34. @TeleOp(name = "Skystone Detector OpenCv", group="DogeCV")
  35.  
  36. public class OpenCvShit extends LinearOpMode {
  37.     //private OpenCvCamera phoneCam;
  38.     private OpenCvWebcam webcam;
  39.     private SkystoneDetector skyStoneDetector;
  40.     private StoneDetector stoneDetector;
  41.     public static double yellowt = 100;
  42.     public static int grayt = 40;
  43.  
  44.     @Override
  45.     public void runOpMode() {
  46.         /*
  47.          * Instantiate an OpenCvCamera object for the camera we'll be using.
  48.          * In this sample, we're using the phone's internal camera. We pass it a
  49.          * CameraDirection enum indicating whether to use the front or back facing
  50.          * camera, as well as the view that we wish to use for camera monitor (on
  51.          * the RC phone). If no camera monitor is desired, use the alternate
  52.          * single-parameter constructor instead (commented out below)
  53.          */
  54.         //int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
  55.         //phoneCam = new OpenCvInternalCamera(OpenCvInternalCamera.CameraDirection.BACK, cameraMonitorViewId);
  56.         webcam = new OpenCvWebcam(hardwareMap.get(WebcamName.class,"camera porno"));
  57.         // OR...  Do Not Activate the Camera Monitor View
  58.         //phoneCam = new OpenCvInternalCamera(OpenCvInternalCamera.CameraDirection.BACK);
  59.  
  60.         /*
  61.          * Open the connection to the camera device
  62.          */
  63.         //phoneCam.openCameraDevice();
  64.         webcam.openCameraDevice();
  65.  
  66.         /*
  67.          * Specify the image processing pipeline we wish to invoke upon receipt
  68.          * of a frame from the camera. Note that switching pipelines on-the-fly
  69.          * (while a streaming session is in flight) *IS* supported.
  70.          */
  71.  
  72.         //phoneCam.setPipeline(skyStoneDetector);
  73.         stoneDetector = new StoneDetector();
  74.         skyStoneDetector = new SkystoneDetector();
  75.         webcam.setPipeline(skyStoneDetector);
  76.  
  77.  
  78.         /*
  79.          * Tell the camera to start streaming images to us! Note that you must make sure
  80.          * the resolution you specify is supported by the camera. If it is not, an exception
  81.          * will be thrown.
  82.          *
  83.          * Also, we specify the rotation that the camera is used in. This is so that the image
  84.          * from the camera sensor can be rotated such that it is always displayed with the image upright.
  85.          * For a front facing camera, rotation is defined assuming the user is looking at the screen.
  86.          * For a rear facing camera or a webcam, rotation is defined assuming the camera is facing
  87.          * away from the user.
  88.          */
  89.  
  90.         webcam.startStreaming(640,480, OpenCvCameraRotation.UPRIGHT);
  91.         skyStoneDetector.blackFilter = new GrayscaleFilter(0, grayt);
  92.         skyStoneDetector.yellowFilter = new LeviColorFilter(LeviColorFilter.ColorPreset.YELLOW, yellowt);
  93.         FtcDashboard ftcDashboard = FtcDashboard.getInstance();
  94.         ftcDashboard.startCameraStream(webcam, 30);
  95.         /*
  96.          * Wait for the user to press start on the Driver Station
  97.          */
  98.         waitForStart();
  99.  
  100.         while (opModeIsActive())
  101.         {
  102.             /*
  103.              * Send some stats to the telemetry
  104.              */
  105.            /* int nr =1;
  106.             for(Point point : stoneDetector.foundScreenPositions()){
  107.                 telemetry.addData("stone" + nr, point.x);
  108.                 nr++;
  109.             } */
  110.             String position = detecteaza();
  111.             telemetry.addData("poztie", position);
  112.             telemetry.addData("Stone Position X", skyStoneDetector.getScreenPosition().x);
  113.             telemetry.addData("Stone Position Y", skyStoneDetector.getScreenPosition().y);
  114.             telemetry.addData("Frame Count", webcam.getFrameCount());
  115.             telemetry.addData("FPS", String.format(Locale.US, "%.2f", webcam.getFps()));
  116.             telemetry.addData("Total frame time ms", webcam.getTotalFrameTimeMs());
  117.             telemetry.addData("Pipeline time ms", webcam.getPipelineTimeMs());
  118.             telemetry.addData("Overhead time ms", webcam.getOverheadTimeMs());
  119.             telemetry.addData("Theoretical max FPS", webcam.getCurrentPipelineMaxFps());
  120.             telemetry.update();
  121.  
  122.             /*
  123.              * NOTE: stopping the stream from the camera early (before the end of the OpMode
  124.              * when it will be automatically stopped for you) *IS* supported. The "if" statement
  125.              * below will stop streaming from the camera when the "A" button on gamepad 1 is pressed.
  126.              */
  127.             if(gamepad1.a)
  128.             {
  129.                 /*
  130.                  * IMPORTANT NOTE: calling stopStreaming() will indeed stop the stream of images
  131.                  * from the camera (and, by extension, stop calling your vision pipeline). HOWEVER,
  132.                  * if the reason you wish to stop the stream early is to switch use of the camera
  133.                  * over to, say, Vuforia or TFOD, you will also need to call closeCameraDevice()
  134.                  * (commented out below), because according to the Android Camera API documentation:
  135.                  *         "Your application should only have one Camera object active at a time for
  136.                  *          a particular hardware camera."
  137.                  *
  138.                  * NB: calling closeCameraDevice() will internally call stopStreaming() if applicable,
  139.                  * but it doesn't hurt to call it anyway, if for no other reason than clarity.
  140.                  *
  141.                  * NB2: if you are stopping the camera stream to simply save some processing power
  142.                  * (or battery power) for a short while when you do not need your vision pipeline,
  143.                  * it is recommended to NOT call closeCameraDevice() as you will then need to re-open
  144.                  * it the next time you wish to activate your vision pipeline, which can take a bit of
  145.                  * time. Of course, this comment is irrelevant in light of the use case described in
  146.                  * the above "important note".
  147.                  */
  148.                 webcam.stopStreaming();
  149.                 //webcam.closeCameraDevice();
  150.             }
  151.  
  152.             /*
  153.              * The viewport (if one was specified in the constructor) can also be dynamically "paused"
  154.              * and "resumed". The primary use case of this is to reduce CPU, memory, and power load
  155.              * when you need your vision pipeline running, but do not require a live preview on the
  156.              * robot controller screen. For instance, this could be useful if you wish to see the live
  157.              * camera preview as you are initializing your robot, but you no longer require the live
  158.              * preview after you have finished your initialization process; pausing the viewport does
  159.              * not stop running your pipeline.
  160.              *
  161.              * The "if" statements below will pause the viewport if the "X" button on gamepad1 is pressed,
  162.              * and resume the viewport if the "Y" button on gamepad1 is pressed.
  163.              */
  164.             else if(gamepad1.x) {
  165.                 webcam.pauseViewport();
  166.             }
  167.             else if(gamepad1.y) {
  168.                 webcam.resumeViewport();
  169.             }
  170.         }
  171.     }
  172.  
  173.     public String detecteaza(){
  174.         ElapsedTime runtime = new ElapsedTime();
  175.         double x_position =0 , y_position= 0;
  176.         sleep(200);
  177.  
  178.         runtime.reset();
  179.         while (runtime.seconds() < 1.5){
  180.             x_position = skyStoneDetector.getScreenPosition().x;
  181.             y_position = skyStoneDetector.getScreenPosition().y;
  182.         }
  183.  
  184.         if( x_position < 330){
  185.             return "stanga";
  186.         }else if( x_position > 380 && x_position< 500){
  187.             return "mijloc";
  188.         }else return  "dreapta";
  189.     }
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement