Advertisement
Guest User

Control PI + rampa

a guest
Oct 31st, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1.   /*
  2.  
  3. * 25 september
  4. * PID control
  5. */
  6.  
  7. #include <TimerOne.h>
  8. #include <PID_v1.h>
  9.  
  10. #define a 5
  11. #define encoder0PinA 2
  12. #define encoder0PinB 3
  13. volatile long encoder0Pos=0; //alias pulses
  14.  
  15. double setPoint=60; //rpm max 40 for current constraints
  16.  
  17. double rpm;
  18. float Dangle;
  19. double mvolt;
  20.  
  21. const int enPinmot1 = 5; // H-Bridge enable pin
  22. const int in1Pinmot1 = 7; // H-Bridge input pins
  23. const int in2Pinmot1 = 4;
  24.  
  25. //tuning parameters
  26. // 131:1
  27. /*
  28. const double Kp=0.6;
  29. const double Kd=0;
  30. const double Ki=1.5;
  31. */
  32. const double Kp=0.6;
  33. const double Kd=0;
  34. const double Ki=2.0;
  35.  
  36. //manual tuning:
  37. //1) increase K_p until oscillation
  38. //2) increase K_d until oscillation disappears
  39. //3) increase K_i until the steady state error goes to zero
  40.  
  41. //Load a new PID controller
  42. PID myPID(&rpm, &mvolt, &setPoint, Kp, Ki, Kd, DIRECT);
  43. const long sampleRate = 50;
  44. // frequency of the digital controller
  45.  
  46. unsigned long currentTime;
  47. unsigned long loopTime;
  48. long loopPulses;
  49.  
  50. long isr_time=50000;//us
  51.  
  52. const int pulse_per_rev = 8384;
  53.  
  54. void setup()
  55. {
  56.   pinMode(in1Pinmot1, OUTPUT);
  57.   pinMode(in2Pinmot1, OUTPUT);
  58.   //pinMode(in1Pinmot2, OUTPUT);
  59.   //pinMode(in2Pinmot2, OUTPUT);
  60.  
  61.   pinMode(encoder0PinA, INPUT);
  62.   pinMode(encoder0PinB, INPUT);
  63.  
  64.   attachInterrupt(0, doEncoderA, CHANGE);
  65.   attachInterrupt(1, doEncoderB, CHANGE);
  66.  
  67.     //turn the PID on
  68.    myPID.SetMode(AUTOMATIC);
  69.    myPID.SetSampleTime(sampleRate);
  70.    
  71.    //myPID.SetOutputLimits(0,12.34);
  72.   myPID.SetOutputLimits(0,10);
  73.   //here call the variables for optical encoder???
  74.  
  75.   Timer1.initialize(isr_time);
  76.   Timer1.attachInterrupt(timerISR);
  77.    
  78.   loopPulses=encoder0Pos;
  79.  
  80.   Serial.begin(115200);
  81.  
  82.  
  83. }
  84.  
  85. double setPoint1=30;
  86. double setPoint2=60;
  87. double v;
  88. double t=0.0;
  89.  
  90. void loop()
  91. {
  92.   if (v<setPoint2) {
  93.     v= setPoint1 + a*t;
  94.     t+= 1;
  95.     setPoint= v;
  96.     delay (1);
  97.     }
  98.   /*
  99.     currentTime=millis();
  100.   //200 Hz
  101.   if (currentTime>=(loopTime+500)){
  102.     Serial.println(pulses);
  103.    loopTime=currentTime;//refresh looptime
  104.   }// end current time
  105.   */
  106.   //start loop block (without ISR)
  107.   /*
  108.   currentTime=millis();
  109.   // Hz
  110.   if (currentTime>=(loopTime+1000)){
  111.       Dangle=encoder0Pos-loopPulses;
  112.       Serial.println(Dangle);
  113.       loopPulses=encoder0Pos;// refresh pulses
  114.       loopTime=currentTime;//refresh looptime
  115.     }//end current time
  116.   //end loop block
  117.     */
  118.  
  119.   myPID.Compute(); // this function updates the control action.
  120.  
  121.     int mvoltbit=map(mvolt,0,10,0,255);
  122.  
  123.   analogWrite(enPinmot1, mvoltbit);
  124. //analogWrite(enPinmot2, speed);
  125.  
  126. digitalWrite(in1Pinmot1,LOW);
  127. digitalWrite(in2Pinmot1,HIGH);
  128.  
  129. }//end loop
  130.  
  131. void doEncoderA(){
  132.   if (digitalRead(encoder0PinA)==HIGH){
  133.     if (digitalRead(encoder0PinB)==LOW){
  134.       encoder0Pos++;
  135.     }
  136.     else{
  137.       encoder0Pos--; //CCW
  138.     }
  139.   }
  140.   else
  141.   {
  142.     if (digitalRead(encoder0PinB)==HIGH){
  143.       encoder0Pos++;
  144.     }
  145.     else{
  146.       encoder0Pos--;
  147.     }
  148.   }
  149.   //Serial.println(encoder0Pos);
  150. }
  151.  
  152. void doEncoderB(){
  153.    if (digitalRead(encoder0PinB)==HIGH){
  154.       if (digitalRead(encoder0PinA)==HIGH){
  155.         encoder0Pos++;
  156.         }
  157.       else{
  158.         encoder0Pos--;
  159.         }
  160.     }
  161.     else{
  162.        if (digitalRead(encoder0PinA)==LOW){
  163.            encoder0Pos++;
  164.          }
  165.          else{
  166.            encoder0Pos--;
  167.          }
  168.      }
  169.       //Serial.println(encoder0Pos);
  170.    }
  171.  
  172. void timerISR(){
  173.     Dangle=(encoder0Pos-loopPulses)* (2*PI/ pulse_per_rev);//rad
  174.     //float Dangle=(pulses-loopPulses);
  175.     rpm=(Dangle/isr_time)*(30000000.0/PI);
  176.     //Serial.println(Dangle);
  177.    
  178.      Serial.print(rpm);
  179.      
  180.   Serial.print("\t");
  181.   Serial.print(0);
  182.   Serial.print("\t");
  183.   Serial.print(0);
  184.   Serial.print("\t");
  185.   Serial.print(0);
  186.   Serial.print("\t");
  187.   Serial.print(0);
  188.   Serial.print("\t");
  189.   Serial.println(0);
  190.  
  191.  
  192.    
  193.    loopPulses=encoder0Pos;// refresh pulses
  194.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement