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

林枫 发表于 2022-1-21 10:45

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

本帖最后由 林枫 于 2022-1-21 11:08 编辑

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

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

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


其他角度图片见帖子最后。




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


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

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

/******************************************************************
* set pins connected to PS2 controller:
*   - 1e column: original
*   - 2e colmun: Stef?
* replace pin numbers by the ones you use
******************************************************************/
#define PS2_DAT      A1//   
#define PS2_CMD      A2//
#define PS2_SEL      A3//CS
#define PS2_CLK      A4//


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

const int MA1 = 8;
const int MA2 = 9;
const int MB1 = 12;
const int MB2 = 13;


/******************************************************************
* select modes of PS2 controller:
*   - pressures = analog reading of push-butttons
*   - rumble    = motor rumbling
* uncomment 1 of the lines for each mode selection
******************************************************************/
#define pressures   true
//#define pressures   false
#define rumble      true
//#define rumble      false

PS2X ps2x; // create PS2 Controller Class

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

int error = 0;
byte type = 0;
byte vibrate = 0;
bool smart = false;
int pwm1_value = 0;
int pwm2_value = 0;


// Reset func
void (* resetFunc) (void) = 0;

void setup(){

Serial.begin(115200);

delay(500);//added delay to give wireless ps2 module some time to startup, before configuring it
pinMode(PMW1,OUTPUT);
pinMode(PMW2,OUTPUT);

pinMode(MA1, OUTPUT);
pinMode(MA2, OUTPUT);
pinMode(MB1, OUTPUT);
pinMode(MB2, OUTPUT);

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

//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);

if(error == 0){
    Serial.println("Found Controller, configured successful ");
    Serial.print("pressures = ");
if (pressures)
    Serial.println("true ");
else
    Serial.println("false");
Serial.print("rumble = ");
if (rumble)
    Serial.println("true");
else
    Serial.println("false");
    Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
    Serial.println("holding L1 or R1 will print out the analog stick values.");
    Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
}
else if(error == 1)
    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
   
else if(error == 2)
    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");

else if(error == 3)
    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");

type = ps2x.readType();
switch(type) {
    case 0:
      Serial.println("Unknown Controller type found ");
      break;
    case 1:
      Serial.println("DualShock Controller found ");
      break;
    case 2:
      Serial.println("GuitarHero Controller found ");
      break;
case 3:
      Serial.println("Wireless Sony DualShock Controller found ");
      break;
   }
}


void runup(){
digitalWrite(MA1, LOW);
digitalWrite(MA2, HIGH);
digitalWrite(MB1, LOW);
digitalWrite(MB2, HIGH);
//Serial.println("行进方向:前进");
}

void backdown(){
digitalWrite(MA1, HIGH);
digitalWrite(MA2, LOW);
digitalWrite(MB1, HIGH);
digitalWrite(MB2, LOW);
//Serial.println("行进方向:后退");
}

void pwm(int pmw1,int pmw2){
pwm1_value = pmw1;
pwm2_value = pmw2;

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

void loop() {
/* You must Read Gamepad to get new values and set vibration values
   ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
   if you don't enable the rumble, use ps2x.read_gamepad(); with no values
   You should call this at least once a second
   你必须阅读手柄来获得新的值和设置振动值
   ps2x。Read_gamepad(小电机开/关,大电机强度0-255)
   如果你不启用rumble,使用ps2x.read_gamepad();没有值
   你应该至少每秒钟调用一次
   */
//Serial.println("测试手柄是否初始化!!!");
if(error == 1){ //skip loop if no controller found
    resetFunc();
}

else { //DualShock Controller
    ps2x.read_gamepad(smart, vibrate); //read controller and set large motor to spin at 'vibrate' speed
   
    if(ps2x.Button(PSB_START))         //will be TRUE as long as button is pressed
      Serial.println("Start is being held");
    if(ps2x.Button(PSB_SELECT))
      Serial.println("Select is being held");      

    if(ps2x.Button(PSB_PAD_UP)) {      //will be TRUE as long as button is pressed
      Serial.print("Up held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
      delay(50);
      smart = true;                                       //左侧上按键控制右电机振动开启
    }
    if(ps2x.Button(PSB_PAD_RIGHT)){
      Serial.print("Right held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
    }
    if(ps2x.Button(PSB_PAD_LEFT)){
      Serial.print("LEFT held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
      
    }
    if(ps2x.Button(PSB_PAD_DOWN)){
      Serial.print("DOWN held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
      delay(50);
      smart = false;                                    //左侧下按键控制右电机振动关闭
    }   

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

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

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

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

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

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

      if(Y1<=127){                           //判断Y摇杆向上越过中心点为前进
      runup();
      if(X1<128){                        //判断摇杆X方向位置
          //Serial.println("右电机加速");
          pwm(X2,Y2);
      }
      else if(X1>128){
          //Serial.println("左电机加速");
          pwm(Y2,X2);
      }
      else{
          //Serial.println("两电机等速直行");
          pwm(Y2,Y2);
      }   
      }
      else if(Y1>127){                      //判断Y摇杆向下越过中心点为后退
      backdown();
      if(X1<128){                         //判断摇杆X方向位置
          //Serial.println("右电机加速");
          pwm(X2,Y2);
      }
      else if(X1>128){
          //Serial.println("左电机加速");
          pwm(Y2,X2);
      }
      else{
          //Serial.println("两电机等速直行");
          pwm(Y2,Y2);
      }
      }
   
      //vibrate = Y2;//摇杆的模拟值反馈到电机振动上,范围0-255,开启后手柄振动随车速而增强

/*
      Serial.print("摇杆Y向居中值:");
      Serial.println(Y2);
      Serial.print("摇杆X向原始值:");
      Serial.println(X1);
      Serial.print("摇杆X向居中值:");
      Serial.println(X2);
*/      
    }   
}
delay(10);
}


未完待续!





页: [1]
查看完整版本: 通过PS2的摇杆控制履带车变速前进、后退及差速