风筝
发表于: 2019-8-19 22:04:50 | 显示全部楼层

控制器在我们的生活中有着悠久的历史;从控制遥控车和四轮车到游戏控制器......,他们都使用无线或有线控制器。在本篇文章中,您将学习如何使用Arduino连接和使用PS2游戏机控制器。


PS2控制器的功能

每个人都知道索尼游戏机称为PlayStation。索尼游戏控制器有12个对压力敏感的模拟键(4个方向键、4个操作键、十字、三角、圆形和方形,L1、L2、R1和R2)和5个数字键(MODE,START,SELECT, R3,L3)和2个模拟操纵杆。


控制器内部有2个电机,由于它们的不平衡才会产生振动。

PS2_1.jpg


无线控制器工作频率为2.4GHz,射程为10米。它还有一个用于发送和接收数据的光学指示器。该控制器仅需3节AAA电池供电(在某些情况下,它只需要2节AAA电池)。


你可以使用PS2控制器:

●    控制轮式机器人和遥控车

●    控制机器人手臂

●    控制相机

●    控制飞行机器人

●    ..


PS2控制器引脚分布说明

PS2控制器接收器包括9个引脚:

ps2_controller-2.jpg


1. Data:主机线,用于向从站发送数据(MOSI)

2. Command:从机线,用于向主站发送数据(MISO)

3. Vibration:振动电机的电源; 7.2V至9V

4. Ground:电路接地

5. VCC:电源供电; 3.3伏

6. Attention:CS或芯片选择引脚用于调用从机并准备连接

7. Clock:相当于时钟的SCK引脚

8. No Connection:没用

9. Acknowledge:从控制器到PS2接收器的应答信号


连接PS2控制器和Arduino

为了使用PS2控制器,您需要将控制器的按键引入Arduino。然后根据您的项目为每个键选择适当的功能。


电路连接

Untitled-2.jpg


代码

在代码中,您需要PS2X库。将库添加到Arduino后,您可以打开PS2X库示例或复制以下代码并将其上载到您的主板上。您可以通过按不同的键在串行监视器窗口中查看结果。

  1. #include #include <PS2X_lib.h>   

  2. PS2X ps2x;

  3. //right now, the library does NOT support hot-pluggable controllers, meaning
  4. //you must always either restart your Arduino after you connect 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.   
  12. error = ps2x.config_gamepad(13,11,10,12, true, true);   //GamePad(clock, command, attention, data, Pressures?, Rumble?)

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

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

  70.     if(ps2x.ButtonPressed(STAR_POWER))
  71.      Serial.println("Star Power Command");
  72.    
  73.     if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
  74.      Serial.println("Up Strum");
  75.     if(ps2x.Button(DOWN_STRUM))
  76.      Serial.println("DOWN Strum");
  77.   

  78.     if(ps2x.Button(PSB_START))                   //will be TRUE as long as button is pressed
  79.          Serial.println("Start is being held");
  80.     if(ps2x.Button(PSB_SELECT))
  81.          Serial.println("Select is being held");

  82.    
  83.     if(ps2x.Button(ORANGE_FRET)) // print stick value IF TRUE
  84.     {
  85.         Serial.print("Wammy Bar Position:");
  86.         Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
  87.     }
  88. }

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

  157. }

  158. delay(50);
  159.      
  160. }
复制代码

以下是该代码库中最实用的函数:

ps2x.config_gamepad(clock, command, attention, data, Pressures? Rumble?);函数设置控制器引脚和对电机压力和振动的敏感性。 如果您希望键对压力不敏感,或者电机没有振动,请将“Pressures”和“Rumble”设置为“false”。 此函数返回错误值。


ready();函数确定检测到的控制器类型。 0表示未正确检测到控制器,1表示检测到DualShock控制器,2表示检测到GuitarHero控制器。


read_gamepad(boolean motor1, byte motor2);当确定电动机的振动状态时,该函数开始读取键的状态。


Button (but type);当按下函数参数中的特定键时,该函数返回1。

ps2_controller.jpg

Analog (but type);函数返回模拟键值,然后您可以相应地决定您的操作。


以上就是本篇文章的全部内容。接下来,您可以尝试通过按下方向键来控制遥控车,使其向不同的方向移动。 汽车的速度随着按键上的压力而变化。 按下方形键,汽车会发出蜂鸣声,按下十字键打开灯光。如果有任何问题,请随时在本帖下面进行回复。

跳转到指定楼层
回复

使用道具 举报

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

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-19 21:44 , Processed in 0.151481 second(s), 8 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

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