SHARE
TWEET

Untitled

a guest Oct 21st, 2019 73 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. /****************************DEFINE VARIABLES************************/
  5. volatile long EncoderCount = 0; // variable to count pulses sent by the encoder
  6. int ChannelA; // flag variables for channel A and B
  7. int ChannelB;
  8. float DiscAngle, PreviousDiscAngle; // define the DiscAngle variables
  9. float DiscSpeed; // define the DiscSpeed variable
  10. unsigned long Time, PreviousTime ; // define the time variables
  11. Adafruit_MCP4725 mydac; // an instance of adafruit DAC class
  12. int DACvalue = 0; // voltage to Motor controller is 5*DACvalue/4095
  13. /*******************************************************************/
  14. void setup() // code here will run once at the beginning:
  15. {
  16. Serial.begin(38400); // start serial communication through USB
  17. pinMode(2, INPUT); // define encoder pins as inputs
  18. pinMode(3, INPUT);
  19. attachInterrupt(digitalPinToInterrupt(2), checkChannelA, CHANGE); // start interrupts on encoder pins.
  20. attachInterrupt(digitalPinToInterrupt(3), checkChannelB, CHANGE);
  21.  mydac.begin(0x62); // setup the DAC board
  22.  mydac.setVoltage(0,false); // set DAC value to 0, to stop the motor at beginning.
  23.  delay(3000); // pause the code to give you some time at the start
  24.  // initialize the 'previous' variables before starting the loop
  25.  PreviousDiscAngle = EncoderCount*2.0*M_PI/1024.0; // initialize the disc angle variable
  26.  PreviousTime = millis(); // initialize the time variable
  27.  delay(10); // wait a bit
  28. }
  29. void loop() // put your main code here, it will run repeatedly:
  30. {
  31. DiscAngle = EncoderCount*2.0*M_PI/1024.0; // convert encoder counts to angle
  32. Time = millis(); // ask arduino for time in milliseconds
  33. DiscSpeed = (DiscAngle-PreviousDiscAngle)/((Time-PreviousTime)/1000.0);
  34. // This is numerical differentiation (get speed in rad/s)
  35. float voltage = 1.5*(DiscSpeed - DesiredSpeed) ; //voltage you want to give to the motor
  36. DACvalue = (voltage – c)/m; // convert voltage to DAC value. Voltage = m*DAC +c
  37. // PLEASE TYPE THE VALUES OF c AND m
  38. if(DACvalue > 4095) { DACvalue = 4095; } // limit the DAC value in the intended range.
  39. if(DACvalue < 0) { DACvalue = 0; }
  40. mydac.setVoltage(DACvalue,false); // set the voltage!
  41.  
  42. // print stuff on serial monitor
  43. Serial.print(Time); // print time in ms
  44. Serial.print(" "); // print space
  45. Serial.print(DiscSpeed); // print disc speed in rad/s
  46. Serial.print(" "); // print space
  47. Serial.println(DACvalue); // print DAC value
  48. // update the 'previous' variables before starting the next loop
  49. PreviousDiscAngle = DiscAngle;
  50. PreviousTime = Time;
  51. delay(10); // pause to make loop time about 10ms for consistency.
  52. }
  53. /*************** Functions to count encoder ticks*****************/
  54. void checkChannelA() { // Interrupt on A changing state
  55. if (digitalRead(2) == HIGH) { // Low to High transition?
  56.  ChannelA = 1;
  57.  if (!ChannelB) {
  58.  EncoderCount = EncoderCount + 1;
  59.  }
  60. }
  61. if (digitalRead(2) == LOW) { // High-to-low transition?
  62.  ChannelA = 0;
  63. }
  64. }
  65. void checkChannelB() { // Interrupt on B changing state
  66. if (digitalRead(3) == HIGH) { // Low-to-high transition?
  67.  ChannelB = 1;
  68.  if (!ChannelA) {
  69.  EncoderCount = EncoderCount - 1;
  70.  }
  71. }
  72. if (digitalRead(3) == LOW) { // High-to-low transition?
  73.  ChannelB = 0;
  74. }
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top