找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 14689|回复: 11
收起左侧

arduino ps2遥控小车基于无线模块

  [复制链接]
ID:305173 发表于 2018-4-27 12:15 | 显示全部楼层 |阅读模式
基于无线模块的遥控控制小车
0.jpg 0.jpg

  
手柄按键
  
发送字符
接收十六进制代码
小车动作
  
按键上或左摇杆上
  
a
0x61
前进
  
按键下或左摇杆下
  
b
0x62
后退
  
按键左或左摇杆左
  
c
0x63
左转
  
按键右或左摇杆右
  
d
0x64
右转
  
  



  
红圆圈
  
e
0x65
左原地转圈
  
粉方块
  
f
0x66
右原地转圈
  
绿三角
  
g
0x67
划弧度
  
  



  
1或右1
  
h
0x68
左平移
  
2或右2
  
i
0x69
右平移
  
  



  
右摇杆上
  
g
0x6a
45度左前
  
右摇杆下
  
k
0x6b
45度右后
  
右摇杆左
  
l
0x6c
45度右前
  
右摇杆右
  
m
0x6d
45度左后
  
  



  
标志位
  
n
0x6e
停止

arduino源程序如下:
  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. //不支持热插拔,连线后需要重新启动Arduino板

  7. int PS_LX;//PS2手柄左摇杆X轴数据
  8. int PS_LY;//PS2手柄左摇杆Y轴数据
  9. int PS_RX;//PS2手柄右摇杆X轴数据
  10. int PS_RY;//PS2手柄右摇杆Y轴数据

  11. int error = 0;         //连接正确与否的判断标志
  12. byte type = 0;         //ps2x.readType()转换判别标志     
  13. byte vibrate = 0;
  14. int banduan=0;

  15. void setup(){
  16. Serial.begin(57600);

  17. //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************

  18. error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  19.                                                          //检查引脚是否有连接错误
  20.                                                          
  21. if(error == 0){
  22.    Serial.println("Found Controller, configured successful");
  23.    Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
  24.   Serial.println("holding L1 or R1 will print out the analog stick values.");
  25.   Serial.println("Go to [url]www*billporter*info[/url] for updates and to report bugs.");
  26. }  //0号错误的串口提示信息                        

  27.   else if(error == 1)
  28.    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit [url]www*billporter*info[/url] for troubleshooting tips");
  29.                   //1号错误的串口提示信息
  30.   else if(error == 2)
  31.    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit [url]www*billporter*info[/url] for troubleshooting tips");
  32.                  //2号错误的串口提示信息
  33.   else if(error == 3)
  34.    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  35.                  //3号错误的串口提示信息

  36.    //Serial.print(ps2x.Analog(1), HEX);

  37.    type = ps2x.readType();    //正确连接后串口提示的信息  
  38.      switch(type) {
  39.        case 0:
  40.         Serial.println("Unknown Controller type");
  41.        break;
  42.        case 1:
  43.         Serial.println("DualShock Controller Found");
  44.        break;
  45.        case 2:
  46.          Serial.println("GuitarHero Controller Found");
  47.        break;
  48.      }

  49. }

  50. void loop(){
  51.    /* You must Read Gamepad to get new values
  52.    Read GamePad and set vibration values
  53.    ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
  54.    if you don't enable the rumble, use ps2x.read_gamepad(); with no values
  55.    
  56.    you should call this at least once a second
  57.    */
  58. //用Read Gamepad函数读取按键值


  59. if(error == 1) //skip loop if no controller found
  60.   return;
  61.   
  62.   
  63.   
  64.   /******************************************************
  65.    Guitar Hero Controller不是手柄选用的控制器
  66.   //Guitar Hero Controller
  67. if(type == 2)            
  68. {
  69.   ps2x.read_gamepad();          //read controller

  70.    if(ps2x.ButtonPressed(GREEN_FRET))
  71.      Serial.println("Green Fret Pressed");
  72.    if(ps2x.ButtonPressed(RED_FRET))
  73.      Serial.println("Red Fret Pressed");
  74.    if(ps2x.ButtonPressed(YELLOW_FRET))
  75.      Serial.println("Yellow Fret Pressed");
  76.    if(ps2x.ButtonPressed(BLUE_FRET))
  77.      Serial.println("Blue Fret Pressed");
  78.    if(ps2x.ButtonPressed(ORANGE_FRET))
  79.      Serial.println("Orange Fret Pressed");


  80.     if(ps2x.ButtonPressed(STAR_POWER))
  81.      Serial.println("Star Power Command");

  82.     if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
  83.      Serial.println("Up Strum");
  84.     if(ps2x.Button(DOWN_STRUM))
  85.      Serial.println("DOWN Strum");


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


  90.     if(ps2x.Button(ORANGE_FRET)) // print stick value IF TRUE
  91.     {
  92.         Serial.print("Wammy Bar Position:");
  93.         Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
  94.     }
  95. }
  96. **********************************************************************/
  97. else { //DualShock Controller手柄选用的控制器
  98.     ps2x.read_gamepad(false, vibrate);          //read controller and set large motor to spin at 'vibrate' speed
  99.    
  100.     if(ps2x.Button(PSB_START))                      //start选中 //will be TRUE as long as button is pressed
  101.       Serial.println("Start is being held");   
  102.     if(ps2x.Button(PSB_SELECT))                     //select选中
  103.       Serial.println("Select is being held");

  104.     PS_LX=ps2x.Analog(PSS_LX);           //把PS2手柄左摇杆X轴数据读到变量PS_LX
  105.    PS_RX=ps2x.Analog(PSS_RX);           //把PS2手柄右摇杆X轴数据读到变量PS_RX
  106.    PS_LY=ps2x.Analog(PSS_LY);           //把PS2手柄左摇杆Y轴数据读到变量PS_LY
  107.    PS_RY=ps2x.Analog(PSS_RY);           //把PS2手柄右摇杆Y轴数据读到变量PS_RY
  108.   if(PS_RX<5)                  //从左到右0~255,从上到下0~255      
  109.   { banduan=1;
  110.         Serial.print("l");  
  111.       }
  112.    if(PS_RX>250)
  113.    { banduan=1;
  114.         Serial.print("m");  
  115.       }
  116.     if(PS_RY<5)
  117.   { banduan=1;
  118.         Serial.print("j");  
  119.       }
  120.       if(PS_RY>250)
  121.   { banduan=1;
  122.         Serial.print("k");  
  123.       }




  124.     if(PS_LY<5||ps2x.Button(PSB_PAD_UP)) //上                   //will be TRUE as long as button is pressed
  125.     {  banduan=1;
  126.        Serial.print("a");               
  127.      }
  128.      
  129.      if(PS_LY>250||ps2x.Button(PSB_PAD_DOWN))//下
  130.       { banduan=1;
  131.        Serial.print("b");     
  132.       }
  133.      
  134.          if(PS_LX<5||ps2x.Button(PSB_PAD_LEFT))//左
  135.       { banduan=1;
  136.       Serial.print("c");   
  137.       }
  138.       
  139.      if(PS_LX>250||ps2x.Button(PSB_PAD_RIGHT))//右
  140.      { banduan=1;
  141.         Serial.print("d");  
  142.       }
  143.       
  144.      
  145.      if(ps2x.Button(PSB_RED))               //红圆圈
  146.          { banduan=1;
  147.         Serial.print("e");  
  148.       }

  149.     if(ps2x.Button(PSB_PINK))             //粉方形
  150.        { banduan=1;
  151.         Serial.print("f");  
  152.       }
  153.   
  154.       
  155.       
  156.       
  157.       vibrate = ps2x.Analog(PSAB_BLUE);        //this will set the large motor vibrate speed based on how hard you press the blue (X) button   
  158.      if(ps2x.NewButtonState(PSB_BLUE))        //蓝叉叉
  159.         Serial.print("XX");                                         

  160.     if (ps2x.Button(PSB_GREEN))              //绿三角   //will be TRUE if any button changes state (on to off, or off to on)
  161.      { banduan=1;
  162.         Serial.print("g");  
  163.       }


  164.   


  165.     if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) //左1右1// print stick values if either is TRUE
  166.      { banduan=1;
  167.         Serial.print("h");  
  168.       }
  169.    if(ps2x.Button(PSB_L2) || ps2x.Button(PSB_R2)) //左2右2// print stick values if either is TRUE
  170.      { banduan=1;
  171.         Serial.print("i");  
  172.       }


  173.   if( banduan==0)
  174.       Serial.print("n");
  175.      else
  176.          banduan=0;
  177.      
  178. }
  179. delay(50);

  180. }
复制代码


所有资料51hei提供下载:
遥控小车.rar (12.07 MB, 下载次数: 155)
0.jpg
回复

使用道具 举报

ID:18156 发表于 2018-9-9 16:29 | 显示全部楼层
看轮子应该是可以横向运动的。
回复

使用道具 举报

ID:32574 发表于 2018-10-17 14:13 | 显示全部楼层
楼主厉害,轮子是叫什么名字?
回复

使用道具 举报

ID:79544 发表于 2018-10-19 08:25 | 显示全部楼层
楼主  接收是用的什么模块?
回复

使用道具 举报

ID:390585 发表于 2018-11-8 19:06 | 显示全部楼层
redtxd 发表于 2018-10-17 14:13
楼主厉害,轮子是叫什么名字?

麦克纳姆轮
回复

使用道具 举报

ID:460190 发表于 2019-2-15 21:00 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:448145 发表于 2019-11-7 12:22 | 显示全部楼层

感谢分享
回复

使用道具 举报

ID:676603 发表于 2019-12-29 15:03 | 显示全部楼层
很棒,一会研究源码
回复

使用道具 举报

ID:646222 发表于 2020-2-5 19:14 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:652804 发表于 2020-2-25 21:37 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:643484 发表于 2020-3-3 15:07 | 显示全部楼层
感谢分享
回复

使用道具 举报

ID:171746 发表于 2021-1-26 16:57 | 显示全部楼层
有ARDUINO 小车程序吗?     哪个是51单片机的
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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