Advertisement
TungstenVn

MAIN_MiniRBC

Jul 3rd, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. #include <PS2X_lib.h>
  2. #include <SimpleKalmanFilter.h>
  3. SimpleKalmanFilter Ch0(10, 10, 5);
  4. SimpleKalmanFilter Ch1(10, 10, 5);
  5. SimpleKalmanFilter Ch2(10, 10, 5);
  6. #define PS2_DAT 13 // data
  7. #define PS2_CMD 11 //command
  8. #define PS2_SEL 10 // attention
  9. #define PS2_CLK 12 //clock
  10. PS2X ps2x; // tạo PS2 điều khiển lớp
  11. int error = 0;
  12. byte type = 0;
  13. byte vibrate = 0;
  14.  
  15. //Chẵn là dương thì thuận chiều, lẽ là dương thì ngược chiều
  16.  
  17. const byte MotorInput[4] = {3, 9, 10, 11};
  18. const byte MotorOutput[4] = (5, 8, 12, 13};
  19. //Pittong - Dùng High,Low
  20. #define LeftHand 1
  21. #define RightHand 2
  22. #define MiddleHand 4
  23. bool isCloseL =false; //Ktra tay trái đóng hay mở
  24. bool isCloseR =false;
  25. bool isCloseM = false;
  26. bool isCloseBoth = false; //Ktra cả 2 tay đang đóng hay mở
  27. bool isPress1 =false,isPress2 = false;
  28. //Ổ bi,dùng analogWrite
  29. #define obi_1 6
  30. #define obi_2 7
  31.  
  32. //---------------------------------------------------------------------------------------------------------------------------------
  33. void setup()
  34. {
  35.     Serial.begin(57600);
  36.     error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, true,true);
  37.     if (error == 0) {
  38.         Serial.print("Đã tìm thấy bộ điều khiển ");
  39.     }
  40.     else if (error == 1)
  41.         Serial.println("Không kết nối đc, thử lại...");
  42.  
  43.     else if (error == 2)
  44.         Serial.println("Bộ điều khiển tìm thấy nhưng không chấp nhận lệnh");
  45.  
  46.     else if (error == 3)
  47.         Serial.println("Bộ điều khiển từ chối để vào chế độ Pressures ,hoặc ko hỗ trợ");
  48.  
  49.     type = ps2x.readType();
  50.     switch (type) {
  51.     case 0:
  52.         Serial.print("Tay điều khiển không phù hợp ");
  53.         break;
  54.     case 1:
  55.         Serial.print("Đã tìm thấy DualShock ");
  56.         break;
  57.     }
  58. }
  59.  
  60.  
  61. int Channels[3] = {0};
  62. long long int sys_start = 0;
  63. int MAX_SPEED = 255;
  64. int MIN_SPEED = -MAX_SPEED;
  65.  //=======================================================================================================================================
  66. void loop(){
  67.   buttonState();
  68.   ProcessMotor();
  69. }
  70.  
  71. void buttonState(){
  72.   if(error == 0){
  73.       return;
  74.   }else{
  75.       ps2x.read_gamepad(false, vibrate);
  76.       //vibrate = ps2x.Analog(PSAB_BLUE);
  77.  
  78.   }  
  79.     Channels[0] = map(ps2x.Analog(PSS_LY), 256, 0, -255, 255);
  80.     Channels[0] = Ch0.updateEstimate(Channels[0]);
  81.     Channels[1] = map(ps2x.Analog(PSS_LX), 0, 256, -255, 255);
  82.     Channels[1] = Ch1.updateEstimate(Channels[1]);
  83.     Channels[2] = map(ps2x.Analog(PSS_RX), 0, 256, -255, 255);
  84.     Channels[2] = Ch2.updateEstimate(Channels[2]);
  85.  
  86.     for (int i = 0; i < 3; i++) {
  87.       if (abs(Channels[i]) <= 100) {
  88.         Channels[i] = 0;
  89.       }
  90.     }
  91.  
  92.     int sum = 0;
  93.     for (int i = 0; i < 3; i++) {
  94.       sum += abs(Channels[i]);
  95.     }
  96.     sum /= 3;
  97.     if (sum < 20) {
  98.       sys_start = millis();
  99.     }
  100.  
  101.     Channels[2] /= 3.5;
  102.     if (millis() - sys_start < 400) {
  103.       Channels[2] = (float)(Channels[2] * ((millis() - sys_start + 310)) / 710);
  104.     }
  105.    
  106.     taygap();
  107. }
  108.  
  109. void taygap(){
  110.   //----------------Ổ bi-------------------------------------------------------------------------
  111.  
  112.    //Khi bấm nút thì if đầu tiên đc gọi, nếu đang giữ nút PAD_UP thì gọi kéo từ từ lên,
  113.    //Nếu đang giữ nút PAD_UP mà bấm PAD_DOWN sẽ k có gì mới xảy ra
  114.    //Nếu thả nút PAD_UP thì if đầu tiên đc gọi , nếu ko giữ nút nữa thì dừng động cơ, tương tự kéo xuống
  115.    
  116.     if (ps2x.NewButtonState()){ // chỉ chạy 1 lần khi có bất kỳ 1 nút nào đó chuyển trạng thái on->off,off->on
  117.       if(!isPress2){
  118.          if(ps2x.Button(PSB_PAD_UP)){
  119.            isPress1 = true;
  120.            analogWrite(obi_1,100); //Nâng lên từ từ
  121.            analogWrite(obi_2,0);
  122.          }else{
  123.            analogWrite(obi_1,0);
  124.            analogWrite(obi_2,0);
  125.            isPress1 = false;
  126.          }
  127.       }
  128.       if(!isPress1){
  129.          if(ps2x.Button(PSB_PAD_DOWN)){
  130.            isPress2 = true;
  131.            analogWrite(obi_1,0);
  132.            analogWrite(obi_2,100); //Nâng lên từ từ
  133.          }else{
  134.            isPress2 = false;
  135.            analogWrite(obi_1,0);
  136.            analogWrite(obi_2,0);
  137.          }
  138.       }
  139.     }
  140.  
  141.    
  142.     //------------------Tay Gắp--------------------------------------
  143.  
  144.    
  145.     if(ps2x.ButtonPressed(PSB_PINK)){ //Tay Trái
  146.       //Code trong này chỉ chạy 1 lần dù có nhấn giữ  
  147.        if(!isCloseL){
  148.          digitalWrite(LeftHand,HIGH);
  149.          isCloseL = true;  
  150.       }else{
  151.          digitalWrite(LeftHand,LOW);
  152.          isCloseL = false;
  153.       }
  154.     }
  155.  
  156.     if(ps2x.ButtonPressed(PSB_RED)){ //Tay Phải
  157.       //Code trong này chỉ chạy 1 lần dù có nhấn giữ  
  158.        if(!isCloseR){
  159.          digitalWrite(RightHand,HIGH);
  160.          isCloseR = true;  
  161.       }else{
  162.          digitalWrite(RightHand,LOW);
  163.          isCloseR = false;
  164.       }
  165.     }
  166.  
  167.     if(ps2x.ButtonPressed(PSB_GREEN)){ //Tay Giữa
  168.        if(!isCloseM){
  169.          digitalWrite(MiddleHand,HIGH);
  170.          isCloseM = true;  
  171.       }else{
  172.          digitalWrite(MiddleHand,LOW);
  173.          isCloseM = false;
  174.       }
  175.     }
  176.  
  177.     if(ps2x.ButtonPressed(PSB_BLUE)){ //Đóng mở 2 tay cùng lúc
  178.       //Code trong này chỉ chạy 1 lần dù có nhấn giữ  
  179.        if(!isCloseBoth){
  180.          digitalWrite(LeftHand,HIGH);
  181.          digitalWrite(RightHand,HIGH);
  182.          isCloseL = true;
  183.          isCloseR = true;
  184.       }else{
  185.          digitalWrite(LeftHand,LOW);
  186.          digitalWrite(RightHand,LOW);
  187.          isCloseL = false;
  188.          isCloseR = false;
  189.       }
  190.     }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement