Advertisement
Mikkel_Serrantes

PASTEPPP

Feb 21st, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.26 KB | None | 0 0
  1. #include "robot-config.h"
  2. #include <cmath>
  3. #include "math.h"
  4.  
  5. enum class TurnDir { Left, Right };
  6. enum class StartSide { Blue, Red };
  7. enum class AutoRoutine { Routine1, Routine2 };
  8.  
  9. int count = 0;
  10. int target = 0;
  11. bool driveMode = false;
  12. int PreviousErrorLeft = 0;
  13. int PreviousErrorRight = 0;
  14. vex::digital_in AutoStartSide = vex::digital_in(Brain.ThreeWirePort.B);
  15. vex::digital_in Routine = vex::digital_in(Brain.ThreeWirePort.C);
  16.  
  17. vex::competition Competition;
  18.  
  19. /*---------------------------------------------------------------------------*/
  20. void pre_auton(void) {
  21. }
  22.  
  23. /*---------------------------------------------------------------------------*/
  24. /* */
  25. /* Autonomous Task */
  26. /* */
  27. /* This task is used to control your robot during the autonomous phase of */
  28. /* a VEX Competition. */
  29. /* */
  30. /* You must modify the code to add your own robot specific commands here. */
  31. /*---------------------------------------------------------------------------*/
  32.  
  33.  
  34. void RightMotorFB(double speed) {
  35. RightMotorF.spin(directionType::fwd,speed,velocityUnits::pct);
  36. RightMotorB.spin(directionType::fwd,speed,velocityUnits::pct);
  37. }
  38.  
  39. void LeftMotorFB(double speed) {
  40. LeftMotorF.spin(directionType::fwd,speed,velocityUnits::pct);
  41. LeftMotorB.spin(directionType::fwd,speed,velocityUnits::pct);
  42. }
  43.  
  44. void slop(int distance) {
  45. if (distance < 0) {
  46. LeftMotorFB(-40);
  47. vex::task::sleep(35);
  48. }
  49. }
  50.  
  51.  
  52. const int accel_step = 9;
  53. const int deccel_step = 100; // no decel slew
  54. static int leftSpeed = 0;
  55. static int rightSpeed = 0;
  56.  
  57. void LeftMotorFB(int leftTarget){
  58. int step;
  59.  
  60. if(abs(leftSpeed) < abs(leftTarget))
  61. step = accel_step;
  62. else
  63. step = deccel_step;
  64.  
  65. if(leftTarget > leftSpeed + step)
  66. leftSpeed += step;
  67. else if(leftTarget < leftSpeed - step)
  68. leftSpeed -= step;
  69. else
  70. leftSpeed = leftTarget;
  71.  
  72. LeftMotorFB(leftSpeed);
  73. }
  74. //slew control
  75. void RightMotorFB(int rightTarget){
  76. int step;
  77.  
  78. if(abs(rightSpeed) < abs(rightTarget))
  79. step = accel_step;
  80. else
  81. step = deccel_step;
  82.  
  83. if(rightTarget > rightSpeed + step)
  84. rightSpeed += step;
  85. else if(rightTarget < rightSpeed - step)
  86. rightSpeed -= step;
  87. else
  88. rightSpeed = rightTarget;
  89.  
  90. RightMotorFB(rightSpeed);
  91. }
  92.  
  93. int drivePos() {
  94. return (LeftMotorF.rotation(rotationUnits::deg) + RightMotorF.rotation(rotationUnits::deg))/2;
  95. }
  96. bool isDriving() {
  97. return(LeftMotorF.isSpinning() || LeftMotorB.isSpinning() || RightMotorF.isSpinning() || RightMotorB.isSpinning());
  98.  
  99. }
  100.  
  101. void PidBasicAsync(int distance) {
  102. slop(distance);
  103. count = 0;
  104. target = distance;
  105. driveMode = true;
  106. LeftMotorF.resetRotation();
  107. RightMotorF.resetRotation();
  108. LeftMotorB.resetRotation();
  109. RightMotorB.resetRotation();
  110. PreviousErrorLeft = 0;
  111. PreviousErrorRight = 0;
  112. }
  113.  
  114. void PidBasic (int distance) {
  115. if (driveMode) {
  116.  
  117. double Kp = 0.18;///0.2 or .215
  118. double Kd = 0.5;//0.28;///0.275 or .0325
  119.  
  120.  
  121. int Leftdegrees=LeftMotorF.rotation(rotationUnits::deg);
  122. int Rightdegrees=RightMotorF.rotation(rotationUnits::deg);
  123.  
  124. int ErrorLeft = distance - Leftdegrees;
  125. int ErrorRight = distance - Rightdegrees;
  126.  
  127.  
  128.  
  129. Leftdegrees=LeftMotorF.rotation(rotationUnits::deg);
  130. Rightdegrees=RightMotorF.rotation(rotationUnits::deg);
  131.  
  132. ErrorLeft = distance - Leftdegrees;
  133. ErrorRight = distance - Rightdegrees;
  134.  
  135. int LeftsideProportionalError = Kp * ErrorLeft;
  136. int RightsideProportionalError = Kp * ErrorRight;
  137.  
  138. double LeftsidedDerivativeError = Kd * (ErrorLeft - PreviousErrorLeft);
  139. double RightsidedDerivativeError = Kd * (ErrorRight - PreviousErrorRight);
  140.  
  141. LeftMotorFB(LeftsideProportionalError + LeftsidedDerivativeError);
  142. RightMotorFB(RightsideProportionalError + RightsidedDerivativeError);
  143.  
  144. PreviousErrorLeft = ErrorLeft;
  145. PreviousErrorRight = ErrorRight;
  146. /*if (abs(ErrorLeft) < 6 && abs(ErrorRight) < 6) {
  147. break;
  148. }*/
  149. if (!isDriving()) {
  150. count++;
  151. }
  152. if (count > 30) {
  153. driveMode = false;
  154. }
  155. vex::task::sleep(30);
  156. }
  157. }
  158.  
  159. void Turn (int distance) {
  160. LeftMotorF.resetRotation();
  161. RightMotorF.resetRotation();
  162. LeftMotorB.resetRotation();
  163. RightMotorB.resetRotation();
  164. double Kp = .3;
  165. double Kd = .35;
  166.  
  167. int Leftdegrees=LeftMotorF.rotation(rotationUnits::deg);
  168. int Rightdegrees=RightMotorF.rotation(rotationUnits::deg);
  169.  
  170. int ErrorLeft = -1*Leftdegrees - distance;
  171. int ErrorRight = distance - Rightdegrees;
  172.  
  173. int PreviousErrorLeft = 0;
  174. int PreviousErrorRight = 0;
  175.  
  176. while(1) {
  177. Leftdegrees=LeftMotorF.rotation(rotationUnits::deg);
  178. Rightdegrees=RightMotorF.rotation(rotationUnits::deg);
  179.  
  180. ErrorLeft = -1*Leftdegrees - distance;
  181. ErrorRight = distance - Rightdegrees;
  182.  
  183. int LeftsideProportionalError = Kp * ErrorLeft;
  184. int RightsideProportionalError = Kp * ErrorRight;
  185.  
  186. double LeftsidedDerivativeError = Kd * (ErrorLeft - PreviousErrorLeft);
  187. double RightsidedDerivativeError = Kd * (ErrorRight - PreviousErrorRight);
  188.  
  189. LeftMotorFB(LeftsideProportionalError + LeftsidedDerivativeError);
  190. RightMotorFB(RightsideProportionalError + RightsidedDerivativeError);
  191.  
  192. PreviousErrorLeft = ErrorLeft;
  193. PreviousErrorRight = ErrorRight;
  194. if (abs(ErrorLeft) < 6 && abs(ErrorRight) < 6) {
  195. break;
  196. }
  197. vex::task::sleep(30);
  198. }
  199. }
  200. /////////////////////////////////////////////////////////////////////////////////////////////
  201.  
  202. void StopMotor(vex::brakeType type = brakeType::coast){
  203. LeftMotorF.stop(type);
  204. RightMotorF.stop(type);
  205. LeftMotorB.stop(type);
  206. RightMotorB.stop(type);
  207. }
  208. void SetMotorBrakingType(brakeType type = brakeType::coast) {
  209. LeftMotorF.setStopping(type);
  210. RightMotorF.setStopping(type);
  211. LeftMotorB.setStopping(type);
  212. RightMotorB.setStopping(type);
  213. }
  214. int AutoBallIntakeFlipCap() {
  215. int timeout = 4000;
  216. Scooper.setStopping(brakeType::coast);
  217. int sleepTimeout = 20;
  218. Scooper.spin(directionType::rev, 50, velocityUnits::pct);
  219.  
  220. while (timeout > 0) {
  221. task::sleep(sleepTimeout);
  222. timeout = timeout - sleepTimeout;
  223. }
  224.  
  225. Scooper.stop(brakeType::coast);
  226.  
  227. return(0);
  228. }
  229. int AutoBallIntakeFast() {
  230. int timeout = 4000;
  231. Scooper.setStopping(brakeType::coast);
  232. int sleepTimeout = 20;
  233. Scooper.spin(directionType::fwd, 100, velocityUnits::pct);
  234.  
  235. while (timeout > 0) {
  236. task::sleep(sleepTimeout);
  237. timeout = timeout - sleepTimeout;
  238. }
  239. Scooper.stop(brakeType::coast);
  240. return(0);
  241. }
  242. int AutoBallIntakeTakeBall(int timeout = 5000) {
  243.  
  244. Scooper.setStopping(brakeType::coast);
  245. int sleepTimeout = 20;
  246. Scooper.spin(directionType::fwd, 60, velocityUnits::pct);
  247. while (timeout > 0) {
  248. task::sleep(sleepTimeout);
  249. timeout = timeout - sleepTimeout;
  250. }
  251.  
  252. Scooper.stop(brakeType::coast);
  253.  
  254. return(0);
  255. }
  256. void Drive(double numOfRevs, int velocity = 50, bool fwd = true, int timeout = 5000)
  257. {
  258. Brain.Screen.printAt(1, 40, "numofrevs: %f, vel: %f", numOfRevs, velocity);
  259. SetMotorBrakingType();
  260.  
  261. int sleepTimeout = 20;
  262.  
  263. if (fwd == false) {
  264. numOfRevs = numOfRevs * -1;
  265. }
  266.  
  267. LeftMotorF.startRotateFor(numOfRevs, rotationUnits::rev, velocity, velocityUnits::pct);
  268. RightMotorF.startRotateFor(numOfRevs, rotationUnits::rev, velocity, velocityUnits::pct);
  269. LeftMotorB.startRotateFor(numOfRevs, rotationUnits::rev, velocity, velocityUnits::pct);
  270. RightMotorB.startRotateFor(numOfRevs, rotationUnits::rev, velocity, velocityUnits::pct);
  271.  
  272. while(LeftMotorF.isSpinning()||RightMotorF.isSpinning()||
  273. LeftMotorB.isSpinning()||RightMotorB.isSpinning()) {
  274.  
  275. if (timeout <= 0) {
  276. break;
  277. }
  278. task::sleep(sleepTimeout);
  279. timeout = timeout - sleepTimeout;
  280. }
  281.  
  282. StopMotor();
  283. }
  284.  
  285. void DriveInCM(double distanceInCM, int velocity, bool fwd = true, int timeout = 5000) {
  286. // double gearRatio = 1.67;
  287. // double wheelDiameter = 10.16;
  288. // double circumference = wheelDiameter * M_PI;
  289. // double numOfRevs = (distanceInCM)/ (circumference * gearRatio);
  290. double numOfRevs = distanceInCM/53.2;
  291. Drive(numOfRevs, velocity, fwd, timeout);
  292. }
  293.  
  294. void Turn(double degrees, TurnDir turnDir, int velocity = 25, int timeout = 5000){
  295. int sleepTimeout = 10;
  296.  
  297. SetMotorBrakingType();
  298. LeftMotorF.resetRotation();
  299. RightMotorF.resetRotation();
  300.  
  301. if (turnDir == TurnDir::Right) {
  302. LeftMotorF.spin(directionType::fwd, velocity, velocityUnits::pct);
  303. LeftMotorB.spin(directionType::fwd, velocity, velocityUnits::pct);
  304.  
  305. RightMotorF.spin(directionType::rev, velocity, velocityUnits::pct);
  306. RightMotorB.spin(directionType::rev, velocity, velocityUnits::pct);
  307. }
  308. else
  309. {
  310. LeftMotorF.spin(directionType::rev, velocity, velocityUnits::pct);
  311. LeftMotorB.spin(directionType::rev, velocity, velocityUnits::pct);
  312.  
  313. RightMotorF.spin(directionType::fwd, velocity, velocityUnits::pct);
  314. RightMotorB.spin(directionType::fwd, velocity, velocityUnits::pct);
  315. }
  316. while(true) {
  317.  
  318. double flmDeg = std::abs(LeftMotorF.rotation(rotationUnits::deg));
  319. double frmDeg = std::abs(RightMotorF.rotation(rotationUnits::deg));
  320. Brain.Screen.clearScreen();
  321. Brain.Screen.printAt(1, 20, "FL: %f FR: %f", flmDeg, frmDeg);
  322.  
  323. if (frmDeg > degrees) {
  324. Brain.Screen.printAt(1, 40, "Deg Break: FL: %f FR: %f", flmDeg, frmDeg);
  325. break;
  326. }
  327.  
  328. if (timeout <= 0) {
  329. Brain.Screen.printAt(1, 40, "Timeout Break: FL: %f FR: %f", flmDeg, frmDeg);
  330. break;
  331. }
  332.  
  333. task::sleep(sleepTimeout);
  334. timeout = timeout - sleepTimeout;
  335. }
  336.  
  337. StopMotor(brakeType::coast);
  338. }
  339.  
  340. void TurnRight90Degrees()
  341. {
  342. Turn(85, TurnDir::Right);
  343. }
  344.  
  345. void TurnLeft90Degrees()
  346. {
  347. Turn(85, TurnDir::Left);
  348. }
  349.  
  350. void CatapultLaunch() {
  351.  
  352. int timeout = 5000;
  353.  
  354. CatapultMotor.setStopping(brakeType::coast);
  355. CatapultMotor2.setStopping(brakeType::coast);
  356.  
  357. int sleepTimeout = 30;
  358. while (timeout > 0) {
  359.  
  360. Brain.Screen.clearScreen();
  361.  
  362. float catPos = P1.value(rotationUnits::deg);
  363.  
  364. Brain.Screen.printAt(1, 40, "In launch CatPos: %f", catPos);
  365.  
  366. if (catPos == 0)
  367. {
  368. task::sleep(sleepTimeout);
  369. continue;
  370. }
  371.  
  372. // Lowest position for launch
  373. if (catPos <= 54)
  374. {
  375. Brain.Screen.printAt(1, 80, "Launched: %f", catPos);
  376. break;
  377. }
  378.  
  379. // In case lowest position did not reach and catapult already launched.
  380. // This is failsafe
  381.  
  382. if (catPos >= 141)
  383. {
  384. break;
  385. }
  386.  
  387. CatapultMotor.spin(directionType::fwd, 100, velocityUnits::pct);
  388. CatapultMotor2.spin(directionType::fwd, 100, velocityUnits::pct);
  389.  
  390. task::sleep(sleepTimeout);
  391.  
  392. timeout = timeout - sleepTimeout;
  393.  
  394. Brain.Screen.printAt(1, 20, "Timeout: %d", timeout);
  395. }
  396.  
  397. CatapultMotor.stop(brakeType::coast);
  398. CatapultMotor2.stop(brakeType::coast);
  399. }
  400.  
  401. void CatapultLoadPosition() {
  402.  
  403. int timeout = 5000;
  404.  
  405. CatapultMotor.setStopping(brakeType::coast);
  406. CatapultMotor2.setStopping(brakeType::coast);
  407.  
  408.  
  409. int sleepTimeout = 30;
  410.  
  411. while (timeout > 0) {
  412.  
  413. Brain.Screen.clearScreen();
  414.  
  415. float catPos = P1.value(rotationUnits::deg);
  416.  
  417. Brain.Screen.printAt(1, 40, "In load CatPos: %f", catPos);
  418.  
  419. if (catPos == 0)
  420. {
  421. task::sleep(sleepTimeout);
  422. continue;
  423. }
  424.  
  425. // load position
  426. if (catPos <= 72)
  427. {
  428. break;
  429. }
  430.  
  431. CatapultMotor.spin(directionType::fwd, 100, velocityUnits::pct);
  432. CatapultMotor2.spin(directionType::fwd, 100, velocityUnits::pct);
  433.  
  434. task::sleep(sleepTimeout);
  435.  
  436. timeout = timeout - sleepTimeout;
  437. Brain.Screen.printAt(1, 20, "Timeout: %d", timeout);
  438. }
  439.  
  440. CatapultMotor.stop(brakeType::coast);
  441. CatapultMotor2.stop(brakeType::coast);
  442. }
  443.  
  444. /////////////////////////////////////////////////////////////////////////////////
  445. void LiftPosition1 () {
  446.  
  447. int timeout = 5000;
  448.  
  449. Arm2000.setStopping(brakeType::coast);
  450.  
  451. int sleepTimeout = 30;
  452. while (timeout > 0) {
  453.  
  454. Brain.Screen.clearScreen();
  455.  
  456. float LiftPos = P2.value(rotationUnits::deg);
  457.  
  458. Brain.Screen.printAt(2, 40, "LiftPos: %f", LiftPos);
  459.  
  460. if (LiftPos == 0)
  461. {
  462. task::sleep(sleepTimeout);
  463. continue;
  464. }
  465.  
  466. // Lowest position for launch
  467. if (LiftPos >= 245)
  468. {
  469. Brain.Screen.printAt(2, 80, "Stopped: %f", LiftPos);
  470. break;
  471. }
  472.  
  473. Arm2000.spin(directionType::fwd, 60, velocityUnits::pct);
  474.  
  475. task::sleep(sleepTimeout);
  476.  
  477. timeout = timeout - sleepTimeout;
  478.  
  479. Brain.Screen.printAt(2, 20, "Timeout: %d", timeout);
  480. }
  481.  
  482. Arm2000.stop(brakeType::hold);
  483. }
  484.  
  485. void LiftPosition2() {
  486.  
  487. int timeout = 5000;
  488.  
  489. Arm2000.setStopping(brakeType::coast);
  490.  
  491. int sleepTimeout = 30;
  492.  
  493. while (timeout > 0) {
  494.  
  495. Brain.Screen.clearScreen();
  496.  
  497. float LiftPos = P2.value(rotationUnits::deg);
  498.  
  499. Brain.Screen.printAt(2, 40, "LiftPos: %f", LiftPos);
  500.  
  501. if (LiftPos == 0)
  502. {
  503. task::sleep(sleepTimeout);
  504. continue;
  505. }
  506.  
  507. // load position
  508. if (LiftPos <= 76)
  509. {
  510. break;
  511. }
  512.  
  513. Arm2000.spin(directionType::rev, 80, velocityUnits::pct);
  514.  
  515. task::sleep(sleepTimeout);
  516.  
  517. timeout = timeout - sleepTimeout;
  518. Brain.Screen.printAt(2, 20, "Timeout: %d", timeout);
  519. }
  520.  
  521. Arm2000.stop(brakeType::coast);
  522. }
  523.  
  524. void LiftPosition3() {
  525. int timeout = 5000;
  526.  
  527. Arm2000.setStopping(brakeType::coast);
  528.  
  529. int sleepTimeout = 30;
  530.  
  531. while (timeout > 0) {
  532.  
  533. Brain.Screen.clearScreen();
  534.  
  535. float LiftPos = P2.value(rotationUnits::deg);
  536.  
  537. Brain.Screen.printAt(2, 40, "LiftPos: %f", LiftPos);
  538.  
  539. if (LiftPos == 0)
  540. {
  541. task::sleep(sleepTimeout);
  542. continue;
  543. }
  544.  
  545. // load position
  546. if (LiftPos >= 210)
  547. {
  548. break;
  549. }
  550.  
  551. Arm2000.spin(directionType::fwd, 80, velocityUnits::pct);
  552.  
  553. task::sleep(sleepTimeout);
  554.  
  555. timeout = timeout - sleepTimeout;
  556. Brain.Screen.printAt(2, 20, "Timeout: %d", timeout);
  557. }
  558.  
  559. Arm2000.stop(brakeType::coast);
  560. }
  561.  
  562.  
  563.  
  564. void AutoRoutine1(StartSide startSide)
  565. {
  566.  
  567. //
  568. DriveInCM(5, 33);
  569. PidBasicAsync(700);
  570.  
  571. while(driveMode) {
  572.  
  573. PidBasic(target);
  574. if (drivePos() > 400) {
  575. Scooper.spin(directionType::fwd, 100, velocityUnits::pct);
  576. }
  577.  
  578.  
  579. }
  580.  
  581. DriveInCM(0, 30);
  582.  
  583.  
  584. vex::task::sleep(200);
  585.  
  586.  
  587.  
  588.  
  589. PidBasicAsync(-680);
  590.  
  591. DriveInCM(-1, 10);
  592.  
  593.  
  594. DriveInCM(0, 5);
  595.  
  596. vex::task::sleep(200);
  597.  
  598.  
  599. while(driveMode) {
  600.  
  601. PidBasic(target);
  602. if (drivePos() < -400) {
  603. Scooper.spin(directionType::fwd, 0, velocityUnits::pct);
  604. }
  605.  
  606.  
  607. }
  608.  
  609.  
  610. vex::task::sleep(600);
  611.  
  612. DriveInCM(2, 5);
  613.  
  614. PidBasicAsync(80);
  615.  
  616. DriveInCM(0, 5);
  617.  
  618. while(driveMode) {
  619.  
  620. PidBasic(target);
  621.  
  622.  
  623. }
  624. vex::task::sleep(600);
  625.  
  626.  
  627. if ( startSide == StartSide::Blue)
  628. {
  629. Turn(-128.5);////126.5
  630. }
  631. else
  632. {
  633. Turn(128.5);////126.5
  634. }
  635.  
  636.  
  637. LeftMotorF.stop(brakeType::brake);
  638. RightMotorB.stop(brakeType::brake);
  639. LeftMotorB.stop(brakeType::brake);
  640. RightMotorF.stop(brakeType::brake);
  641.  
  642.  
  643.  
  644.  
  645. vex::task::sleep(200);
  646.  
  647.  
  648.  
  649. DriveInCM(0, 10);
  650. vex::task::sleep(500);
  651. PidBasicAsync(305);
  652.  
  653.  
  654. while(driveMode) {
  655.  
  656. PidBasic(target);
  657.  
  658.  
  659. }
  660.  
  661. vex::task::sleep(200);
  662. CatapultLaunch();
  663.  
  664.  
  665. if ( startSide == StartSide::Blue)
  666. {
  667. Turn(-21);
  668. }
  669. else
  670. {
  671. Turn(21);
  672. }
  673.  
  674. DriveInCM(0, 10);
  675.  
  676. vex::task::sleep(10);
  677.  
  678.  
  679.  
  680. PidBasicAsync(700);
  681.  
  682. DriveInCM(0, 10);
  683.  
  684. {
  685. while(driveMode) {
  686. int catPos = P1.value(rotationUnits::deg);
  687. Brain.Screen.printAt(5, 20, "catpos: %d degrees", catPos);
  688. PidBasic(target);
  689.  
  690. if (drivePos() > 300) {
  691. Scooper.spin(directionType::fwd, 60, velocityUnits::pct);
  692. }
  693.  
  694. }
  695. }
  696.  
  697. vex::task::sleep(200);
  698.  
  699.  
  700.  
  701. while(driveMode) {
  702.  
  703. PidBasic(target);
  704.  
  705.  
  706. }
  707.  
  708.  
  709.  
  710. PidBasicAsync(0);
  711. while(driveMode) {
  712.  
  713. PidBasic(target);
  714.  
  715. if (drivePos() > 100) {
  716. driveMode = false;
  717. }
  718. }
  719.  
  720. LeftMotorF.stop(brakeType::coast);
  721. RightMotorB.stop(brakeType::coast);
  722. LeftMotorB.stop(brakeType::coast);
  723. RightMotorF.stop(brakeType::coast);
  724.  
  725.  
  726. }
  727.  
  728.  
  729. void CheckCatPotValues() {
  730.  
  731. while (1)
  732. {
  733. Brain.Screen.clearScreen();
  734. Brain.Screen.printAt(1, 80, "CheckCatPot");
  735. float catPos = P1.value(rotationUnits::deg);
  736. Brain.Screen.printAt(1, 20, "rotation: %f degrees", catPos);
  737.  
  738. //Sleep the task for a short amount of time to prevent wasted resources.
  739. task::sleep(20);
  740. }
  741. }
  742.  
  743.  
  744. void CheckLiftPotValues() {
  745.  
  746. while (1)
  747. {
  748. Brain.Screen.clearScreen();
  749. Brain.Screen.printAt(1, 80, "CheckLiftPot");
  750. float LiftPos = P2.value(rotationUnits::deg);
  751. Brain.Screen.printAt(1, 20, "rotation: %f degrees", LiftPos);
  752.  
  753. //Sleep the task for a short amount of time to prevent wasted resources.
  754. task::sleep(20);
  755. }
  756. }
  757. ///////////////////////////
  758. void AutoRoutine2(StartSide startSide)
  759. {
  760. DriveInCM(8, 33);
  761.  
  762. PidBasicAsync(700);
  763. while(driveMode) {
  764.  
  765. PidBasic(target);
  766. if (drivePos() > 400) {
  767. Scooper.spin(directionType::fwd, 60, velocityUnits::pct);
  768. }
  769.  
  770.  
  771. }
  772. DriveInCM(1, 33);
  773. vex::task::sleep(200);
  774.  
  775. PidBasicAsync(-80);
  776. while(driveMode) {
  777.  
  778. PidBasic(target);
  779. if (drivePos() < -400) {
  780. Scooper.spin(directionType::fwd, 0, velocityUnits::pct);
  781. }
  782.  
  783.  
  784. }
  785.  
  786. vex::task::sleep(300);
  787.  
  788. if ( startSide == StartSide::Blue)
  789. {
  790. Turn(-123);
  791. }
  792. else
  793. {
  794. Turn(123);
  795. }
  796.  
  797. vex::task::sleep(200);
  798. PidBasicAsync(130);
  799. while(driveMode) {
  800.  
  801. PidBasic(target);
  802.  
  803.  
  804. }
  805.  
  806.  
  807.  
  808. if ( startSide == StartSide::Blue)
  809. {
  810. Turn(20);
  811. }
  812. else
  813. {
  814. Turn(-20);
  815. }
  816. PidBasicAsync(-100);
  817. while(driveMode) {
  818.  
  819. PidBasic(target);
  820.  
  821.  
  822. }
  823.  
  824.  
  825.  
  826. vex::task::sleep(200);
  827.  
  828.  
  829. PidBasicAsync(1100);
  830. while(driveMode) {
  831.  
  832. PidBasic(target);
  833.  
  834. if (drivePos() > 1000) {
  835. driveMode = false;
  836. }
  837. }
  838.  
  839. LeftMotorF.stop(brakeType::brake);
  840. RightMotorB.stop(brakeType::brake);
  841. LeftMotorB.stop(brakeType::brake);
  842. RightMotorF.stop(brakeType::brake);
  843.  
  844.  
  845.  
  846.  
  847. }
  848.  
  849. void autonomous(void) {
  850. int startSideValue = AutoStartSide.value();
  851. StartSide startSide;
  852.  
  853. if (startSideValue == 1)
  854. {
  855. startSide = StartSide::Blue;
  856. }
  857. else
  858. {
  859. startSide = StartSide::Red;
  860. }
  861.  
  862. int routineValue = Routine.value();
  863. AutoRoutine routine;
  864.  
  865. // no pin
  866. if (routineValue == 1)
  867. {
  868.  
  869. routine = AutoRoutine::Routine1;
  870. }
  871. else
  872. {
  873.  
  874. routine = AutoRoutine::Routine2;
  875. }
  876.  
  877. if (routine == AutoRoutine::Routine1)
  878. {
  879.  
  880. AutoRoutine1(startSide);
  881. }
  882. else
  883. {
  884.  
  885. AutoRoutine2(startSide);
  886. }
  887.  
  888. return;
  889. }
  890.  
  891. int DriveTask() {
  892. while (true) {
  893. if (driveMode) {
  894. PidBasic(target);
  895. }
  896. }
  897. }
  898.  
  899. int CatapultTask()
  900. {
  901. while (true) {
  902. Brain.Screen.clearScreen();
  903. float catPos = P1.value(rotationUnits::deg);
  904.  
  905. Brain.Screen.printAt(1, 40, "CatPos: %f", catPos);
  906.  
  907. if (Controller1.ButtonR1.pressing()) {
  908. // Load
  909. CatapultLoadPosition();
  910. }
  911. else if (Controller1.ButtonR2.pressing()) {
  912. // Launch
  913. CatapultLaunch();
  914. }
  915.  
  916. task::sleep(30);
  917. }
  918. return (0);
  919. }
  920.  
  921. int LiftTask()
  922. {
  923. while (true) {
  924. Brain.Screen.clearScreen();
  925. float LiftPos = P1.value(rotationUnits::deg);
  926.  
  927. Brain.Screen.printAt(2, 40, "LiftPos: %f", LiftPos);
  928.  
  929. if (Controller1.ButtonY.pressing()) {
  930. // Load
  931. LiftPosition1();
  932. }
  933. else if (Controller1.ButtonA.pressing()) {
  934. // Launch
  935. LiftPosition2();
  936. }
  937.  
  938. else if (Controller1.ButtonB.pressing()) {
  939. // Launch
  940. LiftPosition3();
  941. }
  942.  
  943. task::sleep(30);
  944. }
  945. return (0);
  946. }
  947. /*----------------------------------------------------------------------------*/
  948. /* */
  949. /* User Control Task */
  950. /* */
  951. /* This task is used to control your robot during the user control phase of */
  952. /* a VEX Competition. */
  953. /* */
  954. /* You must modify the code to add your own robot specific commands here. */
  955. /*----------------------------------------------------------------------------*/
  956. void usercontrol(void) {
  957.  
  958. // CheckLiftPotValues();
  959.  
  960. task CallLiftTask(LiftTask);
  961. task CallCatapultTask(CatapultTask);
  962.  
  963.  
  964. while (true) {
  965.  
  966. RightMotorF.spin(directionType::fwd, Controller1.Axis2.position(percentUnits::pct), velocityUnits::pct);
  967. RightMotorB.spin(directionType::fwd, Controller1.Axis2.position(percentUnits::pct), velocityUnits::pct);
  968. LeftMotorF.spin(directionType::fwd, Controller1.Axis3.position(percentUnits::pct), velocityUnits::pct);
  969. LeftMotorB.spin(directionType::fwd, Controller1.Axis3.position(percentUnits::pct), velocityUnits::pct);
  970.  
  971.  
  972. LeftMotorF.setStopping(brakeType::coast);
  973. RightMotorB.setStopping(brakeType::coast);
  974. LeftMotorB.setStopping(brakeType::coast);
  975. RightMotorF.setStopping(brakeType::coast);
  976.  
  977.  
  978.  
  979. if (Controller1.ButtonL2.pressing()) {
  980. //If button up is pressed...Spin the arm motor forward
  981. Scooper.spin(vex::directionType::fwd, 100, velocityUnits::pct);
  982. }
  983. else if (Controller1.ButtonL1.pressing()) { //If the down button is pressed...
  984. Scooper.spin(vex::directionType::rev, 100, velocityUnits::pct);
  985. }else if (Controller2.ButtonUp.pressing()) {
  986. //If button up is pressed...Spin the arm motor forward
  987. Scooper.spin(vex::directionType::fwd, 100, velocityUnits::pct);
  988. }
  989. else if (Controller2.ButtonLeft.pressing()) { //If the down button is pressed...
  990. Scooper.spin(vex::directionType::rev, 100, velocityUnits::pct);
  991. }
  992. else { //If the the up or down button is not pressed...
  993. Scooper.stop(brakeType::coast);
  994. }
  995.  
  996.  
  997. /*
  998. if (Controller1.ButtonB.pressing()) {
  999. //If button up is pressed...Spin the arm motor forward
  1000. Arm2000.spin(vex::directionType::fwd, 100, velocityUnits::pct);
  1001. }
  1002. else if (Controller1.ButtonA.pressing()) { //If the down button is pressed...
  1003. Arm2000.spin(vex::directionType::rev, 100, velocityUnits::pct);
  1004. }
  1005. else { //If the the up or down button is not pressed...
  1006. Arm2000.stop(brakeType::coast);
  1007. }
  1008. */
  1009.  
  1010.  
  1011. }
  1012. }
  1013.  
  1014.  
  1015. int main() {
  1016.  
  1017. Brain.Screen.clearScreen();
  1018. float LiftPos = P2.value(rotationUnits::deg);
  1019. Brain.Screen.printAt(1, 40, "LiftPos: %e", LiftPos);
  1020.  
  1021.  
  1022. Brain.Screen.clearScreen();
  1023. float catPos = P1.value(rotationUnits::deg);
  1024. Brain.Screen.printAt(1, 40, "CatPos: %f", catPos);
  1025.  
  1026.  
  1027. pre_auton();
  1028. Competition.autonomous(autonomous);
  1029. Competition.drivercontrol(usercontrol);
  1030.  
  1031. //Prevent main from exiting with an infinite loop.
  1032.  
  1033. while (1) {
  1034.  
  1035. vex::task::sleep(100);//Sleep the task for a short amount of time to prevent wasted resources.
  1036. }
  1037. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement