Foro x-plane.es

Foro General => X-Plane 11 => Mensaje iniciado por: Uriel_jisa en 03 Agosto, 2022, 23:10:26



Título: USO DE ENCODERS PARA XPLANE 11
Publicado por: Uriel_jisa en 03 Agosto, 2022, 23:10:26
Buen dia, quiero hacer uso de un encoder conectado a un Arduino Uno en los pines 2 y 3 (pin A y Pin B respectivamente) para la modificacion de comandos en xplane con el plugin XPLDirect. Quisiera saber si me pueden ayudar con el codigo, ya que he hecho el mío pero no puedo ya que se activan dos comandos (altitud en alto y bajo al mismo tiempo).

#include <arduino.h>
#include <XPLDirect.h>
#include <stdio.h>
XPLDirect Xinterface(&Serial);

const int PinA = 2;
const int PinB = 3;
float horizUp;
float horizDown;

const int timeThreshold = 50;
long timeCounter = 0;

const int pasosMax = 255;
volatile int ISRCounter = 0;
int contador = 0;

bool giroCW = true;

void setup()
{
   pinMode(PinA, INPUT_PULLUP);
   pinMode(PinB, INPUT_PULLUP);
   attachInterrupt(digitalPinToInterrupt(PinA), doEncodeA, CHANGE);
   attachInterrupt(digitalPinToInterrupt(PinB), doEncodeB, CHANGE);
   
   Serial.begin(XPLDIRECT_BAUDRATE); // start serial interface.  Baudrate is specified in the header, dont change   
   Xinterface.begin(\"Xplane Demo\");
 


//Xinterface.registerCommand(\"sim/instruments/barometer_down\", XPL_READ, 100, 0, &verticalSpeed);
Xinterface.registerCommand(\"sim/instruments/barometer_down\", &horizDown);
Xinterface.registerCommand(\"sim/instruments/barometer_up\", &horizUp);


}
void loop()

{
  Xinterface.xloop();
   if (contador != ISRCounter)
   {
      contador = ISRCounter;
      Serial.println(contador);
   }
   delay(100);
}

void doEncodeA()
{
   if (millis() > timeCounter + timeThreshold)
   {
      if (digitalRead(PinA) == digitalRead(PinB))
      {
         giroCW = true;
         if (horizUp + 1 <= pasosMax) horizUp++;
      }
      else
      {
         giroCW = false;
         if (horizDown - 1 > 0) horizDown--;
      }
      timeCounter = millis();
   }
}

void doEncodeB()
{
   if (millis() > timeCounter + timeThreshold)
   {
      if (digitalRead(PinA) != digitalRead(PinB))
      {
         giroCW = true;
         if (horizUp + 1 <= pasosMax) horizUp++;
      }
      else
      {
        giroCW = false;
         if (horizDown - 1 > 0) horizDown--;
      }
      timeCounter = millis();
   }
}


Título: Re: USO DE ENCODERS PARA XPLANE 11
Publicado por: bokepacha en 04 Agosto, 2022, 09:09:48
YO en arduino solo he hecho un script (y suerte que me salió bien...) así que dudo que pueda ser de mucha ayuda, pero si no compartes el código poco podemos decir :)


Título: Re: USO DE ENCODERS PARA XPLANE 11
Publicado por: Uriel_jisa en 04 Agosto, 2022, 17:35:21
Muchas gracias, ya lo he modificado y enviado el código :)