Robotc IR Autonomous Code


SUBMITTED BY: Guest

DATE: Jan. 26, 2014, 7:43 p.m.

FORMAT: Text only

SIZE: 6.9 kB

HITS: 1466

  1. #pragma config(Hubs, S1, HTMotor, HTMotor, HTServo, none)
  2. #pragma config(Hubs, S2, HTMotor, HTMotor, none, none)
  3. #pragma config(Sensor, S3, IRSeeker2, sensorHiTechnicIRSeeker1200)
  4. #pragma config(Motor, mtr_S1_C1_1, motor_lin1, tmotorTetrix, PIDControl, encoder)
  5. #pragma config(Motor, mtr_S1_C1_2, motor_lin2, tmotorTetrix, PIDControl, encoder)
  6. #pragma config(Motor, mtr_S1_C2_1, motor_left1, tmotorTetrix, PIDControl, encoder)
  7. #pragma config(Motor, mtr_S1_C2_2, motor_left2, tmotorTetrix, PIDControl, encoder)
  8. #pragma config(Motor, mtr_S2_C1_1, motor_right1, tmotorTetrix, PIDControl, encoder)
  9. #pragma config(Motor, mtr_S2_C1_2, motor_right2, tmotorTetrix, PIDControl, encoder)
  10. #pragma config(Motor, mtr_S2_C2_1, motor_lin3, tmotorTetrix, PIDControl, encoder)
  11. #pragma config(Motor, mtr_S2_C2_2, motor_lin4, tmotorTetrix, PIDControl, encoder)
  12. #pragma config(Servo, srvo_S1_C3_1, servo1, tServoStandard)
  13. #pragma config(Servo, srvo_S1_C3_2, servo2, tServoStandard)
  14. #pragma config(Servo, srvo_S1_C3_3, servo3, tServoNone)
  15. #pragma config(Servo, srvo_S1_C3_4, servo4, tServoNone)
  16. #pragma config(Servo, srvo_S1_C3_5, servo5, tServoNone)
  17. #pragma config(Servo, srvo_S1_C3_6, servo6, tServoNone)
  18. //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
  19. #include "hitechnic-irseeker-v2.h" //required driver for ir sensor
  20. #include "JoystickDriver.c" //required driver for joystick
  21. void initializeRobot() //place for sensor resets and pre-game preparation.
  22. {
  23. nMotorEncoder(motor_left1) =0;//D //Set all motor rotation counts to 0
  24. nMotorEncoder(motor_right1) =0;//E
  25. nMotorEncoder(motor_left2) =0;//F
  26. nMotorEncoder(motor_right2) =0;//G
  27. nMotorEncoder(motor_lin1) =0;
  28. return;
  29. }
  30. task main() //main instruction for the robot starts
  31. {
  32. initializeRobot(); //Prepares robot, communicates with Field control system.
  33. waitForStart(); // Wait for the beginning of autonomous phase.
  34. while(true) // Repeat the below code for ever until specified otherwise.
  35. {
  36. int sen1 = SensorValue[IRSeeker2]; //Obtain ir seeker sensor value each time loop is run.
  37. nxtDisplayTextLine(3, "%d", SensorValue[IRSeeker2]); //Display on the third line of the NXT brick display the ir seeker value.
  38. //Value ranges from 1 to 9: 1 being left and 9 being right.
  39. if(sen1 != 9) //If the ir sensor does not receive an ir signal from the 9th sector, which is the right side, run the following code.
  40. {
  41. motor(motor_left1)= -60; //move forward
  42. motor(motor_left2)= 60;
  43. motor(motor_right1)= -60;
  44. motor(motor_right2)= 60;
  45. nxtDisplayTextLine(1, "%d", nMotorEncoder(motor_left1)); //Display rotation values of the motors on the 1st, 2nd, 3rd, and 4th lines of the NXT display.
  46. nxtDisplayTextLine(2, "%d", nMotorEncoder(motor_left2));
  47. nxtDisplayTextLine(3, "%d", nMotorEncoder(motor_right1));
  48. nxtDisplayTextLine(4, "%d", nMotorEncoder(motor_right2));
  49. }
  50. else
  51. {
  52. if(sen1 == 9) //If the ir sensor receives a signal from the 9th sector, run the following code.
  53. {
  54. int Dm = nMotorEncoder(motor_left1); //Save the current rotation values to integers Dm, Em, Fm, and Gm for future
  55. int Em = nMotorEncoder(motor_left2);
  56. int Fm = nMotorEncoder(motor_right1);
  57. int Gm = nMotorEncoder(motor_right2);
  58. nxtDisplayTextLine(5, "%d", Dm); //Display the rotations taken to reach the ir beacon.
  59. motor(motor_left1)= -60; // goes past bucket a little
  60. motor(motor_left2)= 60;
  61. motor(motor_right1)= -60;
  62. motor(motor_right2)= 60;
  63. wait1Msec(400);
  64. motor(motor_left1)= 0; // stops
  65. motor(motor_left2)= 0;
  66. motor(motor_right1)= 0;
  67. motor(motor_right2)= 0;
  68. wait1Msec(300);
  69. motor(motor_left1)= -60; // turn towards boxes
  70. motor(motor_left2)= -60;
  71. motor(motor_right1)= -60;
  72. motor(motor_right2)= -60;
  73. wait1Msec(700);
  74. motor(motor_left1)= 0; // stops
  75. motor(motor_left2)= 0;
  76. motor(motor_right1)= 0;
  77. motor(motor_right2)= 0;
  78. wait1Msec(300);
  79. //change linear slide raise to here
  80. //raise it, then dump
  81. while(nMotorEncoder(motor_lin1 < x) // x = # rotations it takes to raise the slide)
  82. {
  83. motor(motor_lin1)= 60; //move linear slide up
  84. motor(motor_lin2)= 60;
  85. motor(motor_lin3)= -60;
  86. motor(motor_lin4)= -60;
  87. }
  88. // need dump
  89. //lower slide a tad
  90. motor(motor_left1)= 60; // turn away from boxes
  91. motor(motor_left2)= 60;
  92. motor(motor_right1)= 60;
  93. motor(motor_right2)= 60;
  94. wait1Msec(700);
  95. motor(motor_left1)= 0; // stop
  96. motor(motor_left2)= 0;
  97. motor(motor_right1)= 0;
  98. motor(motor_right2)= 0;
  99. wait1Msec(300);
  100. int DDm = nMotorEncoder(motor_left1);
  101. int newdm = Dm;
  102. while(nMotorEncoder(motor_left1) < newdm) //Run code until rotation values reach Dm. (Go back the same distance that it took to come to the beacon(?))
  103. {
  104. motor(motor_left1)= 60; //goes forward
  105. motor(motor_left2)= -60;
  106. motor(motor_right1)= 60;
  107. motor(motor_right2)= -60;
  108. }
  109. motor(motor_left1)= 0; //wait
  110. motor(motor_left2)= 0;
  111. motor(motor_right1)= 0;
  112. motor(motor_right2)= 0;
  113. wait1Msec(300);
  114. motor(motor_left1)= 60; //turn
  115. motor(motor_left2)= 60;
  116. motor(motor_right1)= 60;
  117. motor(motor_right2)= 60;
  118. wait1Msec(340);
  119. motor(motor_left1)= 60; //back
  120. motor(motor_left2)= -60;
  121. motor(motor_right1)= 60;
  122. motor(motor_right2)= -60;
  123. wait1Msec(1400);
  124. motor(motor_left1)= 60; //turn
  125. motor(motor_left2)= 60;
  126. motor(motor_right1)= 60;
  127. motor(motor_right2)= 60;
  128. wait1Msec(340);
  129. motor(motor_left1)= 60; //back
  130. motor(motor_left2)= -60;
  131. motor(motor_right1)= 60;
  132. motor(motor_right2)= -60;
  133. wait1Msec(900);
  134. motor(motor_left1)= 0; //wait
  135. motor(motor_left2)= 0;
  136. motor(motor_right1)= 0;
  137. motor(motor_right2)= 0;
  138. wait1Msec(350);
  139. motor(motor_left1)= 60; //turn
  140. motor(motor_left2)= 60;
  141. motor(motor_right1)= 60;
  142. motor(motor_right2)= 60;
  143. wait1Msec(640);
  144. motor(motor_left1)= 0; //wait
  145. motor(motor_left2)= 0;
  146. motor(motor_right1)= 0;
  147. motor(motor_right2)= 0;
  148. wait1Msec(300);
  149. motor(motor_left1)= 60; // bridge
  150. motor(motor_left2)= -60;
  151. motor(motor_right2)= 60;
  152. motor(motor_right2)= -60;
  153. wait1Msec(2000);
  154. //end
  155. break;
  156. }
  157. }
  158. }
  159. }

comments powered by Disqus