Advertisement
Guest User

Untitled

a guest
Jun 30th, 2011
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. #define QUAD_A_PIN            2
  2. #define QUAD_B_PIN            3
  3. #define DEBUG_PIN             4
  4. #define MOTOR_SPEED_PIN       5
  5. #define MOTOR_DIR_PIN         6
  6. #define MOTOR_SLEEP_PIN       7
  7. #define MOTOR_MODE_PIN        8
  8. #define STEP_PIN              11
  9. #define DIR_PIN               12
  10. #define ENABLE_PIN            13
  11.  
  12. volatile int position;
  13. int target;
  14.  
  15. bool newStep = false;
  16. bool newEnable = false;
  17. bool oldStep = false;
  18. bool oldEnable = true;
  19. bool dir = false;
  20. bool olddir = false;
  21.  
  22. void setup()
  23. {
  24.     Serial.begin(19200);
  25.     Serial.println("MakerBot DC Servo Controller v1.0");
  26.  
  27.     pinMode(QUAD_A_PIN, INPUT);
  28.     pinMode(QUAD_B_PIN, INPUT);
  29.  
  30.     pinMode(DEBUG_PIN, OUTPUT);
  31.  
  32.     pinMode(MOTOR_SLEEP_PIN, OUTPUT);
  33.     digitalWrite(MOTOR_SLEEP_PIN, LOW);
  34.  
  35.     pinMode(MOTOR_MODE_PIN, OUTPUT);
  36.     digitalWrite(MOTOR_MODE_PIN, LOW);
  37.  
  38.     pinMode(MOTOR_SPEED_PIN, OUTPUT);
  39.     digitalWrite(MOTOR_SPEED_PIN, LOW);
  40.  
  41.     pinMode(MOTOR_DIR_PIN, OUTPUT);
  42.     digitalWrite(MOTOR_DIR_PIN, LOW);
  43.  
  44.     pinMode(STEP_PIN, INPUT);
  45.     pinMode(DIR_PIN, INPUT);
  46.     pinMode(ENABLE_PIN, INPUT);
  47.  
  48.     attachInterrupt(0, read_quadrature_a, CHANGE);
  49.     attachInterrupt(1, read_quadrature_b, CHANGE);
  50. }
  51.  
  52. void loop()
  53. {
  54.     newEnable = digitalRead(ENABLE_PIN);
  55.     newStep = digitalRead(STEP_PIN);
  56.     dir = digitalRead(DIR_PIN);
  57.  
  58.     //low to high transition
  59.     if (!oldEnable && newEnable)
  60.     {
  61.         //enable is active low, so disable.
  62.         digitalWrite(MOTOR_SLEEP_PIN, LOW);
  63.         digitalWrite(MOTOR_SPEED_PIN, LOW);
  64.     }
  65.     // high to low transition
  66.     else if (oldEnable && !newEnable)
  67.     {
  68.         //enable is active low, so enable.
  69.         digitalWrite(MOTOR_SLEEP_PIN, HIGH);
  70.         delay(1); //give it a millisecond to turn on.
  71.     }
  72.  
  73.     //enable is active low, so only do this if we're enabled.
  74.     if (!newEnable)
  75.     {
  76.    
  77.         if (olddir != dir) {
  78.           target=0;
  79.           position=0;
  80.       olddir = dir;
  81.         }
  82.  
  83.         // step signal is on the low to high transition.
  84.         if (!oldStep && newStep)
  85.         {
  86.             if (dir)
  87.                 target++;
  88.             else
  89.                 target--;
  90.         }
  91.  
  92.                 byte motor_speed = 0;
  93.                 int distance = abs(position-target);
  94.                 if (distance > 255)
  95.                   motor_speed = 255;
  96.                 else
  97.                   motor_speed = distance;
  98.  
  99.         //super primitive control of the motor.
  100.         if (position > target)
  101.         {
  102.             digitalWrite(MOTOR_DIR_PIN, HIGH);
  103.             analogWrite(MOTOR_SPEED_PIN, motor_speed);
  104.         }
  105.         else if (position < target)
  106.         {
  107.             digitalWrite(MOTOR_DIR_PIN, LOW);
  108.             analogWrite(MOTOR_SPEED_PIN, motor_speed);
  109.         }
  110.         else
  111.         {
  112.             digitalWrite(MOTOR_SPEED_PIN, LOW);
  113.         }
  114.  
  115.         oldStep = newStep;
  116.     }
  117.  
  118.     oldEnable = newEnable;
  119.  
  120.         /*
  121.           delay(1000);
  122.           Serial.print("Pos:");
  123.           Serial.println(position, DEC);
  124.         */
  125. }
  126.  
  127. void read_quadrature_a()
  128. {
  129.     // found a low-to-high on channel A
  130.     if (digitalRead(QUAD_A_PIN) == HIGH)
  131.     {
  132.         // check channel B to see which way
  133.         if (digitalRead(QUAD_B_PIN) == LOW)
  134.             position--;
  135.         else
  136.             position++;
  137.     } // found a high-to-low on channel A
  138.     else
  139.     { // check channel B to see which way
  140.          if (digitalRead(QUAD_B_PIN) == LOW)
  141.              position++;
  142.          else
  143.             position--;
  144.     }
  145. }
  146.  
  147. void read_quadrature_b()
  148. {
  149.     // found a low-to-high on channel A
  150.     if (digitalRead(QUAD_B_PIN) == HIGH)
  151.     {
  152.         // check channel B to see which way
  153.         if (digitalRead(QUAD_A_PIN) == LOW)
  154.             position++;
  155.         else
  156.             position--;
  157.     } // found a high-to-low on channel A
  158.     else
  159.     { // check channel B to see which way
  160.          if (digitalRead(QUAD_A_PIN) == LOW)
  161.              position--;
  162.          else
  163.             position++;
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement