Advertisement
Guest User

pid thing

a guest
Apr 30th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. //this is not the whole thing, too much stuff happening just to make the ARM processor run
  2.  
  3.  
  4.  
  5. uint32_t lastTime, lastFpsTime, delayFps = 0;
  6. int fps = 200, sampleTime=100, targetFps = 125, outMax = 1000, outMin = -1000;
  7. double lastInput, ITerm;
  8. double kp = 5;
  9. double ki = 0.1;
  10. double kd = 1;
  11.  
  12.  
  13. void initPID(){
  14.   ITerm = delayFps;
  15.   lastInput = fps;
  16.   if(ITerm > outMax) ITerm = outMax; //there's a special place in hell for this type of coding
  17.   else if(ITerm < outMin) ITerm = outMin;
  18.   lastTime = HAL_GetTick() - sampleTime;
  19. }
  20.  
  21. int calcPID(double input){
  22.   uint32_t now = HAL_GetTick();
  23.   uint32_t deltaT = now - lastTime;
  24.   if(deltaT > sampleTime){
  25.     double error = targetFps - input;
  26.    
  27.     ITerm+= (ki * sampleTime / 1000.0 * error);
  28.    
  29.     if(ITerm > outMax) ITerm= outMax;
  30.     else if(ITerm < outMin) ITerm= outMin;
  31.    
  32.     double dInput = (input - lastInput);
  33.    
  34.     int result = kp * error + ITerm- kd  / sampleTime / 1000 * dInput;
  35.    
  36.     if(result > outMax) result = outMax;
  37.     else if(result < outMin) result = outMin;
  38.    
  39.     lastTime = HAL_GetTick();
  40.    
  41.     return result;
  42.   }
  43.   return 0;
  44. }
  45.  
  46.  
  47. void main(void)
  48. {
  49.   initPID();
  50.   lastFpsTime = HAL_GetTick();
  51.   int frameCounter = 0;
  52.  
  53.   while (1)
  54.   {
  55.     //cumpute the extremities of the ball: left, right, top, bottom
  56.     static int marginX, marginY;
  57.     marginX = circleX + directionX + directionX*circleR;
  58.     marginY = circleY + directionY + directionY*circleR;
  59.    
  60.    
  61.     //between moving a pixel in any direction, delay for "delayFPS" * "whatever time it takes to compute a random number, a division and a sum" (rand()%1000+1000)
  62.     for(int i=0;i<=delayFps;i++)
  63.       const int gretfd = rand()%1000+1000;
  64.     //after the delay clear the current circle and eventually draw the next circle at the new position
  65.     display.fillFastCircle(circleX, circleY, circleR, BLACK);
  66.    
  67.     //check if the ball has hit one of the margins; if it's at a margin, change the movement direction
  68.     if(marginX == 0 || marginX == 127 || (circleY <= 13 && marginX <= 20)){
  69.       directionX *= -1;
  70.       fill = !fill;
  71.     }
  72.     if(marginY == 0 || marginY == 31 || (circleX <= 23 && marginY <= 10)){
  73.       directionY *= -1;
  74.       fill = !fill;
  75.     }
  76.     circleX += directionX;
  77.    
  78.     if(divideY > divThreshold){
  79.       circleY += directionY;
  80.       divideY = 0;
  81.       divThreshold = rand() % 4;
  82.     }
  83.     else{
  84.       divideY++;
  85.     }
  86.    
  87.     //draw the circle at the new position
  88.     if(fill)
  89.       display.fillFastCircle(circleX, circleY, circleR, WHITE);
  90.     else
  91.       display.drawFastCircle(circleX, circleY, circleR, WHITE);
  92.    
  93.     //compute number of pixels the ball will travel in a second
  94.     fps = 1000 / (HAL_GetTick() - lastFpsTime); //HAL_GetTick() returns how many milliseconds have passed since the program began running
  95.     if(fps>=1000){
  96.       fps = 999;
  97.     }
  98.     lastFpsTime = HAL_GetTick();
  99.    
  100.     //once every 80 frames interrupt everything and update the on-screen fps counter
  101.     if(frameCounter >= 80){
  102.       HAL_SuspendTick(); //suspends the tick counter
  103.       char buf[5] = "";
  104.       itoa(fps, buf, 10);
  105.       drawFPSFrame();
  106.       display.print(buf,2,2,1,WHITE,BLACK);
  107.       frameCounter = 0;
  108.       HAL_ResumeTick(); //resumes the tick counter
  109.     }
  110.     else{
  111.       frameCounter++;
  112.     }
  113.     //adjust the delay to achieve the desired pixels per second movement speed of the ball
  114.     delayFps -= calcPID(fps);
  115.   }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement