黄工无刷电机学习
直播中

张强

7年用户 1333经验值
私信 关注
[问答]

如何通过Arduino mega 2560读取PS2派的左信号并驱动运动?

如何通过Arduino mega 2560读取PS2派的左信号并驱动运动?

回帖(1)

刘平

2021-11-1 10:06:45
1

前言最近开始学习和树莓派小车的编程相关内容,本例较简单,可以通过Arduino mega 2560 读取PS2 手柄的左摇信号并驱动运动。
2 硬件连接

硬件连接是编程的基础,也正是在本例的探索过程中,我在网上发现了很多例子借用了自己的图片、程序,需要的图片和程序存在无法对应的问题,浪费了我很多时间。但绝对,确保了图片和程序的对应性问题
。11 硬件构成了简介

本例使用的小车为双例电机(供电12V旅游比电机30)后驱力,前轮转向为DS3119舵机驱动;使用购买L298N电机驱动板直接驱动两个后轮驱动电机运动;本示例使用的驱动为Arduino mega 2560,前轮转向舵机采用L298N驱动板的5V输出供电,信号线直接与Arduino相连;无线手柄为购买的仿制PS2手柄,但好在一个组成基本相同且有标;小车采用318650个电池组的电池组供应,游电压11.V。本示例涉及硬件如下表:
[tr]名称主要参数备注[/tr]
后轮驱动电机太阳能电机,12V 旅游电压,30 停车比带编码,但本例未编程器
前轮转向舵机DS3119数字舵机与主流995等舵机控制相同
驱动板L298N四路驱动板四路刚好驱动两个电机的正驱动
Arduino 巨型 2560手头刚好有,其实用UNO也可以
无线手柄仿制PS2无线手柄手柄无线接收器有明确的信号口标识
主电源3x18650电池组,北极电压11.1V带双路圆口输出,一公一母,公头可直接插入arduino,母头需自己再配一个公头转接L298N电机驱动驱动
副电源5V(本例使用电压源)本驱动舵机使用arduino或驱动板的5V电源,但会严重影响Arduino与无线手柄之间的通讯,遂单独使用5V电源驱动舵机

排线等待排线以一公一头母的左右,大约10根左右
本例涉及的所有硬件(5V电压源除外)如下图所示:





2.2硬件连线

所用端口、硬件连线如下图所示:





3 程序编制

本例所用全部程序如下:

#include   //for v1.6


#include     //这是控制舵机必须包含的头文件


PS2X ps2x; // create PS2 Controller Class
Servo myservo;        //创建一个舵机控制对象
//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you conect the controller,
//or call config_gamepad(pins) again after connecting the controller.
int Left_motor_back=6;       //左电机后退(IN1)
int Left_motor_go=7;         //左电机前进(IN2)

int Right_motor_go=5;        // 右电机前进(IN3)
int Right_motor_back=4;      // 右电机后退(IN4)

int Velocity=0;              //前进或后退的速度大小
int Direction=90;            //转向舵机的角度,舵机初始都是在90度,对应前轮在中位,本例小车底盘大概左转极限对应舵机70度,右转极限对应舵机110度


int error = 0;               //这些是人家原来的手柄信号读取例程中的内容
byte type = 0;
byte vibrate = 0;

void setup(){
Serial.begin(57600);
//初始化电机驱动IO为输出方式
  pinMode(Left_motor_go,OUTPUT); //将控制电机的四路信号口设置为输出模式
  pinMode(Left_motor_back,OUTPUT);  
  pinMode(Right_motor_go,OUTPUT);
  pinMode(Right_motor_back,OUTPUT);
  myservo.attach(8);             //将舵机信号口设置为相应的舵机模式
//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
  
error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error

if(error == 0){
   Serial.println("Found Controller, configured successful");
   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("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. ");
   
   //Serial.print(ps2x.Analog(1), HEX);
   
   type = ps2x.readType();
     switch(type) {
       case 0:
        Serial.println("Unknown Controller type");
       break;
       case 1:
        Serial.println("DualShock Controller Found");
       break;
       case 2:
        Serial.println("GuitarHero Controller Found");
       break;
     }
  
}

void loop(){
   /* You must Read Gamepad to get new values
   Read GamePad 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
   */
   
if(error == 1) //skip loop if no controller found
  return;
  
ps2x.read_gamepad(false, vibrate);          //read controller and set large motor to spin at 'vibrate' speed
if(ps2x.Analog(PSS_LY) <128){               //通过测试可知,左摇杆Y轴中位时是128,前推到顶为0,故此范围内执行if语句,为前进
  Velocity = map(ps2x.Analog(PSS_LY), 0, 128, 255, 0);//通过map函数将128-0的摇杆值对应为255-0的速度值
  analogWrite(Left_motor_go, Velocity);               //前进状态,两个电机的go通道为速度值,back通道为0
  analogWrite(Right_motor_go, Velocity);
  analogWrite(Left_motor_back, 0);
  analogWrite(Right_motor_back, 0);
}
else {                                      //相应的,左摇杆Y轴在128-255的时候,意味着后退
  Velocity = map(ps2x.Analog(PSS_LY), 128, 255, 0, 255);//通过map函数将128-255的摇杆值对应位0-255的速度值
  analogWrite(Left_motor_back, Velocity);               //后退状态,两个电机的back通道为速度值,go通道为0
  analogWrite(Right_motor_back, Velocity);
  analogWrite(Left_motor_go, 0);
  analogWrite(Right_motor_go, 0);
}


Direction = map(ps2x.Analog(PSS_LX), 0, 255, 70, 110); //转向的控制较为简单,只需通过map函数将0-255的左摇杆X轴值对应为70-110的舵机角度
myservo.write(Direction);                              //将计算所得的舵机角度写入舵机
   
delay(50);                                             //这个50ms的延时非常重要,去掉会影响arduino与手柄接收器的通讯
     
}
这个50ms的延时非常重要,去掉会影响arduino与手柄接收器的通讯     } 程序的说明均已在注释中表述,这里不再赘述。


  • 莫名不同之间的共地一定要做好,一个司机和司机驱动板之间等,否则会出现名莫名其妙的错误。
  • 舵机供电最好不要用的5V和驱动的5V,因为板是通过一个超级高的芯片得来的,带上负载能力不是最高的,所以很可能会带不起无人机。的这套硬件就是这样。。。
  • 排线接插的方式虽然方便,但容易产生虚接,有劣质的排线甚至折出很真实的内部就断了。出问题的时候,要检查,有一个万用表最好,用蜂鸣通断功能逐个追。
  • 糖果安装上有很假,中位可能导致前轮的中位并不对应无人机的90度初始位置,这个时候要学会根据实际效果微调。
  • 的时候不用让小测试车跑,可以像我把车架起来,方便。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分