Advertisement
Guest User

MotorContoller

a guest
Jun 26th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.97 KB | None | 0 0
  1. /*
  2. IBT-2 Motor Control Board driven by Arduino.
  3.  
  4. Speed and direction controlled by a potentiometer attached to analog input 0.
  5. One side pin of the potentiometer (either one) to ground; the other side pin to +5V
  6.  
  7. Connection to the RIGHT IBT-2 board:
  8. IBT-2 pin 1 (RPWM) to Arduino pin 5(PWM)
  9. IBT-2 pin 2 (LPWM) to Arduino pin 6(PWM)
  10. IBT-2 pins 3 (R_EN), 4 (L_EN), 7 (VCC) to Arduino 5V pin
  11. IBT-2 pin 8 (GND) to Arduino GND
  12. IBT-2 pins 5 (R_IS) and 6 (L_IS) not connected
  13.  
  14. Connection to the LEFT IBT-2 board:
  15. IBT-2 pin 1 (RPWM) to Arduino pin 9(PWM)
  16. IBT-2 pin 2 (LPWM) to Arduino pin 10(PWM)
  17. IBT-2 pins 3 (R_EN), 4 (L_EN), 7 (VCC) to Arduino 5V pin
  18. IBT-2 pin 8 (GND) to Arduino GND
  19. IBT-2 pins 5 (R_IS) and 6 (L_IS) not connected
  20. */
  21.  
  22. int R_SENSOR_PIN = 0; // center pin of the potentiometer
  23. int L_SENSOR_PIN = 1; // center pin of the potentiometer
  24.  
  25. //Right Motor
  26. int R_RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
  27. int R_LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)
  28.  
  29. //Left Motor
  30. int L_RPWM_Output = 9; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
  31. int L_LPWM_Output = 10; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)
  32.  
  33. #define CH1_PIN  2
  34. #define CH2_PIN  3
  35.  
  36. double RAWPWM_CH1;
  37. double RAWPWM_CH2;
  38.  
  39. double PWM_CH1;
  40. double PWM_CH2;
  41.  
  42. const int ledPin =  13;      // the number of the LED pin
  43. int ledState = LOW;             // ledState used to set the LED
  44. unsigned long previousMillis = 0;        // will store last time LED was updated
  45. const long interval = 100;           // interval at which to blink (milliseconds)
  46.  
  47.  
  48. void setup()
  49. {
  50.   pinMode(ledPin, OUTPUT);
  51.   Serial.begin(9600);
  52.  
  53.   pinMode(CH1_PIN, INPUT);
  54.   pinMode(CH2_PIN, INPUT);
  55.  
  56. //Right Motor
  57.   pinMode(R_RPWM_Output, OUTPUT);
  58.   pinMode(R_LPWM_Output, OUTPUT);
  59.  
  60. //Left Motor
  61.   pinMode(L_RPWM_Output, OUTPUT);
  62.   pinMode(L_LPWM_Output, OUTPUT);
  63. }
  64.  
  65.  
  66. void loop()
  67. {
  68. //Read Right & Left POT AnalogIN //
  69.  
  70.   int R_sensorValue = analogRead(R_SENSOR_PIN);
  71.   int L_sensorValue = analogRead(L_SENSOR_PIN);
  72.  
  73. // Get PWM data & Contrain values //
  74.  
  75.   RAWPWM_CH1 = pulseIn(CH1_PIN, HIGH);
  76.   PWM_CH1 = constrain(RAWPWM_CH1, 0, 1970);
  77.  
  78.   RAWPWM_CH2 = pulseIn(CH2_PIN, HIGH);
  79.   PWM_CH2 = constrain(RAWPWM_CH2, 0, 1970);
  80.    
  81.   Serial.print("PWM_CH1 - ");
  82.   Serial.print(PWM_CH1);
  83.   Serial.print(" - ");
  84.  
  85.   Serial.print("PWM_CH2 - ");
  86.   Serial.print(PWM_CH2);
  87.   Serial.print(" - ");
  88.  
  89.  
  90. // FAILSAFE //
  91.  
  92.    if (PWM_CH1 == 0 || PWM_CH2 == 0)
  93.   {
  94. analogWrite(R_LPWM_Output, 0);
  95. analogWrite(R_RPWM_Output, 0);
  96. analogWrite(L_LPWM_Output, 0);
  97. analogWrite(L_RPWM_Output, 0);
  98. Serial.println("!! FAILSAFE !!");
  99. Blink();
  100.   }
  101.  else
  102.  {
  103.  
  104. // Stick Centered //
  105.  
  106.  if (PWM_CH1 >= 1464 && PWM_CH1 <= 1530)
  107.   {
  108. analogWrite(R_LPWM_Output, 0);
  109. analogWrite(R_RPWM_Output, 0);
  110. Serial.print("CH1 Centered - ");
  111.   }
  112.  if (PWM_CH2 >= 1464 && PWM_CH2 <= 1530)
  113.   {
  114. analogWrite(L_LPWM_Output, 0);
  115. analogWrite(L_RPWM_Output, 0);
  116. Serial.print("CH2 Centered - ");
  117.   }
  118.  
  119.  
  120.  
  121. // Right Motor Control Loop //
  122.  
  123.   if (PWM_CH1 >=1 && PWM_CH1 <= 1463)
  124.   {
  125.     // reverse rotation
  126.     int R_reversePWM = -(PWM_CH1 - 1495) / 2;
  127.     analogWrite(R_LPWM_Output, 0);
  128.     analogWrite(R_RPWM_Output, R_reversePWM);
  129.     Serial.print("R_Reverse - ");
  130.     Serial.print(R_reversePWM);
  131.     Serial.print(" ");
  132.   }
  133.   else if (PWM_CH1 >=1531)
  134.   {
  135.     // forward rotation
  136.     int R_forwardPWM = (PWM_CH1 - 1462) / 2;
  137.     analogWrite(R_LPWM_Output, R_forwardPWM);
  138.     analogWrite(R_RPWM_Output, 0);
  139.     Serial.print("R_Forward - ");
  140.     Serial.print(R_forwardPWM);
  141.     Serial.print(" ");
  142.   }
  143.  
  144. // Left Motor Control Loop //
  145.  
  146.   if (PWM_CH2 >=1 && PWM_CH2 <= 1463)
  147.   {
  148.     // reverse rotation
  149.     int L_reversePWM = -(PWM_CH2 - 1495) / 2;
  150.     analogWrite(L_LPWM_Output, 0);
  151.     analogWrite(L_RPWM_Output, L_reversePWM);
  152.     Serial.print("L_Reverse - ");
  153.     Serial.print(L_reversePWM);
  154.     Serial.print(" ");
  155.    
  156.   }
  157.   else if (PWM_CH2 >=1531)
  158.   {
  159.     // forward rotation
  160.     int L_forwardPWM = (PWM_CH2 - 1462) / 2;
  161.     analogWrite(L_LPWM_Output, L_forwardPWM);
  162.     analogWrite(L_RPWM_Output, 0);
  163.     Serial.print("L_Forward - ");
  164.     Serial.print(L_forwardPWM);
  165.     Serial.print(" ");
  166.   }
  167.   }
  168.   Serial.println(" ");
  169.   }
  170.  
  171.  
  172. void Blink()
  173. {
  174.   // here is where you'd put code that needs to be running all the time.
  175.  
  176.   // check to see if it's time to blink the LED; that is, if the
  177.   // difference between the current time and last time you blinked
  178.   // the LED is bigger than the interval at which you want to
  179.   // blink the LED.
  180.   unsigned long currentMillis = millis();
  181.  
  182.   if(currentMillis - previousMillis >= interval) {
  183.     // save the last time you blinked the LED
  184.     previousMillis = currentMillis;  
  185.  
  186.     // if the LED is off turn it on and vice-versa:
  187.     if (ledState == LOW)
  188.       ledState = HIGH;
  189.     else
  190.       ledState = LOW;
  191.  
  192.     // set the LED with the ledState of the variable:
  193.     digitalWrite(ledPin, ledState);
  194.   }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement