// ESP32 PS3 RC Crawler v0.1
// Target device : ESP-WROOM-32
// Date : 2023/12/22
// Author : Takishita of Delivery Toy Hospital
// http://wwww.takishita.jp/toy_hospital
//
#include <Ps3Controller.h>
// R-ch
#define PWM_PIN1_1 25 // 25pin
#define PWM_PIN1_2 26 // 26pin
// L-ch
#define PWM_PIN2_1 32 // 32pin
#define PWM_PIN2_2 33 // 33pin
#define CH0 0 // CH number 0
#define CH1 1 // CH number 1
#define CH2 2 // CH number 2
#define CH3 3 // CH number 3
#define DUTY_F 127 // duty full
#define DUTY_Z 0 // duty zero
#define FREQ 1000 // 1kHz
#define BIT_RES 7 // 7 bit resolution(0-127)
// Globals
int8_t duty_x = 0; // X axis 0% = PWM OFF
int8_t duty_y = 0; // Y axis 0% = PWM OFF
// int8_t player = 0;
// int8_t battery = 0;
void setup() {
Serial.begin(9600);
// Ps3.attach(notify);
// Ps3.attachOnConnect(onConnect);
Ps3.begin("xx:xx:xx:xx:xx:xx");
// Serial.println("Ready.");
// Pin initilal setting
// R-ch
pinMode(PWM_PIN1_1, OUTPUT);
pinMode(PWM_PIN1_2, OUTPUT);
// L-ch
pinMode(PWM_PIN2_1, OUTPUT);
pinMode(PWM_PIN2_2, OUTPUT);
// PWM initial setting
ledcSetup(CH0, FREQ, BIT_RES);
ledcSetup(CH1, FREQ, BIT_RES);
ledcAttachPin(PWM_PIN1_1, CH0);
ledcAttachPin(PWM_PIN1_2, CH1);
ledcSetup(CH2, FREQ, BIT_RES);
ledcSetup(CH3, FREQ, BIT_RES);
ledcAttachPin(PWM_PIN2_1, CH2);
ledcAttachPin(PWM_PIN2_2, CH3);
}
void loop() {
// Foward or Backward
if( abs(Ps3.data.analog.stick.rx) < 10) { // Exclusive processing with left or right rotation stick posion
// Serial.print("y="); Serial.println(Ps3.data.analog.stick.ly, DEC); // debug
// Serial.print("duty_y="); Serial.println(duty_y, BIN); // debug
// Stop
if( Ps3.data.analog.stick.ly >= -15 && Ps3.data.analog.stick.ly <= 15 ){ // Stop zone
// R-ch
ledcWrite(CH0, DUTY_Z); // duty zero
ledcWrite(CH1, DUTY_Z); // duty zero
// L-ch
ledcWrite(CH2, DUTY_Z); // duty zero
ledcWrite(CH3, DUTY_Z); // duty zero
// Foward
} else if( Ps3.data.analog.stick.ly < -15 ){ // Foward detection
duty_y = map(Ps3.data.analog.stick.ly, 0, -128, 128, 0); // Negative to positive and top to bottom mapping
if( duty_y >= 115 ) { // Top saturation
duty_y = 127;
}
// Serial.print("duty_y="); Serial.println(duty_y, DEC); // debug
// R-ch
ledcWrite(CH0, duty_y); // PWM output
ledcWrite(CH1, DUTY_F); // Hi
// L-ch
ledcWrite(CH2, duty_y); // PWM output
ledcWrite(CH3, DUTY_F); // Hi
// Backward
} else if( Ps3.data.analog.stick.ly > 15 ){ // Backward detection
duty_y = map(Ps3.data.analog.stick.ly, 0, 128, 128, 0); // Top to bottom mapping
if( duty_y >= 115 ) { // Top saturation
duty_y = 127;
}
// Serial.print("duty_y="); Serial.println(duty_y, DEC); // debug
// R-ch
ledcWrite(CH0, DUTY_F); // Hi
ledcWrite(CH1, duty_y); // PWM output
// L-ch
ledcWrite(CH2, DUTY_F); // Hi
ledcWrite(CH3, duty_y); // PWM output
}
}
// Right or Left
if( abs(Ps3.data.analog.stick.ly) < 10) { // Exclusive processing with Foward or Backward stick posion
// Serial.print("x="); Serial.println(Ps3.data.analog.stick.rx, DEC); // debug
// Serial.print("duty_x="); Serial.println(duty_x, BIN); // debug
// Stop
if( Ps3.data.analog.stick.rx >= -15 && Ps3.data.analog.stick.rx <= 15 ){ // Stop zone
// R-ch
ledcWrite(CH0, DUTY_Z); // duty zero
ledcWrite(CH1, DUTY_Z); // duty zero
// L-ch
ledcWrite(CH2, DUTY_Z); // duty zero
ledcWrite(CH3, DUTY_Z); // duty zero
// Right
} else if( Ps3.data.analog.stick.rx < -15 ){ // Right detection
duty_x = map(Ps3.data.analog.stick.rx, 0, -128, 128, 0); // Negative to positive and top to bottom mapping
if( duty_x >= 115 ) { // Top saturation
duty_x = 127;
}
// Serial.print("duty_x="); Serial.println(duty_x, DEC); // debug
// R-ch
ledcWrite(CH0, duty_x); // PWM output
ledcWrite(CH1, DUTY_F); // Hi
// L-ch
ledcWrite(CH2, DUTY_F); // Hi
ledcWrite(CH3, duty_x); // PWM output
// Backward
} else if( Ps3.data.analog.stick.rx > 15 ){ // Backward detection
duty_x = map(Ps3.data.analog.stick.rx, 0, 128, 128, 0); // Top to bottom mapping
if( duty_x >= 115 ) { // Top saturation
duty_x = 127;
}
// Serial.print("duty_x="); Serial.println(duty_x, DEC); // debug
// R-ch
ledcWrite(CH0, DUTY_F); // Hi
ledcWrite(CH1, duty_x); // PWM output
// L-ch
ledcWrite(CH2, duty_x); // PWM output
ledcWrite(CH3, DUTY_F); // Hi
}
}
}