极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 19927|回复: 5

【求教】Arduino ps2手柄 控制云台 程序

[复制链接]
发表于 2015-10-28 19:43:19 | 显示全部楼层 |阅读模式
求教,才接触Arduino 买的是unor3 想做一个用ps2手柄控制云台。目前按下手柄在串口显示器上显示按下的键会用库实现了,单独输入角度控制舵机也找到程序了 但是无奈不会把两个程序合在一起。求大神帮忙。
IDE是1.6.5
ps2是 Arduino-PS2X-master\PS2X_lib\Examples\PS2X_Example
舵机是 arduino学习笔记23 - 任意输出舵机角度实验

希望能做到简单点的按下手柄比如:SELECT 输入0 舵机转到0度 START 输入15 舵机转到15度 这样就好


当然很希望能够做到摇杆控制但是目前不敢奢求太多.....


因为目前只需要这部分 所以没打算系统学习 哪位大大能告诉我 去学哪部分能解决这个问题也好QAQ


回复

使用道具 举报

发表于 2015-10-28 20:49:59 | 显示全部楼层
你的问题还是太笼统了。最好问你遇到的具体问题
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-10-28 22:09:07 | 显示全部楼层
本帖最后由 Errock 于 2015-10-28 22:13 编辑
zoologist 发表于 2015-10-28 20:49
你的问题还是太笼统了。最好问你遇到的具体问题


大大泥豪~就是 想把两个程序合起来...
  1. int i,val;
  2. char a[3];
  3. boolean display;
  4. #include <Servo.h>
  5. Servo servo1;

  6. void setup()
  7. {
  8.   Serial.begin(9600);
  9.   servo1.attach(4);//舵機一接pin4
  10. }

  11. void loop()
  12. {
  13.   if (Serial.available()){        //如果有数据输入.....
  14.     delay(30);                    //等待30毫秒让所有输入数据从串口传输完毕.....
  15.     if (Serial.available() <= 3){ //如果输入数据位数'<=3'.....
  16.       while (Serial.available()){ //开始读取数据直到[串口输入缓存被清空]
  17.         a[i++] = Serial.read();   //读取数据到[数组"a"]
  18.       }
  19.       display = 1;                    //数据读取完毕以后'打开'显示输出开关
  20.     }
  21.     else {                        //如果输入数据位数'>3'.....
  22.       Serial.flush();             //刷新串口输入缓存
  23.     }
  24.   }
  25. /*======================直接通过串口返回输入数值模块======================
  26.   if (display)                    //如果[显示输出开关]被'打开'则显示[数组"a"]的数据
  27.   {
  28.     for (i = 0; i <= sizeof(a); i++)
  29.     {
  30.      Serial.print("a[");
  31.      Serial.print(i);
  32.      Serial.print("]= ");
  33.       Serial.print(a[i]);
  34.       Serial.print(" | ");
  35.     }
  36.     Serial.println();
  37.     display = 0;                  //显示完毕'关闭'显示输出开关
  38.     Serial.flush();               //刷新串口输入缓存
  39.     for (i = 0; i <= 3; i++)      //重置[数组"a"]
  40.     {
  41.       a[i] = 0;
  42.     }
  43.     i = 0;                        //重置"计数变量"[i]
  44.   }
  45. //=======================通过加减符号控制舵机增减一度转动=================*/
  46. if (a[0] == 43 && display){
  47. val++;
  48. servo1.write(val);
  49. Serial.println(val);
  50.     display = 0;                  //显示完毕'关闭'显示输出开关
  51.     Serial.flush();               //刷新串口输入缓存
  52.     for (i = 0; i <= 3; i++)      //重置[数组"a"]
  53.     {
  54.       a[i] = 0;
  55.     }
  56.     i = 0;                        //重置"计数变量"[i]
  57. }
  58. if (a[0] == 45 && display){
  59. val--;
  60. servo1.write(val);
  61. Serial.println(val);
  62.     display = 0;                  //显示完毕'关闭'显示输出开关
  63.     Serial.flush();               //刷新串口输入缓存
  64.     for (i = 0; i <= 3; i++)      //重置[数组"a"]
  65.     {
  66.       a[i] = 0;
  67.     }
  68.     i = 0;                        //重置"计数变量"[i]
  69. }

  70. //========================判断及修正输入数据位数模块======================
  71. if (display)                    //如果[显示输出开关]被'打开'则显示[数组"a"]的数据
  72.   {
  73.    if (!a[2]){ //如果输入数据为两位数(最后一位空)
  74.    if (!a[1]){ //如果输入数据为一位数(最后两位空)
  75.    a[2] = a[0];
  76.    a[1] = 48;
  77.    a[0] = 48;
  78.    }
  79.    else {
  80.    a[2] = a[1];
  81.    a[1] = a[0];
  82.    a[0] = 48;
  83.    }
  84.    }
  85. //==============转换变量类型后输出给舵机且通过串口返回结果值==============
  86. for (i=0;i<=3;i++){ //变量类型:char to int (48为0的ASCII)
  87. a[i] -= 48;
  88. }
  89. val = 100*a[0] + 10*a[1] + a[2];
  90. // Serial.print("val: ");
  91. servo1.write(val);
  92. Serial.println(val);

  93. /* int val2 = random(50); //int型变量加法测试
  94. val += val2;
  95. Serial.print("+");
  96. Serial.print(val2);
  97. Serial.print("=");
  98. Serial.println(val);
  99. */
  100.     display = 0;                  //显示完毕'关闭'显示输出开关
  101.     Serial.flush();               //刷新串口输入缓存
  102.     for (i = 0; i <= 3; i++)      //重置[数组"a"]
  103.     {
  104.       a[i] = 0;
  105.     }
  106.     i = 0;                        //重置"计数变量"[i]
  107. //    val = 0;
  108. }
  109. }
