Jostick soll zwei Motoren steuern

Hallo alle mit einander.
Und zwar habe ich folgendes Problem.
Ich möchte mit einem Xbox Controller genauer gesagt mit dem linken Joystick 2 Motoren steuern.
Dazu habe ich den Xbox Controller mittels einem USB-Shield verbunden. Der Controller wird erkannt und zeigt alle Tasten an, soweit so gut.
Nun das Problem:
Und zwar weiß ich nicht genau wie ich den Quelltext für die beiden Motoren in den Quelltext für den Xbox Controller integrieren kann, damit ich mit dem Joystock die Motoren steuern kann.
Wird der Joystick nach vorne gekippt sollen beide Motoren vorwärts drehen.
Wird er nachhinten gekippt sollen beide Motoren Rückwärts drehen.
Wird der Joystick Schräg nach vorne gekippt soll der eine Motor schneller als der andere Motor drehen.
Ich möchte damit also eine Kleines Kettenfahrzeug betreiben.

Motoransteuerung erfolgt mittels H-bridge.

Code für die beiden Motoren:

int motor1_A=11;
int motor1_B=10;
int motor1_Speed=9;
  
int motor2_A=5;
int motor2_B=4;
int motor2_Speed=3;
  
void setup(){
  pinMode(motor1_A,OUTPUT);
  pinMode(motor1_B,OUTPUT);
  
  pinMode(motor2_A,OUTPUT);
  pinMode(motor2_B,OUTPUT);
}
  
void loop(){
  // motor1
  for (int i=0; i>256; i+=5){
    digitalWrite(motor1_A,HIGH); // A = HIGH and B = LOW means the motor will turn right
    digitalWrite(motor1_B,LOW);
    analogWrite(motor1_Speed,i); // speed counts from 0 to 255
    delay(20);
  }

  // motor2
  for (int i=0; i<256; i+=5){
    digitalWrite(motor2_A,HIGH); // A = HIGH and B = LOW means the motor will turn right
    digitalWrite(motor2_B,LOW);
    analogWrite(motor2_Speed,i); // speed counts from 0 to 255
    delay(20);
  }
  
}

Code für den Controller:

/*
 Example sketch for the Xbox Wireless Reciver library - developed by Kristian Lauszus
 It supports up to four controllers wirelessly
 For more information see the blog post: http://blog.tkjelectronics.dk/2012/12/xbox-360-receiver-added-to-the-usb-host-library/ or
 send me an e-mail:  kristianl@tkjelectronics.com
 */

#include <XBOXRECV.h>
// Satisfy IDE, which only needs to see the include statment in the ino.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif

USB Usb;
XBOXRECV Xbox(&Usb);

