Guest User

Untitled

a guest
Apr 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #pragma config(Sensor, in1, armEncoder, sensorPotentiometer)
  2. #pragma config(Motor, port1, rightDrive, tmotorVex393_HBridge, openLoop, reversed, driveRight)
  3. #pragma config(Motor, port2, limitSwitch, tmotorVex393_MC29, openLoop, reversed)
  4. #pragma config(Motor, port6, claw, tmotorVex393_MC29, openLoop)
  5. #pragma config(Motor, port7, armMotor, tmotorVex393_MC29, openLoop, reversed)
  6. #pragma config(Motor, port10, leftDrive, tmotorVex393_HBridge, openLoop, driveLeft)
  7. //!!Code automatically generated by 'ROBOTC' configuration wizard !!//
  8.  
  9. //gain = skim threshold = driving
  10. float gain = .8;
  11. int threshold = 5;
  12.  
  13. //PID initial values/constants
  14. float Kp = .12;
  15. float Kd = 0;
  16. float Ki = .000001; // ki = .00001 and kp = .1 is good
  17. float errorSum = 0;
  18. float lastError = 0;
  19. int setpoint = 1810;
  20.  
  21. //PID variables
  22. float curPos;
  23. float diff;
  24. int errorTerm;
  25. int mVal;
  26.  
  27. //driving
  28. int throttle, turn;
  29.  
  30. //driving
  31. int leftMotor, tempLeft;
  32. int rightMotor, tempRight;
  33.  
  34. //arm and claw
  35. int clawUp, clawDown,clawFinal;
  36. int armUp, armDown,armFinal;
  37.  
  38. //button Presets right side
  39. int enablePID, disablePID, incPID_Setpoint, decPID_Setpoint;
  40. bool isPIDActive = false;
  41.  
  42. //inverses the value (so must use opposite to skim)
  43. float skim( float f ) {
  44.  
  45. if( f >127 ){
  46. return - (f - 127) * gain;
  47. }
  48.  
  49. else if( f < -127 ) {
  50. return - (f + 127) * gain;
  51. }
  52.  
  53. else {
  54. return 0;
  55. }
  56. }
  57.  
  58. void codePID(){
  59.  
  60. curPos = SensorValue(armEncoder); // Display the reading of the Potentiometer to current cursor position
  61. errorTerm = setpoint - curPos;
  62. mVal = Kp*errorTerm; //.1 kp is most precise
  63.  
  64. if(errorTerm < 400 && errorTerm > -400) {
  65. errorSum += errorTerm;
  66. }
  67.  
  68. diff = errorTerm - lastError;
  69.  
  70. lastError = errorTerm;
  71.  
  72. mVal += Ki*errorSum;
  73. mVal += Kd*diff;
  74.  
  75. if(mVal > 127) {
  76. mVal = 127;
  77. }
  78. else if(mVal < -127) {
  79. mVal = -127;
  80. }
  81. }
  82.  
  83. void autoPID(){
  84.  
  85. enablePID = vexRT[Btn8R];
  86. disablePID = vexRT[Btn8L];
  87. incPID_Setpoint = vexRT[Btn8U];
  88. decPID_Setpoint = vexRT[Btn8D];
  89.  
  90. if(incPID_Setpoint == 1){
  91. setpoint += 1;
  92. }
  93.  
  94. //1 * 16 encoder
  95.  
  96. if(decPID_Setpoint == 1){
  97. setpoint -= 1;
  98. }
  99.  
  100. if(enablePID == 1 && !isPIDActive){
  101. isPIDActive = true;
  102. setpoint = SensorValue(armEncoder);
  103. }else if(disablePID == 1 ){
  104. isPIDActive = false;
  105. }
  106.  
  107. if(isPIDActive){
  108. codePID();
  109. }
  110. }
  111.  
  112. task armMovement(){
  113.  
  114. while(true){
  115. armUp = vexRT[Btn6U];
  116. armDown = vexRT[Btn6D];
  117.  
  118. if(armUp == 1){
  119. armFinal = 50;
  120. }else if(armDown == 1){
  121. armFinal = -50;
  122. }else if(SensorValue(armEncoder) > 1810){
  123. armFinal = -5;
  124. }else{
  125. armFinal = 5;
  126. }
  127.  
  128. if( armFinal < -20 && SensorValue[limitSwitch] == 1){
  129. armFinal = 0;
  130. }
  131.  
  132. // autoPID();
  133.  
  134. if(isPIDActive){
  135. motor[armMotor] = mVal;
  136. }else{
  137. motor[armMotor] = armFinal;
  138. }
  139.  
  140. wait1Msec(10);
  141. }
  142. }
  143.  
  144. task driving(){
  145.  
  146. while( true ) {
  147.  
  148. throttle = vexRT[Ch4];
  149. turn = vexRT[Ch2];
  150.  
  151. if(throttle < threshold && throttle > -threshold){
  152. throttle = 0;
  153. }
  154. if(turn < threshold && turn > -threshold){
  155. turn = 0;
  156. }
  157.  
  158. tempRight = throttle + turn;
  159. tempLeft = throttle - turn;
  160.  
  161. leftMotor = skim(tempRight) + tempLeft;
  162. rightMotor = tempRight + skim(tempLeft);
  163.  
  164. motor[ rightDrive ] = rightMotor;
  165. motor[ leftDrive] = leftMotor;
  166.  
  167. wait1Msec(10);
  168. }
  169. }
  170.  
  171. task clawMovement(){
  172.  
  173. while(true){
  174.  
  175. clawUp = vexRT[Btn5U];
  176. clawDown = vexRT[Btn5D];
  177.  
  178.  
  179. if(clawUp == 1){
  180. clawFinal = 70;
  181. }else if(clawDown == 1){
  182. clawFinal = -70;
  183. }else{
  184. clawFinal = 0;
  185. }
  186. motor[claw] = clawFinal;
  187.  
  188.  
  189. wait1Msec(10);
  190. }
  191. }
  192.  
  193. task main()
  194. {
  195. wait1Msec(20);
  196.  
  197. startTask(driving);
  198. startTask(armMovement);
  199. startTask(clawMovement);
  200. }
Add Comment
Please, Sign In to add comment