Monday, May 25, 2020

ESP01 - X-plane data

A trial parser code in Arduino decode X-Plane UDP data-gram, a sample data array assigned for trial:

In the Sample, data of the output Packet: Pitch, roll, heading:
pitch, deg The aircraft’s pitch, measured in body-axis Euler angles.
roll, deg The aircraft’s roll, measured in body-axis Euler angles.
hding, true The aircraft’s true heading, measured in body-axis Euler angles.
hding, mag The aircraft’s magnetic heading, in degrees.

The UDP data-gram sample, 41 byte:
444154412A11000000DC5C7E3F285277BED1DFCB415862E3410C0790400C079C400C079C400C07914

444154412A11 = DATA*(tab)
000000 = Index
Data 8*4byte
DC5C7E3F:285277BE
D1DFCB41:5862E341
00C079C4:00C079C4
00C079C4:00C079C4


The meaning of test data-gram in index number:
Head=0-5, index=6-8, pitch=9-12, roll=13-16, true=17-20, mag=21-24, null=25-40

Swap endiannes Hex to floating point:
DC5C7E3F => 3F7E5CDC =    0.993604 pitch
285277BE => BE775228 =   -0.241524 roll
D1DFCB41 => 41CBDFD1 =   25.4843 heading(true)
5862E341 => 41E36258 =   28.423 heading(magnetic)
00C079C4 => C479C000 = -999
00C079C4 => C479C000 = -999
00C079C4 => C479C000 = -999
00C079C4 => C479C000 = -999

float to Hex calculation: https://gregstoll.com/~gregstoll/floattohex/

_____________________________________________________________________

Arduino Program:

// Demo to decode the X-Plane UDP data packet from swap endianness HEX to 32bit floting point convertion.

char testdata[] = {
      0x44, 0x41, 0x54, 0x41, 0x2A, 0x11, 0x00, 0x00, 0x01,
      0xDC, 0x5C, 0x7E, 0x3F,
      0x28, 0x52, 0x77, 0xBE,
      0xD1, 0xDF, 0xCB, 0x41,
      0x58, 0x62, 0xE3, 0x41,
      0x00, 0xC0, 0x79, 0xC4,
      0x00, 0xC0, 0x79, 0xC4,
      0x00, 0xC0, 0x79, 0xC4,
      0x00, 0xC0, 0x79, 0xC4,
    };

long t;
int packet_index;
int x;
const char* head = "DATA*";

void setup() {
  Serial.begin(115200);
}

void loop() {
       Serial.printf("\nUDP packet contents:\n");
       // data-gram header text = DATA*
       for (int idx = 0; idx <= sizeof(head) ; idx++)
       {
        Serial.print(testdata[idx]);
       }
       // data-gram idx:
       packet_index = (testdata[6]<<16)+(testdata[7]<<8)+(testdata[8]);
       Serial.println(packet_index);

       // Only 8 data in one packet, idx will start at
       // 1=idx(9), 2=idx(13), 3=17, 4=21, 5=25, 6=29, 7=33, 8=37.
       for (int idx = 9; idx <41 ; idx = idx + 4)
       {
        // swap endiness hex to floting point convertion.
       t = (testdata[idx+3]<<24)+(testdata[idx+2]<<16)+(testdata[idx+1]<<8)+(testdata[idx]);
       Serial.println(*(float*) &t, 6);       
       }

      // below just display the original data-gram from UDP packet.
      Serial.println("UDP original Data-gram:");
         for (int idx = 0; idx < sizeof(testdata) ; idx++)
      {
          if (idx <= 8){
            Serial.print(testdata[idx]);
          }
          else {
            if (idx == 9) Serial.print("\n");
            if (testdata[idx]<15) Serial.print("0");
            Serial.print(testdata[idx], HEX);
            x += 1;
            if (x == 4 || x == 8 || x == 12)Serial.print(":");
            else if (x == 16){
              Serial.print("\n");
              x = 0;
            }
          }
      }
  delay(5000);
}

No comments:

Post a Comment