Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**************INCLUDE ADAFRUIT LIBRARIES FOR DAC********************/
  2. #include <Wire.h>                     // bunch of code to communicate with DAC
  3. #include <Adafruit_MCP4725.h>         // more code to use the DAC from adafruit.
  4.  
  5. /****************************DEFINE VARIABLES************************/
  6. volatile long EncoderCount = 0;        // variable to keep track of number of pulses sent by the encoder
  7. int ChannelA;                          // flag variables for channel A and B
  8. int ChannelB;
  9. float DiscAngle, PreviousDiscAngle;    // define the DiscAngle variables
  10. float DiscSpeed, Error_in_DiscSpeed, Error_integral;                       // define the DiscSpeed variables
  11. unsigned long Time, PreviousTime ;     // define the time variables
  12.  
  13. Adafruit_MCP4725 mydac;                // an instance of adafruit DAC class
  14. int DACvalue = 0;                      // voltage given to Motor controller voltage by the DAC = 5*DACvalue/4095  
  15.  
  16. /*******************************************************************/
  17. void setup() {                         // put your setup code here, it will run once at the beginning:
  18.   Serial.begin(38400);                 // start serial communication through USB
  19.  
  20.   pinMode(2, INPUT);                   // define encoder pins as inputs
  21.   pinMode(3, INPUT);
  22.   attachInterrupt(digitalPinToInterrupt(2), checkChannelA, CHANGE); // start interrupts on the encoder pins.
  23.   attachInterrupt(digitalPinToInterrupt(3), checkChannelB, CHANGE);
  24.  
  25.   mydac.begin(0x62);                  // setup the DAC board
  26.   mydac.setVoltage(0,false);          // set DAC voltage value to 0, to stop the motor at beginning.
  27.   delay(3000);                        // pause the code just to give you some time at the start after you upload the code
  28.  
  29.   // initialize the 'previous' variables before starting the loop
  30.   PreviousDiscAngle = EncoderCount*2.0*M_PI/1024.0;           // initialize the disc angle variable
  31.   Error_integral = 0.0;                                       // initialize the error integral
  32.   PreviousTime = millis();                                    // initialize the time variable
  33.   delay(10);                                                  // wait a bit
  34. }
  35.  
  36. void loop() {
  37.   // put your main code here, it will run repeatedly:
  38.        
  39.         DiscAngle = EncoderCount*2.0*M_PI/1024.0;           // convert encoder counts to angle
  40.         Time = millis();                                    // ask arduino for time in milliseconds
  41.         DiscSpeed =  (DiscAngle-PreviousDiscAngle)/((Time-PreviousTime)/1000.0);      // This is numerical differentiation  (get speed in rad/s)
  42.        
  43.  
  44.  float kp = 1.175;                                          // proportional gain
  45.  float ki = 3.1756;                                         // proportional gain
  46.  float Vmin = 0.539;                                        // minimum voltage needed to make the motor move
  47.  float voltage;                                             // voltage you want to give to the motor
  48.  float desired_speed = 12.0;      
  49.  Error_in_DiscSpeed = (desired_speed - DiscSpeed);
  50.  Error_integral     = Error_integral + Error_in_DiscSpeed*((Time-PreviousTime)/1000.0) ;                    
  51.  voltage = kp*(desired_speed - DiscSpeed)+ki*(Error_integral)+Vmin;     // This is your PI Controller!
  52.  
  53.  float c = -16.61;                                          // convert voltage to DAC value. Voltage = m*DAC +c
  54.  float m = 0.012;                                                // PLEASE TYPE THE VALUES OF c AND m    
  55.  DACvalue = (voltage - c)/m;                                  
  56.                                                            
  57.  if(DACvalue > 4095)  { DACvalue = 4095; }                  // limit the DAC value in the intended range.
  58.  if(DACvalue < 0)  { DACvalue = 0;  }    
  59.  mydac.setVoltage(DACvalue,false);                          // set the voltage!
  60.  
  61.   // print stuff on serial monitor
  62.         Serial.print(Time);                                 // print time in ms
  63.         Serial.print(" ");                                  // print space
  64.         Serial.print(DiscSpeed);                            // print disc speed in rad/s
  65.         Serial.print(" ");                                  // print space
  66.         Serial.println(DACvalue);                           // print DAC 'voltage' value
  67.  
  68.   // update the 'previous' variables before starting the next loop
  69.        PreviousDiscAngle = DiscAngle;
  70.        PreviousTime      = Time;
  71.        delay(10);                                          // pause just to make loop time about 10ms for consistency.
  72.   }
  73.      
  74.    
  75. /*************** Functions to count encoder ticks*****************/
  76. void checkChannelA() {
  77.  
  78.   // Low to High transition?
  79.   if (digitalRead(2) == HIGH) {
  80.     ChannelA = 1;
  81.     if (!ChannelB) {
  82.       EncoderCount = EncoderCount + 1;
  83.  
  84.     }
  85.   }
  86.  
  87.   // High-to-low transition?
  88.   if (digitalRead(2) == LOW) {
  89.     ChannelA = 0;
  90.   }
  91.  
  92. }
  93.  
  94.  
  95. // Interrupt on B changing state
  96. void checkChannelB() {
  97.  
  98.   // Low-to-high transition?
  99.   if (digitalRead(3) == HIGH) {
  100.     ChannelB = 1;
  101.     if (!ChannelA) {
  102.       EncoderCount = EncoderCount - 1;
  103.     }
  104.   }
  105.  
  106.   // High-to-low transition?
  107.   if (digitalRead(3) == LOW) {
  108.     ChannelB = 0;
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement