AsianInvasion

CurrentAutonCode

Feb 21st, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.26 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode;
  2.  
  3. import com.qualcomm.hardware.bosch.BNO055IMU;
  4. import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
  5. import com.qualcomm.robotcore.eventloop.opmode.OpMode;
  6. import com.qualcomm.robotcore.hardware.DcMotor;
  7. import com.qualcomm.robotcore.hardware.Servo;
  8.  
  9. import org.firstinspires.ftc.robotcore.external.ClassFactory;
  10. import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
  11. import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
  12. import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
  13. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
  14. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer.CameraDirection;
  15. import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
  16. import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
  17.  
  18. import java.util.List;
  19.  
  20. @Autonomous(name = "Wen'asdasd")
  21.  
  22. public class TensorFlowAlonePracticePurged extends OpMode {
  23.  
  24.  
  25. //fellow comrades, please note that the encoder ticks below are just for reference, since they
  26. //must be calculated later on
  27. //this is for crater
  28.  
  29.  
  30. //@Disa
  31.  
  32. //Declare OpMode members
  33. private DcMotor upperLeftDrive = null;
  34. private DcMotor lowerLeftDrive = null;
  35. private DcMotor upperRightDrive = null;
  36. private DcMotor lowerRightDrive = null;
  37. private DcMotor hook = null;
  38. private Servo teamMarker = null;
  39. private DcMotor intakeArm = null;
  40. private BNO055IMU imu;
  41. private AngleUnit unit = AngleUnit.DEGREES;
  42.  
  43.  
  44. //these are example ratios and circumferences for the drive train,
  45. //change accordingly, btw for 2:1 it is 2, 80 tooth to 40 tooth
  46. //for gyro, i am assuming that the head of the robot is at 0 initially, and goes clockwise
  47.  
  48. private double DriveGearRatio = 1.0 / 2.0;
  49. private double DriveCircumference = (1.97 * 2) * Math.PI;
  50.  
  51. //not for the actual motor, but for the final gear
  52. private int ticksPerRotation = (int) (DriveGearRatio * 1120);
  53.  
  54. private static final String TFOD_MODEL_ASSET = "RoverRuckus.tflite";
  55. private static final String LABEL_GOLD_MINERAL = "Gold Mineral";
  56. private static final String LABEL_SILVER_MINERAL = "Silver Mineral";
  57. private static final String VUFORIA_KEY = "AbZUuPf/////AAAAGUmS0Chan00iu7rnRhzu63+JgDtPo889M6dNtjvv+WKxiMJ8w2DgSJdM2/zEI+a759I7DlPj++D2Ryr5sEHAg4k1bGKdo3BKtkSeh8hCy78w0SIwoOACschF/ImuyP/V259ytjiFtEF6TX4teE8zYpQZiVkCQy0CmHI9Ymoa7NEvFEqfb3S4P6SicguAtQ2NSLJUX+Fdn49SEJKvpSyhwyjbrinJbak7GWqBHcp7fGh7TNFcfPFMacXg28XxlvVpQaVNgkvuqolN7wkTiR9ZMg6Fnm0zN4Xjr5lRtDHeE51Y0bZoBUbyLWSA+ts3SyDjDPPUU7GMI+Ed/ifb0csVpM12aOiNr8d+HsfF2Frnzrj2";
  58. private VuforiaLocalizer vuforia;
  59. private TFObjectDetector tfod;
  60. private String mineralPosition = "";
  61. private int step = 1;
  62. private int nextstep = 0;
  63. private int targetPosition = 0;
  64. private int position = 1;
  65. private boolean fl;
  66. private boolean fr;
  67. private boolean dl;
  68. private boolean dr;
  69. private double gyroTarget;
  70. private double gyroRange;
  71. private double minSpeed;
  72. private double addSpeed;
  73. private double power;
  74. private double distance;
  75. private long startTime;
  76. private int encoderSign;
  77. private List<Recognition> alwaysNonNull;
  78.  
  79.  
  80. @Override
  81. public void init() {
  82. // The TFObjectDetector uses the camera frames from the VuforiaLocalizer, so we create that
  83. // first.
  84.  
  85. telemetry.addData("Status", "Initialized");
  86. telemetry.update();
  87.  
  88. upperLeftDrive = hardwareMap.get(DcMotor.class, "front left");
  89. upperRightDrive = hardwareMap.get(DcMotor.class, "front right");
  90. lowerLeftDrive = hardwareMap.get(DcMotor.class, "back left");
  91. lowerRightDrive = hardwareMap.get(DcMotor.class, "back right");
  92. teamMarker = hardwareMap.get(Servo.class, "team marker");
  93. hook = hardwareMap.get(DcMotor.class, "hook");
  94. intakeArm = hardwareMap.get(DcMotor.class, "intake arm");
  95. imu = hardwareMap.get(BNO055IMU.class, "imu");
  96.  
  97. //initialization
  98. upperLeftDrive.setDirection(DcMotor.Direction.FORWARD);
  99. upperRightDrive.setDirection(DcMotor.Direction.REVERSE);
  100. lowerLeftDrive.setDirection(DcMotor.Direction.FORWARD);
  101. lowerRightDrive.setDirection(DcMotor.Direction.REVERSE);
  102.  
  103. initVuforia();
  104. initTfod();
  105. if (tfod != null) {
  106. tfod.activate();
  107. }
  108. }
  109.  
  110. @Override
  111. public void init_loop() {
  112. }
  113.  
  114. @Override
  115. public void start() {
  116. }
  117.  
  118. //if opMode is active
  119.  
  120. //assumes there's a bug in the code because there is no communication between the robot and the phones
  121. //state machine, upon finishing a state, one can go to another state
  122. //similar to a flow chart, its linear
  123. //make one variable in case 0, but in each case, make it different things, different ways to refer to the same object
  124. //do hook and vuforia
  125. //break makes it exit the switch statement
  126. //make a variable that is 8400
  127. @Override
  128. //you can make a varible hold different objects if you want, for example different motors
  129. public void loop() {
  130.  
  131. // getUpdatedRecognitions() will return null if no new information is available since
  132. // the last time that call was made.
  133. List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();
  134. if (updatedRecognitions == null) {
  135.  
  136. } else {
  137. alwaysNonNull = updatedRecognitions;
  138. }
  139. if (alwaysNonNull != null) {
  140. telemetry.addData("# Object Detected", alwaysNonNull.size());
  141. for (int i = 0; i < alwaysNonNull.size(); i++) {
  142. Recognition recognition = alwaysNonNull.get(i);
  143. //get the label of the recognition according to the neural
  144. //network
  145.  
  146. telemetry.addData(Integer.toString(i) + recognition.getLabel() + ": ", recognition.getLeft());
  147.  
  148. //otherwise strafe to the other one to the right
  149.  
  150. }
  151.  
  152.  
  153. } else {
  154. telemetry.addLine("feels bad man");
  155. }
  156. }
  157.  
  158.  
  159. @Override
  160. public void stop() {
  161. if (tfod != null) {
  162. tfod.shutdown();
  163. }
  164. telemetry.addLine("Long live the russian regime! (aka Sasha)");
  165. telemetry.update();
  166. }
  167.  
  168. private void initVuforia() {
  169. //Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
  170. //config the vuforia with parameters and variables
  171.  
  172. VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
  173.  
  174. parameters.vuforiaLicenseKey = VUFORIA_KEY;
  175. parameters.cameraDirection = CameraDirection.FRONT;
  176.  
  177. // Instantiate the Vuforia engine
  178. vuforia = ClassFactory.getInstance().createVuforia(parameters);
  179.  
  180. // Loading trackables is not necessary for the Tensor Flow Object Detection engine.
  181. }
  182.  
  183. /**
  184. * Initialize the Tensor Flow Object Detection engine.
  185. */
  186. public void initTfod() {
  187. //stores an id for where to later draw the image, a window per se
  188. int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifier(
  189. "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName());
  190.  
  191. TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId);
  192.  
  193. //implements the parameters, and passes the image to tfod
  194. tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia);
  195.  
  196. /* professor Wen states that this is a neural network, so each parameter is linked
  197. to a specific identification system and we are simply giving names to those final
  198. identifications to reference them later on */
  199. tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_GOLD_MINERAL, LABEL_SILVER_MINERAL);
  200. }
  201.  
  202. public void DrivePosition(int position) {
  203. //apparently using negative or positive power here doesn't matter
  204. hook.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  205.  
  206. hook.setTargetPosition(position);
  207.  
  208. hook.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  209.  
  210. hook.setPower(1);
  211. }
  212.  
  213. public void DrivePositionArm(int position) {
  214. intakeArm.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  215.  
  216. intakeArm.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  217.  
  218. intakeArm.setTargetPosition(position);
  219.  
  220. intakeArm.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  221.  
  222. intakeArm.setPower(1);
  223. }
  224.  
  225. //for forwards, power = -1, and for backwards, power = 1
  226. public int DriveTrain(double distance) {
  227. //example gear ratio, 2:1
  228. //example circumference
  229. //distance is in inches
  230.  
  231. double circumferenceTraveled = distance / DriveCircumference;
  232.  
  233. int position = (int) (ticksPerRotation * circumferenceTraveled);
  234.  
  235. position = encoderSign * position;
  236.  
  237. double power = powerScaleDistance(distance);
  238.  
  239. upperLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  240. upperLeftDrive.setTargetPosition(position);
  241. upperLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  242.  
  243. lowerLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  244. lowerLeftDrive.setTargetPosition(position);
  245. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  246.  
  247. upperRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  248. upperRightDrive.setTargetPosition(position);
  249. upperRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  250.  
  251. lowerRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  252. lowerRightDrive.setTargetPosition(position);
  253. lowerRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  254.  
  255. upperRightDrive.setPower(power);
  256. lowerLeftDrive.setPower(power);
  257. upperRightDrive.setPower(power);
  258. lowerRightDrive.setPower(power);
  259.  
  260. return position;
  261. }
  262.  
  263. //power scale according to distance
  264. public double powerScaleDistance(double distance) {
  265. double DrivePower = 0;
  266. if (distance <= 24) {
  267. DrivePower = 0.5;
  268. } else if (distance > 24) {
  269. DrivePower = 1;
  270. }
  271. return DrivePower;
  272. }
  273.  
  274. public int strafeleft(double distance) {
  275. //guess and check, then check formulas later on
  276. //assuming a similar relationship from distance to ticks in the drive train, probably going to
  277. //be much less
  278.  
  279. //how much the wheel actually needs to turn
  280. double circumferenceTraveled = distance / DriveCircumference;
  281.  
  282. //here position must be positive
  283. int position = (int) (ticksPerRotation * circumferenceTraveled);
  284.  
  285. double power = powerScaleDistance(distance);
  286.  
  287. upperLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  288. upperLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  289. upperLeftDrive.setTargetPosition(-position);
  290. upperLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  291. upperLeftDrive.setPower(powerScaleDistance(power));
  292.  
  293. upperRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  294. upperRightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  295. upperRightDrive.setTargetPosition(position);
  296. upperRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  297. upperRightDrive.setPower(powerScaleDistance(power));
  298.  
  299. lowerLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  300. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  301. lowerLeftDrive.setTargetPosition(position);
  302. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  303. lowerLeftDrive.setPower(powerScaleDistance(power));
  304.  
  305. lowerRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  306. lowerRightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  307. lowerRightDrive.setTargetPosition(-position);
  308. lowerRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  309. lowerRightDrive.setPower(powerScaleDistance(power));
  310.  
  311. return position;
  312. }
  313.  
  314. public int straferight(double distance) {
  315. //guess and check, then check formulas later on
  316. //assuming a similar relationship from distance to ticks in the drive train, probably going to
  317. //be much less
  318.  
  319. double circumferenceTraveled = distance / DriveCircumference;
  320.  
  321. int position = (int) (ticksPerRotation * circumferenceTraveled);
  322.  
  323. double power = powerScaleDistance(distance);
  324.  
  325. upperLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  326. upperLeftDrive.setTargetPosition(position);
  327. upperLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  328.  
  329. upperLeftDrive.setPower(powerScaleDistance(power));
  330.  
  331. upperRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  332. upperRightDrive.setTargetPosition(-position);
  333. upperRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  334.  
  335.  
  336. lowerLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  337. lowerLeftDrive.setTargetPosition(-position);
  338. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  339.  
  340.  
  341. lowerRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  342. lowerRightDrive.setTargetPosition(position);
  343. lowerRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  344.  
  345. upperLeftDrive.setPower(powerScaleDistance(power));
  346. upperRightDrive.setPower(powerScaleDistance(power));
  347. lowerLeftDrive.setPower(powerScaleDistance(power));
  348. lowerRightDrive.setPower(powerScaleDistance(power));
  349.  
  350. return position;
  351.  
  352.  
  353. }
  354.  
  355. public void gyroCorrect(double gyroTarget, double gyroRange, double gyroActual, double minSpeed,
  356. double addSpeed) {
  357.  
  358. //this makes it an actual angle between 0 and 360 anyways, so it doesn't matter if the target
  359. //is weird for the gyro or actually between 0 and 360
  360. double delta = (gyroTarget - gyroActual + 360.0) % 360.0; // in case it is negative
  361.  
  362. if (delta > 180.0) {
  363. delta -= 360.0; // delta becomes between -180 and 180
  364. //because the range is from 0-> 180 and -180-> 0 instead of 0-> 360
  365. }
  366.  
  367. if (Math.abs(delta) > gyroRange) {
  368. double gyroMod = delta / 45.0; // if delta is less than 45 and bigger than -45, this will make a scale from
  369. // -1 to 1
  370.  
  371. if (Math.abs(gyroMod) > 1.0) {
  372. gyroMod = Math.signum(gyroMod); //makes gyroMod -1 or 1 if error is more than 45
  373. // or less than -45 degrees
  374. }
  375.  
  376. //if the error is more than 180, then the power is positive, and it turns to the left
  377. //if the error is less than 180, the power in the turn in negative, and it turns to the
  378. //right
  379. //if the error is larger, faster speed
  380.  
  381. this.turn(minSpeed * Math.signum(gyroMod) + addSpeed * gyroMod);
  382.  
  383. telemetry.addLine(minSpeed * Math.signum(gyroMod) + addSpeed * gyroMod + "");
  384. telemetry.update();
  385.  
  386. } else {
  387. turn(0.0);
  388.  
  389. }
  390. }
  391.  
  392.  
  393. public void turn(double power) { //this is opposite to the varsity because our gear train makes
  394. //forwards negative and backwards positive
  395. upperLeftDrive.setPower(power);
  396. upperRightDrive.setPower(-power);
  397. lowerLeftDrive.setPower(power);
  398. lowerRightDrive.setPower(-power);
  399. }
  400.  
  401. //gets current angle position
  402. public float getGyroRotation(AngleUnit unit) {
  403. return imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, unit).firstAngle;
  404. }
  405.  
  406. //this is a weird turn because only the left half of the motors move
  407. public void weirdTurn(double distance) {
  408. //example gear ratio, 2:1
  409. //example circumference
  410. //distance is in inches
  411.  
  412. double circumferenceTraveled = distance / DriveCircumference;
  413.  
  414. int position = (int) (ticksPerRotation * circumferenceTraveled);
  415.  
  416. //if it is forwards or backwards, change sign of position
  417. double power = powerScaleDistance(distance);
  418.  
  419. //position is positive because it will make the motors go backwards
  420. upperLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  421. upperLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  422. upperLeftDrive.setTargetPosition(position);
  423. upperLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  424. upperLeftDrive.setPower(powerScaleDistance(power));
  425.  
  426. lowerLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  427. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  428. lowerLeftDrive.setTargetPosition(position);
  429. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  430. lowerLeftDrive.setPower(power);
  431. }
  432.  
  433. //depending on the Direction, the position is either positive or negative for the encoders
  434. public int setDirection(Direction direction) {
  435. int positionDirection = 0;
  436. switch (direction) {
  437. case FORWARDS:
  438. positionDirection = 1;
  439. break;
  440. case BACKWARDS:
  441. positionDirection = -1;
  442. break;
  443. }
  444. return positionDirection;
  445. }
  446. }
Advertisement
Add Comment
Please, Sign In to add comment