本文主要是介绍arduino(17):特别好玩的玩具,使用ESP32接入PS2无线手柄,然后控制小车,使用遥控杆控制前后左右,解决启动空转,和连接不上手柄一直空转问题,有接线图和源代码,给小盆友玩。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- 前言
- 1,关于arduino 控制PS2手柄
- 2,接线配置
- 3,总结
前言
相关arduino 全部分类:
https://blog.csdn.net/freewebsys/category_8799254.html
本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/104855458
未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys
1,关于arduino 控制PS2手柄
上回把 PS2 手柄研究了一下。这次使用遥感控制车前后运动。
https://blog.csdn.net/freewebsys/article/details/104734241
还是非常不错的,并且经过修改接上了ESP32 的板子上。
这次要用这个手柄控制小车,特意购买了新的18650 电池两节。
然后驱动四轮小车。
使用的 L298N 的芯片控制的两边的电机旋转。
四轮小车和 L298N 可以去搜索去购买,乐趣在于自己去组装。并且编写 Arduino 代码。
能够控制小车非常不错。
PS2摇杆,空是127 或者 128,前进是0,后退是255 。进行控制。
2,接线配置
L298N 这个需要注意下,测试了几个只能接图上面的白色的引脚。
我这里接了 GPIO33,GPIO25,GPIO26,GPIO27 四个,然后电源,接5V和GND。
实际接线效果图:
全部控制代码:
/******************************************************************* set pins connected to PS2 controller:* - 1e column: original * - 2e colmun: Stef?* replace pin numbers by the ones you use******************************************************************/// ESP32 pin
// https://github.com/espressif/arduino-esp32/blob/master/docs/esp32_pinmap.png
// ESP32 : https://github.com/MyArduinoLib/Arduino-PS2X-ESP32 #include <PS2X_lib.h> //for v1.6#define PS2_DAT 19 //MISO 19
#define PS2_CMD 23 //MOSI 23
#define PS2_SEL 5 //SS 5
#define PS2_CLK 18 //SLK 18/******************************************************************* 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 false
#define rumble falsePS2X 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 = -1;
byte type = 0;
byte vibrate = 0;
int tryNum = 1;/******************************************************************* L298N motor ******************************************************************/// ESP 32 pin
const int N1 = 26;//motor 1
const int N2 = 27;const int N3 = 33;//motor 2
const int N4 = 25;void LeftForword()
{digitalWrite(N1,HIGH);digitalWrite(N2,LOW);Serial.println("################LeftForword");
}
void RightForword()
{digitalWrite(N3,HIGH);digitalWrite(N4,LOW);Serial.println("################RightForword");
}
void LeftBackword()
{digitalWrite(N1,LOW);digitalWrite(N2,HIGH);Serial.println("################LeftBackword");
}
void RightBackword()
{digitalWrite(N3,LOW);digitalWrite(N4,HIGH);Serial.println("################RightBackword");
}
void LeftStop()
{digitalWrite(N1,LOW);digitalWrite(N2,LOW);Serial.println("################LeftStop");
}
void RightStop()
{digitalWrite(N3,LOW);digitalWrite(N4,LOW);Serial.println("################RightStop");
}void setup()
{// 115200 Serial.begin(115200);pinMode(N1,OUTPUT);pinMode(N2,OUTPUT);pinMode(N3,OUTPUT);pinMode(N4,OUTPUT);//SET ALL MOTOR STOPLeftStop();RightStop();//added delay to give wireless ps2 module some time to startup, before configuring it//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************while (error != 0) {delay(1000);// 1 second wait//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for errorerror = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);Serial.print("#try config ");Serial.println(tryNum);tryNum ++;}Serial.println(ps2x.Analog(1), HEX);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 loop() {if(type == 1){ //DualShock Controllerps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed byte pss_ly = ps2x.Analog(PSS_LY);if( pss_ly == 0 ){//FORWORDLeftForword();}else if( pss_ly == 127 || pss_ly == 128){//STOPLeftStop();}else if( pss_ly == 255 ){//BACKWORDLeftBackword();}byte pss_ry = ps2x.Analog(PSS_RY);if( pss_ry == 0 ){//FORWORDRightForword();}else if( pss_ry == 127 || pss_ry == 128){//STOPRightStop();}else if( pss_ry == 255 ){//BACKWORDRightBackword();}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); }else{LeftStop();RightStop();}delay(50);
}
最关键的代码:
byte pss_ry = ps2x.Analog(PSS_RY);if( pss_ry == 0 ){//FORWORDRightForword();}else if( pss_ry == 127 || pss_ry == 128){//STOPRightStop();}else if( pss_ry == 255 ){//BACKWORDRightBackword();}
pss_ry 就是遥感,只获得y 坐标的值,往前是前进,往后是后退,左右先不管。
可以用一个控制前后左右,但是有两个,就按照坦克的方式去操作了。可以一前一后原地打转,挺好玩的。
这个地方有个 BUG,在PS2手柄没有连接的时候,获得的数据是128 ,而连接上了之后是127 。
所以判断都判断这两个值,发现匹配的时候就停止电机转动。
小车是之前组装的,单侧的电机需要反向连接。
直接用两个线给反向焊死了。以后也不拆了。
因为ESP32 的电流特别小,所以控制不了电机,就需要这个L298N了。
输入是12V,我这边用两节 18650 是 7.4 V 左右,也可以驱动起来,要是太低了估计就够呛了。
拆掉USB线之后,12V可以直接输出 5V 给ESP32 供电。不再需要其他单独的电源了。
板载5V输出,给ESP32 供电。
演示视频:
B站:
使用esp32接入ps手柄。用摇杆控制小车前进后退。
3,总结
还是要坚持才可以有收获呢,虽然改的代码不多,但是成功控制小车还是特别的高兴的呢。
代码都是一点一点测试,运行才够的。
其中有个bug折腾半天才弄明白,这个需要打日志,看明白。
就是一通电之后,四轮就都转动,一直没有找到停止的控制地方。
打了日志之后发现,是因为这个PS2的手柄初始化的时候造成遥感也有值了。是128 ,不是127 。奇怪了。
本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/104855458
博主地址是:https://blog.csdn.net/freewebsys
这篇关于arduino(17):特别好玩的玩具,使用ESP32接入PS2无线手柄,然后控制小车,使用遥控杆控制前后左右,解决启动空转,和连接不上手柄一直空转问题,有接线图和源代码,给小盆友玩。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!