void setup() {
  Serial.begin(115200);
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
}
void loop() {
  Usb.Task();
  if (Xbox.XboxReceiverConnected) {
    for (uint8_t i = 0; i < 4; i++) {
      if (Xbox.Xbox360Connected[i]) {
        if (Xbox.getButtonPress(L2, i) || Xbox.getButtonPress(R2, i)) {
          Serial.print("L2: ");
          Serial.print(Xbox.getButtonPress(L2, i));
          Serial.print("\tR2: ");
          Serial.println(Xbox.getButtonPress(R2, i));
          Xbox.setRumbleOn(Xbox.getButtonPress(L2, i), Xbox.getButtonPress(R2, i), i);
        }

        if (Xbox.getAnalogHat(LeftHatX, i) > 7500 || Xbox.getAnalogHat(LeftHatX, i) < -7500 || Xbox.getAnalogHat(LeftHatY, i) > 7500 || Xbox.getAnalogHat(LeftHatY, i) < -7500 || Xbox.getAnalogHat(RightHatX, i) > 7500 || Xbox.getAnalogHat(RightHatX, i) < -7500 || Xbox.getAnalogHat(RightHatY, i) > 7500 || Xbox.getAnalogHat(RightHatY, i) < -7500) {
          if (Xbox.getAnalogHat(LeftHatX, i) > 7500 || Xbox.getAnalogHat(LeftHatX, i) < -7500) {
            Serial.print(F("LeftHatX: "));
            Serial.print(Xbox.getAnalogHat(LeftHatX, i));
            Serial.print("\t");
          }
          if (Xbox.getAnalogHat(LeftHatY, i) > 7500 || Xbox.getAnalogHat(LeftHatY, i) < -7500) {
            Serial.print(F("LeftHatY: "));
            Serial.print(Xbox.getAnalogHat(LeftHatY, i));
            Serial.print("\t");
          }
          if (Xbox.getAnalogHat(RightHatX, i) > 7500 || Xbox.getAnalogHat(RightHatX, i) < -7500) {
            Serial.print(F("RightHatX: "));
            Serial.print(Xbox.getAnalogHat(RightHatX, i));
            Serial.print("\t");
          }
          if (Xbox.getAnalogHat(RightHatY, i) > 7500 || Xbox.getAnalogHat(RightHatY, i) < -7500) {
            Serial.print(F("RightHatY: "));
            Serial.print(Xbox.getAnalogHat(RightHatY, i));
          }
          Serial.println();
        }

        if (Xbox.getButtonClick(UP, i)) {
          Xbox.setLedOn(LED1, i);
          Serial.println(F("Up"));
        }
        if (Xbox.getButtonClick(DOWN, i)) {
          Xbox.setLedOn(LED4, i);
          Serial.println(F("Down"));
        }
        if (Xbox.getButtonClick(LEFT, i)) {
          Xbox.setLedOn(LED3, i);
          Serial.println(F("Left"));
        }
        if (Xbox.getButtonClick(RIGHT, i)) {
          Xbox.setLedOn(LED2, i);
          Serial.println(F("Right"));
        }

        if (Xbox.getButtonClick(START, i)) {
          Xbox.setLedMode(ALTERNATING, i);
          Serial.println(F("Start"));
        }
        if (Xbox.getButtonClick(BACK, i)) {
          Xbox.setLedBlink(ALL, i);
          Serial.println(F("Back"));
        }
        if (Xbox.getButtonClick(L3, i))
          Serial.println(F("L3"));
        if (Xbox.getButtonClick(R3, i))
          Serial.println(F("R3"));

        if (Xbox.getButtonClick(L1, i))
          Serial.println(F("L1"));
        if (Xbox.getButtonClick(R1, i))
          Serial.println(F("R1"));
        if (Xbox.getButtonClick(XBOX, i)) {
          Xbox.setLedMode(ROTATING, i);
          Serial.print(F("Xbox (Battery: "));
          Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3
          Serial.println(F(")"));
        }
        if (Xbox.getButtonClick(SYNC, i)) {
          Serial.println(F("Sync"));
          Xbox.disconnect(i);
        }

        if (Xbox.getButtonClick(A, i))
          Serial.println(F("A"));
        if (Xbox.getButtonClick(B, i))
          Serial.println(F("B"));
        if (Xbox.getButtonClick(X, i))
          Serial.println(F("X"));
        if (Xbox.getButtonClick(Y, i))
          Serial.println(F("Y"));
      }
    }
  }
}

Ich hoffe ihr könnt mir dabei weiter helfen.
mfg bro

hey
ich hatte ein ähnliches Problem:
hatte ein Joystick der einfach aus zwei poti aufgebaut ist, dann musste ich den auslesen und die motoren genau gleich ansteuern wie du:

dafür habe ich das hier geschrieben:

float Speed;

float Fix = 519;

float haelfte = 512;
float alpha;

float motR = 0;
float motL = 0;

int xAchsePin = A0;
int yAchsePin = A1;

int xAchse;
int yAchse

int marche = 8;