复制代码


  1. #include <PS2X_lib.h>  //for v1.6

  2. PS2X ps2x; // create PS2 Controller Class

  3. //right now, the library does NOT support hot pluggable controllers, meaning
  4. //you must always either restart your Arduino after you conect the controller,
  5. //or call config_gamepad(pins) again after connecting the controller.
  6. int error = 0;
  7. byte type = 0;
  8. byte vibrate = 0;

  9. void setup(){
  10. Serial.begin(57600);

  11. //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
  12.   
  13. error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error

  14. if(error == 0){
  15.    Serial.println("Found Controller, configured successful");
  16.    Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
  17.   Serial.println("holding L1 or R1 will print out the analog stick values.");
  18.   Serial.println("Go to www.billporter.info for updates and to report bugs.");
  19. }
  20.    
  21.   else if(error == 1)
  22.    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
  23.    
  24.   else if(error == 2)
  25.    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
  26.    
  27.   else if(error == 3)
  28.    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  29.    
  30.    //Serial.print(ps2x.Analog(1), HEX);
  31.    
  32.    type = ps2x.readType();
  33.      switch(type) {
  34.        case 0:
  35.         Serial.println("Unknown Controller type");
  36.        break;
  37.        case 1:
  38.         Serial.println("DualShock Controller Found");
  39.        break;
  40.        case 2:
  41.          Serial.println("GuitarHero Controller Found");
  42.        break;
  43.      }
  44.   
  45. }

  46. void loop(){
  47.    /* You must Read Gamepad to get new values
  48.    Read GamePad and set vibration values
  49.    ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
  50.    if you don't enable the rumble, use ps2x.read_gamepad(); with no values
  51.    
  52.    you should call this at least once a second
  53.    */
  54.    
  55.    
  56.    
  57. if(error == 1) //skip loop if no controller found
  58.   return;
  59.   
  60. if(type == 2){ //Guitar Hero Controller
  61.    
  62.    ps2x.read_gamepad();          //read controller
  63.    
  64.    if(ps2x.ButtonPressed(GREEN_FRET))
  65.      Serial.println("Green Fret Pressed");
  66.    if(ps2x.ButtonPressed(RED_FRET))
  67.      Serial.println("Red Fret Pressed");
  68.    if(ps2x.ButtonPressed(YELLOW_FRET))
  69.      Serial.println("Yellow Fret Pressed");
  70.    if(ps2x.ButtonPressed(BLUE_FRET))
  71.      Serial.println("Blue Fret Pressed");
  72.    if(ps2x.ButtonPressed(ORANGE_FRET))
  73.      Serial.println("Orange Fret Pressed");
  74.      

  75.     if(ps2x.ButtonPressed(STAR_POWER))
  76.      Serial.println("Star Power Command");
  77.    
  78.     if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
  79.      Serial.println("Up Strum");
  80.     if(ps2x.Button(DOWN_STRUM))
  81.      Serial.println("DOWN Strum");
  82.   

  83.     if(ps2x.Button(PSB_START))                   //will be TRUE as long as button is pressed
  84.          Serial.println("Start is being held");
  85.     if(ps2x.Button(PSB_SELECT))
  86.          Serial.println("Select is being held");

  87.    
  88.     if(ps2x.Button(ORANGE_FRET)) // print stick value IF TRUE
  89.     {
  90.         Serial.print("Wammy Bar Position:");
  91.         Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
  92.     }
  93. }

  94. else { //DualShock Controller
  95.   
  96.     ps2x.read_gamepad(false, vibrate);          //read controller and set large motor to spin at 'vibrate' speed
  97.    
  98.     if(ps2x.Button(PSB_START))                   //will be TRUE as long as button is pressed
  99.          Serial.println("Start is being held");
  100.     if(ps2x.Button(PSB_SELECT))
  101.          Serial.println("Select is being held");
  102.          
  103.          
  104.      if(ps2x.Button(PSB_PAD_UP)) {         //will be TRUE as long as button is pressed
  105.        Serial.print("Up held this hard: ");
  106.        Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
  107.       }
  108.       if(ps2x.Button(PSB_PAD_RIGHT)){
  109.        Serial.print("Right held this hard: ");
  110.         Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
  111.       }
  112.       if(ps2x.Button(PSB_PAD_LEFT)){
  113.        Serial.print("LEFT held this hard: ");
  114.         Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
  115.       }
  116.       if(ps2x.Button(PSB_PAD_DOWN)){
  117.        Serial.print("DOWN held this hard: ");
  118.      Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
  119.       }   
  120.   
  121.    
  122.       vibrate = ps2x.Analog(PSAB_BLUE);        //this will set the large motor vibrate speed based on
  123.                                               //how hard you press the blue (X) button   
  124.    
  125.     if (ps2x.NewButtonState())               //will be TRUE if any button changes state (on to off, or off to on)
  126.     {
  127.      
  128.       
  129.          
  130.         if(ps2x.Button(PSB_L3))
  131.          Serial.println("L3 pressed");
  132.         if(ps2x.Button(PSB_R3))
  133.          Serial.println("R3 pressed");
  134.         if(ps2x.Button(PSB_L2))
  135.          Serial.println("L2 pressed");
  136.         if(ps2x.Button(PSB_R2))
  137.          Serial.println("R2 pressed");
  138.         if(ps2x.Button(PSB_GREEN))
  139.          Serial.println("Triangle pressed");
  140.          
  141.     }   
  142.          
  143.    
  144.     if(ps2x.ButtonPressed(PSB_RED))             //will be TRUE if button was JUST pressed
  145.          Serial.println("Circle just pressed");
  146.          
  147.     if(ps2x.ButtonReleased(PSB_PINK))             //will be TRUE if button was JUST released
  148.          Serial.println("Square just released");     
  149.    
  150.     if(ps2x.NewButtonState(PSB_BLUE))            //will be TRUE if button was JUST pressed OR released
  151.          Serial.println("X just changed");   
  152.    
  153.    
  154.     if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) // print stick values if either is TRUE
  155.     {
  156.         Serial.print("Stick Values:");
  157.         Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX  
  158.         Serial.print(",");
  159.         Serial.print(ps2x.Analog(PSS_LX), DEC);
  160.         Serial.print(",");
  161.         Serial.print(ps2x.Analog(PSS_RY), DEC);
  162.         Serial.print(",");
  163.         Serial.println(ps2x.Analog(PSS_RX), DEC);
  164.     }
  165.    
  166.    
  167. }


  168. delay(50);
  169.      
  170. }
