转网友版
电机:有刷直流电机
驱动器:L298n逻辑
控制器:Arduino Mega2560
程序功能实现:
- 按下START键,开始前进;
- 通过按PS2手柄左边的“上下左右”键实现前进 、后退、左转、右转。
- 按SELECT键停止;
#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
******************************************************************/ //PS2手柄引脚; #define PS2_DAT 13 //14 #define PS2_CMD 11 //15 #define PS2_SEL 10 //16 #define PS2_CLK 12 //17 // 电机控制引脚; #define IN1 4 #define IN2 5 #define IN3 6 #define IN4 7 //PWM控制引脚; int speedPinA = 8; int speedPinB = 9; int speed; /******************************************************************
* 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; void setup(){ pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); speed =200; Serial.begin(57600); delay(300) ; //added delay to give wireless ps2 module some time to startup, before configuring it //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.print("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 http://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 http://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 http://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.print("Unknown Controller type found "); break; case 1: Serial.print("DualShock Controller found "); break; case 2: Serial.print("GuitarHero Controller found "); break; case 3: Serial.print("Wireless Sony DualShock Controller found "); break; } } void turnLeft(){ digitalWrite(IN1,HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4,HIGH); } void turnRight(){ digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } void forward(){ digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } void back(){ digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); } 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
*/ if(error == 1) //skip loop if no controller found return; if(type == 2) {//Guitar Hero Controller return; } else { //DualShock Controller ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed //start 开始运行,电机初PWM为120; if(ps2x.Button(PSB_START)) { Serial.println("Start is being held"); speed = 120; analogWrite(speedPinA, speed); analogWrite(speedPinB, speed); forward(); } // 电机正转; if(ps2x.Button(PSB_PAD_UP)){ Serial.println("Up held this hard: "); speed= 200; analogWrite(speedPinA, speed); analogWrite(speedPinB, speed); forward(); } // 电机反转; if(ps2x.Button(PSB_PAD_DOWN)){ Serial.print("Down held this hard: "); speed= 200; analogWrite(speedPinA, speed); analogWrite(speedPinB, speed); back(); } //左转; if(ps2x.Button(PSB_PAD_LEFT)){ Serial.println("turn left "); analogWrite(speedPinA, speed); analogWrite(speedPinB, 0); turnLeft(); } //右转; if(ps2x.Button(PSB_PAD_RIGHT)){ Serial.println("turn right"); analogWrite(speedPinA, 0); analogWrite(speedPinB, speed); turnRight(); } // Stop if(ps2x.Button(PSB_SELECT)){ Serial.println("stop"); speed = 0; analogWrite(speedPinA,speed); analogWrite(speedPinB,speed); } } }
---------------------
作者:卧龙_
来源:CSDN
原文:https://blog.csdn.net/Draonly/article/details/77319435
版权声明:本文为博主原创文章,转载请附上博文链接!
电机:有刷直流电机
驱动器:L298n逻辑
控制器:Arduino Mega2560
程序功能实现:
- 按下START键,开始前进;
- 通过按PS2手柄左边的“上下左右”键实现前进 、后退、左转、右转。
- 按SELECT键停止;
#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
******************************************************************/ //PS2手柄引脚; #define PS2_DAT 13 //14 #define PS2_CMD 11 //15 #define PS2_SEL 10 //16 #define PS2_CLK 12 //17 // 电机控制引脚; #define IN1 4 #define IN2 5 #define IN3 6 #define IN4 7 //PWM控制引脚; int speedPinA = 8; int speedPinB = 9; int speed; /******************************************************************
* 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; void setup(){ pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); speed =200; Serial.begin(57600); delay(300) ; //added delay to give wireless ps2 module some time to startup, before configuring it //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.print("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 http://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 http://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 http://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.print("Unknown Controller type found "); break; case 1: Serial.print("DualShock Controller found "); break; case 2: Serial.print("GuitarHero Controller found "); break; case 3: Serial.print("Wireless Sony DualShock Controller found "); break; } } void turnLeft(){ digitalWrite(IN1,HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4,HIGH); } void turnRight(){ digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } void forward(){ digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } void back(){ digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); } 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
*/ if(error == 1) //skip loop if no controller found return; if(type == 2) {//Guitar Hero Controller return; } else { //DualShock Controller ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed //start 开始运行,电机初PWM为120; if(ps2x.Button(PSB_START)) { Serial.println("Start is being held"); speed = 120; analogWrite(speedPinA, speed); analogWrite(speedPinB, speed); forward(); } // 电机正转; if(ps2x.Button(PSB_PAD_UP)){ Serial.println("Up held this hard: "); speed= 200; analogWrite(speedPinA, speed); analogWrite(speedPinB, speed); forward(); } // 电机反转; if(ps2x.Button(PSB_PAD_DOWN)){ Serial.print("Down held this hard: "); speed= 200; analogWrite(speedPinA, speed); analogWrite(speedPinB, speed); back(); } //左转; if(ps2x.Button(PSB_PAD_LEFT)){ Serial.println("turn left "); analogWrite(speedPinA, speed); analogWrite(speedPinB, 0); turnLeft(); } //右转; if(ps2x.Button(PSB_PAD_RIGHT)){ Serial.println("turn right"); analogWrite(speedPinA, 0); analogWrite(speedPinB, speed); turnRight(); } // Stop if(ps2x.Button(PSB_SELECT)){ Serial.println("stop"); speed = 0; analogWrite(speedPinA,speed); analogWrite(speedPinB,speed); } } }
---------------------
作者:卧龙_
来源:CSDN
原文:https://blog.csdn.net/Draonly/article/details/77319435
版权声明:本文为博主原创文章,转载请附上博文链接!