void Speed() {
  xAchse = analogRead(xAchsePin);
  yAchse = analogRead(yAchsePin);
  Speed = sqrt(sq(xAchse-haelfte)+sq(yAchse-haelfte));
  if (yAchsePos()) {
    if (xAchsePos()) {
      motR = -1;
      alpha = degrees(asin(map(yAchse, haelfte, 1023, 0, 1)));
      motL = map(alpha, 0, 90, 1, -1);
    }
    else if(xAchseNeg()) {
      motL = -1;
      alpha = degrees(asin(map(yAchse, haelfte, 1023, 0, 1)));
      motR = map(alpha, 0, 90, 1, -1);
    }
    else {
      motR = -1;
      motL = -1;
      Speed = yAchse-haelfte;
    }
  }
  else if (yAchseNeg()) {
    if (xAchsePos()) {
      motL = 1;
      alpha = degrees(asin(map(yAchse, haelfte, 0, 0, 1)));
      motR = map(alpha, 0, 90, -1, 1);
    }
    else if (xAchseNeg()) {
      motR = 1;
      alpha = degrees(asin(map(yAchse, haelfte, 0, 0, 1)));
      motL = map(alpha, 0, 90, -1, 1);
    }
    else {
      motR = 1;
      motL = 1;
      Speed = sqrt(sq(yAchse-haelfte));
    }
  }
  else {
    if (xAchsePos()) {
      motL = 1;
      motR = -1;
      Speed = xAchse-haelfte;
    }
    else if (xAchseNeg()) {
      motL = -1;
      motR = 1;
      Speed = sqrt(sq(xAchse-haelfte));
    }
    else {
      motL = 0;
      motR = 0;
      Speed = 0;
    }
  }
  if (Speed > 512) Speed = 512;
  motR *= Speed;
  motL *= Speed;
  motR = int(map(motR, -512, 512, 1000, 2000));
  motL = int(map(motL, -512, 512, 1000, 2000));
}

boolean yAchsePos() {
  if (yAchse > Fix+marche) return true;
  else return false;
}

boolean yAchseNeg() {
  if (yAchse < Fix-marche) return true;
  else return false;
}

boolean xAchsePos() {
  if (xAchse > Fix+marche) return true;
  else return false;
}

boolean xAchseNeg() {
  if (xAchse < Fix-marche) return true;
  else return false;
}

ist zwar nicht mit einem xbox Controller aber vielleicht hilft es dir.

und wichtig ist, dass du bei deinen Motoren Rücklaufdioden anbringst, sonnst geht dir dein Treiber über kurz oder lang kaputt.

Hey Danke für deine Antwort, ist ja eigentlich das gleiche, muss ich mal gucken ob ich das hinbekomme.

und wichtig ist, dass du bei deinen Motoren Rücklaufdioden anbringst, sonnst geht dir dein Treiber über kurz oder lang kaputt.

Aber dafür benutze ich doch die H-Bridge mit dem L293D. Oder brauche trotzdem welche?

http://www.arduino-tutorial.de/2010/06/motorsteuerung-mit-einem-h-bridge-ic/

Sorry das war falsch.

Der L293D hat die Rücklaufdioden schon eingebaut, beim L293 musst du sie noch extern anfügen.

mfg

ich merke gerade dass ich diesen Sketch für brushlessmotoren geschrieben habe.

motR und motL geben somit immer einen Wert zwischen 1000 und 2000 zurück.

1000 bedeutet vollgas rückwärts 2000 vollgas vorwärts. den rest kannst du dir ja denken :wink:

aber ich glaube so langsam oder sicher bist du wesentlich schneller wenn du dir da selber etwas im ähnlichen Prinzip ausdenkst, als teile von meinem Sketch einzubauen.. :slight_smile:

achso :slight_smile:
ich muss erstmal schauen wo ich das da einbauen muss, also wo ich dazwischen gehen kann mit dem code vom motor.

-Jonas-:
ich merke gerade dass ich diesen Sketch für brushlessmotoren geschrieben habe.

motR und motL geben somit immer einen Wert zwischen 1000 und 2000 zurück.

1000 bedeutet vollgas rückwärts 2000 vollgas vorwärts. den rest kannst du dir ja denken :wink:
von meinem Sketch einzubauen.. :slight_smile:

Das kommt darauf an. ESP-s für Flugmotoren haben keinen Rückwertsgang. Da ist 1000 aus und 2000 Vollgas. Die ESP für Fahrmotoren von Fahrzeugen haben einene Rückwertsgang.

Grüße Uwe