Advertisement
Trainlover08

Untitled

Nov 14th, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 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. //define global variables
  17. int axis3Drive = 0;
  18. int axis1Drive = 0;
  19. float coef = 1.28;
  20.  
  21. // Brain should be defined by default
  22. brain Brain;
  23.  
  24.  
  25. // START V5 MACROS
  26. #define waitUntil(condition) \
  27. do { \
  28. wait(5, msec); \
  29. } while (!(condition))
  30.  
  31. #define repeat(iterations) \
  32. for (int iterator = 0; iterator < iterations; iterator++)
  33. // END V5 MACROS
  34.  
  35.  
  36. // Robot configuration code.
  37. controller Controller1 = controller(primary);
  38. rotation Rotation1 = rotation(PORT19, false);
  39. distance Distance1 = distance(PORT17);
  40. motor leftMotorA = motor(PORT3, ratio6_1, false);
  41. motor leftMotorB = motor(PORT7, ratio6_1, false);
  42. motor_group LeftDriveSmart = motor_group(leftMotorA, leftMotorB);
  43. motor rightMotorA = motor(PORT4, ratio6_1, true);
  44. motor rightMotorB = motor(PORT8, ratio6_1, true);
  45. motor_group RightDriveSmart = motor_group(rightMotorA, rightMotorB);
  46. drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 219.44, 295, 40, mm, 1);
  47.  
  48. motor cataMotorA = motor(PORT14, ratio36_1, true);
  49. motor cataMotorB = motor(PORT10, ratio36_1, false);
  50. motor_group cata = motor_group(cataMotorA, cataMotorB);
  51.  
  52. motor intake = motor(PORT12, ratio36_1, true);
  53.  
  54. digital_out DigitalOutB = digital_out(Brain.ThreeWirePort.B);
  55. digital_out DigitalOutH = digital_out(Brain.ThreeWirePort.H);
  56.  
  57. digital_in DigitalInD = digital_in(Brain.ThreeWirePort.D);
  58.  
  59. // define variable for remote controller enable/disable
  60. bool RemoteControlCodeEnabled = true;
  61. // define variables used for controlling motors based on controller inputs
  62. bool DrivetrainLNeedsToBeStopped_Controller1 = true;
  63. bool DrivetrainRNeedsToBeStopped_Controller1 = true;
  64.  
  65. int axis3calc() {
  66. while(true){
  67. //get controller info and prep for processing
  68.  
  69. //initalize variables first
  70. double y = 0;
  71.  
  72. int x = fabs(Controller1.Axis3.position() * coef);
  73.  
  74. switch(x){
  75. case 0 ... 60:
  76. y = pow(1.04029, x * 1.284467);
  77. break;
  78. case 61 ... 80:
  79. y = 0.75 * x -25;
  80. break;
  81. case 81 ... 108:
  82. y = 1.0357 * x - 47.85714857;
  83. break;
  84. case 109 ... 127:
  85. y = 63 + pow(1.232099, x - 108);
  86. break;
  87. default:
  88. y = 128;
  89. break;
  90. }
  91. y = y / coef;
  92.  
  93. if(Controller1.Axis3.position() < 0){
  94. y = -y;
  95. }
  96.  
  97. axis3Drive = -y;
  98. wait(2.5, msec);
  99. }
  100. return 0;
  101. }
  102. int axis1calc(){
  103. while(true){
  104. //get controller info and prep for processing
  105.  
  106. //initalize variables first
  107. double y = 0;
  108.  
  109. int x = fabs(Controller1.Axis1.position() * coef);
  110.  
  111. switch(x){
  112. case 0 ... 60:
  113. y = pow(1.04029, x * 1.284467);
  114. break;
  115. case 61 ... 80:
  116. y = 0.75 * x -25;
  117. break;
  118. case 81 ... 108:
  119. y = 1.0357 * x - 47.85714857;
  120. break;
  121. case 109 ... 127:
  122. y = 63 + pow(1.232099, x - 108);
  123. break;
  124. default:
  125. y = 128;
  126. break;
  127. }
  128. y = y / coef;
  129.  
  130. if(Controller1.Axis1.position() < 0){
  131. y = -y;
  132. }
  133. axis1Drive = y;
  134. wait(2.5, msec);
  135. }
  136. return 0;
  137. }
  138.  
  139. // define a task that will handle monitoring inputs from Controller1
  140. int rc_auto_loop_function_Controller1() {
  141. // process the controller input every 20 milliseconds
  142. // update the motors based on the input values
  143. while(true) {
  144.  
  145. vex::task ws4(axis1calc);
  146.  
  147. vex::task ws3(axis3calc);
  148.  
  149. if(RemoteControlCodeEnabled) {
  150. int drivetrainLeftSideSpeed = axis3Drive - axis1Drive;
  151. int drivetrainRightSideSpeed = axis3Drive + axis1Drive;
  152.  
  153. // check if the value is inside of the deadband range
  154. if (drivetrainLeftSideSpeed < 5 && drivetrainLeftSideSpeed > -5) {
  155. // check if the left motor has already been stopped
  156. if (DrivetrainLNeedsToBeStopped_Controller1) {
  157. // stop the left drive motor
  158. LeftDriveSmart.stop();
  159. // tell the code that the left motor has been stopped
  160. DrivetrainLNeedsToBeStopped_Controller1 = false;
  161. }
  162. } else {
  163. // reset the toggle so that the deadband code knows to stop the left motor nexttime the input is in the deadband range
  164. DrivetrainLNeedsToBeStopped_Controller1 = true;
  165. }
  166. // check if the value is inside of the deadband range
  167. if (drivetrainRightSideSpeed < 5 && drivetrainRightSideSpeed > -5) {
  168. // check if the right motor has already been stopped
  169. if (DrivetrainRNeedsToBeStopped_Controller1) {
  170. // stop the right drive motor
  171. RightDriveSmart.stop();
  172. // tell the code that the right motor has been stopped
  173. DrivetrainRNeedsToBeStopped_Controller1 = false;
  174. }
  175. } else {
  176. // reset the toggle so that the deadband code knows to stop the right motor next time the input is in the deadband range
  177. DrivetrainRNeedsToBeStopped_Controller1 = true;
  178. }
  179.  
  180. // only tell the left drive motor to spin if the values are not in the deadband range
  181. if (DrivetrainLNeedsToBeStopped_Controller1) {
  182. LeftDriveSmart.setVelocity(drivetrainLeftSideSpeed, percent);
  183. LeftDriveSmart.spin(forward);
  184. }
  185. // only tell the right drive motor to spin if the values are not in the deadband range
  186. if (DrivetrainRNeedsToBeStopped_Controller1) {
  187. RightDriveSmart.setVelocity(drivetrainRightSideSpeed, percent);
  188. RightDriveSmart.spin(forward);
  189. }
  190. }
  191. // wait before repeating the process
  192. wait(5, msec);
  193. }
  194. return 0;
  195. }
  196.  
  197. task rc_auto_loop_task_Controller1(rc_auto_loop_function_Controller1);
  198.  
  199. #pragma endregion VEXcode Generated Robot Configuration
  200. bool intakeInterlock = false;
  201. bool cataInterlock = false;
  202. int switchpoint = 0;
  203.  
  204. void setDown(){
  205. while(true){
  206. if(Rotation1.position(degrees) < 40){
  207. cata.setVelocity(100, percent);
  208. printf("%lf\n", Rotation1.position(degrees));
  209. cata.spin(forward);
  210. } else {
  211. cata.setVelocity(30, percent);
  212. cata.spin(forward);
  213. while(Rotation1.position(degrees) > 43 && Rotation1.position(degrees) < 43){
  214. printf("eidi\n");
  215. cata.stop();
  216. }
  217. if(Rotation1.position(degrees) > 72){
  218. printf("Error: 'setDown' function; %lf\n", Rotation1.position(degrees));
  219. }
  220. }
  221. }
  222. }
  223.  
  224. int Interlock(){
  225. while(true){
  226. if(DigitalInD.value() == 0){
  227. intakeInterlock = true;
  228. } else {
  229. intakeInterlock = false;
  230. }
  231. if(DigitalOutB.value() == 1){
  232. cataInterlock = false;
  233. } else {
  234. cataInterlock = true;
  235. }
  236. wait(5, msec);
  237. }
  238. return 0;
  239. }
  240.  
  241. int Intake(){
  242. bool intakeToggle = false;
  243. while(true){
  244. intake.setMaxTorque(100, percent);
  245. intake.spin(forward);
  246. if(Controller1.ButtonR1.pressing()){
  247. intakeToggle = !intakeToggle;
  248. wait(250, msec);
  249. }
  250. if(Distance1.objectDistance(inches) < 4.0){
  251. intake.setStopping(hold);
  252. intake.stop();
  253. intake.setVelocity(0, percent);
  254. } else {
  255. intake.setVelocity(60, percent);
  256. }
  257. wait(5, msec);
  258. }
  259. return 0;
  260. }
  261.  
  262. int cataFire(){
  263. cata.setMaxTorque(100, percent);
  264. cata.setVelocity(100, percent);
  265. cata.setStopping(hold);
  266. while(true){
  267. if(Distance1.objectDistance(inches) < 4.0){
  268. cata.spin(forward);
  269. if(Rotation1.position(degrees) > 90)
  270. setDown();
  271. }
  272. wait(5, msec);
  273. printf("%lf\n", Rotation1.position(degrees));
  274. }
  275. return 0;
  276. }
  277.  
  278. int wings(){
  279. while(true){
  280. if(Controller1.ButtonL1.pressing()){
  281. DigitalOutH.set(true);
  282. }else{
  283. DigitalOutH.set(false);
  284. }
  285. wait(5, msec);
  286. }
  287. return 0;
  288. }
  289.  
  290. void whenStarted(){
  291. DigitalOutB.set(true);
  292. cata.setMaxTorque(100, percent);
  293. cata.setVelocity(100, percent);
  294. cata.setStopping(hold);
  295. printf("%lf\n", Rotation1.position(degrees));
  296. Rotation1.setPosition(0, degrees);
  297. wait(50, msec);
  298. cata.stop();
  299. printf("%lf\n", Rotation1.position(degrees));
  300. setDown();
  301. }
  302.  
  303. int main() {
  304. // post event registration
  305.  
  306. // set default print color to black
  307. printf("\033[30m");
  308.  
  309. // wait for rotation sensor to fully initialize
  310. wait(30, msec);
  311.  
  312. //run the important things
  313. whenStarted();
  314. vex::task ws5(Interlock);
  315. vex::task ws6(wings);
  316. vex::task ws1(Intake);
  317. vex::task ws2(cataFire);
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement