Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. package org.usfirst.frc.team6083.robot;
  2.  
  3. import java.io.PipedReader;
  4.  
  5. import org.opencv.core.Rect;
  6. import org.opencv.imgproc.Imgproc;
  7.  
  8. import edu.wpi.cscore.AxisCamera;
  9. import edu.wpi.cscore.CvSource;
  10. import edu.wpi.first.wpilibj.CameraServer;
  11. import edu.wpi.first.wpilibj.IterativeRobot;
  12. import edu.wpi.first.wpilibj.VictorSP;
  13. import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
  14. import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
  15. import edu.wpi.first.wpilibj.vision.VisionThread;
  16.  
  17. /**
  18. * The VM is configured to automatically run this class, and to call the
  19. * functions corresponding to each mode, as described in the IterativeRobot
  20. * documentation. If you change the name of this class or the package after
  21. * creating this project, you must also update the manifest file in the resource
  22. * directory.
  23. */
  24. public class Robot extends IterativeRobot {
  25. final String defaultAuto = "Default";
  26. final String customAuto = "My Auto";
  27. String autoSelected;
  28. SendableChooser<String> chooser = new SendableChooser<>();
  29. static final int width = 640, height = 480;
  30. double centerX, left, length;
  31. private VisionThread visionThread;
  32. Object imglock = new Object();
  33. VictorSP motor1 = new VictorSP(1);
  34. VictorSP motor2 = new VictorSP(2);
  35. AxisCamera camera = new AxisCamera("Axis Camera 1", "axis-camera1.local");
  36. private static double speedl, speedr, max_turn = 0.2;
  37. private static int item_width = 75;
  38.  
  39. CvSource output;
  40.  
  41. /**
  42. * This function is run when the robot is first started up and should be
  43. * used for any initialization code.
  44. */
  45. @Override
  46. public void robotInit() {
  47. chooser.addDefault("Def ault Auto", defaultAuto);
  48. chooser.addObject("My Auto", customAuto);
  49. SmartDashboard.putData("Auto choices", chooser);
  50.  
  51. camera.setResolution(width, height);
  52. output = CameraServer.getInstance().putVideo("Processed: ", 640, 360);
  53.  
  54. visionThread = new VisionThread(camera, new GripPipeline(), pipeline -> {
  55. if (!pipeline.filterContoursOutput().isEmpty()) {
  56. Rect r = Imgproc.boundingRect(pipeline.filterContoursOutput().get(0));
  57. synchronized (imglock) {
  58. centerX = r.x + (r.width / 2);
  59. SmartDashboard.putNumber("centerX", centerX);
  60. left = r.x;
  61. length = r.width;
  62. }
  63.  
  64. }
  65. output.putFrame(pipeline.rgbThresholdOutput());
  66. });
  67. visionThread.start();
  68.  
  69. SmartDashboard.putNumber("turn", 0);
  70. SmartDashboard.putNumber("forward", 0);
  71. SmartDashboard.putNumber("x", 0);
  72. SmartDashboard.putNumber("speedl", 0);
  73. SmartDashboard.putNumber("speedr", 0);
  74. SmartDashboard.putNumber("turn_speed", 0);
  75. }
  76.  
  77. /**
  78. * This autonomous (along with the chooser code above) shows how to select
  79. * between different autonomous modes using the dashboard. The sendable
  80. * chooser code works with the Java SmartDashboard. If you prefer the
  81. * LabVIEW Dashboard, remove all of the chooser code and uncomment the
  82. * getString line to get the auto name from the text box below the Gyro
  83. *
  84. * You can add additional auto modes by adding additional comparisons to the
  85. * switch structure below with additional strings. If using the
  86. * SendableChooser make sure to add them to the chooser code above as well.
  87. */
  88. @Override
  89. public void autonomousInit() {
  90. autoSelected = chooser.getSelected();
  91. // autoSelected = SmartDashboard.getString("Auto Selector",
  92. // defaultAuto);
  93. System.out.println("Auto selected: " + autoSelected);
  94. }
  95.  
  96. /**
  97. * This function is called periodically during autonomous
  98. */
  99. @Override
  100. public void autonomousPeriodic() {
  101. double centerX, left, length, forward;
  102. synchronized (imglock) {
  103. centerX = this.centerX;
  104. left = this.left;
  105. length = this.length;
  106. }
  107. double turn = centerX - (width / 2);
  108. if (length > 5) {
  109. forward=length/item_width;
  110. } else {
  111. forward = 0;
  112. }
  113. if(Math.abs(turn * 0.001)>max_turn){
  114. speedl=turn/Math.abs(turn)*0.2;
  115. speedr=turn/Math.abs(turn)*-0.2;
  116. }else if(Math.abs(turn * 0.001)<0.07){
  117. speedl =0;
  118. speedr =0;
  119. }else{
  120. speedl =turn * 0.001;
  121. speedr =-turn * 0.001;
  122. }
  123. if(forward>1.05){
  124. speedl-=0.15;
  125. speedr-=0.15;
  126. }else if(forward<0.9&&forward>0.5){
  127. speedl+=0.15;
  128. speedr+=0.15;
  129. }
  130.  
  131. motor1.set(speedl);//+ forward
  132. motor2.set(-speedr);//+ back
  133. SmartDashboard.putNumber("turn", turn);
  134. SmartDashboard.putNumber("forward", forward);
  135. SmartDashboard.putNumber("turn_speed", turn*0.001);
  136. SmartDashboard.putNumber("centerX", centerX);
  137. SmartDashboard.putNumber("speedl", speedl);
  138. SmartDashboard.putNumber("speedr", speedr);
  139. }
  140.  
  141. /**
  142. * This function is called periodically during operator control
  143. */
  144. @Override
  145. public void teleopPeriodic() {
  146.  
  147. }
  148.  
  149. /**
  150. * This function is called periodically during test mode
  151. */
  152. @Override
  153. public void testPeriodic() {
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement