Arduino Polska Forum

Pełna wersja: Esp32 i kontroler ps3 sterowanie analog joystick
Aktualnie przeglądasz uproszczoną wersję forum. Kliknij tutaj, by zobaczyć wersję z pełnym formatowaniem.
Witam
Jestem nowy i to mój pierwszy projekt.
Testuje gotowe wsady z YT chce użyć pilota z ps3 do sterowania zabawką. Mam taki problem że wszystkie wsady zawierają sterowanie pojazdem typu czołg gdzie kierunek w przód uruchamia dwa silniki na raz ja chce jeden silnik do sterowania(lewo prawo) drugi do jazdy (przód tył) nie potrafię tego rozdzielić. Drugi problem to dziwna sprawa niby wywołanie jest do lewej gałki jako do jazdy przód tył a prawa gałka do skręcania a jest taki misz masz że totalnie nie rozumiem tego. chce aby jeden joystick sterował jednym silnikiem a drugi drugim bez wzajemnej reakcji. Aktualnie sparowałem ps3 pada z esp32 do esp 32 podłączyłem gotowy 2x mostek typu H po dwa piny do sterowania pwm na silnik (pinIn; High,low= przód; low,High=tył). Komunikacja jest reakcja na wyjściu mostka jest. Początkowo dryfujące potencjometry robiły problem wiec zwiększyłem wartość referencyjna 0 na 10/-10 uruchamia sie z wartością 0V. Najlepiej jak by ktoś poprowadził jak sterować jednym silnikiem a już drugi nie będzie problemu. W tym szkicu jest trochę dziwnych funkcji których nie rozumiem. Ucinam przerabiam ale bez rezultatu. Nie wiem która funkcja scala te dwa silniki i joysticki razem. niby lewy lxAxis jest tylko przód tył a działa też lyAxis a nie powinien tylko ryAxis, po prostu działają wszystkie osie a nie powinny.
Kod:
#include <Ps3Controller.h>

//Right motor
int enableRightMotor=22;
int rightMotorPin1=16;
int rightMotorPin2=17;
//Left motor
int enableLeftMotor=23;
int leftMotorPin1=18;
int leftMotorPin2=19;

const int PWMFreq = 1000; /* 1 KHz */
const int PWMResolution = 8;
const int rightMotorPWMSpeedChannel = 4;
const int leftMotorPWMSpeedChannel = 5;

void notify()
{
  int yAxisValue =(Ps3.data.analog.stick.ly);  //Left stick  - y axis - forward/backward car movement
  int xAxisValue =(Ps3.data.analog.stick.rx);  //Right stick - x axis - left/right car movement

  int throttle = map( yAxisValue, -127, 127, -255, 255);
  int steering = map( xAxisValue, -127, 127, -255, 255); 
  int motorDirection = 1;
 
  if (throttle < -10)       //Move car backward
  {
    motorDirection = -1;   
  }

  int rightMotorSpeed, leftMotorSpeed;
  rightMotorSpeed =  abs(throttle);
  leftMotorSpeed =  abs(steering);
  rightMotorSpeed = constrain(rightMotorSpeed, 0, 255);
  leftMotorSpeed = constrain(leftMotorSpeed, 0, 255);

  //Serial.println(rightMotorSpeed);
  //Serial.println(leftMotorSpeed); 
  rotateMotor(rightMotorSpeed * motorDirection, leftMotorSpeed * motorDirection);

}

void onConnect()
{
  Serial.println("Connected!.");
}

void onDisConnect()
{
  rotateMotor(0, 0);
  Serial.println("Disconnected!.");   
}

void rotateMotor(int rightMotorSpeed, int leftMotorSpeed)
{
  if (rightMotorSpeed < -10)
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,HIGH);   
  }
  else if (rightMotorSpeed > 10)
  {
    digitalWrite(rightMotorPin1,HIGH);
    digitalWrite(rightMotorPin2,LOW);     
  }
  else
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,LOW);     
  }
 
  if (leftMotorSpeed < -10)
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,HIGH);   
  }
  else if (leftMotorSpeed > 10)
  {
    digitalWrite(leftMotorPin1,HIGH);
    digitalWrite(leftMotorPin2,LOW);     
  }
  else
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,LOW);     
  }
  ledcWrite(rightMotorPWMSpeedChannel, abs(rightMotorSpeed));
  ledcWrite(leftMotorPWMSpeedChannel, abs(leftMotorSpeed));   
}

void setUpPinModes()
{
  pinMode(enableRightMotor,OUTPUT);
  pinMode(rightMotorPin1,OUTPUT);
  pinMode(rightMotorPin2,OUTPUT);
 
  pinMode(enableLeftMotor,OUTPUT);
  pinMode(leftMotorPin1,OUTPUT);
  pinMode(leftMotorPin2,OUTPUT);

  //Set up PWM for motor speed
  ledcSetup(rightMotorPWMSpeedChannel, PWMFreq, PWMResolution);
  ledcSetup(leftMotorPWMSpeedChannel, PWMFreq, PWMResolution); 
  ledcAttachPin(enableRightMotor, rightMotorPWMSpeedChannel);
  ledcAttachPin(enableLeftMotor, leftMotorPWMSpeedChannel); 
 
  rotateMotor(0, 0);
}


void setup()
{
  setUpPinModes();
  Serial.begin(115200);
  Ps3.attach(notify);
  Ps3.attachOnConnect(onConnect);
  Ps3.attachOnDisconnect(onDisConnect);
  Ps3.begin();
  Serial.println("Ready.");
}

void loop()
{
}
Jak się ktoś domyśli nad jakim programem pracujesz i jaki masz schemat połączeń to pewnie coś pomoże, tymczasem poczekamy.
udało się!!! kod działa zamieszczam dla kogoś kto będzie potrzebował mamy revers wszystko działa piny eneble nie skasowałem u mnie są nie potrzebne wiec nie podłączam. używam mostka H ZK-5AD.

AKTUALIZACJA: Jednak jest problem z PWM wydaje mi się ze właśnie przez pin Enable. Mój mostek h go nie posiada a sterowanie PWM jest po jednym z pinów który odpowiada za kierunek jak mamy argument high, low. To pin w stanie high będzie miał sygnał PWM. Szukam opcji zmiany i wywalenia opcji pin ENABLE. Jakieś pomysły?
Kod:
#include <Ps3Controller.h>

//Right motor
int enableRightMotor=22;
int rightMotorPin1=16;
int rightMotorPin2=17;
//Left motor
int enableLeftMotor=23;
int leftMotorPin1=18;
int leftMotorPin2=19;

const int PWMFreq = 1000; /* 1 KHz */
const int PWMResolution = 8;
const int rightMotorPWMSpeedChannel = 4;
const int leftMotorPWMSpeedChannel = 5;

void notify()
{
  int yAxisValue =(Ps3.data.analog.stick.ly);  //Left stick  - y axis - forward/backward car movement
  int xAxisValue =(Ps3.data.analog.stick.rx);  //Right stick - x axis - left/right car movement

  int throttle = map( yAxisValue, -127, 127, -255, 255);
  int steering = map( xAxisValue, -127, 127, -255, 255); 
  int rightMotorSpeed, leftMotorSpeed;
  int motorDirectionR = 1;
 
  if (throttle < 0)       //Move car backward
  {
    motorDirectionR = -1;   
  }
  int motorDirectionL = 1;
 
  if (steering < 0)       //Move car backward
  {
    motorDirectionL = -1;   
  }
 
  rightMotorSpeed =  abs(throttle);
  leftMotorSpeed =  abs(steering);
  rightMotorSpeed = constrain(rightMotorSpeed, 0, 255);
  leftMotorSpeed = constrain(leftMotorSpeed, 0, 255);

  //Serial.println(rightMotorSpeed);
  //Serial.println(leftMotorSpeed); 
  rotateMotor(rightMotorSpeed * motorDirectionR, leftMotorSpeed * motorDirectionL );
}

void onConnect()
{
  Serial.println("Connected!.");
}

void onDisConnect()
{
  rotateMotor(0, 0);
  Serial.println("Disconnected!.");   
}

void rotateMotor(int rightMotorSpeed, int leftMotorSpeed)
{
  if (rightMotorSpeed < -10)
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,HIGH);   
  }
  else if (rightMotorSpeed > 10)
  {
    digitalWrite(rightMotorPin1,HIGH);
    digitalWrite(rightMotorPin2,LOW);     
  }
  else
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,LOW);     
  }
 
  if (leftMotorSpeed < -10)
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,HIGH);   
  }
  else if (leftMotorSpeed > 10)
  {
    digitalWrite(leftMotorPin1,HIGH);
    digitalWrite(leftMotorPin2,LOW);     
  }
  else
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,LOW);     
  }
  ledcWrite(rightMotorPWMSpeedChannel, abs(rightMotorSpeed));
  ledcWrite(leftMotorPWMSpeedChannel, abs(leftMotorSpeed));  
}

void setUpPinModes()
{
  pinMode(enableRightMotor,OUTPUT);
  pinMode(rightMotorPin1,OUTPUT);
  pinMode(rightMotorPin2,OUTPUT);
 
  pinMode(enableLeftMotor,OUTPUT);
  pinMode(leftMotorPin1,OUTPUT);
  pinMode(leftMotorPin2,OUTPUT);

  //Set up PWM for motor speed
  ledcSetup(rightMotorPWMSpeedChannel, PWMFreq, PWMResolution);
  ledcSetup(leftMotorPWMSpeedChannel, PWMFreq, PWMResolution); 
  ledcAttachPin(enableRightMotor, rightMotorPWMSpeedChannel);
  ledcAttachPin(enableLeftMotor, leftMotorPWMSpeedChannel); 
 
  rotateMotor(0, 0);
}


void setup()
{
  setUpPinModes();
  Serial.begin(115200);
  Ps3.attach(notify);
  Ps3.attachOnConnect(onConnect);
  Ps3.attachOnDisconnect(onDisConnect);
  Ps3.begin();
  Serial.println("Ready.");
}

void loop()
{
}