Guest User

Untitled

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