SHARE
TWEET

Untitled

a guest Oct 21st, 2019 80 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()
  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 = 12.5 ; //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. // print stuff on serial monitor
  42. Serial.print(Time); // print time in ms
  43. Serial.print(" "); // print space
  44. Serial.print(DiscSpeed); // print disc speed in rad/s
  45. Serial.print(" "); // print space
  46. Serial.println(DACvalue); // print DAC value
  47. // update the 'previous' variables before starting the next loop
  48. PreviousDiscAngle = DiscAngle;
  49. PreviousTime = Time;
  50. delay(10); // pause to make loop time about 10ms for consistency.
  51. }
  52. /*************** Functions to count encoder ticks*****************/
  53. void checkChannelA() { // Interrupt on A changing state
  54. if (digitalRead(2) == HIGH) { // Low to High transition?
  55.  ChannelA = 1;
  56.  if (!ChannelB) {
  57.  EncoderCount = EncoderCount + 1;
  58.  }
  59. }
  60. if (digitalRead(2) == LOW) { // High-to-low transition?
  61.  ChannelA = 0;
  62. }
  63. }
  64. void checkChannelB() { // Interrupt on B changing state
  65. if (digitalRead(3) == HIGH) { // Low-to-high transition?
  66.  ChannelB = 1;
  67.  if (!ChannelA) {
  68.  EncoderCount = EncoderCount - 1;
  69.  }
  70. }
  71. if (digitalRead(3) == LOW) { // High-to-low transition?
  72.  ChannelB = 0;
  73. }
  74. }
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