Foro x-plane.es

Foro General => X-Plane 11 => Mensaje iniciado por: Uriel_jisa en 27 Julio, 2022, 17:54:17



Título: HELP
Publicado por: Uriel_jisa en 27 Julio, 2022, 17:54:17
Buen día para todos, espero puedan ayuadarme. Necesito utilizar un encoder rotatorio de 3 pines para modificar algún comando de XPlane 11, sin embargo no puedo hacer que mi código haga que gire en ambos sentidos el comando del altímetro, solo suma al conteo pero en ningun momento baja así le esté girando en sentido antihorario.
#include <arduino.h>
#include <XPLDirect.h>
#include <stdio.h>
XPLDirect Xinterface(&Serial);

const int PinA = 2;
const int PinB = 3;
float Up;
float Down;

const int timeThreshold = 50;
long timeCounter = 0;

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

bool giroCW = true;
bool vueltaCW = true;

void setup()
{
   pinMode(PinA, INPUT);
   pinMode(PinB, INPUT);
   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(\"XPLDirect Command Demo\");
 


//Xinterface.registerCommand(\"sim/instruments/barometer_down\", XPL_READ, 100, 0, &verticalSpeed);
 Xinterface.registerCommand(\"sim/instruments/barometer_copilot_down\", &Down);
//Xinterface.registerCommand(\"sim/instruments/barometer_copilot_up\", &Up);


}
void loop()

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

void doEncodeA()
{
   if (millis() > timeCounter - timeThreshold)
   {
      if (digitalRead(PinA) == digitalRead(PinB))
      {
         giroCW = true;
         if (ISRCounter + 1 <= pasosMax) ISRCounter++, Up++;
      }
   }
     /* if (digitalRead(PinB) != digitalRead(PinA))
      {
         giroCW = false;
         if (ISRCounter - 1 <= 0) ISRCounter--, horizDown++;
      }*/
      timeCounter = millis();     
    }
 


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




//sim/instruments/barometer_down
//sim/instruments/barometer_up

sim/instruments/barometer_copilot_up
sim/instruments/barometer_copilot_down*/


Título: Re: HELP
Publicado por: grrr05 en 27 Julio, 2022, 19:38:02
Aunque el C y todos sus derivados me suenan a chino, veo un par de cosas;

- ¿De donde sacas las variables horizUp y horizDown? Sólo tienes declaradas Up y Down.
- en doEncodeA, para el giro antihorario (el cual tienes comentado) la condicional debería ser (ISRCounter - 1 > 0) en vez de menor o igual que 0 - entiendo que ISRCounter sólo puede ir de 0 a 255.

Veo mas correcto doEncodeB (comentado completamente) que doEncodeA, salvo por las variables horiz*, pero tampoco me hagas mucho caso...

Citar
[...]

float Up;
float Down;

[...]

void doEncodeA()
{
   if (millis() > timeCounter - timeThreshold)
   {
      if (digitalRead(PinA) == digitalRead(PinB))
      {
         giroCW = true;
         if (ISRCounter + 1 <= pasosMax) ISRCounter++, Up++;
      }
   }
     /* if (digitalRead(PinB) != digitalRead(PinA))
      {
         giroCW = false;
         if (ISRCounter - 1 <= 0) ISRCounter--, horizDown++;
      }*/
      timeCounter = millis();    
    }
 


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


Por cierto estaría bien que cambiases el título de tu hilo para que describa mejor tu problema, un hilo que se llama simplemente "help" o "ayuda" sin más no suele despertar mucho interes y puede acabar perdido fácilmente.