SHARE
TWEET

Untitled

a guest Oct 21st, 2019 65 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. Serial.begin(38400); // start serial communication through USB
  16. pinMode(2, INPUT); // define encoder pins as inputs
  17. pinMode(3, INPUT);
  18. attachInterrupt(digitalPinToInterrupt(2), checkChannelA, CHANGE); // start interrupts on encoder pins.
  19. attachInterrupt(digitalPinToInterrupt(3), checkChannelB, CHANGE);
  20.  mydac.begin(0x62); // setup the DAC board
  21.  mydac.setVoltage(0,false); // set DAC value to 0, to stop the motor at beginning.
  22.  delay(3000); // pause the code to give you some time at the start
  23.  // initialize the 'previous' variables before starting the loop
  24.  PreviousDiscAngle = EncoderCount*2.0*M_PI/1024.0; // initialize the disc angle variable
  25.  PreviousTime = millis(); // initialize the time variable
  26.  delay(10); // wait a bit
  27. }
  28. void loop() { // put your main code here, it will run repeatedly:
  29. DiscAngle = EncoderCount*2.0*M_PI/1024.0; // convert encoder counts to angle
  30. Time = millis(); // ask arduino for time in milliseconds
  31. DiscSpeed = (DiscAngle-PreviousDiscAngle)/((Time-PreviousTime)/1000.0);
  32. // This is numerical differentiation (get speed in rad/s)
  33. float voltage = 12.5 ; //voltage you want to give to the motor
  34. DACvalue = (voltage – c)/m; // convert voltage to DAC value. Voltage = m*DAC +c
  35. // PLEASE TYPE THE VALUES OF c AND m
  36. if(DACvalue > 4095) { DACvalue = 4095; } // limit the DAC value in the intended range.
  37. if(DACvalue < 0) { DACvalue = 0; }
  38. mydac.setVoltage(DACvalue,false); // set the voltage!
  39. // print stuff on serial monitor
  40. Serial.print(Time); // print time in ms
  41. Serial.print(" "); // print space
  42. Serial.print(DiscSpeed); // print disc speed in rad/s
  43. Serial.print(" "); // print space
  44. Serial.println(DACvalue); // print DAC value
  45. // update the 'previous' variables before starting the next loop
  46. PreviousDiscAngle = DiscAngle;
  47. PreviousTime = Time;
  48. delay(10); // pause to make loop time about 10ms for consistency.
  49. }
  50. /*************** Functions to count encoder ticks*****************/
  51. void checkChannelA() { // Interrupt on A changing state
  52. if (digitalRead(2) == HIGH) { // Low to High transition?
  53.  ChannelA = 1;
  54.  if (!ChannelB) {
  55.  EncoderCount = EncoderCount + 1;
  56.  }
  57. }
  58. if (digitalRead(2) == LOW) { // High-to-low transition?
  59.  ChannelA = 0;
  60. }
  61. }
  62. void checkChannelB() { // Interrupt on B changing state
  63. if (digitalRead(3) == HIGH) { // Low-to-high transition?
  64.  ChannelB = 1;
  65.  if (!ChannelA) {
  66.  EncoderCount = EncoderCount - 1;
  67.  }
  68. }
  69. if (digitalRead(3) == LOW) { // High-to-low transition?
  70.  ChannelB = 0;
  71. }
  72. }
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