Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. #pragma config(Sensor, dgtl1, f1c, sensorTouch)
  2. #pragma config(Sensor, dgtl2, f2c, sensorTouch)
  3. #pragma config(Sensor, dgtl3, f3c, sensorTouch)
  4. #pragma config(Sensor, dgtl4, s, sensorSONAR_cm)
  5. #pragma config(Sensor, dgtl7, f1i, sensorLEDtoVCC)
  6. #pragma config(Sensor, dgtl8, f2i, sensorLEDtoVCC)
  7. #pragma config(Sensor, dgtl9, f3i, sensorLEDtoVCC)
  8. #pragma config(Motor, port2, d1, tmotorVex393_HBridge, openLoop)
  9. //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
  10.  
  11. // <configuration>
  12. float K_P = 5.6; // Proportional coefficient
  13. float K_I = 0.001; // Integral coefficient
  14. float K_D = 0.3; // Derivative coefficient
  15. int K_DT = 250; // Time difference between PID iterations
  16. float ERROR_THRESH = 1.1; // Target threshold
  17. int FLOOR_HEIGHT = 9; // Height of a floor, in CM
  18. int FLOOR_1_OFFSET = 3; // Distance of the first floor from the ultrasonic sensor
  19. int IDLE_TIME = 10000; // Idle time threshold
  20. // </configuration>
  21.  
  22. int integral = 0; // PID integral value
  23. int lastError = 0; // Error value in the previous iteration
  24.  
  25. /**
  26. * Clamps a value inclusively between two bounds.
  27. */
  28. int clamp(int n, int lower, int upper);
  29.  
  30. /**
  31. * Sets the motor output.
  32. */
  33. void out(int power);
  34.  
  35. /**
  36. * Executes one iteration of the PID algorithm.
  37. */
  38. void pid(float error);
  39.  
  40. /**
  41. * Calculates an error value for a setpoint and measured value.
  42. */
  43. float error(int expected, int actual);
  44.  
  45. /**
  46. * Updates floor indicator LEDs.
  47. */
  48. void ledUpdate(int floorNum);
  49.  
  50. /**
  51. * Instructs the elevator to travel to a level and blocks until the operation is finished.
  52. */
  53. void goToFloor(int floorNum);
  54.  
  55. task main() {
  56. int idleSince = nSysTime; // Store current system time as time of last action
  57. while (true) {
  58. if (SensorValue[f1c] == 1) { // Check if floor buttons are pressed
  59. goToFloor(0); // And go to the respective floor
  60. idleSince = nSysTime; // Then reset the idle time
  61. } else if (SensorValue[f2c] == 1) { // Ditto
  62. goToFloor(1);
  63. idleSince = nSysTime;
  64. } if (SensorValue[f3c] == 1) { // Ditto
  65. goToFloor(2);
  66. idleSince = nSysTime;
  67. } else { // If no floor button is pressed
  68. if (nSysTime - idleSince > IDLE_TIME) // Check if idle time exceeds threshold
  69. goToFloor(0); // Then go back to the ground floor
  70. }
  71. }
  72. }
  73.  
  74. int clamp(int n, int lower, int upper) {
  75. return n < lower ? lower : n > upper ? upper : n;
  76. }
  77.  
  78. void out(int power) {
  79. if (power < 0) // Threshold the power level so the motor still drives at lower power levels
  80. power = clamp(power, -96, -18);
  81. else if (power > 0)
  82. power = clamp(power, 18, 96);
  83. motor[d1] = power; // Set motor d1's output to power
  84. }
  85.  
  86. void pid(float error) {
  87. integral += error * K_DT; // Add the current error value to the integral cumulator
  88. float pTerm = K_P * error; // Calculate proportional term
  89. float iTerm = K_I * integral; // Calculate integral term
  90. float dTerm = K_D * (error - lastError) / K_DT; // Calculate derivative term
  91. out(pTerm + iTerm + dTerm); // Set output to clamped value of u(t)
  92. lastError = error; // Store error value to lastError for calculating derivative later
  93. }
  94.  
  95. float error(int expected, int actual) {
  96. return actual - expected; // Difference between setpoint and measured value
  97. }
  98.  
  99. void ledUpdate(int floorNum) {
  100. SensorValue[f1i] = SensorValue[f2i] = SensorValue[f3i] = 0; // Turn off all LEDs
  101. switch (floorNum) { // Switch on the new floor number
  102. case 0: // If the floor is appropriate
  103. SensorValue[f1i] = 1; // Turn on the corresponding LED
  104. break;
  105. case 1: // Ditto
  106. SensorValue[f2i] = 1;
  107. break;
  108. case 2: // Ditto
  109. SensorValue[f3i] = 1;
  110. break;
  111. }
  112. }
  113.  
  114. void goToFloor(int floorNum) {
  115. integral = 0; // Reset integral and last error value variables
  116. lastError = 0;
  117. int setPoint = floorNum * FLOOR_HEIGHT + FLOOR_1_OFFSET; // Determine PID setpoint
  118. int errorValue; // Declare error value variable
  119. while (true) {
  120. errorValue = error(setPoint, SensorValue[s]); // Calculate error value
  121. if (abs(errorValue) <= ERROR_THRESH) // If within the error threshold...
  122. break; // ...break the PID loop
  123. pid(errorValue); // Execute PID iteration
  124. waitInMilliseconds(K_DT); // Wait for next PID iteration
  125. }
  126. out(0); // Stop the motors
  127. ledUpdate(floorNum); // Update the LEDs
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement