通过PS2的摇杆控制履带车变速前进、后退及差速-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2166|回复: 0

通过PS2的摇杆控制履带车变速前进、后退及差速

[复制链接]
发表于 2022-1-21 10:45 | 显示全部楼层 |阅读模式
本帖最后由 林枫 于 2022-1-21 11:08 编辑

最近疫情比较紧张,过年可能无法回老家了,想着家里还有一些“电子垃圾”吃灰好久了,过年期间比较无聊,特搞了个PS2手柄玩起来。

主要功能如下:
1、将摇杆的模拟值移植到pwm上用于履带车两个电机的调速和差速;
2、并且通过推动油门摇杆使手柄左侧电机有对应的震动反馈(可移植到避障函数上通过距离改变振幅),通过按键启动右侧电机反馈。

如图为小车全家桶照片,图中小车仅通过Arduino接收摇杆数据驱动。


其他角度图片见帖子最后。
ps2-car.jpeg



还有很多玩法,如:
1、通过手柄右侧摇杆控制车头舵机运动
2、将树莓派与Arduino相连后可以在树莓派中运行扫地机器人地图算法用于扫描各房间
3、将光线传感器、人体传感器接入树莓派用于开、关灯
4、还有oled屏幕、bmp280等诸多硬件没有集成上来有待开发


废话不多说,PS2控制部分源码如下:

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

  2. /******************************************************************
  3. * set pins connected to PS2 controller:
  4. *   - 1e column: original
  5. *   - 2e colmun: Stef?
  6. * replace pin numbers by the ones you use
  7. ******************************************************************/
  8. #define PS2_DAT        A1  //   
  9. #define PS2_CMD        A2  //
  10. #define PS2_SEL        A3  //CS
  11. #define PS2_CLK        A4  //


  12. /*******************************************************************
  13. * 定义小车调速引脚及电机驱动引脚
  14. * 10对应8、9引脚调速信号
  15. * 11对应12、13引脚调速信号
  16. ******************************************************************/
  17. const int PMW1 = 10;
  18. const int PMW2 = 11;

  19. const int MA1 = 8;
  20. const int MA2 = 9;
  21. const int MB1 = 12;
  22. const int MB2 = 13;


  23. /******************************************************************
  24. * select modes of PS2 controller:
  25. *   - pressures = analog reading of push-butttons
  26. *   - rumble    = motor rumbling
  27. * uncomment 1 of the lines for each mode selection
  28. ******************************************************************/
  29. #define pressures   true
  30. //#define pressures   false
  31. #define rumble      true
  32. //#define rumble      false

  33. PS2X ps2x; // create PS2 Controller Class

  34. //right now, the library does NOT support hot pluggable controllers, meaning
  35. //you must always either restart your Arduino after you connect the controller,
  36. //or call config_gamepad(pins) again after connecting the controller.

  37. int error = 0;
  38. byte type = 0;
  39. byte vibrate = 0;
  40. bool smart = false;
  41. int pwm1_value = 0;
  42. int pwm2_value = 0;


  43. // Reset func
  44. void (* resetFunc) (void) = 0;

  45. void setup(){

  46.   Serial.begin(115200);
  47.   
  48.   delay(500);  //added delay to give wireless ps2 module some time to startup, before configuring it
  49.   pinMode(PMW1,OUTPUT);
  50.   pinMode(PMW2,OUTPUT);

  51.   pinMode(MA1, OUTPUT);
  52.   pinMode(MA2, OUTPUT);
  53.   pinMode(MB1, OUTPUT);
  54.   pinMode(MB2, OUTPUT);
  55.   
  56.   //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
  57.   
  58.   //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  59.   error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
  60.   
  61.   if(error == 0){
  62.     Serial.println("Found Controller, configured successful ");
  63.     Serial.print("pressures = ");
  64.   if (pressures)
  65.     Serial.println("true ");
  66.   else
  67.     Serial.println("false");
  68.   Serial.print("rumble = ");
  69.   if (rumble)
  70.     Serial.println("true");
  71.   else
  72.     Serial.println("false");
  73.     Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
  74.     Serial.println("holding L1 or R1 will print out the analog stick values.");
  75.     Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
  76.   }  
  77.   else if(error == 1)
  78.     Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
  79.    
  80.   else if(error == 2)
  81.     Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");

  82.   else if(error == 3)
  83.     Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  84.   
  85.   type = ps2x.readType();
  86.   switch(type) {
  87.     case 0:
  88.       Serial.println("Unknown Controller type found ");
  89.       break;
  90.     case 1:
  91.       Serial.println("DualShock Controller found ");
  92.       break;
  93.     case 2:
  94.       Serial.println("GuitarHero Controller found ");
  95.       break;
  96.   case 3:
  97.       Serial.println("Wireless Sony DualShock Controller found ");
  98.       break;
  99.    }
  100. }


  101. void runup(){
  102.   digitalWrite(MA1, LOW);
  103.   digitalWrite(MA2, HIGH);
  104.   digitalWrite(MB1, LOW);
  105.   digitalWrite(MB2, HIGH);
  106.   //Serial.println("行进方向:前进");
  107. }

  108. void backdown(){
  109.   digitalWrite(MA1, HIGH);
  110.   digitalWrite(MA2, LOW);
  111.   digitalWrite(MB1, HIGH);
  112.   digitalWrite(MB2, LOW);
  113.   //Serial.println("行进方向:后退");
  114. }

  115. void pwm(int pmw1,int pmw2){
  116.   pwm1_value = pmw1;
  117.   pwm2_value = pmw2;

  118.   analogWrite(PMW1,pwm1_value);//将摇杆值输出到pwm口用于调速,范围0-255
  119.   analogWrite(PMW2,pwm2_value);
  120. }

  121. void loop() {
  122.   /* You must Read Gamepad to get new values and set vibration values
  123.      ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
  124.      if you don't enable the rumble, use ps2x.read_gamepad(); with no values
  125.      You should call this at least once a second
  126.      你必须阅读手柄来获得新的值和设置振动值
  127.      ps2x。Read_gamepad(小电机开/关,大电机强度0-255)
  128.      如果你不启用rumble,使用ps2x.read_gamepad();没有值
  129.      你应该至少每秒钟调用一次
  130.    */  
  131.   //Serial.println("测试手柄是否初始化!!!");
  132.   if(error == 1){ //skip loop if no controller found
  133.     resetFunc();
  134.   }
  135.   
  136.   else { //DualShock Controller
  137.     ps2x.read_gamepad(smart, vibrate); //read controller and set large motor to spin at 'vibrate' speed
  138.    
  139.     if(ps2x.Button(PSB_START))         //will be TRUE as long as button is pressed
  140.       Serial.println("Start is being held");
  141.     if(ps2x.Button(PSB_SELECT))
  142.       Serial.println("Select is being held");      

  143.     if(ps2x.Button(PSB_PAD_UP)) {      //will be TRUE as long as button is pressed
  144.       Serial.print("Up held this hard: ");
  145.       Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
  146.       delay(50);
  147.       smart = true;                                       //左侧上按键控制右电机振动开启
  148.     }
  149.     if(ps2x.Button(PSB_PAD_RIGHT)){
  150.       Serial.print("Right held this hard: ");
  151.       Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
  152.     }
  153.     if(ps2x.Button(PSB_PAD_LEFT)){
  154.       Serial.print("LEFT held this hard: ");
  155.       Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
  156.       
  157.     }
  158.     if(ps2x.Button(PSB_PAD_DOWN)){
  159.       Serial.print("DOWN held this hard: ");
  160.       Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
  161.       delay(50);
  162.       smart = false;                                      //左侧下按键控制右电机振动关闭
  163.     }   

  164.     //vibrate = ps2x.Analog(PSAB_CROSS);  //this will set the large motor vibrate speed based on how hard you press the blue (X) button,这将根据你按下蓝色(X)按钮的力度来设置大电机振动速度
  165.     //Serial.print(ps2x.Analog(PSAB_CROSS), DEC);
  166.     if (ps2x.NewButtonState()) {        //will be TRUE if any button changes state (on to off, or off to on)
  167.       if(ps2x.Button(PSB_L3))
  168.         Serial.println("L3 pressed");
  169.       if(ps2x.Button(PSB_R3))
  170.         Serial.println("R3 pressed");
  171.       if(ps2x.Button(PSB_L2))
  172.         Serial.println("L2 pressed");
  173.       if(ps2x.Button(PSB_R2))
  174.         Serial.println("R2 pressed");
  175.       if(ps2x.Button(PSB_TRIANGLE))
  176.         Serial.println("Triangle pressed");        
  177.     }

  178.     if(ps2x.ButtonPressed(PSB_CIRCLE))               //will be TRUE if button was JUST pressed
  179.       Serial.println("Circle just pressed");
  180.     if(ps2x.NewButtonState(PSB_CROSS))               //will be TRUE if button was JUST pressed OR released
  181.       Serial.println("X just changed");
  182.     if(ps2x.ButtonReleased(PSB_SQUARE))              //will be TRUE if button was JUST released
  183.       Serial.println("Square just released");

  184.     if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUE
  185.       Serial.print("Stick Values:");
  186.       Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX  
  187.       Serial.print(",");
  188.       Serial.print(ps2x.Analog(PSS_LX), DEC);
  189.       Serial.print(",");
  190.       Serial.print(ps2x.Analog(PSS_RY), DEC);
  191.       Serial.print(",");
  192.       Serial.println(ps2x.Analog(PSS_RX), DEC);

  193.       /**********************************************************************
  194.        * 将摇杆中心点值转换为以0起始值,并放大对应到255
  195.        * 通过判断摇杆中心点位置来决定小车前进/后退
  196.        *********************************************************************/

  197.       int Y1 = ps2x.Analog(PSS_LY);//将摇杆模拟值赋值给变量Y1,用于控制前进速度
  198.       int Y2 = abs(Y1-127)*2;//将摇杆模拟值经计算后转化为中心点为0,上下摇动值最大为255
  199.       if (Y2 <= 255)
  200.       {
  201.         Y2 = Y2;
  202.       }
  203.       else
  204.       {
  205.         Y2 = Y2-1;
  206.       }

  207.       int X1 = ps2x.Analog(PSS_LX);//将摇杆模拟值赋值给变量X1,用于控制差速
  208.       int X2 = abs(X1-128)*2;//将摇杆模拟值经计算后转化为中心点为0,左右摇动值最大为255
  209.       if (X2 <= 255)
  210.       {
  211.         X2 = X2;
  212.       }
  213.       else
  214.       {
  215.         X2 = X2-1;
  216.       }

  217.       if(Y1<=127){                           //判断Y摇杆向上越过中心点为前进
  218.         runup();
  219.         if(X1<128){                          //判断摇杆X方向位置
  220.           //Serial.println("右电机加速");
  221.           pwm(X2,Y2);
  222.         }
  223.         else if(X1>128){
  224.           //Serial.println("左电机加速");
  225.           pwm(Y2,X2);
  226.         }
  227.         else{
  228.           //Serial.println("两电机等速直行");
  229.           pwm(Y2,Y2);
  230.         }   
  231.       }
  232.       else if(Y1>127){                      //判断Y摇杆向下越过中心点为后退
  233.         backdown();
  234.         if(X1<128){                         //判断摇杆X方向位置
  235.           //Serial.println("右电机加速");
  236.           pwm(X2,Y2);
  237.         }
  238.         else if(X1>128){
  239.           //Serial.println("左电机加速");
  240.           pwm(Y2,X2);
  241.         }
  242.         else{
  243.           //Serial.println("两电机等速直行");
  244.           pwm(Y2,Y2);
  245.         }  
  246.       }
  247.    
  248.       //vibrate = Y2;//摇杆的模拟值反馈到电机振动上,范围0-255,开启后手柄振动随车速而增强

  249. /*
  250.       Serial.print("摇杆Y向居中值:");
  251.       Serial.println(Y2);
  252.       Serial.print("摇杆X向原始值:");
  253.       Serial.println(X1);
  254.       Serial.print("摇杆X向居中值:");
  255.       Serial.println(X2);
  256. */      
  257.     }     
  258.   }
  259.   delay(10);  
  260. }
复制代码


WechatIMG3107.jpeg WechatIMG3105.jpeg WechatIMG3106.jpeg WechatIMG3104.jpeg
未完待续!





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

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 06:34 , Processed in 0.076651 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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