Advertisement
Guest User

Untitled

a guest
Jul 7th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 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.print("newStep=");
  70.             Serial.println(newStep,DEC);
  71.         }else if (incoming == 'e'){  //enable
  72.             newEnable ^= newEnable;
  73.             Serial.flush();
  74.             Serial.print("newEnable=");
  75.             Serial.println(newEnable,DEC);
  76.         }else if (incoming == 'a'){  //direction
  77.             digitalWrite(MOTOR_DIR_PIN, HIGH);
  78.             Serial.flush();
  79.             Serial.println("Dir=1");
  80.         }else if (incoming == 'd') { //direction
  81.             digitalWrite(MOTOR_DIR_PIN, LOW);
  82.             Serial.flush();
  83.             Serial.println("Dir=0");
  84.         }else if (incoming == '\n' || incoming == '\r'){ //enter
  85.             cmd_arr[i]=0;
  86.             motor_speed=atoi(cmd_arr);
  87.             analogWrite(MOTOR_SPEED_PIN, motor_speed);
  88.             Serial.flush();
  89.             Serial.print("motor_speed=");
  90.             Serial.println(motor_speed,DEC);
  91.             i=0;
  92.             cmd_arr[i]=0;
  93.         }else if ((incoming > 48) or (incoming < 57)){ //numbers only
  94.             cmd_arr[i]=incoming; //copy the serial byte to the array
  95.             i++; // increase the array index
  96.             Serial.flush();
  97.             //Serial.print(i);
  98.             //Serial.println(incoming);
  99.         }
  100.     }
  101.      
  102.  
  103.     //low to high transition
  104.     if (!oldEnable && newEnable)
  105.     {
  106.         //enable is active low, so disable.
  107.         digitalWrite(MOTOR_SLEEP_PIN, LOW);
  108.         digitalWrite(MOTOR_SPEED_PIN, LOW);
  109.     }
  110.     // high to low transition
  111.     else if (oldEnable && !newEnable)
  112.     {
  113.         //enable is active low, so enable.
  114.         digitalWrite(MOTOR_SLEEP_PIN, HIGH);
  115.         delay(1); //give it a millisecond to turn on.
  116.     }
  117.  
  118.     //enable is active low, so only do this if we're enabled.
  119.     if (!newEnable)
  120.     {
  121.    
  122.         if (olddir != dir) {
  123.           target=0;
  124.           position=0;
  125.       olddir = dir;
  126.         }
  127.  
  128.         // step signal is on the low to high transition.
  129.         if (!oldStep && newStep)
  130.         {
  131.             if (dir)
  132.                 target++;
  133.             else
  134.                 target--;
  135.         }
  136.  
  137.                 motor_speed = 0;
  138.                 int distance = abs(position-target);
  139.  
  140.                 if (distance > 255)
  141.                   motor_speed = 255;
  142.                 else
  143.                   motor_speed = distance;
  144.  
  145.         //super primitive control of the motor.
  146.         if (position > target)
  147.         {
  148.             digitalWrite(MOTOR_DIR_PIN, HIGH);
  149.             analogWrite(MOTOR_SPEED_PIN, motor_speed);
  150.         }
  151.         else if (position < target)
  152.         {
  153.             digitalWrite(MOTOR_DIR_PIN, LOW);
  154.             analogWrite(MOTOR_SPEED_PIN, motor_speed);
  155.         }
  156.         else
  157.         {
  158.             digitalWrite(MOTOR_SPEED_PIN, LOW);
  159.         }
  160.  
  161.         oldStep = newStep;
  162.     }
  163.  
  164.     oldEnable = newEnable;
  165.  
  166.         /*
  167.           delay(1000);
  168.           Serial.print("Pos:");
  169.           Serial.println(position, DEC);
  170.         */
  171. }
  172.  
  173. void read_quadrature_a()
  174. {
  175.     // found a low-to-high on channel A
  176.     if (digitalRead(QUAD_A_PIN) == HIGH)
  177.     {
  178.         // check channel B to see which way
  179.         if (digitalRead(QUAD_B_PIN) == LOW)
  180.             position--;
  181.         else
  182.             position++;
  183.     } // found a high-to-low on channel A
  184.     else
  185.     { // check channel B to see which way
  186.          if (digitalRead(QUAD_B_PIN) == LOW)
  187.              position++;
  188.          else
  189.             position--;
  190.     }
  191. }
  192.  
  193. void read_quadrature_b()
  194. {
  195.     // found a low-to-high on channel A
  196.     if (digitalRead(QUAD_B_PIN) == HIGH)
  197.     {
  198.         // check channel B to see which way
  199.         if (digitalRead(QUAD_A_PIN) == LOW)
  200.             position++;
  201.         else
  202.             position--;
  203.     } // found a high-to-low on channel A
  204.     else
  205.     { // check channel B to see which way
  206.          if (digitalRead(QUAD_A_PIN) == LOW)
  207.              position--;
  208.          else
  209.             position++;
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement