HOME Blog SimVimCockpit SimVimPanel Baron-58 sim ArdSimX About/Donate

One of the first practical sketch that receives UDP packet from X-Plane and outputs specific data. (May 2012)
It receives UDP packet, selects a group #67 (Gear) from the packet and sends it to the servo.



Video of this test with the servo -





/*
Test sketch that receives UDP packet from X-Plane
and controls Servo.

(Servo shows position of the Gear #1 )

May 2012
Vlad S
http://simvim.com
 */

#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Servo.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 4);    // local IP
unsigned int localPort = 49001;      // local port to listen
byte DataBuf[200];   // 

float Dataref_Value; // Float result


// Vars
float Gear1 = 0; // Gear #1 position


// Translate 4-byte to float
union u_tag {
byte b[4];
float fval;
} u;

// 
Servo GearGauge;  // servo object #1 
int GearVar;       // position variable

EthernetUDP Udp; 

//-------------------------------------------------------------------------------

void setup() 
{
  GearGauge.attach(9);                // attaches on pin 9
  GearGauge.writeMicroseconds(1400);  // set initial position
  
  Ethernet.begin(mac,ip);   // start the Ethernet 
  Udp.begin(localPort);   

}

void loop() {
        
        GearVar = map(Gear1, 0, 1000, 2000, 1000);
        GearGauge.writeMicroseconds(GearVar); 
        
  delay(5); 
  

  int packetSize = Udp.parsePacket();   //  read a packet
  if(packetSize)
  {
     Udp.read ( DataBuf, packetSize );    // read all packet to Data buffer 
    
 for (int i = 5; i < packetSize; i += 36)   // check each 36-th byte for group index number
 
       if ( DataBuf[i] = 67) {         // -- if index number=67, this is the group "Gear"
         
            u.b[0] = DataBuf[i+4];     // -- read the first 4 bytes after index  - Gear #1
            u.b[1] = DataBuf[i+5];
            u.b[2] = DataBuf[i+6];
            u.b[3] = DataBuf[i+7];
            
            Dataref_Value = u.fval;       // -- tranlate 4-byte to float ( dataref value)
            
             Gear1 = Dataref_Value*1000;   // -- Gear #1 position
            }
        } 
          
    }
  


  
   







© Copyright 2012-2016 - SimVim