Advertisement
Trainlover08

Skills

Oct 20th, 2023 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.95 KB | None | 0 0
  1. #pragma region VEXcode Generated Robot Configuration
  2. // Make sure all required headers are included.
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6. #include <math.h>
  7. #include <string.h>
  8.  
  9.  
  10. #include "vex.h"
  11.  
  12. using namespace vex;
  13.  
  14. competition Competition;
  15.  
  16. // Brain should be defined by default
  17. brain Brain;
  18.  
  19.  
  20. // START V5 MACROS
  21. #define waitUntil(condition) \
  22. do { \
  23. wait(5, msec); \
  24. } while (!(condition))
  25.  
  26. #define repeat(iterations) \
  27. for (int iterator = 0; iterator < iterations; iterator++)
  28. // END V5 MACROS
  29.  
  30.  
  31. // Robot configuration code.
  32. controller Controller1 = controller(primary);
  33. distance Distance1 = distance(PORT7);
  34. motor leftMotorA = motor(PORT14, ratio6_1, false);
  35. motor leftMotorB = motor(PORT11, ratio6_1, false);
  36. motor_group LeftDriveSmart = motor_group(leftMotorA, leftMotorB);
  37. motor rightMotorA = motor(PORT17, ratio6_1, true);
  38. motor rightMotorB = motor(PORT19, ratio6_1, true);
  39. motor_group RightDriveSmart = motor_group(rightMotorA, rightMotorB);
  40. drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 219.44, 295, 40, mm, 1);
  41.  
  42. motor cataMotorA = motor(PORT8, ratio36_1, false);
  43. motor cataMotorB = motor(PORT3, ratio36_1, true);
  44. motor_group cata = motor_group(cataMotorA, cataMotorB);
  45.  
  46. motor MotorGroup7MotorA = motor(PORT4, ratio36_1, true);
  47. motor MotorGroup7MotorB = motor(PORT9, ratio36_1, false);
  48. motor_group MotorGroup7 = motor_group(MotorGroup7MotorA, MotorGroup7MotorB);
  49.  
  50. digital_out DigitalOutB = digital_out(Brain.ThreeWirePort.B);
  51. digital_out DigitalOutD = digital_out(Brain.ThreeWirePort.D);
  52.  
  53.  
  54.  
  55. // define variable for remote controller enable/disable
  56. bool RemoteControlCodeEnabled = true;
  57. // define variables used for controlling motors based on controller inputs
  58. bool DrivetrainLNeedsToBeStopped_Controller1 = true;
  59. bool DrivetrainRNeedsToBeStopped_Controller1 = true;
  60.  
  61. // define a task that will handle monitoring inputs from Controller1
  62. int rc_auto_loop_function_Controller1() {
  63. // process the controller input every 20 milliseconds
  64. // update the motors based on the input values
  65. while(true) {
  66. if(RemoteControlCodeEnabled) {
  67.  
  68. // calculate the drivetrain motor velocities from the controller joystick axies
  69. // left = Axis3 + Axis1
  70. // right = Axis3 - Axis1
  71. double y = 0;
  72. double x = Controller1.Axis3.position();
  73.  
  74. x = x * 1.28;
  75.  
  76. x = fabs(x);
  77.  
  78. if(0<=x&&x<60){
  79. y = pow(1.04029, x * 1.284467);
  80. }else if(60<=x&&x<80){
  81. y = 0.75 * x -25;
  82. }else if(80<=x&&x<=108){
  83. y = 1.0357 * x - 47.85714857;
  84. }else if(x > 108){
  85. y = 63 + pow(1.232099, x - 108);
  86. }
  87.  
  88. y = y/1.28;
  89.  
  90. if(Controller1.Axis3.position() < 0){
  91. y = -y;
  92. }
  93. double sy = 0;
  94. double sx = Controller1.Axis1.position();
  95.  
  96. sx = sx * 1.28;
  97.  
  98. sx = fabs(sx);
  99.  
  100. if(0<=sx&&sx<60){
  101. sy = pow(1.04029, sx * 1.284467);
  102. }else if(60<=sx&&sx<80){
  103. sy = 0.75 * sx -25;
  104. }else if(80<=sx&&sx<=108){
  105. sy = 1.0357 * sx - 47.85714857;
  106. }else if(sx > 108){
  107. sy = 63 + pow(1.232099, sx - 108);
  108. }
  109.  
  110. sy = sy/1.28;
  111.  
  112. if(Controller1.Axis1.position() < 0){
  113. sy = -sy;
  114. }
  115.  
  116. y = -y;
  117.  
  118. int drivetrainLeftSideSpeed = y + sy;
  119. int drivetrainRightSideSpeed = y - sy;
  120.  
  121. // check if the value is inside of the deadband range
  122. if (drivetrainLeftSideSpeed < 5 && drivetrainLeftSideSpeed > -5) {
  123. // check if the left motor has already been stopped
  124. if (DrivetrainLNeedsToBeStopped_Controller1) {
  125. // stop the left drive motor
  126. LeftDriveSmart.stop();
  127. // tell the code that the left motor has been stopped
  128. DrivetrainLNeedsToBeStopped_Controller1 = false;
  129. }
  130. } else {
  131. // reset the toggle so that the deadband code knows to stop the left motor nexttime the input is in the deadband range
  132. DrivetrainLNeedsToBeStopped_Controller1 = true;
  133. }
  134. // check if the value is inside of the deadband range
  135. if (drivetrainRightSideSpeed < 5 && drivetrainRightSideSpeed > -5) {
  136. // check if the right motor has already been stopped
  137. if (DrivetrainRNeedsToBeStopped_Controller1) {
  138. // stop the right drive motor
  139. RightDriveSmart.stop();
  140. // tell the code that the right motor has been stopped
  141. DrivetrainRNeedsToBeStopped_Controller1 = false;
  142. }
  143. } else {
  144. // reset the toggle so that the deadband code knows to stop the right motor next time the input is in the deadband range
  145. DrivetrainRNeedsToBeStopped_Controller1 = true;
  146. }
  147.  
  148. // only tell the left drive motor to spin if the values are not in the deadband range
  149. if (DrivetrainLNeedsToBeStopped_Controller1) {
  150. LeftDriveSmart.setVelocity(drivetrainLeftSideSpeed, percent);
  151. LeftDriveSmart.spin(forward);
  152. }
  153. // only tell the right drive motor to spin if the values are not in the deadband range
  154. if (DrivetrainRNeedsToBeStopped_Controller1) {
  155. RightDriveSmart.setVelocity(drivetrainRightSideSpeed, percent);
  156. RightDriveSmart.spin(forward);
  157. }
  158. }
  159. // wait before repeating the process
  160. wait(20, msec);
  161. }
  162. return 0;
  163. }
  164.  
  165.  
  166. task rc_auto_loop_task_Controller1(rc_auto_loop_function_Controller1);
  167.  
  168. #pragma endregion VEXcode Generated Robot Configuration
  169.  
  170.  
  171. // Include the V5 Library
  172. #include "vex.h"
  173.  
  174. // Allows for easier use of the VEX Library
  175. using namespace vex;
  176.  
  177. // "when started" hat block
  178. int whenStarted1() {
  179. Drivetrain.setDriveVelocity(600.0, rpm);
  180. Drivetrain.setTurnVelocity(100.0, percent);
  181. MotorGroup7.setMaxTorque(100, percent);
  182. MotorGroup7.setVelocity(100, percent);
  183. cata.setMaxTorque(100, percent);
  184. cata.setVelocity(100, percent);
  185. bool wingState = false;
  186. bool digitalB1 = false;
  187. bool digitalB2 = false;
  188. bool pl = false;
  189. bool ytoggle = false;
  190. float catapos = 0;
  191. int turng = 0;
  192. //read controller
  193. int counter = 0;
  194. int tempA = 0;
  195. int tempB = 0;
  196. int tempL1 = 0;
  197. int tempR1 = 0;
  198. int tempU = 0;
  199. int tempD = 0;
  200. int tempR = 0;
  201. int tempL = 0;
  202. float temp3 = 0.0;
  203. float temp1 = 0.0;
  204. bool matchload = false;
  205. int i = 0;
  206. while (true) {
  207. cata.setStopping(hold);
  208. cata.stop();
  209. if(Controller1.ButtonR1.pressing()){
  210. if(Controller1.Axis3.position() < -15.0){
  211. MotorGroup7.spin(reverse);
  212. } else {
  213. MotorGroup7.spin(forward);
  214. }
  215. } else {
  216. MotorGroup7.spin(forward);
  217. }
  218. turng = floor(cata.position(turns));
  219. if(Controller1.ButtonDown.pressing()){
  220. cata.spin(reverse);
  221. }
  222. if(Controller1.ButtonUp.pressing()){
  223. cata.spin(forward);
  224. }
  225. if(catapos <= 60 || catapos >= 330){
  226. digitalB2 = false;
  227. } else {
  228. digitalB2 = true;
  229. }
  230. if(Controller1.ButtonL1.pressing()){
  231. wingState = !wingState;
  232. }
  233. if (Controller1.ButtonL1.pressing()){
  234. DigitalOutD.set(true);
  235. wait(150, msec);
  236. } else{
  237. DigitalOutD.set(false);
  238. wait(150, msec);
  239. }
  240. if(Controller1.ButtonLeft.pressing()){
  241. pl = !pl;
  242. wait(150, msec);
  243. }
  244. catapos = cata.position(degrees) - (turng * 360);
  245. //if(pl == true){
  246. //DigitalOutB.set(true);
  247. //} else if((digitalB1 == true || digitalB2 == true)||(digitalB1 == true & digitalB2 == true)){
  248. //DigitalOutB.set(true);
  249. //} else {
  250. //DigitalOutB.set(false);
  251. //}
  252. if(Controller1.ButtonY.pressing()){
  253. ytoggle = !ytoggle;
  254. }
  255. if(ytoggle == true){
  256. //DigitalOutB.set(true);
  257. }else{
  258. //DigitalOutB.set(false);
  259. }
  260.  
  261. if(Controller1.ButtonA.pressing()){
  262. matchload = !matchload;
  263. wait(350, msec);
  264. }
  265. if(matchload == true){
  266. DigitalOutB.set(true);
  267. MotorGroup7.stop();
  268. printf("%.1f\n", (Distance1.objectDistance(inches)));
  269. if(i == 0){
  270. cata.spinFor(forward, 318, degrees);
  271. ++i;
  272. }
  273. if(Distance1.objectDistance(inches) < 1.5){
  274. wait(150, msec);
  275. cata.spinFor(forward, 1, turns);
  276. }
  277. } else{
  278. DigitalOutB.set(false);
  279. MotorGroup7.setVelocity(100, percent);
  280. i = 0;
  281. if(i == 1){
  282. cata.spinFor(reverse, 318, degrees);
  283. }}
  284.  
  285. wait(5, msec);
  286. }
  287. return 0;
  288. }
  289.  
  290. int onauton_autonomous_0() {
  291. int n = 0;
  292. cata.setMaxTorque(100, percent);
  293. cata.setVelocity(50, percent);
  294. Drivetrain.setDriveVelocity(100, percent);
  295. DigitalOutB.set(true);
  296. MotorGroup7.stop();
  297. printf("%.1f\n", (Distance1.objectDistance(inches)));
  298. cata.spinFor(forward, 358, degrees);
  299. while(true){
  300. if(Distance1.objectDistance(inches) < 1.5){
  301. wait(150, msec);
  302. cata.spinFor(forward, 1, turns);
  303. ++n;
  304. }
  305. if(n == 11){
  306. n = 0;
  307. Drivetrain.drive(forward);
  308. wait(1500, msec);
  309. Drivetrain.drive(reverse);
  310. wait(1500, msec);
  311. Drivetrain.stop();
  312. }
  313. }
  314. return 0;
  315. }
  316.  
  317. int ondriver_drivercontrol_0() {
  318. cata.spinFor(reverse, 358, degrees);
  319. whenStarted1();
  320. return 0;
  321. }
  322.  
  323. void VEXcode_driver_task() {
  324. // Start the driver control tasks....
  325. vex::task drive0(ondriver_drivercontrol_0);
  326. while(Competition.isDriverControl() && Competition.isEnabled()) {this_thread::sleep_for(10);}
  327. drive0.stop();
  328. return;
  329. }
  330.  
  331. void VEXcode_auton_task() {
  332. // Start the auton control tasks....
  333. vex::task auto0(onauton_autonomous_0);
  334. while(Competition.isAutonomous() && Competition.isEnabled()) {this_thread::sleep_for(10);}
  335. auto0.stop();
  336. return;
  337. }
  338.  
  339.  
  340. int main() {
  341. // post event registration
  342.  
  343. // set default print color to black
  344. printf("\033[30m");
  345.  
  346. vex::competition::bStopTasksBetweenModes = false;
  347. Competition.autonomous(VEXcode_auton_task);
  348. Competition.drivercontrol(VEXcode_driver_task);
  349.  
  350. // wait for rotation sensor to fully initialize
  351. wait(30, msec);
  352.  
  353.  
  354. }
  355.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement