arduino (17): A particularly fun toy. Use ESP32 to connect to the PS2 wireless controller, and then control the car, use the remote control lever to control the front, rear, left, and Little friends play.

Preface


All related arduino categories:
https://blog.csdn.net/freewebsys/category_8799254.html

The original link for this article is:
https://blog.csdn.net/freewebsys/article/details/104855458

Reprinting is not allowed without the permission of the blogger.
The blogger's address is: http://blog.csdn.net/freewebsys

1. Regarding arduino control PS2 handle


I studied the PS2 controller last time. This time, remote sensing is used to control the front and back movement of the car.
https://blog.csdn.net/freewebsys/article/details/104734241

It is still very good, and has been modified and connected to the ESP32 board.
This time I want to use this handle to control the car, and I deliberately bought two new 18650 batteries.
Then drive the four-wheel cart.
The L298N chip used to control the rotation of the motors on both sides.
You can search and buy the four-wheeled car and L298N. The fun is to assemble it yourself. And write Arduino code.
Being able to control the car is very good.

PS2 joystick, empty is 127 or 128, forward is 0, and backward is 255. Take control.

2. Wiring configuration


Insert picture description here
L298N needs to pay attention to this. I tested a few pins that can only be connected to the white pins on the picture.
I connected GPIO33, GPIO25, GPIO26, GPIO27 here, and then the power supply, connected to 5V and GND.

Actual wiring effect diagram:

Insert picture description here
all control codes:

/******************************************************************
 * 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      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 = -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 STOP
  LeftStop();
  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 error
    error = 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 Controller
    ps2x.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 ){//FORWORD
      LeftForword();
    }else if( pss_ly == 127 || pss_ly == 128){//STOP
      LeftStop();
    }else if( pss_ly == 255 ){//BACKWORD
      LeftBackword();
    }

    byte pss_ry = ps2x.Analog(PSS_RY);
    if( pss_ry == 0 ){//FORWORD
      RightForword();
    }else if( pss_ry == 127 || pss_ry == 128){//STOP
      RightStop();
    }else if( pss_ry == 255 ){//BACKWORD
      RightBackword();
    }
    
    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);  
}

The most critical code:

 byte pss_ry = ps2x.Analog(PSS_RY);
    if( pss_ry == 0 ){//FORWORD
      RightForword();
    }else if( pss_ry == 127 || pss_ry == 128){//STOP
      RightStop();
    }else if( pss_ry == 255 ){//BACKWORD
      RightBackword();
    }

pss_ry is remote sensing, which only obtains the value of y coordinate, forward is forward, backward is backward, regardless of left and right.
You can use one to control the front, rear, left, and right, but there are two, just follow the way of the tank. You can spin around one after the other, which is very fun.

There is a bug in this place. When the PS2 controller is not connected, the data obtained is 128, and after it is connected, it is 127.
Therefore, both values ​​are judged, and the motor is stopped when a match is found.
The trolley is assembled before, and the motor on one side needs to be connected in reverse.

Directly use two wires to reverse welding to death. It will not be demolished in the future.

Because the current of ESP32 is very small, it can not control the motor, so this L298N is needed.
The input is 12V. I use two 18650s and the voltage is about 7.4V. It can also be driven. If it is too low, it may be enough.
After removing the USB cable, 12V can directly output 5V to power the ESP32. No other separate power supply is needed anymore.
The onboard 5V output powers the ESP32.

Demo video:
Station B:

Use esp32 to connect to the ps handle. Use the joystick to control the car forward and backward.

3. Summary


You still have to persevere before you can gain. Although there are not many code changes, it is still very happy to successfully control the car.
The code is tested little by little, and running is enough.
There is a bug that took a long time to figure out. This requires logging and understanding.
After the power was turned on, all four wheels turned, and no place to stop was found.
After checking the log, it was found that the remote sensing was also valuable when the PS2 handle was initialized. It is 128, not 127. Strange.

The original link for this article is:
https://blog.csdn.net/freewebsys/article/details/104855458

The blogger address is: https://blog.csdn.net/freewebsys

Guess you like

Origin blog.csdn.net/freewebsys/article/details/104855458