复制代码



打算把L2 R2 按下后变成30 60 Select 和Start 变成0 和15   虽然不能用摇杆或者按住控制角度  但是可以依靠预设定的角度进行调整也好QAQ....然后直接把舵机控制加进ps2手柄中去发现不能用...555555

大大要是有好的ps2手柄控制 舵机 连接arduino unor3的程序就更好了QAQ
回复 支持 反对

使用道具 举报

发表于 2015-10-29 09:15:38 | 显示全部楼层
可以去  http://www.geek-workshop.com/for ... hlight=%CA%D6%B1%FA  这里学习下弘大的 或许能帮到
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-10-29 12:28:16 | 显示全部楼层
gthrtg 发表于 2015-10-29 09:15
可以去  http://www.geek-workshop.com/forum.php?mod=viewthread&tid=172&highlight=%CA%D6%B1%FA  这里学 ...

看的就是弘大大的俩学习笔记 ....单独使用都可以 但是不会把俩程序连在一起使用QWQ  ....
回复 支持 反对

使用道具 举报

发表于 2016-1-12 10:18:35 | 显示全部楼层
Errock 发表于 2015-10-28 22:09
大大泥豪~就是 想把两个程序合起来...int i,val;
char a[3];
boolean display;

楼主,你的这个ps2的程序在1.6.5上面不抱错吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 10:10 , Processed in 0.037968 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表