Advertisement
Semior001

XBOX robot control sketch

Jul 24th, 2016
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.70 KB | None | 0 0
  1. /*
  2.  Example sketch for the Xbox Wireless Reciver library - developed by Kristian Lauszus
  3.  It supports up to four controllers wirelessly
  4.  For more information see the blog post: http://blog.tkjelectronics.dk/2012/12/xbox-360-receiver-added-to-the-usb-host-library/ or
  5.  send me an e-mail:  kristianl@tkjelectronics.com
  6.  */
  7.  
  8. #include <XBOXRECV.h>
  9. // Satisfy IDE, which only needs to see the include statment in the ino.
  10. #ifdef dobogusinclude
  11. #include <spi4teensy3.h>
  12. #endif
  13.  
  14. USB Usb;
  15. XBOXRECV Xbox(&Usb);
  16.  
  17. #define motorPinOne 5 //The chosen pin must have PWM
  18. #define motorPinTwo 6 //The chosen pin must have PWM
  19.  
  20. #define motor2PinOne 4
  21. #define motor2PinTwo 7
  22. #define BRAKE        2
  23.  
  24. /* Define the rotational speed of the motor. MUST be between 0 and 255. */
  25. int pulse = 0;
  26. int forward = 0;
  27. int brake_on = 0;
  28.  
  29.  
  30. void setup() {
  31.   TCCR1B = TCCR1B & 0b11111000 | 0x02;
  32.   Serial.begin(115200);
  33.   if (Usb.Init() == -1) {
  34.     Serial.print(F("\r\nOSC did not start"));
  35.     while(1); //halt
  36.     motors_stop();
  37.     center();
  38.   }
  39.   Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
  40.   pinMode (motorPinOne, OUTPUT);
  41.   pinMode (motorPinTwo, OUTPUT);
  42.   pinMode (motor2PinOne, OUTPUT);
  43.   pinMode (motor2PinTwo, OUTPUT);
  44.   pinMode (BRAKE, OUTPUT);
  45.    
  46.   digitalWrite (BRAKE, LOW);
  47. }
  48.  
  49. void loop() {
  50.   Usb.Task();
  51.   if(Xbox.XboxReceiverConnected)
  52.     {
  53.     for(uint8_t i=0;i<4;i++)
  54.       {
  55.       if(Xbox.getButtonPress(L2, i))
  56.         {
  57.         Serial.print("L2: ");
  58.         Serial.print(Xbox.getButtonPress(L2, i));
  59.         pulse = Xbox.getButtonPress(L2, i);
  60.         forward = 0;
  61.         clockwise();
  62.         }
  63.          
  64.       if(Xbox.getButtonPress(R2, i))      
  65.         {
  66.         Serial.print("R2: ");
  67.         Serial.println(Xbox.getButtonPress(R2, i));
  68.         pulse = Xbox.getButtonPress(R2, i);
  69.         forward = 1;
  70.         counterClockwise();
  71.         }
  72.          
  73.        if(Xbox.getAnalogHat(LeftHatX, i) > 7500 || Xbox.getAnalogHat(LeftHatX, i) < -7500 || Xbox.getAnalogHat(LeftHatY, i) > 7500 || Xbox.getAnalogHat(LeftHatY, i) < -7500 || Xbox.getAnalogHat(RightHatX, i) > 7500 || Xbox.getAnalogHat(RightHatX, i) < -7500 || Xbox.getAnalogHat(RightHatY, i) > 7500 || Xbox.getAnalogHat(RightHatY, i) < -7500)
  74.          {
  75.           if(Xbox.getAnalogHat(LeftHatX, i) > 7500)
  76.             {
  77.             Serial.print(F("LeftHatX: "));
  78.             Serial.print(Xbox.getAnalogHat(LeftHatX, i));
  79.             Serial.print("\t");
  80.              
  81.             if (Xbox.getAnalogHat(LeftHatX, i) > 26000)
  82.               turn_right();
  83.             else
  84.               center();
  85.             }  
  86.        
  87.         if (Xbox.getAnalogHat(LeftHatX, i) < -7500)
  88.           {
  89.           Serial.print(F("LeftHatX: "));
  90.           Serial.print(Xbox.getAnalogHat(LeftHatX, i));
  91.           Serial.print("\t");
  92.            
  93.           if (Xbox.getAnalogHat(LeftHatX, i) < -26000)
  94.             turn_left();
  95.           else
  96.             center();  
  97.         }
  98.          
  99.         if(Xbox.getAnalogHat(LeftHatY, i) > 7500 || Xbox.getAnalogHat(LeftHatY, i) < -7500)
  100.           {
  101.           Serial.print(F("LeftHatY: "));
  102.           Serial.print(Xbox.getAnalogHat(LeftHatY, i));
  103.           Serial.print("\t");
  104.           }
  105.            
  106.         if(Xbox.getAnalogHat(RightHatX, i) > 7500)
  107.           {
  108.           Serial.print(F("RightHatX: "));
  109.           Serial.print(Xbox.getAnalogHat(RightHatX, i));
  110.           Serial.print("\t");  
  111.           }
  112.        
  113.          if (Xbox.getAnalogHat(RightHatX, i) < -7500)
  114.            {
  115.            Serial.print(F("RightHatX: "));
  116.            Serial.print(Xbox.getAnalogHat(RightHatX, i));
  117.            Serial.print("\t");
  118.            }
  119.          
  120.         if(Xbox.getAnalogHat(RightHatY, i) > 7500 || Xbox.getAnalogHat(RightHatY, i) < -7500) {
  121.           Serial.print(F("RightHatY: "));
  122.           Serial.print(Xbox.getAnalogHat(RightHatY, i));  
  123.         }
  124.         Serial.println();
  125.       }
  126.      
  127.       if(Xbox.getButtonClick(UP, i))
  128.         {
  129.         Serial.println(F("Up"));
  130.         }
  131.          
  132.       if(Xbox.getButtonClick(DOWN, i))
  133.         {
  134.         Serial.println(F("Down"));
  135.         }
  136.          
  137.       if(Xbox.getButtonClick(LEFT, i))
  138.         {
  139.         Serial.println(F("Left"));
  140.         }
  141.          
  142.       if(Xbox.getButtonClick(RIGHT, i))
  143.         {
  144.         Serial.println(F("Right"));
  145.         }
  146.  
  147.       if(Xbox.getButtonClick(START, i))
  148.         {
  149.         Serial.println(F("Start"));
  150.         }
  151.          
  152.       if(Xbox.getButtonClick(BACK, i))
  153.         {
  154.         Serial.println(F("Back"));
  155.         }
  156.        
  157.       if(Xbox.getButtonClick(L3, i))
  158.         Serial.println(F("L3"));
  159.          
  160.       if(Xbox.getButtonClick(R3, i))
  161.         Serial.println(F("R3"));
  162.  
  163.       if(Xbox.getButtonClick(L1, i))
  164.         Serial.println(F("L1"));
  165.          
  166.       if(Xbox.getButtonClick(R1, i))
  167.         Serial.println(F("R1"));
  168.          
  169.       if(Xbox.getButtonClick(XBOX, i))
  170.         {
  171.         Xbox.setLedMode(ROTATING, i);
  172.         Serial.println(F("Xbox"));        
  173.         }
  174.  
  175.       if(Xbox.getButtonClick(A, i))
  176.         {
  177.         Serial.println(F("A"));
  178.       }
  179.        
  180.     if(Xbox.getButtonClick(B, i))
  181.       {
  182.       Serial.println(F("B"));
  183.       digitalWrite(BRAKE, HIGH);
  184.       brake_on = 1;
  185.       pulse = 100;
  186.        
  187.       if (forward)
  188.         clockwise();
  189.       else
  190.         counterClockwise();
  191.       }
  192.        
  193.     if(Xbox.getButtonClick(X, i))
  194.       Serial.println(F("X"));
  195.        
  196.     if(Xbox.getButtonClick(Y, i))
  197.       {
  198.       Serial.println(F("Y"));
  199.       motors_stop();
  200.       }
  201.     }
  202.   }
  203.   delay(1);
  204. }
  205.  
  206. void clockwise()
  207. {
  208.   Serial.print("Rotation is clockwise and speed is ");
  209.   Serial.println(pulse);
  210.   analogWrite(motorPinOne,pulse);   // set leg 1 of the H-bridge low
  211.   analogWrite(motorPinTwo,0);
  212.    
  213.   if (brake_on)
  214.     {
  215.     delay(600);
  216.     digitalWrite(BRAKE, LOW);
  217.     brake_on = 0;
  218.     pulse = 0;    
  219.     }
  220. }
  221.  
  222. void counterClockwise()
  223. {
  224.   Serial.print("Rotation is counter-clockwise and speed is ");
  225.   Serial.println(pulse);
  226.   analogWrite(motorPinOne,0);   // set leg 1 of the H-bridge low
  227.   analogWrite(motorPinTwo,pulse);
  228.    
  229.   if (brake_on)
  230.     {
  231.     delay(600);
  232.     digitalWrite(BRAKE, LOW);
  233.     brake_on = 0;
  234.     pulse = 0;    
  235.     }
  236.      
  237.  
  238. }
  239.  
  240. void turn_left()
  241. {
  242.   digitalWrite(motor2PinOne,HIGH);   // set leg 1 of the H-bridge low
  243.   digitalWrite(motor2PinTwo,LOW);
  244. }
  245.  
  246. void turn_right()
  247. {
  248.   digitalWrite(motor2PinOne,LOW);   // set leg 1 of the H-bridge low
  249.   digitalWrite(motor2PinTwo,HIGH);
  250. }
  251.  
  252. void motors_stop()
  253. {
  254.   analogWrite(motorPinOne,0);
  255.   analogWrite(motorPinTwo,0);
  256.   pulse = 0;
  257. }
  258.  
  259. void center()
  260. {
  261.   digitalWrite(motor2PinOne, LOW);
  262.   digitalWrite(motor2PinTwo, LOW);
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement