Advertisement
Guest User

Untitled

a guest
Jul 7th, 2011
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 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. char incoming; // incoming serial data
  16. char cmd_arr[80];
  17.  
  18. bool newStep = false;
  19. bool newEnable = false;
  20. bool oldStep = false;
  21. bool oldEnable = true;
  22. bool dir = false;
  23. bool olddir = false;
  24.  
  25. void setup()
  26. {
  27.     Serial.begin(19200);
  28.     Serial.println("MakerBot DC Servo Controller v1.0");
  29.  
  30.     pinMode(QUAD_A_PIN, INPUT);
  31.     pinMode(QUAD_B_PIN, INPUT);
  32.  
  33.     pinMode(DEBUG_PIN, OUTPUT);
  34.  
  35.     pinMode(MOTOR_SLEEP_PIN, OUTPUT);
  36.     digitalWrite(MOTOR_SLEEP_PIN, LOW);
  37.  
  38.     pinMode(MOTOR_MODE_PIN, OUTPUT);
  39.     digitalWrite(MOTOR_MODE_PIN, LOW);
  40.  
  41.     pinMode(MOTOR_SPEED_PIN, OUTPUT);
  42.     digitalWrite(MOTOR_SPEED_PIN, LOW);
  43.  
  44.     pinMode(MOTOR_DIR_PIN, OUTPUT);
  45.     digitalWrite(MOTOR_DIR_PIN, LOW);
  46.  
  47.     pinMode(STEP_PIN, INPUT);
  48.     pinMode(DIR_PIN, INPUT);
  49.     pinMode(ENABLE_PIN, INPUT);
  50.  
  51.     attachInterrupt(0, read_quadrature_a, CHANGE);
  52.     attachInterrupt(1, read_quadrature_b, CHANGE);
  53. }
  54.  
  55. void loop()
  56. {
  57.     int i=0;
  58.     newEnable = digitalRead(ENABLE_PIN);
  59.     newStep = digitalRead(STEP_PIN);
  60.     dir = digitalRead(DIR_PIN);
  61.     byte motor_speed = 0;
  62.  
  63.  
  64.     if (Serial.available()) {
  65.         incoming = Serial.read();  
  66.         if (incoming == 's'){  //step
  67.             newStep ^= newStep;
  68.             Serial.flush();
  69.             Serial.println();
  70.         }else if (incoming == 'e'){  //enable
  71.             newEnable ^= newEnable;
  72.             Serial.flush();
  73.             Serial.println();
  74.         }else if (incoming == 'a'){  //direction
  75.             digitalWrite(MOTOR_DIR_PIN, HIGH);
  76.             Serial.flush();
  77.             Serial.println();
  78.         }else if (incoming == 'd') { //direction
  79.             digitalWrite(MOTOR_DIR_PIN, LOW);
  80.             Serial.flush();
  81.             Serial.println();
  82.         }else if (incoming == '\n' || incoming == '\r'){ //enter
  83.             cmd_arr[i]=0;
  84.             motor_speed=atoi(cmd_arr);
  85.             analogWrite(MOTOR_SPEED_PIN, motor_speed);
  86.             Serial.flush();
  87.             Serial.println();
  88.             i=0;
  89.             cmd_arr[i]=0;
  90.         }else if ((incoming > 48) or (incoming < 57)){ //numbers only
  91.             cmd_arr[i]=incoming; //copy the serial byte to the array
  92.             i++; // increase the array index
  93.             Serial.flush();
  94.             //Serial.print(i);
  95.             //Serial.println(incoming);
  96.         }
  97.     }
  98.      
  99.  
  100.     //low to high transition
  101.     if (!oldEnable && newEnable)
  102.     {
  103.         //enable is active low, so disable.
  104.         digitalWrite(MOTOR_SLEEP_PIN, LOW);
  105.         digitalWrite(MOTOR_SPEED_PIN, LOW);
  106.     }
  107.     // high to low transition
  108.     else if (oldEnable && !newEnable)
  109.     {
  110.         //enable is active low, so enable.
  111.         digitalWrite(MOTOR_SLEEP_PIN, HIGH);
  112.         delay(1); //give it a millisecond to turn on.
  113.     }
  114.  
  115.     //enable is active low, so only do this if we're enabled.
  116.     if (!newEnable)
  117.     {
  118.    
  119.         if (olddir != dir) {
  120.           target=0;
  121.           position=0;
  122.       olddir = dir;
  123.         }
  124.  
  125.         // step signal is on the low to high transition.
  126.         if (!oldStep && newStep)
  127.         {
  128.             if (dir)
  129.                 target++;
  130.             else
  131.                 target--;
  132.         }
  133.  
  134.                 motor_speed = 0;
  135.                 int distance = abs(position-target);
  136.  
  137.                 if (distance > 255)
  138.                   motor_speed = 255;
  139.                 else
  140.                   motor_speed = distance;
  141.  
  142.         //super primitive control of the motor.
  143.         if (position > target)
  144.         {
  145.             digitalWrite(MOTOR_DIR_PIN, HIGH);
  146.             analogWrite(MOTOR_SPEED_PIN, motor_speed);
  147.         }
  148.         else if (position < target)
  149.         {
  150.             digitalWrite(MOTOR_DIR_PIN, LOW);
  151.             analogWrite(MOTOR_SPEED_PIN, motor_speed);
  152.         }
  153.         else
  154.         {
  155.             digitalWrite(MOTOR_SPEED_PIN, LOW);
  156.         }
  157.  
  158.         oldStep = newStep;
  159.     }
  160.  
  161.     oldEnable = newEnable;
  162.  
  163.         /*
  164.           delay(1000);
  165.           Serial.print("Pos:");
  166.           Serial.println(position, DEC);
  167.         */
  168. }
  169.  
  170. void read_quadrature_a()
  171. {
  172.     // found a low-to-high on channel A
  173.     if (digitalRead(QUAD_A_PIN) == HIGH)
  174.     {
  175.         // check channel B to see which way
  176.         if (digitalRead(QUAD_B_PIN) == LOW)
  177.             position--;
  178.         else
  179.             position++;
  180.     } // found a high-to-low on channel A
  181.     else
  182.     { // check channel B to see which way
  183.          if (digitalRead(QUAD_B_PIN) == LOW)
  184.              position++;
  185.          else
  186.             position--;
  187.     }
  188. }
  189.  
  190. void read_quadrature_b()
  191. {
  192.     // found a low-to-high on channel A
  193.     if (digitalRead(QUAD_B_PIN) == HIGH)
  194.     {
  195.         // check channel B to see which way
  196.         if (digitalRead(QUAD_A_PIN) == LOW)
  197.             position++;
  198.         else
  199.             position--;
  200.     } // found a high-to-low on channel A
  201.     else
  202.     { // check channel B to see which way
  203.          if (digitalRead(QUAD_A_PIN) == LOW)
  204.              position--;
  205.          else
  206.             position++;
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement