Advertisement
Trainlover08

3.1 Autonumous Encoder

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